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

Unit 3 Lecture 2

Uploaded by

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

Unit 3 Lecture 2

Uploaded by

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

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: 23CSH-241
Chapter: Graph
Subject Coordinator:
Ms. Upasana Tiwari
(E15791)

Graph
Lecture No.-3.1 DISCOVER . LEARN .
EMPOWER
Index
• Traversing a graph
• Operations on graph

2 2
Graph traversal
The graph has two types of traversal algorithms.
1. Breadth First Search (BFS) and
2. Depth First Search (DFS).
BFS algorithm:-
A standard BFS implementation puts each vertex of the graph into one of two categories:

1. Visited

2. Not Visited

The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

The algorithm works as follows:

1. Start by putting any one of the graph's vertices at the back of a queue.

2. Take the front item of the queue and add it to the visited list.

3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the back of the queue.

4. Keep repeating steps 2 and 3 until the queue is empty.


BFS example
Let's see how the Breadth First Search algorithm
works with an example. We use an undirected graph
with 5 vertices.
Continue..
We start from vertex 0, the BFS algorithm starts by
putting it in the Visited list and putting all its adjacent
vertices in the queue.
Continue..
Next, we visit the element at the front of queue i.e. 1
and go to its adjacent nodes. Since 0 has already
been visited, we visit 2 instead.
Continue..
Vertex 2 has an unvisited adjacent vertex in 4, so
we add that to the back of the queue and visit 3,
which is at the front of the queue.
Continue..
Only 4 remains in the queue since the only adjacent
node of 3 i.e. 0 is already visited. We visit it.
Continue..

Since the queue is empty, we have completed the Breadth First


Traversal of the graph.
BFS Algorithm
Applications
• To build index by search index
• For GPS navigation
• Path finding algorithms
• In Ford-Fulkerson algorithm to find maximum flow
in a network
• Cycle detection in an undirected graph
• In minimum spanning tree
Depth First Search (DFS).
A standard DFS implementation puts each vertex of the graph into
one of two categories:
• Visited
• Not Visited
The purpose of the algorithm is to mark each vertex as visited while
avoiding cycles.
The DFS algorithm works as follows:
1. Start by putting any one of the graph's vertices on top of a stack.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which
aren't in the visited list to the top of the stack.
4. Keep repeating steps 2 and 3 until the stack is empty.
DFS example

We start from vertex 0, the DFS algorithm starts by putting it in the


Visited list and putting all its adjacent vertices in the stack.
DFS example

Next, we visit the element at the top of stack i.e. 1 and go to its
adjacent nodes. Since 0 has already been visited, we visit 2 instead.
DFS example

Vertex 2 has an unvisited adjacent vertex in 4, so


we add that to the top of the stack and visit it.
DFS example
DFS example
DFS example

After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so
we have completed the Depth First Traversal of the graph.
Application of DFS
Algorithm
1.For finding the path
2.To test if the graph is bipartite
3.For finding the strongly connected
components of a graph
4.For detecting cycles in a graph
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutorial
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=xlVX7dXLS64
THANK YOU

21

You might also like