Ds 3
Ds 3
Graphs
1. Definitions and Terminology
A graph, G, consists of two sets V and E. V is a finite non−empty set of vertices. E is
a set of pairs of vertices, these pairs are called edges. V(G) and E(G) will represent the sets of
vertices and edges of graph G.
Undirected Graph
In an undirected graph the pair of vertices representing any edge is unordered. Thus,
the pairs (v1, v2) and (v2, v1) represent the same edge.
Directed Graph/Digraph
In a directed graph each edge is represented by a directed pair (v1, v2). v1 is the tail
and v2 the head of the edge. Therefore <v2, v1> and <v1,v2> represent two different edges.
Figure 1 shows three graphs G1, G2 and G3.
Figure 2. Multigraph
1
Department of Computer Science - IGCAS
Subgraph
A subgraph of G is a graph G' such that V(G') V(G) and E(G') E(G). Figure 3 shows
some of the subgraphs of G1 and G3..
2
Department of Computer Science - IGCAS
2. Graph Traversal
3
Department of Computer Science - IGCAS
if VISlTED(w) = 0 then
call DFS(w)
end
end DFS
Ex
If a depth first search is initiated from vertex v1, then the vertices of G are visited in
the order: v1, v2, v4, v8, v5, v6, v3, v7.
4
Department of Computer Science - IGCAS
forever
end BFS
Arrays
The array is a fixed-size sequenced collection of variables belonging to the same data
types. The array has adjacent memory locations to store values. Array provides a convenient
structure for representing data.
Syntax:
Data_type array_name [array_size];
Every item stored in an array is termed as an element. Each memory location of an
element in an array is denoted by a numerical index which is used for identifying the element.
Basic Operations
There is some specific operation that can be performed or those that are supported by
the array. These are:
Traversing: It prints all the array elements one after another.
Inserting: It adds an element at given index.
Deleting: It is used to delete an element at given index.
Searching: It searches for an element(s) using given index or by value.
Updating: It is used to update an element at given index.
One-dimensional arrays
A one-dimensional array (or single dimension array) is a type of linear array.
Accessing its elements involves a single subscript which can either represent a row or column
index.
Example
int a[10];
In the given example the array can contain 10 elements of any value available to
the int type. In C, the array element indices are 0-9 inclusive in this case. For example, the
expressions A[0] and A[9] are the first and last elements respectively.
Multidimensional arrays
A multi-dimensional array is an array of arrays. 2-dimensional arrays are the most
commonly used. They are used to store data in a tabular manner.
Example:
int a[2][3];
5
Department of Computer Science - IGCAS
This means that array a has 2 rows and 3 columns, and the array is of integer type. Here
we can store 6 elements they will be stored linearly but starting from first row linear then
continuing with second row. The above array will be stored as a11, a12, a13, a21, a22, a23.
Ordered List
The structure of an ordered list is a collection of items where each item holds a
relative position that is based upon some underlying characteristic of the item. The ordering
is typically either ascending or descending and assume that list items have a meaningful
comparison operation that is already defined. Many of the ordered list operations are the
same as those of the unordered list.
OrderedList() creates a new ordered list that is empty. It needs no parameters and
returns an empty list.
Add(item) adds a new item to the list making sure that the order is preserved. It needs
the item and returns nothing. Assume the item is not already in the list.
6
Department of Computer Science - IGCAS
Remove(item) removes the item from the list. It needs the item and modifies the list.
Assume the item is present in the list.
Search(item) searches for the item in the list. It needs the item and returns a boolean
value.
IsEmpty() tests to see whether the list is empty. It needs no parameters and returns a
boolean value.
Size() returns the number of items in the list. It needs no parameters and returns an
integer.
Index(item) returns the position of item in the list. It needs the item and returns the
index. Assume the item is in the list.
Pop() removes and returns the last item in the list. It needs nothing and returns an
item. Assume the list has at least one item.
Recursion
void recursion() {
recursion();
}
void main() {
recursion();
}
7
Department of Computer Science - IGCAS
if(x == 1)
return 1;
else
return (x * factorial(x - 1));
}
void main()
{
int i = 5;
printf("Factorial =", factorial(i));
}
When the above code is compiled and executed, it produces the following result
Factorial = 60