-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKrushkal.java
More file actions
82 lines (82 loc) · 2.71 KB
/
Krushkal.java
File metadata and controls
82 lines (82 loc) · 2.71 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
import java.util.*;
import java.io.*;
public class krushkal
{
class Edge implements Comparable<Edge>
{
int src, dest, weight;
public int compareTo(Edge compareEdge)
{
return this.weight - compareEdge.weight;
}
};
int V, E;
Edge edge[];
krushkal(int v, int e)
{
V = v;
E = e;
edge = new Edge[E];
for (int i = 0; i < e; ++i)
edge[i] = new Edge();
}
int find(int subsets[], int i)
{
if (subsets[i] != i)
subsets[i] = find(subsets, subsets[i]);
return subsets[i];
}
void KruskalMST()
{
Edge output[] = new Edge[V];
int e = 0;
int i = 0;
for (i = 0; i < V; ++i)
output[i] = new Edge();
Arrays.sort(edge);
int parent[] = new int[V];
for (int v = 0; v < V; ++v)
parent[v] = v;
i = 0;
while (e < V - 1)
{
Edge next_edge = new Edge();
next_edge = edge[i++];
int x = find(parent, next_edge.src);
int y = find(parent, next_edge.dest);
if (x != y)
{
output[e++] = next_edge;
parent[x]=y;
}
}
int minimumCost = 0;
for (i = 0; i < e; ++i)
{
System.out.println(output[i].src + " -- " + output[i].dest + " => " + output[i].weight);
minimumCost += output[i].weight;
}
System.out.println("Minimum Cost : " + minimumCost);
}
public static void main(String[] args)
{
int V,E;
System.out.println("Enter the no. of vertex and edge in the graph : ");
Scanner sc=new Scanner(System.in);
V=sc.nextInt();
E=sc.nextInt();
krushkal graph = new krushkal(V, E);
System.out.println("Enter the source, destination and the weight of the each vertex :");
for(int i=0;i<E;i++)
{
graph.edge[i].src = sc.nextInt();
graph.edge[i].dest = sc.nextInt();
graph.edge[i].weight = sc.nextInt();
}
long start = System.nanoTime();
graph.KruskalMST();
long end = System.nanoTime();
long microseconds = (end - start) / 1000;
System.out.println("Execution time : "+microseconds+" microseconds");
}
}