-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPrims.java
More file actions
75 lines (62 loc) · 2.28 KB
/
Prims.java
File metadata and controls
75 lines (62 loc) · 2.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
import javax.swing.ToolTipManager;
class Prims{
final static int TOTAL_VERTEX = 6;
static void primsAlgo(int [][] graphs){
// Create 3 Arrays
// Store the Weights
int weights [] = new int[TOTAL_VERTEX];
weights[0] = 0; // so first weight is the smaller
// All the Remaining Weights are consider as MAX
for(int i = 1; i<weights.length; i++){
weights[i] = Integer.MAX_VALUE;
}
// MSET Array
boolean mset [] = new boolean[TOTAL_VERTEX];
// Parent Array
int parent [] = new int[TOTAL_VERTEX];
parent[0] = -1; // SOurce vertex not having parent
// Loop for MST (V-1) Edges
for(int i = 0 ; i<TOTAL_VERTEX-1; i++){
// get the source vertex (Minimum)
int sourceVertex = getMinVertex(weights, mset);
mset[sourceVertex] = true; // Mark it used in MST
// After Getting the Source Vertex than get the Adjacent Vertex
//and do the Relax Operations (Weights Update)
for(int j =0; j<TOTAL_VERTEX; j++){
// Graph Cell weight not be 0 and MSET[index] is false (Not Part of MST) and graph[row][col] value < weight[index]
if(graphs[sourceVertex][j]!=0 && !mset[j]
&& graphs[sourceVertex][j]<weights[j]){
// Now do the relax operation (Update)
weights[j] = graphs[sourceVertex][j];
parent[j] = sourceVertex;
}
}
}
// Print MST
for(int i = 1; i<TOTAL_VERTEX; i++){
System.out.println(parent[i]+" "+i+" "+graphs[parent[i]][i]);
}
}
static int getMinVertex(int [] weights, boolean mset[]){
int min = Integer.MAX_VALUE;
int vertex =-1;
for(int i = 0; i<TOTAL_VERTEX; i++){
if(!mset[i] && weights[i]<min){
vertex = i;
min = weights[i];
}
}
return vertex; // Become Source Vertex
}
public static void main(String[] args) {
int graph[][] = {
{0,4,6,0,0,0},
{4,0,6,3,4,0},
{6,6,0,1,0,0},
{0,3,1,0,2,3},
{0,4,0,2,0,7},
{0,0,0,3,7,0}
};
primsAlgo(graph);
}
}