forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskalsAlgorithmDynamic .java
More file actions
136 lines (113 loc) · 4.28 KB
/
KruskalsAlgorithmDynamic .java
File metadata and controls
136 lines (113 loc) · 4.28 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
import java.util.*;
class Edge implements Comparable<Edge> {
int src, dest, weight;
// Edge constructor: makes an edge between src and dest with a given weight
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
// Compare edges by their weight (smallest first)
@Override
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
class KruskalsAlgorithmDynamic {
// Finds the leader of the group for vertex v with shortcutting for speed
static int find(int[] parent, int v) {
if (parent[v] != v) {
parent[v] = find(parent, parent[v]);
}
return parent[v];
}
// Links two groups together, using rank to keep things balanced
static void union(int[] parent, int[] rank, int u, int v) {
int rootU = find(parent, u);
int rootV = find(parent, v);
if (rootU == rootV)
return;
if (rank[rootU] < rank[rootV]) {
parent[rootU] = rootV;
} else if (rank[rootV] < rank[rootU]) {
parent[rootV] = rootU;
} else {
parent[rootV] = rootU;
rank[rootU]++;
}
}
// Main function: builds the MST and prints result
static void kruskalMST(int V, List<Edge> edges) {
// Sort edges by weight (start from the cheapest)
Collections.sort(edges);
int[] parent = new int[V];
int[] rank = new int[V];
for (int i = 0; i < V; i++) {
parent[i] = i;
rank[i] = 0;
}
List<Edge> mst = new ArrayList<>();
int mstWeight = 0;
int edgesAdded = 0;
for (Edge edge : edges) {
// Ignore edges that start and end at same node (self-loops)
if (edge.src == edge.dest)
continue;
int uSet = find(parent, edge.src);
int vSet = find(parent, edge.dest);
// Only add edge if it joins different groups (avoids cycles)
if (uSet != vSet) {
mst.add(edge);
mstWeight += edge.weight;
union(parent, rank, uSet, vSet);
edgesAdded++;
}
// If we've added enough edges, we can stop
if (edgesAdded == V - 1)
break;
}
// Print results in a friendly way
if (mst.size() < V - 1) {
System.out.println("Sorry, the graph isn't connected – cannot make a spanning tree for all nodes!");
} else {
System.out.println("Here's your Minimum Spanning Tree (MST):");
for (Edge e : mst) {
System.out.println("Node " + e.src + " is linked to node " + e.dest + " with weight " + e.weight);
}
System.out.println("Total weight of MST: " + mstWeight);
}
}
// Helper to read edges from user and remove duplicates
static List<Edge> readEdges(int V, int E, Scanner sc) {
Set<String> uniqueEdges = new HashSet<>();
List<Edge> edges = new ArrayList<>();
System.out.println("Enter edges in format: src dest weight (each between 0 and " + (V - 1) + "):");
for (int i = 0; i < E; i++) {
int src = sc.nextInt();
int dest = sc.nextInt();
int weight = sc.nextInt();
if (src < 0 || src >= V || dest < 0 || dest >= V) {
System.out.println("Bad input: node numbers must be between 0 and " + (V - 1));
i--; // let user retry this edge
continue;
}
String key = (src < dest ? src + ":" + dest : dest + ":" + src);
if (uniqueEdges.contains(key)) {
System.out.println("Edge already entered, skipping duplicate.");
continue;
}
uniqueEdges.add(key);
edges.add(new Edge(src, dest, weight));
}
return edges;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many nodes in your graph? ");
int V = sc.nextInt();
System.out.print("How many edges in your graph? ");
int E = sc.nextInt();
List<Edge> edges = readEdges(V, E, sc);
kruskalMST(V, edges);
}
}