Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Lab#13 Graph and Graph Traversals SSUET/QR/114

LAB # 13

Graph and Graph Traversals


Object
Implementing graph using BFS and DFS.

Theory

The graph is one non-linear data structure which consists of some nodes and their connected
edges. The edges may be directed or undirected. 

Breadth First Search (BFS)


The Breadth First Search (BFS) traversal is an algorithm, which is used to visit all of the nodes
of a given graph. In this traversal algorithm one node is selected and then all of the adjacent
nodes are visited one by one. After completing all of the adjacent vertices, it moves further to
check another vertices and checks its adjacent vertices again.
Algorithm for BFS

o Step 1: SET STATUS = 1 (ready state)


for each node in G
o Step 2: Enqueue the starting node A
and set its STATUS = 2
(waiting state)
o Step 3: Repeat Steps 4 and 5 until
QUEUE is empty
o Step 4: Dequeue a node N. Process it
and set its STATUS = 3
(processed state).
o Step 5: Enqueue all the neighbours of
N that are in the ready state
(whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
o STEP 6: exit

SWE-203L data Structure and Algorithms


Lab#13 Graph and Graph Traversals SSUET/QR/114

Depth First Search (DFS)


The Depth First Search (DFS) is a graph traversal algorithm. In this algorithm one starting vertex
is given, and when an adjacent vertex is found, it moves to that adjacent vertex first and try to
traverse in the same manner.

Algorithm for DFS

o Step 1: SET STATUS = 1(Ready state) for each node in G


o Step 2: Push the starting node A on the stack and set its STATUS =2(waiting state)
o Step 3: Repeat Steps 4 and 5 until STACK is empty
o Step 4: Pop the top node N. Process it and set its STATUS=3 (processed state)
o Step 5: Push on the stackall the neighbours of N that are in the ready state(whose
STATUS=1) and set their
o STATUS=2 (waiting state)
o [END OF LOOP]
o Step 6: EXIT

SWE-203L data Structure and Algorithms


Lab#13 Graph and Graph Traversals SSUET/QR/114

Lab Task

1. Implement BFS and DFS on the given graph.

Home Task

1 Implement BFS and DFS on the below given graph.

SWE-203L data Structure and Algorithms

You might also like