0% found this document useful (0 votes)
7 views15 pages

Week7 Graph Traversals

The document discusses graph traversal techniques, specifically Depth First Search (DFS) and Breadth First Search (BFS), outlining their implementation steps and the concept of spanning trees. It also covers Prim's and Kruskal's algorithms for finding the Minimum Spanning Tree (MST) of a weighted undirected graph, detailing their respective processes. Each algorithm is explained with steps and examples to illustrate how to construct the MST.

Uploaded by

Muskan Aneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Week7 Graph Traversals

The document discusses graph traversal techniques, specifically Depth First Search (DFS) and Breadth First Search (BFS), outlining their implementation steps and the concept of spanning trees. It also covers Prim's and Kruskal's algorithms for finding the Minimum Spanning Tree (MST) of a weighted undirected graph, detailing their respective processes. Each algorithm is explained with steps and examples to illustrate how to construct the MST.

Uploaded by

Muskan Aneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

GRAPH TRAVERSALS

Graph traversal is technique used for searching a vertex in a graph. The


graph traversal is also used to decide the order of vertices to be visit in the
search process. A graph traversal finds the edges to be used in the search
process without creating loops that means using graph traversal we visit all
vertices of graph without getting into looping path.

There are two graph traversal techniques and they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth FirstSearch)

DFS (Depth First Search)

DFS traversal of a graph, produces a spanning tree as final result.


Spanning Tree is a graph without any loops. We use Stack data structure
with maximum size of total number of vertices in the graph to implement
DFS traversal of a graph.

We use the following steps to implement DFS traversal...


• Step 1: Define a Stack of size total number of vertices in the graph.
• Step 2: Select any vertex as starting point for traversal. Visit that
vertex and push it on to the Stack.
• Step 3: Visit any one of the adjacent vertex of the verex which is at
top of the stack which is not visited and push it on to the stack.
• Step 4: Repeat step 3 until there are no new vertex to be visit from the
vertex on top of the stack.
• Step 5: When there is no new vertex to be visit then use back
tracking and pop one vertex from the stack.
• Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7: When stack becomes Empty, then produce final spanning
tree by removing unused edges from the graph

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.

We use the following steps to implement BFS traversal...

• Step 1: Define a Queue of size total number of vertices in the 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.

• Step 6: When queue becomes Empty, then produce final spanning


tree by removing unused edges from the graph

Example

5|Page
6|Page
7|Page
Prim’s Algorithm for Minimum Spanning Tree
(MST)

Prim’s algorithm is a greedy algorithm used to find the Minimum


Spanning Tree (MST) of a weighted undirected graph. It starts from any
node and grows the MST by selecting the smallest edge that connects a
visited node to an unvisited node.

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}

The minimum spanning tree is obtained with the minimum cost = 46

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

Then, construct a forest of the given graph on a single plane.

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

You might also like