Skip to content

Commit 473c349

Browse files
authored
Add PrimsAlgo.java
Program for Prim's Algorithm
1 parent 34483e9 commit 473c349

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

PrimsAlgo.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// A Java program for Prim's Minimum Spanning Tree (MST) algorithm.
2+
// The program is for adjacency matrix representation of the graph
3+
4+
import java.util.*;
5+
import java.lang.*;
6+
import java.io.*;
7+
8+
class PrimsAlgo {
9+
// Number of vertices in the graph
10+
private static final int V = 5;
11+
12+
// A utility function to find the vertex with minimum key
13+
// value, from the set of vertices not yet included in MST
14+
int minKey(int key[], Boolean mstSet[])
15+
{
16+
// Initialize min value
17+
int min = Integer.MAX_VALUE, min_index = -1;
18+
19+
for (int v = 0; v < V; v++)
20+
if (mstSet[v] == false && key[v] < min) {
21+
min = key[v];
22+
min_index = v;
23+
}
24+
25+
return min_index;
26+
}
27+
28+
// A utility function to print the constructed MST stored in
29+
// parent[]
30+
void printMST(int parent[], int graph[][])
31+
{
32+
System.out.println("Edge \tWeight");
33+
for (int i = 1; i < V; i++)
34+
System.out.println(parent[i] + " - " + i + "\t" + graph[i][parent[i]]);
35+
}
36+
37+
// Function to construct and print MST for a graph represented
38+
// using adjacency matrix representation
39+
void primMST(int graph[][])
40+
{
41+
// Array to store constructed MST
42+
int parent[] = new int[V];
43+
44+
// Key values used to pick minimum weight edge in cut
45+
int key[] = new int[V];
46+
47+
// To represent set of vertices included in MST
48+
Boolean mstSet[] = new Boolean[V];
49+
50+
// Initialize all keys as INFINITE
51+
for (int i = 0; i < V; i++) {
52+
key[i] = Integer.MAX_VALUE;
53+
mstSet[i] = false;
54+
}
55+
56+
// Always include first 1st vertex in MST.
57+
key[0] = 0; // Make key 0 so that this vertex is
58+
// picked as first vertex
59+
parent[0] = -1; // First node is always root of MST
60+
61+
// The MST will have V vertices
62+
for (int count = 0; count < V - 1; count++) {
63+
// Pick thd minimum key vertex from the set of vertices
64+
// not yet included in MST
65+
int u = minKey(key, mstSet);
66+
67+
// Add the picked vertex to the MST Set
68+
mstSet[u] = true;
69+
70+
// Update key value and parent index of the adjacent
71+
// vertices of the picked vertex. Consider only those
72+
// vertices which are not yet included in MST
73+
for (int v = 0; v < V; v++)
74+
75+
// graph[u][v] is non zero only for adjacent vertices of m
76+
// mstSet[v] is false for vertices not yet included in MST
77+
// Update the key only if graph[u][v] is smaller than key[v]
78+
if (graph[u][v] != 0 && mstSet[v] == false && graph[u][v] < key[v]) {
79+
parent[v] = u;
80+
key[v] = graph[u][v];
81+
}
82+
}
83+
84+
// print the constructed MST
85+
printMST(parent, graph);
86+
}
87+
88+
public static void main(String[] args)
89+
{
90+
/* Let us create the following graph
91+
2 3
92+
(0)--(1)--(2)
93+
| / \ |
94+
6| 8/ \5 |7
95+
| / \ |
96+
(3)-------(4)
97+
9 */
98+
MST t = new MST();
99+
int graph[][] = new int[][] { { 0, 2, 0, 6, 0 },
100+
{ 2, 0, 3, 8, 5 },
101+
{ 0, 3, 0, 0, 7 },
102+
{ 6, 8, 0, 0, 9 },
103+
{ 0, 5, 7, 9, 0 } };
104+
105+
// Print the solution
106+
t.primMST(graph);
107+
}
108+
}

0 commit comments

Comments
 (0)