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

Greedy Algorithms - (Kruskal's Minimum Spanning Tree Algorithm)

Kruskal's algorithm finds a minimum spanning tree (MST) for a connected, undirected graph. It works by sorting the edges by weight and then progressively adding edges to the MST if they do not form cycles. Specifically, it 1) sorts edges from lowest to highest weight, 2) adds the lowest weight edge that avoids cycles, and 3) repeats step 2 until (V-1) edges are added, where V is the number of vertices. This produces a subgraph that is a MST with total weight lower than or equal to any other spanning tree for the graph. The example demonstrates running Kruskal's algorithm on a 9-vertex graph to build an MST with 8 edges.

Uploaded by

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

Greedy Algorithms - (Kruskal's Minimum Spanning Tree Algorithm)

Kruskal's algorithm finds a minimum spanning tree (MST) for a connected, undirected graph. It works by sorting the edges by weight and then progressively adding edges to the MST if they do not form cycles. Specifically, it 1) sorts edges from lowest to highest weight, 2) adds the lowest weight edge that avoids cycles, and 3) repeats step 2 until (V-1) edges are added, where V is the number of vertices. This produces a subgraph that is a MST with total weight lower than or equal to any other spanning tree for the graph. The example demonstrates running Kruskal's algorithm on a 9-vertex graph to build an MST with 8 edges.

Uploaded by

lakshith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Greedy Algorithms | (Kruskals Minimum Spanning Tree Algorithm)

What is Minimum Spanning Tree?


Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and

connects all the vertices together. A single graph can have many different spanning trees. A minimum
spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph is a
spanning tree with weight less than or equal to the weight of every other spanning tree. The weight of a
spanning tree is the sum of weights given to each edge of the spanning tree.
How many edges does a minimum spanning tree has?
A minimum spanning tree has (V 1) edges where V is the number of vertices in the given graph.
Below are the steps for finding MST using Kruskals algorithm

1. Sort all the edges in non-decreasing order of their weight.


2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this
edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.

The algorithm is a Greedy Algorithm. The Greedy Choice is to pick the smallest weight edge that does not
cause a cycle in the MST constructed so far. Let us understand it with an example: Consider the below input
graph.

The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be having (9 1) = 8
edges.

After sorting:

Weight Src Dest

1 7 6

2 8 2

2 6 5
4 0 1

4 2 5

6 8 6

7 2 3

7 7 8

8 0 7

8 1 2

9 3 4

10 5 4

11 1 7

14 3 5

Now pick all edges one by one from sorted list of edges
1. Pick edge 7-6: No cycle is formed, include it.

2. Pick edge 8-2: No cycle is formed, include it.

3. Pick edge 6-5: No cycle is formed, include it.


4. Pick edge 0-1: No cycle is formed, include it.

5. Pick edge 2-5: No cycle is formed, include it.

6. Pick edge 8-6: Since including this edge results in cycle, discard it.
7. Pick edge 2-3: No cycle is formed, include it.

8. Pick edge 7-8: Since including this edge results in cycle, discard it.
9. Pick edge 0-7: No cycle is formed, include it.

10. Pick edge 1-2: Since including this edge results in cycle, discard it.
11. Pick edge 3-4: No cycle is formed, include it.

Since the number of edges included equals (V 1), the algorithm stops here.

You might also like