package org.example.kruscal;
import java.util.*;
class Graph { class Edge implements Comparable<Edge> { int src, dest, weight;
public int compareTo(Edge compareEdge) { return this.weight - compareEdge.weight;
}
};
class Subset { int parent, rank;
};
int V, E;
Edge[] edges;
String[] vertices;
Graph(int v, int e) { V = v;
E = e;
edges = new Edge[E];
vertices = new String[V];
for (int i = 0; i < e; ++i)
edges[i] = new Edge();
}
int find(Subset subsets[], int i) { if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void union(Subset subsets[], int x, int y) { int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else { subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
void kruskalMST() { Edge[] result = new Edge[V];
int e = 0;
int i = 0;
for (i = 0; i < V; ++i)
result[i] = new Edge();
Arrays.sort(edges);
Subset[] subsets = new Subset[V];
for (i = 0; i < V; ++i)
subsets[i] = new Subset();
for (int v = 0; v < V; ++v) { subsets[v].parent = v;
subsets[v].rank = 0;
}
i = 0;
while (e < V - 1) { Edge nextEdge = edges[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);
if (x != y) { result[e++] = nextEdge;
union(subsets, x, y);
}
}
System.out.println("最小生成树的边:"); for (i = 0; i < e; ++i)
System.out.println(vertices[result[i].src] + " - " + vertices[result[i].dest] + ": " + result[i].weight);
}
}
public class Main { public static void main(String[] args) { int V = 4;
int E = 5;
Graph graph = new Graph(V, E);
graph.vertices[0] = "A";
graph.vertices[1] = "B";
graph.vertices[2] = "C";
graph.vertices[3] = "D";
graph.edges[0].src = 0;
graph.edges[0].dest = 1;
graph.edges[0].weight = 10;
graph.edges[1].src = 0;
graph.edges[1].dest = 2;
graph.edges[1].weight = 6;
graph.edges[2].src = 0;
graph.edges[2].dest = 3;
graph.edges[2].weight = 5;
graph.edges[3].src = 1;
graph.edges[3].dest = 3;
graph.edges[3].weight = 15;
graph.edges[4].src = 2;
graph.edges[4].dest = 3;
graph.edges[4].weight = 4;
graph.kruskalMST();
}
}