|
| 1 | +package graph.mst.kruskal; |
| 2 | + |
| 3 | +import graph.Edge; |
| 4 | +import graph.Vertex; |
| 5 | +import graph.VertexImpl; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +/** |
| 12 | + * Demonstrates the use of Kruskal's algorithm to find the minimum spanning tree (MST) of a graph. |
| 13 | + * This class highlights how Kruskal's algorithm is particularly suited for graphs represented as a list of edges, |
| 14 | + * as it processes edges by increasing weight, irrespective of their position in the graph. This edge list representation |
| 15 | + * is used because Kruskal's algorithm does not require direct access to the graph's adjacency structure, making it |
| 16 | + * efficient and straightforward for calculating the MST in graphs where edge connectivity is the primary concern. |
| 17 | + * <p> |
| 18 | + * The main method sets up vertices and edges, then uses Kruskal's algorithm to calculate and display the MST. |
| 19 | + */ |
| 20 | +public class KruskalMstMain { |
| 21 | + public static void main(String[] args) { |
| 22 | + |
| 23 | + List<Vertex> vertices = new ArrayList<>(); |
| 24 | + // Create vertices |
| 25 | + Vertex a = new VertexImpl("A"); |
| 26 | + Vertex b = new VertexImpl("B"); |
| 27 | + Vertex c = new VertexImpl("C"); |
| 28 | + Vertex d = new VertexImpl("D"); |
| 29 | + |
| 30 | + vertices.add(a); |
| 31 | + vertices.add(b); |
| 32 | + vertices.add(c); |
| 33 | + vertices.add(d); |
| 34 | + |
| 35 | + List<Edge> edges = new ArrayList<>(); |
| 36 | + // Set edges between vertices with specified weights |
| 37 | + edges.add(new Edge(a, b, 1)); |
| 38 | + edges.add(new Edge(d, b, 2)); |
| 39 | + edges.add(new Edge(b, c, 3)); |
| 40 | + edges.add(new Edge(a, d, 4)); |
| 41 | + edges.add(new Edge(d, c, 5)); |
| 42 | + |
| 43 | + // Calculate the MST using Kruskal's algorithm |
| 44 | + List<Edge> mst = buildMst(vertices, edges); |
| 45 | + System.out.println("MST: " + mst); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructs the minimum spanning tree (MST) for a graph represented by vertices and edges. |
| 50 | + * Assumes that the graph is connected. |
| 51 | + */ |
| 52 | + private static List<Edge> buildMst(List<Vertex> vertices, List<Edge> edges) { |
| 53 | + if (vertices == null || vertices.isEmpty()) { |
| 54 | + throw new IllegalArgumentException("Vertex list cannot be null or empty."); |
| 55 | + } |
| 56 | + if (edges == null || edges.isEmpty()) { |
| 57 | + throw new IllegalArgumentException("Edge list cannot be null or empty."); |
| 58 | + } |
| 59 | + |
| 60 | + List<Edge> mst = new ArrayList<>(); |
| 61 | + |
| 62 | + // Sorting edges by weight |
| 63 | + edges.sort(new Comparator<Edge>() { |
| 64 | + @Override |
| 65 | + public int compare(Edge edge1, Edge edge2) { |
| 66 | + return edge1.getWeight() - edge2.getWeight(); |
| 67 | + } |
| 68 | + }); |
| 69 | + UnionFind uf = new UnionFind(vertices); |
| 70 | + |
| 71 | + for (Edge edge : edges) { |
| 72 | + if (uf.find(edge.getSource()) != uf.find(edge.getDestination())) { |
| 73 | + mst.add(edge); |
| 74 | + uf.union(edge.getSource(), edge.getDestination()); |
| 75 | + } |
| 76 | + } |
| 77 | + return mst; |
| 78 | + } |
| 79 | +} |
0 commit comments