|
| 1 | +package main.java.com.thealgorithms.datastructures.graphs; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.PriorityQueue; |
| 5 | + |
| 6 | +// final cost of the Minimum Spanning Tree |
| 7 | +public class PriMincost { |
| 8 | + |
| 9 | + // Edge class to represent an edge between two vertices with a weight |
| 10 | + static class Edge { |
| 11 | + int src; // source vertex |
| 12 | + int dest; // destination vertex |
| 13 | + int wt; // weight of the edge |
| 14 | + |
| 15 | + // Constructor for the Edge class |
| 16 | + Edge(int src, int dest, int wt) { |
| 17 | + this.src = src; |
| 18 | + this.dest = dest; |
| 19 | + this.wt = wt; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // Method to create a graph using adjacency list representation |
| 24 | + static void createGraph(ArrayList<Edge> graph[]) { |
| 25 | + // Initialize each vertex's list in the adjacency list |
| 26 | + for (int i = 0; i < graph.length; i++) { |
| 27 | + graph[i] = new ArrayList<>(); |
| 28 | + } |
| 29 | + |
| 30 | + // Adding edges between vertices (undirected graph) |
| 31 | + graph[0].add(new Edge(0, 1, 10)); // Edge from 0 to 1 with weight 10 |
| 32 | + graph[0].add(new Edge(0, 2, 15)); // Edge from 0 to 2 with weight 15 |
| 33 | + graph[0].add(new Edge(0, 3, 30)); // Edge from 0 to 3 with weight 30 |
| 34 | + |
| 35 | + graph[1].add(new Edge(1, 0, 10)); // Edge from 1 to 0 (since it's undirected) |
| 36 | + graph[1].add(new Edge(1, 3, 40)); // Edge from 1 to 3 with weight 40 |
| 37 | + |
| 38 | + graph[2].add(new Edge(2, 0, 15)); // Edge from 2 to 0 with weight 15 |
| 39 | + graph[2].add(new Edge(2, 3, 50)); // Edge from 2 to 3 with weight 50 |
| 40 | + |
| 41 | + graph[3].add(new Edge(3, 4, 40)); // Edge from 3 to 4 with weight 40 |
| 42 | + graph[3].add(new Edge(3, 2, 50)); // Edge from 3 to 2 with weight 50 (undirected graph) |
| 43 | + } |
| 44 | + |
| 45 | + // Helper class to represent pairs of vertices and their corresponding cost (used in the priority queue) |
| 46 | + static class Pair implements Comparable<Pair> { |
| 47 | + int v; // vertex |
| 48 | + int cost; // cost of the edge to reach the vertex |
| 49 | + |
| 50 | + // Constructor for the Pair class |
| 51 | + Pair(int v, int cost) { |
| 52 | + this.v = v; |
| 53 | + this.cost = cost; |
| 54 | + } |
| 55 | + |
| 56 | + // Comparator to order Pairs by the cost (ascending order) for the priority queue |
| 57 | + @Override |
| 58 | + public int compareTo(Pair p2) { |
| 59 | + return this.cost - p2.cost; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Prim's algorithm to find the Minimum Spanning Tree (MST) |
| 64 | + public static void prims(ArrayList<Edge>[] graph) { |
| 65 | + boolean vis[] = new boolean[graph.length]; // Array to track visited vertices |
| 66 | + PriorityQueue<Pair> pq = new PriorityQueue<>(); // Priority queue to select the edge with the minimum weight |
| 67 | + pq.add(new Pair(0, 0)); // Start from vertex 0 with a cost of 0 |
| 68 | + int finalCost = 0; // Variable to store the total cost of the MST |
| 69 | + |
| 70 | + // While there are still vertices to process in the priority queue |
| 71 | + while (!pq.isEmpty()) { |
| 72 | + Pair curr = pq.remove(); // Get the pair with the smallest cost |
| 73 | + if (!vis[curr.v]) { // If the vertex has not been visited yet |
| 74 | + vis[curr.v] = true; // Mark it as visited |
| 75 | + finalCost += curr.cost; // Add the cost to the final cost |
| 76 | + |
| 77 | + // Iterate through the adjacent edges of the current vertex |
| 78 | + for (int i = 0; i < graph[curr.v].size(); i++) { |
| 79 | + Edge e = graph[curr.v].get(i); // Get each adjacent edge |
| 80 | + pq.add(new Pair(e.dest, e.wt)); // Add the adjacent vertex and edge weight to the priority queue |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Output the final cost of the Minimum Spanning Tree |
| 86 | + System.out.println("Final cost of the Minimum Spanning Tree: " + finalCost); |
| 87 | + } |
| 88 | + |
| 89 | + // Main method to test Prim's algorithm |
| 90 | + public static void main(String[] args) { |
| 91 | + int V = 4; // Number of vertices |
| 92 | + ArrayList<Edge> graph[] = new ArrayList[V]; // Create an array of adjacency lists |
| 93 | + createGraph(graph); // Initialize the graph with edges |
| 94 | + prims(graph); // Call Prim's algorithm to find the MST |
| 95 | + } |
| 96 | +} |
0 commit comments