-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.java
More file actions
148 lines (119 loc) · 3.38 KB
/
Kruskal.java
File metadata and controls
148 lines (119 loc) · 3.38 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
package graphs;
/**
*
* @author Naveen
*/
import java.io.*;
import java.lang.*;
import java.util.*;
import java.io.BufferedReader;
public class kruskalsalgo {
DisJointSet A=new DisJointSet();
edges edgeSet[];
public kruskalsalgo(int e){
edgeSet=new edges[e+1];
}
public void MSTKruskal(int vertices,int edges)
{
for(int i=1;i<=vertices;i++){
A.makeSet(i);
}
for(int i=1;i<=edges;i++){
for(int j=i+1;j<=edges;j++){
edges temp=new edges();
if(edgeSet[i].weight>edgeSet[j].weight){
temp=edgeSet[i];
edgeSet[i]=edgeSet[j];
edgeSet[j]=temp;
}
}
}
int w=0;
for(int i=1;i<=vertices;i++){
if(A.findSet(edgeSet[i].u)!=A.findSet(edgeSet[i].v)){
A.union(edgeSet[i].u,edgeSet[i].v);
System.out.println("The edge "+edgeSet[i].u+","+edgeSet[i].v+" is selected");
w+=edgeSet[i].weight;
}
else{
System.out.println("The edge "+edgeSet[i].u+","+edgeSet[i].v+" is not selected");
}
}
System.out.println("The weight of MST is: "+w);
}
public static void main(String args[]){
int source,destination,vertices,edges,w;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of vertices and edges");
vertices=s.nextInt();
edges=s.nextInt();
kruskalsalgo graph=new kruskalsalgo(edges);
System.out.println("Enter source and destination and weight respectively");
for(int i=1;i<=edges;i++)
{
source=s.nextInt();
destination=s.nextInt();
w=s.nextInt();
graph.edgeSet[i]=new edges();
graph.edgeSet[i].u=source;
graph.edgeSet[i].v=destination;
graph.edgeSet[i].weight=w;
}
graph.MSTKruskal(vertices,edges);
s.close();
}
}
class edges{
int u;
int v;
int weight;
public edges()
{
u=0;
v=0;
weight=0;
}
}
class DisJointSet {
private Map<Integer,Node> map=new HashMap<>();
class Node{
int data;
Node parent;
int rank;
}
public void makeSet(int data){
Node node=new Node();
node.data=data;
node.parent=node;
node.rank=0;
map.put(data, node);
}
public void union(int data1,int data2){
Node node1=map.get(data1);
Node node2=map.get(data2);
Node parent1=findSet(node1);
Node parent2=findSet(node2);
if(parent1.data==parent2.data){
return;
}
if(parent1.rank>=parent2.rank){
parent1.rank=(parent1.rank==parent2.rank)?parent1.rank+1:parent1.rank;
parent2.parent=parent1;
}
else
{
parent1.parent=parent2;
}
}
public long findSet(int data){
return findSet(map.get(data)).data;
}
private Node findSet(Node node){
Node parent=node.parent;
if(parent==node){
return parent;
}
node.parent=findSet(node.parent);
return node.parent;
}
}