0% found this document useful (0 votes)
10 views

4. Graph Based Algorithm

The document discusses graph-based algorithms, focusing on Breadth-First Search (BFS) and Depth-First Search (DFS). It details the processes, time complexities, and applications of both algorithms, emphasizing their traversal methods and how they can determine graph connectivity and detect cycles. The time complexity for BFS and DFS is O(V + E), where V is the number of vertices and E is the number of edges.

Uploaded by

Aakus REvol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

4. Graph Based Algorithm

The document discusses graph-based algorithms, focusing on Breadth-First Search (BFS) and Depth-First Search (DFS). It details the processes, time complexities, and applications of both algorithms, emphasizing their traversal methods and how they can determine graph connectivity and detect cycles. The time complexity for BFS and DFS is O(V + E), where V is the number of vertices and E is the number of edges.

Uploaded by

Aakus REvol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

4 Graph-Based Algorithms

Breadth-First Search Approach:


y The algorithm assumes that the graph
y BFS is a simple algorithm used to traverse G(V,E) is represented in an adjacency list,
a graph, and it is a model used in many and the color of each vertex is stored in
important graph algorithms. the u colour and the predecessors of u in
y BFS can compute the distance(smallest u.π.(If there are no predecessors present
number of edges) from s to other reachable for a vertex, S for example, then S.π =NIL).
vertices. It is the idea used behind Prim’s y A queue Q is used to store the grey vertices.
minimum spanning tree and Dijkstra’s y For a vertex u ∈ V, u.d stores the distance
single-source shortest path algorithms. from the source to the vertex u.
y BFS for a Graph G(V, E) explores the BFS (G, s)
edges of G to “discover” each edge that is //initializing all vertices to white color,
reachable from S, where V= set of vertices, infinite distance and connected to null
E=Set of edges, S belongs to V, and S is 1. For every vertex u ∈ G.V – {s}
the source vertex, and is distinguishable 2. u.color = WHITE
from other vertices. 3. u.d = ∞
4. u.π = NULL
5. s.color = GREY
6. s.d = 0
7. s.π = NULL
8. Q = ∅
9. ENQUEUE(Q,s):
10. While Q is empty
11. u = DEQUEUE(Q)
y BFS uses white, grey, and black colours for 12. For each v ∈ G.Adj[u]
the vertices to keep track the progress. 13. If (v.color == WHITE)
White – not discovered and not explored 14. v.color = GREY
Grey – discovered but not explored 15. v.d = u.d + 1
Black – discovered and explored 16. v.π = u
Discovered – traversed the vertex 17. ENQUEUE(Q, v)
Explored – the vertices that are reachable 18. u.color = BLACK
from a vertex V are discovered. y Line 1-4 except the source vertex all the
y The vertices that are adjacent to a black other vertex’s colours are initialised to
vertex will either be in black or grey. white, u.d vector to infinity, and u.π to null.
y Grey vertices may have adjacent white y Line 5 colours the source vertex to grey,
line 6 initialises s.d to zero, and in line 7
vertices, and at a point, it acts as a
s.π is initialised to null.
border between white and black-coloured
y In line 8, the queue Q is initially empty.
vertices
y Line 9 adds source vertex to the queue.
y BFS starts with source vertex S and then
y Line 11 takes the first element in the queue
includes edges that connect S to its
into u, and for every vertex v adjacent to u
adjacent white vertices.
(by means of for loop) it marks it into grey
y For an edge (u,v) that is added to the BFS, (line 14) if it is white (line 13) and intialises
u is the predecessor or parent to v in the all the vertices v.d vector to u.d+1 (line 15),
Breadth-first tree. and predecessor of v, i.e., v.π as u(line 16).

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:

Traversal vs. Search y Traversal is a search, which involves all


y Traversal of a graph or tree involves the nodes in the graph.
examining every node in the graph or tree. y By using Breadth-First Search (BFS), we
y Search may or may not involve all the nodes can even find out whether a graph is
in visiting the graph in systematic manner. connected or not.

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

executing lines 4–7 of DFS-VISIT is Θ (E).


