Unit-6 Exploring Graphs
Unit-6 Exploring Graphs
(ADA)
GTU # 3150703
Unit-6:
Exploring Graphs
edges
(or links)
D
A
E
C
B
F nodes
(or vertices)
Visited : 1 2 3 6 5 4 7 8
1 2 3 4 5 6 7 8
Queue Q Visited : 1 2 3 4 5 6 7 8
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 12
Breadth First Search - Algorithm
procedure search(G) procedure bfs(v)
for each v Є N do Q ← empty-queue
mark[v] ← not visited mark[v] ← visited
for each v Є N do enqueue v into Q
if mark[v] ≠ visited while Q is not empty do
then bfs(v)
u ← first(Q)
dequeue u from Q
for each node w adjacent to u do
if mark[w] ≠ visited
then mark[w] ← visited
enqueue w into Q
It uses a stack to keep track of the next location It uses a queue to keep track of the next location
to visit. to visit.
DFS requires less memory since only nodes on BFS guarantees that the space of possible moves
the current path are stored. is systematically examined; this search requires
considerably more memory resources.
Does not guarantee to find solution. If there is a solution, BFS is guaranteed to find
Backtracking is required if wrong path is it.
selected.
The Time complexity of both BFS and DFS will be O(V + E),
where V is the number of vertices, and E is the number of
Edges.
D E
Output:
A B C D E F
5 4
2 0 1
Output:
4 5 0 2 3 1
1 0 3
4
2
There are two connected components in the above undirected graph. 0 1 2 and 3 4
1 5
1 2 3
3 7
2 6
4 5
4
Articulation Points:
2,3 Articulation Points: 3
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 21
Thank You!