forked from sunidhi2001/Hacktoberfest2022-accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KruskalAlgorithm.java
147 lines (114 loc) · 5.8 KB
/
KruskalAlgorithm.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kruskals.algorithm;
// import required classes and packages
import java.util.*;
// create KruskalAlgorithm class to create minimum spanning tree of the given graph
class KruskalAlgorithm {
//create class Edge to create an edge of the graph that implements Comparable interface
class Edge implements Comparable<Edge> {
int source, destination, weight;
public int compareTo(Edge edgeToCompare) {
return this.weight - edgeToCompare.weight;
}
};
// create class Subset for union
class Subset {
int parent, value;
};
//initialize vertices, edges and edgeArray
int vertices, edges;
Edge edgeArray[];
// using constructor to create a graph
KruskalAlgorithm(int vertices, int edges) {
this.vertices = vertices;
this.edges = edges;
edgeArray = new Edge[this.edges];
for (int i = 0; i < edges; ++i)
edgeArray[i] = new Edge(); //create edge for all te edges given by the user
}
// create applyKruskal() method for applying Kruskal's Algorithm
void applyKruskal() {
// initialize finalResult array to store the final MST
Edge finalResult[] = new Edge[vertices];
int newEdge = 0;
int index = 0;
for (index = 0; index < vertices; ++index)
finalResult[index] = new Edge();
// using sort() method for sorting the edges
Arrays.sort(edgeArray);
// create an array of the vertices of type Subset for the subsets of vertices
Subset subsetArray[] = new Subset[vertices];
// aloocate memory to create vertices subsets
for (index = 0; index < vertices; ++index)
subsetArray[index] = new Subset();
// it is used to create subset with single element
for (int vertex = 0; vertex < vertices; ++vertex) {
subsetArray[vertex].parent = vertex;
subsetArray[vertex].value = 0;
}
index = 0;
// use for loop to pick the smallers edge from the edges and increment the index for next iteration
while (newEdge < vertices - 1) {
// create an instance of Edge for next edge
Edge nextEdge = new Edge();
nextEdge = edgeArray[index++];
int nextSource = findSetOfElement(subsetArray, nextEdge.source);
int nextDestination = findSetOfElement(subsetArray, nextEdge.destination);
//if the edge doesn't create cycle after including it, we add it in the result and increment the index
if (nextSource != nextDestination) {
finalResult[newEdge++] = nextEdge;
performUnion(subsetArray, nextSource, nextDestination);
}
}
for (index = 0; index < newEdge; ++index)
System.out.println(finalResult[index].source + " - " + finalResult[index].destination + ": " + finalResult[index].weight);
}
// create findSetOfElement() method to get set of an element
int findSetOfElement(Subset subsetArray[], int i) {
if (subsetArray[i].parent != i)
subsetArray[i].parent = findSetOfElement(subsetArray, subsetArray[i].parent);
return subsetArray[i].parent;
}
// create performUnion() method to perform union of two sets
void performUnion(Subset subsetArray[], int sourceRoot, int destinationRoot) {
int nextSourceRoot = findSetOfElement(subsetArray, sourceRoot);
int nextDestinationRoot = findSetOfElement(subsetArray, destinationRoot);
if (subsetArray[nextSourceRoot].value < subsetArray[nextDestinationRoot].value)
subsetArray[nextSourceRoot].parent = nextDestinationRoot;
else if (subsetArray[nextSourceRoot].value > subsetArray[nextDestinationRoot].value)
subsetArray[nextDestinationRoot].parent = nextSourceRoot;
else {
subsetArray[nextDestinationRoot].parent = nextSourceRoot;
subsetArray[nextSourceRoot].value++;
}
}
//main() method starts
public static void main(String[] args) {
int v, e;
//create scanner class object to get input from user
Scanner sc = new Scanner(System.in);
//show custom message
System.out.println("Enter number of vertices: ");
//store user entered value into variable v
v = sc.nextInt();
//show custom message
System.out.println("Enter number of edges");
//store user entered value into variable e
e = sc.nextInt();
KruskalAlgorithm graph = new KruskalAlgorithm(v, e); // use for creating Graph
for(int i = 0; i < e; i++){
System.out.println("Enter source value for edge["+ i +"]");
graph.edgeArray[i].source = sc.nextInt();
System.out.println("Enter destination value for edge["+ i +"]");
graph.edgeArray[i].destination = sc.nextInt();
System.out.println("Enter weight for edge["+i+"]");
graph.edgeArray[i].weight = sc.nextInt();
}
// call applyKruskal() method to get MST
graph.applyKruskal();
}
}