- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathKruskalAlgorithm.java
More file actions
Latest commit
122 lines (107 loc) · 4.25 KB
/
Copy pathKruskalAlgorithm.java
File metadata and controls
122 lines (107 loc) · 4.25 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
packageGraph;
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.Scanner;
/**
* Steps for Kruskal's 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.
*
* Output:
* costMST = 16
* 0 – 1
* 1 – 2
* 1 – 4
* 0 – 3
* Time Complexity: O(E * logE) + O(E * 4*alpha), E*logE for sorting and E*4*alpha for findParent operation ‘E’ times
* Space Complexity: O(N). Parent array+Rank Array
*/
classkruskalNode{ // class for node which stores u, v and edge weight
intnode1;
intnode2;
intedgeWeight;
// constructor
publickruskalNode(intnode1, intnode2, intedgeWeight){
this.node1 = node1;
this.node2 = node2;
this.edgeWeight = edgeWeight;
}
}
// used defined comparator
classsortComparatorimplementsComparator<kruskalNode>{
@Override
publicintcompare(kruskalNodeo1, kruskalNodeo2) {
returnInteger.compare(o1.edgeWeight, o2.edgeWeight);
}
}
publicclassKruskalAlgorithm {
// find() operation
publicstaticintfindParent(intnode, int[] parent){
if(node == parent[node]){
returnnode;
}
returnparent[node] = findParent(parent[node], parent);
}
// union() operation
publicstaticvoidunion(intu, intv, int[] parent, int[] rank){
u = findParent(u, parent);
v = findParent(v, parent);
if(rank[u] < rank[v]){
parent[u] = v;
}elseif(rank[v] < rank[u]){
parent[v] = u;
}else{
parent[v] = u;
rank[u]++;
}
}
publicstaticvoidkruskalAlgorithm(ArrayList<kruskalNode> arrayList, intnumberOfEdges){
Collections.sort(arrayList, newsortComparator()); // sorting arraylist according to the edge weight
int[] parent = newint[numberOfEdges]; // will store parent of the nodes
int[] rank = newint[numberOfEdges]; // will store the rank of the tree forming
// initialization of parent and rank array
for(inti=0 ; i<numberOfEdges ; i++){
parent[i] = i;
rank[i] = 0;
}
intcostMst = 0; // for storing cost of the final MST
ArrayList<kruskalNode> mst = newArrayList<>(); // for storing final MST formed
// traversing arraylist
for(kruskalNodenode : arrayList){
// if both nodes are not in the same components then
// union both nodes and add edge weight of u and v into the costMST variable
// add both nodes into the mst list
if(findParent(node.node1, parent) != findParent(node.node2, parent)){
costMst += node.edgeWeight;
mst.add(node);
union(node.node1, node.node2, parent, rank);
}
}
// printing final MST
System.out.println("\n\nCost of the MST is : " + costMst);
System.out.println("Final MST will be : ");
for(kruskalNodenode : mst){
System.out.println(node.node1 + " - " + node.node2);
}
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.println("Enter the number of edges:");
intnumberOfEdges = sc.nextInt();
ArrayList<kruskalNode> arrayList = newArrayList<>();
System.out.println("Enter data:");
for(inti=1 ; i<=numberOfEdges ; i++){
System.out.println("Enter u:"); // inputting u
intu = sc.nextInt();
System.out.println("Enter v:"); // inputting v
intv = sc.nextInt();
System.out.println("Enter edge weight:"); // inputting edge weight
intedgeWeight = sc.nextInt();
arrayList.add(newkruskalNode(u, v, edgeWeight)); // adding into the arraylist
}
// calling kruskalAlgorithm() method
kruskalAlgorithm(arrayList, numberOfEdges);
}
}