5
5
DEFINITION
A graph G = (V, E) consists of a set of vertices, V and set of edges E.
Vertics are referred to as nodes and the arc between the nodes are referred to as Edges.
Each edge is a pair (v, w) where v, w V. (i.e.) v = V1, w = V2...
V1 V2
V4 V3
Here V1, V2, V3, V4 are the vertices and (V1, V2), (V2, V3), (V3, V4), (V4, V1), (V2, V4), (V1,
V3) are edges.
REPRESENTATION OF GRAPH
Graph can be represented by Adjacency Matrix and Adjacency list.
One simple way to represents a graph is Adjacency Matrix.
The adjacency Matrix A for a graph G = (V, E) with n vertices is an n x n matrix, such that
Aij = 1, if there is an edge Vi to Vj
Aij = 0, if there is no edge.
Adjacency Matrix For Directed Graph
V1 V2 V3 V4
V1 V2 V1 0 1 1 0
V2 0 0 0 1
V3 0 1 0 0
V3 V4
V4 0 0 1 0
Example V1,2 = 1 Since there is an edge V1 to V2
Similarly V1,3 = 1, there is an edge V1 to V3
V1,1 & V1,4 = 0, there is no edge.
Adjacency Matrix For Undirected Graph
V1 V2 V3 V4
V1 V2 V1 0 1 1 0
V2 1 0 1 1
V3 1 1 0 1
V3 V4
V4 0 1 1 0
V1 V2 V3 V4
3
V1 V2 V1 0 3 9
9 1 7 V2 0 7
V3 1 0
V3 V4
8 V4 1 8 0
Advantage
* Simple to implement.
Disadvantage
* Takes O(n2) space to represents the graph
* It takes O(n2) time to solve the most of the problems.
Adjacency List Representation
In this representation, we store a graph as a linked structure. We store all vertices in a list
and then for each vertex, we have a linked list of its adjacency vertices
Adjacency List
1 2 3 4
2 4 5
3 6
4 3 5 6 7
5 7
6
7 6
BASIC TERMINOLOGIES
Directed Graph (or) Digraph
Directed graph is a graph whichconsists of directed edges, where each edge in E is
unidirectional. It is also referred as Digraph. If (v, w) is a directed edge then (v, w) # (w, v)
V1 V2
Undirected Graph
An undirected graph is a graph, which consists of undirected edges. If (v, w) is an undi-
rected edge then (v,w) = (w, v)
V1 V2
Weighted Graph
A graph is said to be weighted graph if every edge in the graph is assigned a weight or
value.
It can be directed or undirected graph.
V1 V2 V1 V2
V3 V3
Complete Graph
A complete graph is a graph in which there is an edge between every pair of vertics. A
complete graph with n vertices will have n (n - 1)/2 edges.
V1 V2 V1 V2
V3 V4 V3 V4
Number of vertics is 4
Number of edges is 6
(i.e) There is a path from every vertex to every other vertex.
A complete digraph is a strongly connected graph.
Strongly Connected Graph
If there is a path from every vertex to every other vertex in a directed graph then it is said
to be strongly connected graph. Otherwise, it is said to be weakly connected graph.
V1
V1
V2 V3 V3 V2
Path
A path in a graph is a sequence of vertices
1 ,2 , such that i ,i1 E for
.................
n
1 i N . Referring the Fig. 4.3.7 the path from V1 to V3 is V1, V2, V3.
Length
The length of the path is the number of edges on the path, which is equal to N-1, where N
represents the number of vertices.
The length of the above path V1 to V3 is 2. (i.e) (V1, V2), (V2, V3).
If there is a path from a vertex to itself, with no edges, then the path length is 0.
Loop
If the graph contains an edge (v, v) from a vertex to itself, then the path is referred to as a
loop.
Simple Path
A simple path is a path such that all vertices on the path, except possibly the first and the
last are distinct.
A simple cycle is the simple path of length atleast one that begins and ends at the same
vertex.
Cycle
A cycle in a graph is a path in which first and last vertex are the same.
1 2
In fig.
V1 V2
V3 V4
Indegree (V1) = 2
Outdegree (V1) = 1
ACyclic Graph
A directed graph which has no cycles is referred to as acyclic graph. It is abbreviated as
DAG.
DAG - Directed Acyclic Graph.
A
B C
D
E
GRAPH TRAVERSAL
A graph traversal is a systematic way of visiting the nodes in a specific order. There are
two types of graph traversal namely,
Depth first traversal
Breadth first traversal
Breadth First Traversal
Breadth First Search (BFS) of a graph G starts from an unvisited vertex u. Then all
unvisited vertices vi adjacent to u are visited and then all unvisited vertices wj adjacent to
vi are visited and so on. The traversal terminates when there are no more nodes to visit.
Breadth first search uses a queue data structure to keep track of the order of nodes whose
adjacent nodes are to be visited.
Steps to implement breadth first search
Step 1: Choose any node in the graph, designate it as the search node and mark it
as visited.
Step 2: Using the adjacency matrix of the graph, find all the unvisited adjacent nodes
to the search node and enqueue them into the queue Q.
Step 3: Then the node is dequeued from the queue. Mark that node as visited and
designate it as the new search node.
Step 4: Repeat step 2 and 3 using the new search node.
Step 5: This process continues until the queue Q which keeps track of the adjacent
nodes is empty.
Routine for breadth first search
Void BFS (vertex u)
{
Initialize queue Q;
visited [u] = 1;
Enqueue (u, Q);
while (! Isempty(Q))
{
u = Dequeue (Q);
print u;
for all vertices v adjacent to u do
if (visited [v] = = 0) then
{
Enqueue (v, Q)
visited [v] = 1;
}
}
}
Example:
A B
Adjacency matrix
C D
A B C D
A 0 1 1 0
B 1 0 1 1
C 1 1 0 1
D 0 1 1 0
Implementation
1. Let ‘A’ be the source vertex. Mark it to as visited.
2. Find the adjacent unvisited vertices of ‘A’ and enqueue then into the queue.
Here B and C are adjacent nodes of A
B C ........
.
and B and C are enqueued.
3. Then vertex ‘B’ is dequeued and its adjacent vertices C and D are taken
from the adjacency matrix for enqueuing. Since vertex C is already in the
queue, vertex D alone is enqueued.
C D ........
.
Here B is dequeued, D is enqueued.
4. Then vertex ‘C’ is dequeued and its adjacent vertices A, B and D are found
out. Since vertices A and B are already visited and vertex D is also in the
queue, no enqueue operation takes place.
D .........
Here C is dequeued
5. Then vertex ‘D’ is dequeued. This process terminates as all the vertices
are visited and the queue is also empty.
B C
Depth first works by selecting one vertex V of G as a start vertex ; V is marked visited.
Then each unvisited vertex adjacent to V is searched in turn using depth first search
recursively. This process continues until a dead end (i.e) a vertex with no adjacent unvisited
vertices is encountered. At a deadend, the algorithm backsup one edge to the vertex it came
from and tries to continue visiting unvisited vertices from there.
The algorithm eventually halts after backing up to the starting vertex, with the latter being
a dead end. By then, all the vertices in the same connected component as the starting vertex
have been visited. If unvisited vertices still remain, the depth first search must be restarted at
any one of them.
Step : 1 Choose any node in the graph. Designate it as the search node and mark it as
visited.
Step : 2 Using the adjacency matrix of the graph, find a node adjacent to the search.
node that has not been visited yet. Designate this as the new search node and
mark it as visited.
Step : 3 Repeat step 2 using the new search node. If no nodes satisfying (2) can be
found, return to the previous search node and continue from there.
Step : 4 When a return to the previous search node in (3) is impossible, the search from
the originally choosen search node is complete.
Step : 5 If the graph still contains unvisited nodes, choose any node that has not been
visited and repeat step (1) through (4).
if (! visited [W])
Dfs (W);
}
Example : -
A B
C D
Adjacency Matrix
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 1 0
Implementation
1. Let ‘A’ be the source vertex. Mark it to be visited.
2. Find the immediate adjacent unvisited vertex ‘B’ of ‘A’ Mark it to be visited.
3. From ‘B’ the next adjacent vertex is ‘d’ Mark it has visited.
4. From ‘D’ the next unvisited vertex is ‘C’ Mark it to be visited.
Undirected Graphs
A undirected graph is ‘connected’ if and only if a depth first search starting from any node
visits every node.
B D E
An Undirected graph
Adjacency Matrix
A B C D E
ABCDE 0 1 0 1 1
1 0 1 1 0
Implementation
0 1 0 1 1
We start at vertex ‘A’. Then Mark A as visited and call DFS (B) recursively, Dfs (B)
1 calls
Marks B as visited and 1 Dfs(c)
1 recursively.
0 0
1 0 1 0 0
A
B
Dfs (c) marks C as visited and calls Dfs (D) recursively. No recursive calls are made to Dfs
(B) since B is already visited.
Dfs(D) marks D as visited. Dfs(D) sees A,B,C as marked so no recursive call is made
there, and Dfs(D) returns back to Dfs(C).
B A
C B
D C
adjacent vertex to C.
Since all the vertices starting from ‘A’ are visited, the above graph is said to be
connected. If the graph is not connected, then processing all nodes requires reversal calls to Dfs,
and each generates a tree. This entire collection is a depth first spanning forest.
TOPOLOGICAL SORT
A topological sort is a linear ordering of vertices in a directed acyclic graph such that if
there is a path from Vi to Vj, then Vj appears after Vi in the linear ordering.
Topological ordering is not possible. If the graph has a cycle, since for two vertices v and
w on the cycle, v precedes w and w precedes v.
To implement the topological sort, perform the following steps.
Step 1 : - Find the indegree for every vertex.
Step 2 : - Place the vertices whose indegree is ‘0’ on the empty queue.
Step 3 : - Dequeue the vertex V and decrement the indegree’s of all its adjacent
vertices.
Step 4 : - Enqueue the vertex on the queue, if its indegree falls to zero.
Step 5 : - Repeat from step 3 until the queue becomes empty.
Step 6 : - The topological ordering is the order in which the vertices dequeued.
Routine to perform Topological Sort
/* Assume that the graph is read into an adjacency matrix and that the indegrees are
computed for every vertices and placed in an array (i.e. Indegree [ ] ) */
void Topsort (Graph G)
{
Queue Q ;
int counter = 0;
Vertex V, W ;
Q = CreateQueue
(NumVertex); Makeempty (Q);
for each vertex V
if (indegree [V] = = 0)
Enqueue (V, Q);
while (! IsEmpty (Q))
{
V = Dequeue (Q);
TopNum [V] = + + counter;
for each W adjacent to V
if (--Indegree [W] = = 0)
Enqueue (W, Q);
}
if (counter ! = NumVertex)
Error (“ Graph has a cycle”);
DisposeQueue (Q); /* Free the Memory */
}
Note :
Enqueue (V, Q) implies to insert a vertex V into the queue
Q. Dequeue (Q) implies to delete a vertex from the queue Q.
TopNum [V] indicates an array to place the topological numbering.
Example 1 :
a a b c d
a 0 1 1 0
b 0 0 1 1
b c
c 0 0 0 1
0 0 0 0
d d
VERTEX 1 2 3 4
a 0 0 0 0
b 1 0 0 0
c 2 1 0 0
d 2 2 1 0
ENQUEUE a b c d
DEQUEUE a b c d
V1 V2
V3 V4 V5
V6 V7
Fig
Adjacency Matrix :-
V1 V2 V3 V4 V5 V6 V7
V1 0 1 1 1 0 0 0
V2 0 0 0 1 1 0 0
V3 0 0 0 0 0 1 0
V4 0 0 1 0 0 1 1
V5 0 0 0 1 0 0 1
V6 0 0 0 0 0 0 0
0 0 0 0 0 1 0
V7
0 1 2 3 1 3 2
INDEGREE