-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.java
More file actions
85 lines (73 loc) · 2.76 KB
/
Kruskal.java
File metadata and controls
85 lines (73 loc) · 2.76 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
/*
* Filename : Kruskal.java
* Problem Statement:
* Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's algorithm
* ------------------------------------------------------------------------------
* AUTHOR : GANESH PAI, Dept. of CS&E, NMAMIT, Nitte
* YEAR : 2021
* E-mail : ganesh.pai@nitte.edu.in
* ------------------------------------------------------------------------------
*/
import java.util.*;
public class Kruskal {
private class Edge { int v1, v2, weight; }
private ArrayList<Edge> edgeList = new ArrayList<>(), minEdgeList = new ArrayList<>();
private int noOfVertices, cost = 0;
public void readGraph() {
int edge = 0;
Scanner ip = new Scanner(System.in);
System.out.print("Enter the number of vertices & edges: ");
noOfVertices = ip.nextInt();
int numberOfEdges = ip.nextInt();
while(edge++ != numberOfEdges)
{
Edge e = new Edge();
System.out.print("Enter start vertex, end vertex and weight of edge " + edge + ": ");
e.v1 = ip.nextInt();
e.v2 = ip.nextInt();
e.weight = ip.nextInt();
edgeList.add(e);
}
}
private int find(int parent[], int x) {
if(parent[x] == -1)
return x;
return find(parent, parent[x]);
}
private void union(int parent[], int x, int y) {
int xRoot = find(parent, x);
int yRoot = find(parent, y);
parent[xRoot] = yRoot;
}
private void computeSpanningTree() {
int treeEdgeCount = 0, parent[] = new int[noOfVertices + 1];
Arrays.fill(parent, -1);
//Find spanning tree from the graph
edgeList.sort((e1, e2) -> { return e1.weight - e2.weight; });
while(treeEdgeCount < noOfVertices - 1)
{
Edge minEdge = edgeList.get(0);
if(find(parent, minEdge.v1) != find(parent, minEdge.v2))
{
union(parent, minEdge.v1, minEdge.v2);
minEdgeList.add(minEdge);
cost += minEdge.weight;
treeEdgeCount++;
}
edgeList.remove(minEdge);
}
}
public void printSpanningTree() {
computeSpanningTree();
//Print spanning tree edges and cost
System.out.println("\n\nEdges of the Spanning Tree:\n");
for (Edge edge : minEdgeList)
System.out.println(" Edge [ " + edge.v1 + " --- " + edge.v2 + " ] Cost: " + edge.weight);
System.out.println("\nCost of Minimum Spanning Tree: " + cost + " units\n\n");
}
public static void main(String[] args) {
Kruskal st = new Kruskal();
st.readGraph();
st.printSpanningTree();
}
}