Non Linear Data Structure
Graph
Prepared By: Vaishali Koria Data Structure and Algorithms
Graph
• Graph is non linear data structure used to represent
relationships.
• Graph consists of Nodes(vertices) and Edges.
• Simple representation of Graph is:
1
2 3
• Here 1,2,3 are nodes and (1,2),(1,3) are edges.
Prepared By: Vaishali Koria Data Structure and Algorithms
Graph Traversal Techniques
• Graph Traversal means to visit the every node of the graph.
• Order of visit of nodes may be different for different
techniques.
• Graph Traversals are important to check for connectivity of a
graph.
• There are two techniques of Graph Travarsal
Graph traversal techniques
Depth First Serach(DFS) Breadth First Search(BFS)
Prepared By: Vaishali Koria Data Structure and Algorithms
BFS algorithm
BFS(v)
• Q<- empty Queue
• mark[v]<- visited
• enqueue v into Q
• While Q is not empty do
• u<- first(Q)
• dequeue u from Q
• For each node w adjacent to u do
• if mark[w]!= visited
• then mark[w]<- visited
• enqueue w into Q
Prepared By: Vaishali Koria Data Structure and Algorithms
1 Nodes Visited Queue
2 3
1 2 3 4
4
2 3 4 5 6
3 4 5 6
5 6 7 8
4 5 6 7 8
5 6 7 8
6 7 8
7 8
8 -
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)
• In DFS, we need to go to depth of one branch before
exploring other nodes. If one branch is explored completely,
then backtracking we can explore other nodes.
• DFS can be applied on both the types of graph, Directed or
Undirected.
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)
• Consider the following example:
• Here If the start vertex is A, then A considered as
A
visited.
• Then we can pick the children of A, either B or D..
D B
• Take B and mark it as visited.
• Until the all branched from B are visited, we need
C to explore the nodes.
• So, the next turn is of C, Mark it as visited.
• Now there is no children from C, backtrack to B.
• No more branches from B are left, so backtrack to
A.
• From A, there is one branch left to D, visit that and mark it as visited.
• So , order of visit is : A B C D
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)
Consider the following example of Undirected Graph:
1 Data Structure to find order of nodes using DFS
2 3 4
3
5 6 7 8 86
75
Order of visit of Nodes using DFS 42
1 2 5 6 3 4 7 8 1
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)
Answer these questions:
• Which Data structure is appropriate for finding DFS traversals?
• Is it possible to have more than one DFS order for same graph?
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)- Algorithm
DFS(v)
• mark[v]<- visited
• For each node w adjacent to v do
• if mark[w]!= visited then
• DFS([w])
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)– Algorithm
Let’s trace the algorithm for small undirected Graph(Start vertex is A)
A DFS(A) DFS(B)
Mark A-visited Mark B-visited
1
B C D Adjacent to A Adjacent to B
are B,C,D is E(unvisited)
B,C,D are
E DFS(E)
unvisited.
Order of visit of Nodes using DFS DFS(B)
2
A B E C D DFS(C) 3
DFS(D)
DFS(E)
DFS(D) DFS(C)
4 Mark E-visited
Mark D-visited Mark C-visited
Adjacent to E
Adjacent to D Adjacent to C
is B(visited)
is A(visited) is A(visited)
Return
Return Return
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)
Find DFS sequence for following Graph (Start vertex is 0)
Prepared By: Vaishali Koria Data Structure and Algorithms
Depth First Search(DFS)
One of the application of DFS is Cycle Detection from Graph.
Consider the given directed graph.
1 Order of visit of nodes using DFS is
1 3 2
2 3
Here, After visiting 2, next node to check is 1 which is already visited, and 1 is not a
parent of 2… So, it can be said that graph contains a cycle.
More formally, For every visited vertex 'v', if there is an adjacent 'u' such that u is
already visited and u is not parent of v, then there is a cycle in graph.
Prepared By: Vaishali Koria Data Structure and Algorithms
Thank You for Watching!!!
Prepared By: Vaishali Koria Data Structure and Algorithms