The total time taken by DFS is therefore Θ
(V +E).
Depth-First Traversal
y Depth-first traversal is also exactly the
same as Breadth-first traversal.
y Here, instead of calling BFS inside that
traversal function, we will call DFS.
y The time and space complexity of depth-
first Traversal and depth-first Search is y Tree Edge [Red edges]
same. Formed after applying DFS on a graph.
DFT (G, n)/* G is the graph & n is the
number of vertices */ y Forward Edge [vertex A to vertex H]
{ Edge (u,v), in which v is a descendent of u.
for i = 1 to n do
visited[i] = 0; // visited [] is an array y Back Edge [vertex F to vertex B]
for i = 1 to n do Edges (u,v) in which v is the ancestor of u.
if(visited[i] == 0) then
DFS(i); y Cross Edge [vertex E to vertex D]
} Edges(u,v) in which v is neither ancestor
nor descendent to u.
Conclusion Undirected graph
y Time complexity in case of adjacency list ⚪ In an undirected graph, forward, and
BFS = BFT = DFS = DFT = O(V + E) backward edges are the same.
y Time complexity in case of adjacency ⚪ Cross edges are not possible in an
matrix undirected graph.

Graph-Based Algorithms 67
Difference between DFS and BFS

Depth-First Search Breadth-First Search

1. Backtracking is possible from a dead end. 1. Backtracking is not possible.


2. Stack is used to traverse the vertices in 2. Queue is used to traverse the vertices
LIFO order. in FIFO order.
3. Search is done in one particular 3. The vertices at the same level are
direction. maintained in parallel.

Topological sort: 1. Therefore, the topological sort takes


O(V + E) time in total.
y DFS is the logic behind topological sort in
DAG-SHORTEST-PATHS(G, w, s)
a directed acyclic graph (DAG).
a) sort the vertices of G topologically
y A topological sort of a DAG G=(V, E) is a
b) start with source
linear ordering of all its vertices, such that
c) for each vertex u, taken in topologically
if G contains an edge (u, v), then u appears
sorted order
before v in the ordering. d) for each vertex v ∈ G.Adj[u]
y If a cycle exists in a graph, there will be no e) RELAX(u, v, w)
linear ordering possible. Analysis
Topological Sorting(G) y The topological sort of line 1 takes Θ (V +E)
Step 1: C
all DFS on the graph, so that it time.
calculates the finishing time of each y The call of INITIALISE-SINGLE-SOURCE in
vertex. line 2 takes Θ (V) time.
Step 2: Based on the finishing time, insert y The for loop of lines 3–5 makes one
them into a linked list and return the iteration per vertex.
list of vertices. y Altogether, the for loop of lines 4–5 relaxes
each edge exactly once.
Analysis
y Because each iteration of the inner for
y DFS takes Θ (V + E) time, and O(V) to add loop takes Θ (1) time.
all vertices one by one into the linked list. y The total time is Θ (V +E).
Examples of a directed acyclic graph (DAG):
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      0 2 6  

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

1. Consider the following graph (G) (C) Directed acyclic graph


C D F (D) None of the above
Solution: (C)
Topological sort can be applied to directed
G acyclic graphs.

3. Consider the directed acyclic graph with


B A E
V={V1, V2, V3, V5, V6, V7} shown below.
Number of cut vertex or articulation
V2 V5
points is _______.
Solution: 2
In an undirected graph, a cut vertex (or
articulation point) is a vertex and if we V1
remove it then the graph splits into two
disconnected components.
Removal of “D” vertex divides the graph into V3 V7
two connected components ({E, F} and {A, B,
C, G}).
Similarly, removal of “C” vertex divides the V6
graph into ({G} and {A, B, C, F}).
Which of the following is not a topological
For this graph D and C are the cut vertices. ordering?
(A) V2V5V1V7V3V6
2. Topological sort can be applied to (B) V5V2V1V7V3V6
(A) Undirected graph (C) V1V5V2V7V3V6
(B) All types of graphs (D) None of the above

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

y BFS – Simple algorithm for traversing a graph (Breadth-wise).


y Traversal Vs. Search – Traversal goes through each vertex, but the search may or may
not.
y DFS – Algorithm used for traversing a graph (depth-wise).
Difference between DFS and BFS

Depth First Search Breadth-First Search

1. Backtracking is possible 1. Backtracking is not possible.


from a dead end. 2. The vertices to be explored are
2. Vertices from which organised as a FIFO queue.
exploration is incomplete 3. The vertices at the same level
are processed in a LIFO are maintained in parallel.
order.
3. Search is done in one
particular direction.

y Tree edge, Back edge, and Cross edge in DFS of a Graph:


Tree edge: Formed after applying DFS on a graph.Red edges are tree edges.
Forward edge: Edge (u,v) in which v is a descendent of u.
eg: 9.
Back edge: Edges (u,v) in which v is the ancestor of u.
eg : 10.

Cross edge : Edges(u,v) in which v is neither ancestor nor descendent to u.


eg : 5
y Topological Sort : “A topological sort of a DEG G=(V, E) is a linear ordering of all its
vertices, such that if G contains an edge (u, v), then u appears before v in the ordering.”

Graph-Based Algorithms 71

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy