-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathKruskal's Algorithm using Priority Queue
61 lines (52 loc) · 1.99 KB
/
Kruskal's Algorithm using Priority Queue
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
public class Main {
public static void main(String[] args) {
int noOfVertices = 5;
List<int[]> adj=new ArrayList<>();
//int[] = {source, destination, weight}
adj.add(new int[]{0,1,4});
adj.add(new int[]{0,2,1});
adj.add(new int[]{1,2,2});
adj.add(new int[]{1,4,5});
adj.add(new int[]{2,3,4});
adj.add(new int[]{3,4,3});
PriorityQueue<int[]> q=new PriorityQueue<>((a,b) -> a[2]-b[2]);
for(int[] edges:adj){
q.add(edges);
}
List<int[]> mst =new ArrayList<>();
while(!q.isEmpty() && mst.size()<noOfVertices-1){
int[] edge=q.poll();
int source=edge[0];
int destination=edge[1];
if(!isCycle(source,destination,mst,new boolean[mst.size()])){
mst.add(edge);
}
}
System.out.println("MST formed by edges :");
for(int[] edge:mst){
System.out.println((char)(edge[0]+'A')+" - "+(char)(edge[1]+'A')+" --> "+edge[2]);
}
}
static boolean isCycle(int source, int destination, List<int[]> mst, boolean[] visited){
if(source==destination){
return true;
}
boolean flag=false;
for(int i=0;i<mst.size();i++){
int[] edge=mst.get(i);
if(!visited[i]){
if(source==edge[0]){
visited[i]=true;
flag=flag || isCycle(edge[1],destination,mst,visited);
visited[i]=false;
}
if(source==edge[1]){
visited[i]=true;
flag=flag || isCycle(edge[0],destination,mst,visited);
visited[i]=false;
}
}
}
return flag;
}
}