Unit 3 Lecture 2
Unit 3 Lecture 2
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.
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.
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
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