You are on page 1of 19

TRAVERSING GRAPHS:

A JOURNEY THROUGH CONNECTED


DATA
What’s in the presentation?
 What is Graph Traversal?
 Types of Graph Traversal
 Implementation of Graph Traversal
 Applications of Graph Traversal
 Advantages of Graph Traversal
 Limitations of Graph Traversal
What is Graph Traversal?

 Graph traversal is a process of visiting each node in a graph.


 It can be used to search for a specific node or to find a path
between two nodes.
 It is an important technique used in computer science and data
structures.
 Graph traversal algorithms can be used to solve problems such
as finding the shortest path between two nodes, finding all
nodes connected to a given node, and determining if a graph is
connected.
Types of Graph Traversal

There are two main types of graph traversal:


 Depth-first search (DFS).
 Breadth-first search (BFS).

BREADTH FIRST SEARCH [BFS]


is a graph traversal algorithm that starts
traversing the graph from the root node and explores all the neighboring

nodes.
• Then, it selects the nearest node and explores all the unexplored nodes.
• While using BFS for traversal, any node in the graph can be considered as
the root node.
Implementation of BFS:
1. Declare a queue and insert the starting vertex.
2. Initialize a visited array and mark the starting vertex as visited.
3. Follow the below process till the queue becomes empty:
I. Remove the first vertex of the queue.
II. Mark that vertex as visited.
III. Insert all the unvisited neighbors of the vertex into the queue.

Step1: Initially queue and visited arrays are empty.


Step2: Push node 0 into queue and mark it visited.

Step 3: Remove node 0 from the front of queue and visit the unvisited neighbour’s and
push them into queue.
Step 4: Remove node 1 from the front of queue and visit the unvisited neighbour’s
and push them into queue.

Step 5: Remove node 2 from the front of queue and visit the unvisited neighbour’s
and push them into queue.
Step 6: Remove node 3 from the front of queue and visit the unvisited neighbours and
push them into queue.
As we can see that every neighbours of node 3 is visited, so move to the next node that
are in the front of the queue.

Step 7: Remove node 4 from the front of queue and visit the unvisited neighbours and
push them into queue.
As we can see that every neighbours of node 4 are visited, so move to the
next node that is in the front of the queue.

Now, Queue becomes empty, So, terminate these process of iteration.


APPLICATIONS OF BFS

Shortest Path
and Minimum
Spanning Tree for GPS
Crawlers in Navigation
unweighted Search Engines
graph systems

Peer-to-Peer Social Broadcasting


Networks Networking in Network: In
Websites Garbage
Collection
Advantages of Breadth First Search:
 BFS will never get trapped exploring the useful path forever.
 If there is a solution, BFS definitely find it out.
 If there is more than one solution then BFS can find the minimal one that
requires less number of steps. If there is a solution then BFS is guaranteed
to find it.
 Low storage requirement: linear with depth.
 Easily programmed.

Disadvantages of Breadth First Search:


 The main drawback of BFS is its memory requirement. Since each level of
the tree must be saved in order to generate the next level and the amount
of memory is proportional to the number of nodes stored.
 As a result, BFS is severely space-bound in practice so will exhaust the
memory available on typical computers in a matter of minutes.
Depth First Search [DFS]
is a recursive algorithm to search all the vertices of a tree
data structure or a graph. The depth-first search (DFS) algorithm starts with the
initial node of graph G and goes deeper until we find the goal node or the node
with no children.
Because of the recursive nature, stack data structure can be used to implement
the DFS algorithm. The process of implementing the DFS is similar to the BFS
algorithm.
The step by step process to implement the DFS traversal is
given as follows –

1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that
vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of
the stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex
on the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
A
Empty Stack
B C

Visit A (Push to Stack)


B C A
A

B Visit C (Push to Stack)


B C
A

Backtrack (Pop)
B C A
A
Backtrack (Pop)
B C

Visit C (Push to Stack)


B C C
A

Backtrack (Pop)
B C

The final output obtained is DFS: A,B,C


Advantages of Depth First Search:
 Memory requirement is only linear with respect to the search graph.
 The reason is that the algorithm only needs to store a stack of nodes on
the path from the root to the current node.
 The time complexity of a depth-first Search to depth d is O(bd) .Thus
practically depth-first search is time-limited rather than space-limited.
 DFS requires less memory since only the nodes on the current path are
stored.

Disadvantages of Depth First Search:


 The disadvantage of Depth-First Search is that there is a
possibility that it may down the left-most path forever.
 Even a finite graph can generate an infinite tree
 Depth-First Search is not guaranteed to find the solution.

You might also like