Week7 Graph Traversals
Week7 Graph Traversals
There are two graph traversal techniques and they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth FirstSearch)
Back tracking is coming back to the vertex from which we came to current vertex.
1|P age
Example
2|P age
3|P age
Example 2:
STEP1: STEP2:
STEP3: STEP4:
STEP5: STEP6:
STEP7: STEP8:
STEP8: STEP9:
STEP10:
4|Page
BFS (Breadth First Search)
or Level Order search
BFS traversal of a graph, produces a spanning tree as final result.
Spanning Tree is a graph without any loops. We use Queue data structure
with maximum size of total number of vertices in the graph to
implement BFS traversal of a graph.
• Step 2: Select any vertex as starting point for traversal. Visit that
vertex and insert it into the Queue.
• Step 3: Visit all the adjacent vertices of the verex which is at front
of the Queue which is not visited and insert them into the Queue.
• Step 4: When there is no new vertex to be visit from the vertex at
front of the Queue then delete that vertex from the Queue.
• Step 5: Repeat step 3 and 4 until queue becomes empty.
Example
5|Page
6|Page
7|Page
Prim’s Algorithm for Minimum Spanning Tree
(MST)
Algorithm
1. Declare an array visited[] to store the visited vertices and firstly, add
the arbitrary root, say S, to the visited array.
2. Check whether the adjacent vertices of the last visited vertex are
present in the visited[] array or not.
3. If the vertices are not in the visited[] array, compare the cost of edges
and add the least cost edge to the output spanning tree.
4. The adjacent unvisited vertex with the least cost edge is added into
the visited[] array and the least cost edge is added to the minimum
spanning tree output.
5. Steps 2 and 4 are repeated for all the unvisited vertices in the graph to
obtain the full minimum spanning tree output for the given graph.
6. Calculate the cost of the minimum spanning tree obtained.
Example:
Find the minimum spanning tree using prim’s method (greedy approach) for
the graph given below with S as the arbitrary root.
8|Page
Solution
Step 1
Create a visited array to store all the visited vertices into it.
V={}
The arbitrary root is mentioned to be S, so among all the edges that are
connected to S we need to find the least cost edge.
S→B=8
V = {S, B}
Step 2
Since B is the last visited, check for the least cost edge that is connected to
the vertex B.
B→A=9
B → C = 16
B → E = 14
Hence, B → A is the edge added to the spanning tree.
V = {S, B, A}
Step 3
Since A is the last visited, check for the least cost edge that is connected to
the vertex A.
9|Page
A → C = 22
A→B=9
A → E = 11
But A → B is already in the spanning tree, check for the next least cost edge.
Hence, A → E is added to the spanning tree.
V = {S, B, A, E}
Step 4
Since E is the last visited, check for the least cost edge that is connected to
the vertex E.
E → C = 18
E→D=3
Therefore, E → D is added to the spanning tree.
V = {S, B, A, E, D}
Step 5
Since D is the last visited, check for the least cost edge that is connected to
the vertex D.
D → C = 15
E→D=3
10 | P a g e
Therefore, D → C is added to the spanning tree.
V = {S, B, A, E, D, C}
Example 2:
Solution:
11 | P a g e
Kruskal's Algorithm
The inputs taken by the kruskal’s algorithm are the graph G {V, E}, where V
is the set of vertices and E is the set of edges, and the source vertex S and
the minimum spanning tree of graph G is obtained as an output.
Algorithm
• Sort all the edges in the graph in an ascending order and store it in an
array edge[].
• Construct the forest of the graph on a plane with all the vertices in it.
• Select the least cost edge from the edge[] array and add it into the
forest of the graph. Mark the vertices visited by adding them into the
visited[] array.
• Repeat the steps 2 and 3 until all the vertices are visited without
having any cycles forming in the graph
• When all the vertices are visited, the minimum spanning tree is
formed.
• Calculate the minimum cost of the output spanning tree formed.
Examples
Construct a minimum spanning tree using kruskal’s algorithm for the graph
given below −
Solution
As the first step, sort all the edges in the given graph in an ascending order
and store the values in an array.
Edge B→D A→B C→F F→E B→C G→F A→G C→D D→E C→G
Cost 5 6 9 10 11 12 15 17 22 25
12 | P a g e
From the list of sorted edge costs, select the least cost edge and add it onto
the forest in output graph.
B→D=5
Minimum cost = 5
Visited array, v = {B, D}
Similarly, the next least cost edge is B → A = 6; so we add it onto the output
graph.
Minimum cost = 5 + 6 = 11
Visited array, v = {B, D, A}
The next least cost edge is C → F = 9; add it onto the output graph.
Minimum Cost = 5 + 6 + 9 = 20
Visited array, v = {B, D, A, C, F}
13 | P a g e
The next edge to be added onto the output graph is F → E = 10.
Minimum Cost = 5 + 6 + 9 + 10 = 30
Visited array, v = {B, D, A, C, F, E}
The next edge from the least cost array is B → C = 11, hence we add it in the
output graph.
Minimum cost = 5 + 6 + 9 + 10 + 11 = 41
Visited array, v = {B, D, A, C, F, E}
The last edge from the least cost array to be added in the output graph is F
→ G = 12.
14 | P a g e
Minimum cost = 5 + 6 + 9 + 10 + 11 + 12 = 53
Visited array, v = {B, D, A, C, F, E, G}
The obtained result is the minimum spanning tree of the given graph with
cost = 53.
Example2:
Solution:
15 | P a g e