You are on page 1of 8

Department of Computer Science - IGCAS

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 1. Three sample graphs.


The graphs G1 and G2 are undirected. G3 is a directed graph.
V (G1) = {1,2,3,4}; E(G1) = {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)}
V (G2) = {1,2,3,4,5,6,7}; E(G2) = {(1,2),(1,3),(2,4),(2,5),(3,6),(3,7)}
V (G3) = {1,2,3}; E(G3) = {<1,2>, <2,1>, <2,3>}.
Multi Graph
A graph may have multiple occurrences of the same edge is referred to as a
multigraph. The graph of figure 2 is a multigraph.

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..

(a) Some of the subgraphs of G1

(b) Some of the subgraphs of G3


Figure 3. Subgraph
Completed Graph
The number of distinct unordered pairs (Vi,Vj) with Vi Vj in a graph with n vertices
is n(n − 1)/2. This is the maximum number of edges in any n vertex undirected graph. An n
vertex undirected graph with exactly n(n − 1)/2 edges is said to be complete.
G1 is the complete graph on 4 vertices while G2 and G3 are not complete graphs.
In the case of a directed graph on n vertices the maximum number of edges is n(n −
1).
Adjacent and Incident
If <v1,v2> is a directed edge, then vertex v1 will be said to be adjacent to v2 while v2
is adjacent from v1. The edge <v1,v2> is incident to v1 and v2.
The vertices adjacent to vertex 2 in G2 are 4, 5 and 1. The edges incident on vertex 3
in G2 are (1,3), (3,6) and (3,7. In G3 the edges incident to vertex 2 are <1,2>, <2,1> and
<2,3>.
Path

2
Department of Computer Science - IGCAS

A path from vertex Vp to vertex Vq in graph G is a sequence of vertices Vp,Vi1,Vi2,


...,Vin,Vq such that (vp,vi1),(vi1,vi2), ...,(vin,vq) are edges in E(G). If G' is directed then the
path consists of vp,vi1>,<vi,vi2>, ...,<vin,vq>, edges in E(G').
Length
The length of a path is the number of edges on it. A simple path is a path in which all
vertices except possibly the first and last are distinct. A path such as (1,2) (2,4) (4,3) we write
as 1,2,4,3. Paths 1,2,4,3 and 1,2,4,2 are both of length 3 in G1. The first is a simple path
while the second is not. 1,2,3 is a simple directed path in G3. 1,2,3,2 is not a path in G3 as the
edge <3,2> is not in E(G3).
Cycle
A cycle is a simple path in which the first and last vertices are the same. 1,2,3,1 is a
cycle in G1. 1,2,1 is a cycle in G3.
Degree
The degree of a vertex is the number of edges incident to that vertex.
In-degree
In a directed graph, the in−degree of a vertex v to be the number of edges for
which v is the head.
Out-degree
The out−degree is defined to be the number of edges for which v is the tail.
Vertex 2 of G3 has in−degree 1, out−degree 2 and degree 3.

2. Graph Traversal

2.1. Depth First Search


The start vertex v is visited. Next an unvisited vertex w adjacent to v is selected and a
depth first search from w initiated. When a vertex u is reached such that all its adjacent
vertices have been visited, we back up to the last vertex visited which has an unvisited vertex
w adjacent to it and initiate a depth first search from w. The search terminates when no
unvisited vertex can be reached from any of the visited one.
Algorithm
procedure DFS(v)
VISITED (v) = 1
for each vertex w adjacent to v do

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.

2.2 Breadth First Search


Starting at vertex v and marking it as visited, breadth first search differs from depth
first search in that all unvisited vertices adjacent to v are visited next. Then unvisited vertices
adjacent to these vertices are visited and so on. A breadth first search beginning at vertex v1
of the graph in figure 6.13(a) would first visit v1 and then v2 and v3. Next vertices v4, v5, v6
and v7 will be visited and finally v8.
Algorithm
procedure BFS(v)
VISITED (v) = 1
initialize Q to be empty
loop
for all vertices w adjacent to v do
if VISITED(w) = 0 then
[
call ADDQ(w,Q);
VISITED(w) = 1
]
end

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.

Example – Matrix Addition


void main()
{
int a[10][10],b[10][10],c[10][10];
scanf (“%d”,&m,&n);
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
scanf(“%d”,&a[i][j]);
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
scanf(“%d”,&b[i][j]);
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
c[i][]j]= a[i][j]) + b[i][j];
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
printf(“%d”,c[i][j]);
}

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

Recursion is the process of repeating items in a self-similar way. In programming


languages, if a program allows to call a function inside the same function, then it is called a
recursive call of the function.

void recursion() {
recursion();
}
void main() {
recursion();
}

The C programming language supports recursion. While using recursion,


programmers need to be careful to define an exit condition from the function, otherwise it
will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc. The following
example calculates the factorial of a given number using a recursive function Live
Demo

int factorial (int x) {

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

You might also like