Kruskal's Algorithm Lab Report
Kruskal's Algorithm Lab Report
Algorithms Lab
CSE206
1 Objective(s)
• To learn Kruskal’s algorithm to find Minimum Spanning Tree (MST) of a graph.
2 Problem Analysis
2.1 Kruskal’s Algorithm
Kruskal’s algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of the edges
of that graph which
• has the minimum sum of weights among all the trees that can be formed from the graph.
• Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle, then
reject this edge.
(a) Choose the edge with the least weight, if there are more
(b) Choose the next shortest edge and add it
than 1, choose anyone
(a) Choose the next shortest edge that doesn’t create a cycle (b) Choose the next shortest edge that doesn’t create a cycle and add it
and add it
1 KRUSKAL(G):
∅
∈
∈
=
∪
4 Implementation in Java
1 // Java program for Kruskal’s algorithm to
2 // find Minimum Spanning Tree of a given //connected,
3 undirected and weighted graph import java.util.*; import
4
java.lang.*; import java.io.*;
5
6
7 class Graph {
8 // A class to represent a graph edge class Edge implements
9 Comparable<Edge>
10 { int src, dest, weight;
11
12 // Comparator function used for // sorting edgesbased
13 on their weight public int compareTo(Edge
14 compareEdge)
15 { return this.weight − compareEdge.weight; }
16 };
17
18 // A class to represent a subset for
19 // union−find class subset
20
{ int parent, rank;
21
};
22
23
24
int V, E; // V−> no. of vertices & E−>no.of edges Edge edge[]; // collection of all
25 edges
26
27 // Creates a graph with V vertices and E edges
28 Graph(int v, int e)
29 {
30 V = v;
31 E = e;
32 edge = new Edge[E]; for (int i = 0; i < e;
33 ++i)
34
35
36
37
38
39edge[i] = new Edge();
40}
41
42// A utility function to find set of an
43// element i (uses path compression technique)
44int find(subset subsets[], int i)
45{
46// find root and make root as parent of i
47// (path compression)
48if (subsets[i].parent != i)
49subsets[i].parent
50= find(subsets, subsets[i].parent);
51
52return subsets[i].parent;
53}
54
55// A function that does union of two sets
56// of x and y (uses union by rank)
57void Union(subset subsets[], int x, int y)
58{
59int xroot = find(subsets, x);
60int yroot = find(subsets, y);
61
62// Attach smaller rank tree under root
63// of high rank tree (Union by Rank)
64if (subsets[xroot].rank
65< subsets[yroot].rank)
66subsets[xroot].parent = yroot;
67else if (subsets[xroot].rank
68> subsets[yroot].rank)
69subsets[yroot].parent = xroot;
70
71// If ranks are same, then make one as
72// root and increment its rank by one
73else {
74subsets[yroot].parent = xroot;
75subsets[xroot].rank++;
76}
77}
78
79// The main function to construct MST using Kruskal’s
80// algorithm
81void KruskalMST()
82{
83// Tnis will store the resultant MST
84Edge result[] = new Edge[V];
85
86// An index variable, used for result[]
87int e = 0;
88
89// An index variable, used for sorted edges
90int i = 0;
91for (i = 0; i < V; ++i)
92result[i] = new Edge();
93
94// Step 1: Sort all the edges in non−decreasing
95// order of their weight. If we are not allowed to 96// change the given graph, we can create
a copy of 97// array of edges
98Arrays.sort(edge);
99
100// Allocate memory for creating V ssubsets
101subset subsets[] = new subset[V];
102for (i = 0; i < V; ++i)
103subsets[i] = new subset();
104
105// Create V subsets with single elements
106for (int v = 0; v < V; ++v)
107{
108subsets[v].parent = v;
109subsets[v].rank = 0;
110}
111
112i = 0; // Index used to pick next edge
113
114// Number of edges to be taken is equal to V−1
115while (e < V − 1)
116{
117// Step 2: Pick the smallest edge. And increment
118// the index for next iteration
119Edge next_edge = edge[i++];
120
121int x = find(subsets, next_edge.src);
122int y = find(subsets, next_edge.dest);
123
124// If including this edge does’t cause cycle,
125// include it in result and increment the index
126// of result for next edge
127if (x != y) {
128result[e++] = next_edge;
129Union(subsets, x, y);
130}
131// Else discard the next_edge
132}
133
134// print the contents of result[] to display
135// the built MST
136System.out.println("Following are the edges in "
137+ "the constructed MST");
138int minimumCost = 0;
139for (i = 0; i < e; ++i)
140{
141System.out.println(result[i].src + " −− "
142+ result[i].dest
143+ " == " + result[i].weight);
144minimumCost += result[i].weight;
145}
146System.out.println("Minimum Cost Spanning Tree "
147+ minimumCost);
148}
149
150// Driver Code
151public static void main(String[] args)
152{
153
10
0−−−−−−−−1
|\ |
6| 5\ |15
| \|
2−−−−−−−−3
4 */
int V = 4; // Number of vertices in graph int E = 5; // Number of
edges in graph Graph graph = new Graph(V, E);
}
}
154/* Let us create following weighted graph
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
2 – 3 == 4
0 – 3 == 5
0 – 1 == 10
7 Lab Task (Please implement yourself and show the output to the instructor)
1. Write a Program in java to find the Second Best Minimum Spanning Tree using Kruskal Algorithm.
7.1 Problem analysis
A Minimum Spanning Tree T is a tree for the given graph G which spans over all vertices of the given graph and has the
minimum weight sum of all the edges, from all the possible spanning trees. A second best MST T0 is a spanning tree, that
has the second minimum weight sum of all the edges, from all the possible spanning trees of the graph G.
1. Sort the edges in O(ElogE), then find a MST using Kruskal in O(E).
2. For each edge in the MST (we will have V−1 edges in it) temporarily exclude it from the edge list so that it cannot
be chosen.
3. Then, again try to find a MST in O(E) using the remaining edges.
4. Do this for all the edges in MST, and take the best of all. Note: we don’t need to sort the edges again in for Step 3.
Figure 5: In this figure left is the MST and right is the second best MST
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be deducted
if any such copying is detected.