4. Graph Based Algorithm
4. Graph Based Algorithm
Graph-Based Algorithms 63
y Line 17 adds each vertex discovered into algorithm, and vertices are enqueued and
queue and line 18 marks the explored dequeued only once, which is tested in
vertex into black, i.e., u.colour = black. line 13.
y This procedure (from line 10 to line 18) is y The enqueuing process and dequeuing
done until the queue gets empty. processes of a vertex take O(1) time which
y The order of vertices resulting from BFS in turn takes O(V) time.
depends on the order of adjacent vertices y The algorithm scans the adjacency list of
visited in line 12: the BFS may be different, each vertex which takes O(E) time.
but the distances computed by the y Since initialisation takes O(V) time, the
algorithm do not. total time taken is O(V+E).
Analysis y Hence, BFS runs in time equal to the size
y All the vertices in the graph are white at of the adjacency-list representation of G,
first except the source S, and the vertices
i.e., O(V+E).
are not changed into white further in the
Example:
64 Graph-Based Algorithms
y We can initialise the visited array of all y For initialisation of visited [ ] array, it takes
the vertex to be zero (i.e., visited [i to n] = O(V) time.
0) and then run the breadth-first search y Therefore, the total time complexity
starting from one of the vertexes, and after = O(V + 2E)
the search finishes, we should examine = O(V + E)
the visited array. y The time complexity is the same for
y In case if visited [ ] array indicates that both the directed graph as well as for
some of the vertices are not visited, then undirected graph.
the reason is definitely that the graph is y Space complexity is also going to be the
not connected. same as Breadth-First Search.
y Therefore, by using breadth-first search, Conclusion
we can say that whether the graph is y The time and space complexity of breadth-
connected or not. first traversal is the same as breadth- first
Breadth-First Traversal search.
BFT can be executed using BFS. y For a given graph,BFT calls BFS on every
Algorithm: node.When BFS is called on a single node
BFT (G, n) /*G is the graph, and ‘n’ is the ,that means we are working on smaller
number of vertices */ part of the graph and then continue with
{ the remaining part.
for i = 1 to n do y So, it is as good as running the Breadth-First
visited [i] = 0; /* visited[] is a search on the entire graph exactly once.
global array of vertices. ‘0’ value Applications of Breadth First Traversal
indicate it is not visited and ‘1’ y Shortest path and minimum spanning tree
indicate it is visited.*/ for weighted graph
for i = 1 to n do y Path finding
if(visited [i] == 0) then y To test if a graph is bipartite
BFS(i); y Finding all nodes within one connected
} component
y Cycle detection in an undirected graph
y For the time complexity of breadth-first y Social networking websites
traversal (BFT), we have to do an aggregate y Crawlers in search engines
analysis.
y Aggregate analysis considers overall work
done. Previous Years’ Question
y In case if we are going to use adjacency
list representation, then every node is The Breadth-First Search (BFS) algorithm
going to have an adjacency list. has been implemented using the
y Whenever we call BFS, then some part queue data structure. Which one of the
of the adjacency list will be completely following is a possible order of visiting
visited, exactly one. the nodes in the graph below?
y Next time, when we call BFS on the
M N O
remaining nodes, then the remaining
nodes which are present on this list will
R Q P
be also visited exactly one.
y Therefore, overall, on average, all the
(A) MNOPQR (B) NQMPOR
nodes will be visited exactly one.
(C) QMNPRO (D) QMNPOR
y In case of the undirected graph, the
Solution: (D) [2017 (Set-2)]
adjacency list contains 2E nodes.
Graph-Based Algorithms 65
Depth First Search (DFS) y The code below is DFS with Graph
y Depth-first search (DFS) is an algorithm G(directed or undirected) as input. Time is
for searching the vertices of a graph a global variable.
(traversing a graph to be specific), that DFS (G):
works by exploring as far into the graph as 1. For each vertex u Î G.V
possible before backtracking. 2. u.color ← WHITE
y Input: 3. u.π ← NULL
Graph G(V,E), where V is the set of vertices 4. Time ← 0
and E is the set of edges. 5. For each vertex u Î G.V
y To keep track of progress, a depth-first 6. If u.color is WHITE
search colours each vertex 7. DFS-VISIT(G,u)
⚪ White: Vertices are white before they end
are discovered DFS-VISIT(G,u)
1. Time ← time + 1 // white vertex u has
⚪ Gray: Vertices are grey when they are
just been discovered
discovered, but their outgoing edges are
2. u.d ← time
still in the process of being explored. 3. u.color ← GREY
⚪ Black: Vertices are black when the 4. For each v Î G.Adj[u] // explore edge
entire subtree starting at the vertex (u,v)
has been explored 5. if v.color is WHITE
y The moment DFS discovers a vertex v in 6 v.π ← u
an adjacency list of already visited u,the 7. DFS-VISIT(G,v)
algorithm marks the predecessor of 8. u.color ← BLACK // blacken u; it is
attribute v. π=u. finished
y Depending upon the source vertex, there 9. Time ← time + 1
will be many subtrees possible for a DFS. 10. u.f ← time
y The predecessor subgraph of a DFS is end
defined as Analyzing DFS(G):
Gπ = (V,Eπ ), where
y All the vertices are coloured white and
their π attributes are initialised to null in
E=
π {(V.π, V) : v ∈ V and v.π ≠ NULL} lines 1-3 of DFS (G).
y The predecessor subgraph of DFS forms y The time variable is reset in line 4.
a forest with several depth-first trees.The y From line 5 to line 7, For any vertex u Î V is
edges in E.π are tree edges. applied DFS-VISIT(G,u) if it is white.
y DFS timestamps each vertex apart y For each time DFS_VISIT(G,u) is called on
from creating a depth-first tree.The a vertex u, then u becomes the new root.
y This DFS-VISIT(G,u) returns the vertex
two timestamps given to a vertex:v.d
with u.d and u.f initialised.
is used to record when the vertex is
discovered(changes v to grey colour) and Analyzing DFS-VISIT(G,u):
v.f is used to assign appropriate finishing y The global variable time is incremented
time after examining v’s adjacency list in line, 1, and the new value of discovery
(changes v to black). time u.d is updated in line 2. The vertex is
coloured grey in line 3.
y Since the adjacency matrix has at most |V|2
y From the 4th to 6th line, the vertices that
entries,the timestamps range between 1
are adjacent to input vertex u are checked.
and |V|2. The timestamp of discovering the
If they are white, their predecessor, i.e.,
vertex and finishing the vertex are v.d and v.p, is initialised to u.
v.f such that v.d<v.f, for every vertex v Î V. y Since every vertex adjacent to u is
y Vertex u is WHITE before u.d, GREY considered in line 4, DFS explores the
between u.d and u.f, and BLACK after u.f. edge (u,v) for v Î Adj[u].
66 Graph-Based Algorithms
y After all the vertices adjacent to u are BFS = BFT = DFS = DFT = O(V2)
explored,8th line to 10th line in algorithm y Space complexity of
colours the vertex to black,increments BFS = BFT = DFS = DFT = O(V).
time, and u.f is noted. Applications of Depth First Search
Note: y Detecting cycle in a graph:
The order of vertices returned by If there is a back edge in DFS, it has a
DFS depends on the order of vertices cycle. Hence DFS can detect a cycle in a
discovered in line 5 of DFS algorithm, graph.
and line 4 of DFS-VISIT algorithm. y Path finding:
The DFS algorithm can be tweaked to
y Apart from the time to execute calls of find a path between two given vertices,
DFS_VISIT,the loops in lines 1-3, and 5-7 in u and z.
DFS gives Θ (V ) time complexity. y Used as logic behind topological sorting
y The algorithm DFS_VISIT discovers every y To test if a graph is bipartite
vertex exactly once, and the vertex on y Finding strongly connected components
which DFS_VISIT is called should be a of a graph
white vertex, and the DFS_VISIT will colour Triee edge, Back edge and Cross edges in
it to grey at the very first step. DFS of graph
y The loop lines 4-7 execute |Adj[v]| times in
the execution of DFS-VISIT algorithm.
∑
v∈V
| Adj[v] |= Θ(E) , the total time for
Graph-Based Algorithms 67
Difference between DFS and BFS
3 4 2 3 4 2
(a) (b)
6 1 6 1
r s t x y z r s t x y z
5 2 7 -1 -2 5 2 7 -1 -2
0 2 6 6 4 0 2 6 5 7
3 4 2 3 4 2
(c ) (d)
6 1
r s t x y z
5 2 7 -1 -2
0 2 6 5 3
3 4 2
(e)
68 Graph-Based Algorithms
Previous Years’ Question
Consider the DAG with consider V={1, 2, 3, 4, 5, 6}, shown below. Which of the following is
NOT a topological ordering? [2007]
2 5
1 4
3 6
(A) 1 2 3 4 5 6 (B) 1 3 2 4 5 6
(C) 1 3 2 4 6 5 (D) 3 2 4 1 6 5
Solution: (D)
Solved Examples
Graph-Based Algorithms 69
Solution: (C) Which all of the above is/are not possible
output(s)?
Here, every edge has a dependency, like this
(A) 1,3, and 4 only
V2 (B) 1 and 3 only
(C) 1, 2, 3, and 4 only
(D) None of the above
V1 Solution: (D)
All the sequences of nodes are the possible
this edge means that V2 comes before V1 output of Depts First Search (DFS).
in topological ordering. Initially, V2 and V5
doesn’t have any dependency. So any one of
5. The Breadth-First Search algorithm has
them can be done independently.
been implemented using the queue data
So, either start with V2 or V5. structure. The possible order of visiting
So, the topological ordering is given below: the nodes of the following graph is (MSQ)
V2V5V1V7V3V6 a
Or
V5V2V1V7V3V6
Another topological ordering is also possible, b c
but V1V5V2V7V3V6 is not correct topological
ordering because it starts with V1 before V2
or V5.
Hence, (C) the correct option.
d e f g
4. Consider the following sequence of nodes
for the undirected graph given below.
1. a b d h e f c g 2. a c g h f e b d
3. a b d h f c g e 4. a b d h g c f e
h
If a Depth-First Search (DFS) is started
(A) abcdefgh
at node a using stack data structure. The
(B) acbfgdeh
nodes are listed in the order they are
first visited. (C) abcedgfh
(D) abchdefg
a
Solution: (A), (B), and (C)
The sequence of nodes given in options (a),
b c (b), and (c) are the correct possible order
of visiting the nodes by using breadth-first
search, because breadth-first search visits
d e f g the “breadth” first, i.e., if it is visiting a node,
then after visiting that node, it will visit
the neighbour nodes (children) first before
h moving on to the next level neighbours.
70 Graph-Based Algorithms
Chapter Summary
Graph-Based Algorithms 71