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

Introduction to Traversal Graphs

Uploaded by

bharathsingh330
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Introduction to Traversal Graphs

Uploaded by

bharathsingh330
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to Traversal Graphs

Graphs: A Versatile Data Structure Types of Graphs

Graphs are a fundamental data structure Graphs can be directed (edges have a direction) or
representing relationships between entities. They undirected (edges don't). They can also be
consist of nodes (vertices) and edges connecting weighted, assigning values to edges representing
them. distances or costs.
Breadth-First Search (BFS)
Exploring Level by Level Queue-Based Approach
BFS systematically explores a graph level by To implement BFS queue datastructure is used
level, starting from a source node. It visits all mostly.
nodes at a given distance before moving to the
next level.
BFS Algorithm Step-by-Step
1 step 1
Get on array of size greater than number of vertices in the graph

2 step 2
Initialize Queue to empty state

3 step 3
enqueue() the first node and mark it as visited

4 step 4
while queue is not empty do the steps "a" to "d"

5 a)dequeue value from queue


b) enqueue all adjacent vertices into the Queue

c)Mark the enqueued vertices as visited

d)If no vertex is left to be enqueued repeat steps a to d


Depth-First Search (DFS)

Descending Deeper Stack-Based Approach


DFS explores the graph by going as deep as possible DFS uses a stack to manage the order of nodes. It
along a branch before backtracking and exploring pushes nodes onto the stack and processes them in
other branches. a Last-In, First-Out (LIFO) order.
DFS Algorithm Step-by-Step
step 1:
define a stack of size total number of vertices in graph

step 2:
select any vertex as starting node and push it on to the stack mark these vertex are visited

step 3:
visited any one of the adjacent vertex of which is at top of stack that is not visited and push it on to the stack

step 4:
repeat the above steps until there are no new vertex to be visit from the vertex on the top of the stack

step 5:
when there is no new vertex to be visited then use back tracking and pop the vertex from the stack.

step 6:
repeat the steps from 3 to 5 until stck becomes empty

step 7:
when stack becomes empty then proceduce Graph traversal of The Graph.
Real-World Applications of BFS and DFS

1
Shortest Path
Finding the shortest path between two nodes, such as in navigation apps.

2
Cycle Detection
Determining if a graph contains cycles, used in dependency analysis.

3
Topological Sorting
Ordering tasks in a graph to ensure dependencies are satisfied, used in scheduling.
THANK YOU!
23911A0576

23911A05A7

23911A05A8

23911A05B5
blah blah

You might also like