-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskalMST.java
More file actions
206 lines (167 loc) · 6.45 KB
/
KruskalMST.java
File metadata and controls
206 lines (167 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
195
196
197
198
199
200
201
202
203
204
205
206
import java.util.*;
// The Graph Node
class GraphNode{
String data;
// adjacent nodes
ArrayList<GraphNode> neighbours;
// weight od edges connecting this node to it's adjacent nodes
HashMap<GraphNode,Integer> weightMap;
// Constructor
GraphNode(String x){
this.data = x;
this.neighbours = new ArrayList<>();
this.weightMap = new HashMap<>();
}
}
// class representing an undirected weighted edge between two nodes: 'firstNode' and 'secondNode'
class UndirectedEdge{
GraphNode firstNode;
GraphNode secondNode;
// weight of edge
int weight;
// constructor
UndirectedEdge(GraphNode x, GraphNode y, int wt){
this.firstNode = x;
this.secondNode = y;
this.weight = wt;
}
}
// class to represent a disjoint set containing a graph node
class DisjointSet{
GraphNode node;
// rank of this disjont set
int rank;
// parent/representative of this disjoint set
DisjointSet parent;
// constructor
DisjointSet(GraphNode node){
this.node = node;
this.rank = 1;
this.parent = null;
}
}
public class KruskalMST {
// list to store all nodes
ArrayList<GraphNode> nodesList;
// list to store all edges in the graph
ArrayList<UndirectedEdge> edgesList;
// a hashmap that to get the disjoint set associated with a graphnode
HashMap<GraphNode,DisjointSet> nodeDisjointSetMap;
// constructor
KruskalMST(){
this.nodesList = new ArrayList<>();
this.edgesList = new ArrayList<>();
this.nodeDisjointSetMap = new HashMap<>();
}
// method to add an undirected weighted edge between nodes at index i and j of nodesList
public void addUndirectedWeightedEdge(int i, int j, int weight){
GraphNode node1 = this.nodesList.get(i);
GraphNode node2 = this.nodesList.get(j);
// adding node 2 in neighbours of node1
node1.neighbours.add(node2);
// adding node2 with weight in the weightmap of node 1
node1.weightMap.put(node2,weight);
// similarly for node 2 as well
node2.neighbours.add(node1);
node2.weightMap.put(node1,weight);
// adding the newly created edge into the edge list too
this.edgesList.add(new UndirectedEdge(node1,node2,weight));
}
// method to sort th undirected Weighted edges in edgesList on the basis of weight in non-decreasing order
// this is done to ensure that we pick the least cost/weight edges for out minimum spanning tree.
public void sortUndirectedEdgesByWeight(){
// we have to provide a comparator and overload the compareTo method to sort the arraylist containing 'UndirectedEdge'
Comparator<UndirectedEdge> comparator = new Comparator<UndirectedEdge>() {
@Override
public int compare(UndirectedEdge o1, UndirectedEdge o2) {
return o1.weight - o2.weight;
}
};
// finally sorting the edgesList on the basis of edge weight with the help of comparator
Collections.sort(this.edgesList, comparator);
}
// method to do makeset operation of disjoint set
public void makeSet(){
for(int i=0;i<this.nodesList.size();i++){
DisjointSet disJointSet = new DisjointSet(this.nodesList.get(i));
disJointSet.parent = disJointSet;
nodeDisjointSetMap.put(this.nodesList.get(i),disJointSet);
}
}
// mehod to implement find set method of disjoint set using path compression
public DisjointSet find(DisjointSet node){
DisjointSet parent = node.parent;
if(parent==node){
return parent;
}
else{
parent = find(parent);
}
node.parent = parent;
return parent;
}
// method to find the union of two disjoint sets
public int union(DisjointSet node1Set, DisjointSet node2Set){
DisjointSet parentNode1 = find(node1Set);
DisjointSet parentNode2 = find(node2Set);
// if same parent
if(parentNode1==parentNode2){
// returning -1 indicates we have encountered a cycle
return -1;
}
// if parents/representatives are different for both disjoint sets
if(parentNode1.rank>=parentNode2.rank){
parentNode1.rank = parentNode1.rank + parentNode2.rank;
parentNode2.parent = parentNode1;
}
else{
parentNode2.rank = parentNode2.rank + parentNode1.rank;
parentNode1.parent = parentNode2;
}
return 0;
}
// method to implement Kruskal Algorithm to find Minimum Spanning Tree (MST)
public void kruskal(){
makeSet();
sortUndirectedEdgesByWeight();
int totalWeight = 0;
// for each edge in the graph
for(int i=0;i<edgesList.size();i++){
UndirectedEdge currentEdge = this.edgesList.get(i);
GraphNode node1 = currentEdge.firstNode;
GraphNode node2 = currentEdge.secondNode;
DisjointSet node1Set = this.nodeDisjointSetMap.get(node1);
DisjointSet node2Set = this.nodeDisjointSetMap.get(node2);
int unionSet = union(node1Set,node2Set);
// if cycle exits
if(unionSet==-1){
continue;
}
else{
System.out.println("Edge: " + node1.data + "------" + node2.data +" || weight= " + currentEdge.weight);
totalWeight += currentEdge.weight;
}
}
System.out.println("Total weight of MST: " + totalWeight);
}
public static void main(String[] args) throws Exception {
KruskalMST graph = new KruskalMST();
// adding vertices/nodes
for(int i=0;i<4;i++){
graph.nodesList.add(new GraphNode("V"+i));
}
// adding undirected, weighted edges
graph.addUndirectedWeightedEdge(0,1,10);
graph.addUndirectedWeightedEdge(1,3,15);
graph.addUndirectedWeightedEdge(3,2,4);
graph.addUndirectedWeightedEdge(2,0,6);
graph.addUndirectedWeightedEdge(3,0,5);
graph.kruskal();
}
}
/*======================OUTPUT=======================
Edge: V3------V2 || weight= 4
Edge: V3------V0 || weight= 5
Edge: V0------V1 || weight= 10
Total weight of MST: 19
====================================================*/