Documente Academic
Documente Profesional
Documente Cultură
6. Graphs
Graph is a non-linear data structure. It holds multiple elements (called as Vertices)
and these vertices are connected to each other by links (called as Edges).
Definition : A Graph consists of set of Nodes (or Vertices) and a set of links
connecting the two nodes called Arcs (or Edges).
G = ( V, E ) V Vertices, E Edges
They differ from other data structures in one major concept: each node may have
multiple successors as well as multiple predecessors.
To represent the complex data like networks we use Graphs.
They can be used to solve complex routing problems such as designing and routing
airlines among the airports they serve.
A
B
Fig. a
Fig. b
Two vertices are said to be Adjacent Vertices (or neighbours) if there their exists
an edge that directly connects them. In fig. a vertex A and B are adjacent to each
other, where as in fig. b, vertex B is adjacent to A, but B is not adjacent to A.
A Cycle is a path consisting of at least three vertices that starts and ends with the
same vertex. In Fig. (a) above A, B, E, C, A is a cycle and even the links between C,
E, F also form a cycle. Note, that the same vertices in digraph of fig. b, do not form
a cycle. A Loop is a special type of cycle in which a single edge (or arc) starts and
ends on the same vertex.
Two vertices are said to be connected if there is path between them. A graph is said
to be Connected if, suppressing the directions, there is a path from any vertex to
any other vertex in a graph. A directed graph is Strongly connected if there is path
from each vertex to every other vertex in the graph. A directed graph is Weakly
connected if there are at least two vertices that are not connected.
Prepared by, Santosh Kabir.
Mobile : 98336 29398
Pg 1of 8
Graphs
www.santoshkabirsir.com
Fig. a
Fig. b
Degree of a vertex is a number of edges incident to it. In above fig. (b) degree of
vertex E is 3 and degree of vertex C is 4. The Outdgree of a vertex in a directed
graph is a number of arcs leaving the vertex; the Indgree of a vertex is a number of
arcs entering the vertex.
Graph Representations:
To represent a graph we need to store two sets. One set for vertices of a graph and a
second to represent the edges. The most common structures used to represent
graph are 1)Two-dimensional array and 2) Linked lists.
1) Adjacency Matrix:
Adjacency matrix is a two dimensional array of size N x N, where N in a number of
Vertices in a graph. The array element ( i , j ) holds value true if there is edge
existing between the ith and jth node, for a undirected graph, or an arc exists from ith
to jth node, in a digraph, else the element holds false. One can even use values 1 or
0 at the place of true or false.
( In case of weighted graphs, every (i, j)th element of the matrix holds the weight for
edge i,j or infinity (large value) to indicate absence of edge).
The two types of graphs and their Adjacency matrices are shown below,
A
B
Fig. a
A B
A
B
Pg 2of 8
C
D
E
F
0
0
1
0
0
0
C D
1
0
0
0
0
0
Fig. b
0 0 0 0
0 0 1 0
Graphs
0 1 1 1
www.santoshkabirsir.com
0 0 0 0
0 0 0 1
0 0 0 0
A
B
C
D
E
F
0
1
1
0
0
0
1
0
0
0
1
0
E F
1
0
0
1
1
1
0
0
1
0
0
0
0
1
1
0
0
1
0
0
1
0
1
0
2) Adjacency List:
Adjacency list uses multiple linked lists to represent the given graph. A vertex
linked list or header node linked list in which each node holds vertex number and
information in the vertex of a given graph and two pointers. One pointer is used to
form a link between all the vertices of a graph and the other pointer works like
header or root of another linked list that holds vertex information that are
connected to the current vertex. Figure below shows adjacency list for digraph of
fig. (b) above.
For example the horizontal linked list starting from vertex C indicate that vertex C
has links to Vertices A, D, E and F.
A
E
/
Graph Traversals:
We have worked with the traversals of structures such as lists or binary trees.
These structures have a root node from where the traversals can start. In case of
graphs the traversals can be started from any node of a graph. Depending upon
sequence in which the nodes are visited there are two types of graph traversals.
Breadth-first traversal and Depth-first traversal.
Pg 3of 8
Graphs
www.santoshkabirsir.com
Lets apply above steps to the following graph with 6 vertices, starting at vertex A.
A
B
Steps performed
Keep vertex A in queue
Queue contents
A
Process A
B C
Process B
C E
Process C
E D F
Process E
D F
Process D
Process F
Thus, in BFT the nodes of the above graph will be traversed as follows,
A B C E D F
Pg 4of 8
Graphs
www.santoshkabirsir.com
Lets apply above steps to the following graph with 6 vertices, starting at vertex A.
A
B
Steps performed
Stack Contents
A
Push A on stack
C B
F E D B
E D B
D B
Pg 5of 8
Graphs
www.santoshkabirsir.com
stak.top = -1;
v = 0;
// first vertex (i.e. A)
processed[v] = 1;
Push( v );
while( ! IsStackEmpty() )
{
v = Pop();
printf( "%c\t" , (char)('A' + v ));
for( i=0; i<n; i++ )
{
if( A[v][i] == 1 && !processed[i]
{
Push( i );
processed[ i ] = 1;
}
}
}
Pg 6of 8
Graphs
www.santoshkabirsir.com
q.fe=q.re = -1;
v = 0; // first vertex (i.e. A)
processed[v] = 1;
Add( v );
while( ! IsQEmpty() )
{
v = Remove();
printf( "%c\t", (char)('A' + v));
for( i=0; i< n; i++ )
{
if( A[v][i] == 1 && !processed[i]
{
Add( i );
processed[ i ] = 1;
}
}
}
}
void main( )
{
int A[10][10];
// Adjacency matrix
int n; // no. of vertices
int opt;
while( 1 )
{
printf("\nInput Matrix ...1\n");
printf("DFS ............2\n");
printf("BFS ............3\n");
printf("Quit ...........4\n");
printf("Enter option: ");
scanf("%d", &opt);
switch( opt )
{
case 1:
printf("\nEnter number of vertices (max 10) :");
scanf("%d", &n);
InputMatrix( A, n );
break;
case 2:
printf("\nDepth First Search ...\n");
DFS( A, n );
Prepared by, Santosh Kabir.
Mobile : 98336 29398
Pg 7of 8
Graphs
www.santoshkabirsir.com
Note :
Some more topics of Graphs for IT branch in separate notes.
------0000------
Sem IV - IT
Web Programming ( WP )
[ HTML, ASP.Net with C#, PHP, JSP, MySQL etc. ]
Pg 8of 8
Graphs
www.santoshkabirsir.com