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

Kruskal's Algorithm Lab Report

The document describes implementing Kruskal's algorithm to find the minimum spanning tree (MST) of a graph. It begins with objectives and an overview of Kruskal's algorithm and how it works. Pseudocode and a Java implementation of the algorithm are provided, along with sample input/output. The document concludes with an exercise to find the second best MST using Kruskal's algorithm and an exercise to count the number of distinct MSTs for a graph.

Uploaded by

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

Kruskal's Algorithm Lab Report

The document describes implementing Kruskal's algorithm to find the minimum spanning tree (MST) of a graph. It begins with objectives and an overview of Kruskal's algorithm and how it works. Pseudocode and a Java implementation of the algorithm are provided, along with sample input/output. The document concludes with an exercise to find the second best MST using Kruskal's algorithm and an exercise to count the number of distinct MSTs for a graph.

Uploaded by

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

Department of

Computer Science and Engineering

Title: Implement Kruskal’s Algorithm

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

• form a tree that includes every vertex.

• has the minimum sum of weights among all the trees that can be formed from the graph.

2.2 How Kruskal’s algorithm works


It falls under a class of algorithms called greedy algorithms that find the local optimum in the hopes of finding a global
optimum. We start from the edges with the lowest weight and keep adding edges until we reach our goal. The steps for
implementing Kruskal’s algorithm are as follows:

• Sort all the edges from low weight to high.

• 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.

• Keep adding edges until we reach all vertices.


2.3 Kruskal’s Algorithm Complexity
The time complexity Of Kruskal’s Algorithm is: O(E log E).

2.4 Example of Kruskal’s algorithm

Figure 1: Start with a weighted graph

(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

Figure 2: Step 2 and 3

(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

Figure 3: Step 4 and 5


Figure 4: Repeat until you have a spanning tree
3 Algorithm

Algorithm 1: Kruskal Algorithm

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);

// add edge 0−1 graph.edge[0].src = 0;


graph.edge[0].dest = 1; graph.edge[0].weight =
10;

// add edge 0−2 graph.edge[1].src = 0;


graph.edge[1].dest = 2; graph.edge[1].weight =
6;

// add edge 0−3 graph.edge[2].src = 0;


graph.edge[2].dest = 3; graph.edge[2].weight =
5;

// add edge 1−3 graph.edge[3].src = 1;


graph.edge[3].dest = 3; graph.edge[3].weight =
15;

// add edge 2−3 graph.edge[4].src = 2;


graph.edge[4].dest = 3; graph.edge[4].weight =
4;

// Function call graph.KruskalMST();

}
}
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

5 Sample Input/Output (Compilation, Debugging & Testing)


Following are the edges in the constructed MST

2 – 3 == 4
0 – 3 == 5
0 – 1 == 10

Minimum Cost Spanning Tree: 19

6 Discussion & Conclusion


Based on the focused objective(s) to understand about the MST algorithms, the additional lab exercise made me more
confident towards the fulfilment of the objectives(s).

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.

7.2 Using Kruskal’s Algorithm


We can use Kruskal’s algorithm to find the MST first, and then just try to remove a single edge from it and replace it with
another.

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.

(a) MST (b) Second Best MST

Figure 5: In this figure left is the MST and right is the second best MST

8 Lab Exercise (Submit as a report)


• Find the number of distinct minimum spanning trees for a given weighted graph.

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.

You might also like