- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPrimsAlgorithmMST.java
More file actions
Latest commit
160 lines (135 loc) · 6.32 KB
/
Copy pathPrimsAlgorithmMST.java
File metadata and controls
160 lines (135 loc) · 6.32 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
packageGraph;
importjava.util.*;
/**
* Input :
* Adjacency List :
* 1 -> (2, 2), (4, 6)
* 2 -> (1, 2), (4, 8), (3, 3), (5, 5)
* 3 -> (2, 3), (5, 7)
* 4 -> (1, 6), (2, 8)
* 5 -> (2, 5), (3, 7)
* Output :
* MST of the given graph is :
* Parent of node 1 is : 1
* Parent of node 2 is : 1
* Parent of node 3 is : 2
* Parent of node 4 is : 1
* Parent of node 5 is : 2
*/
publicclassPrimsAlgorithmMST {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
ArrayList<ArrayList<Pair2>> adjacencyList = newArrayList<>();
// Taking input for number of nodes
System.out.println("Enter the number of nodes:");
intnumberOfNodes = sc.nextInt();
// initializing adjacencyList with new ArrayList<>()
for (inti = 1; i <= numberOfNodes + 1; i++) {
adjacencyList.add(newArrayList<>());
}
// taking input of adjacent nodes of particular node
for (inti = 1; i <= numberOfNodes; i++) {
System.out.println("Enter number of adjacent nodes to the node : " + i);
intcountOfAdjacentNodes = sc.nextInt();
for (intj = 0; j < countOfAdjacentNodes; j++) {
// node value
System.out.println("Enter node:");
intnode = sc.nextInt();
// edge weight
System.out.println("Enter edge weight:");
intedgeWeight = sc.nextInt();
// adding (node, edge weight) in to the ith index of adjacency list
adjacencyList.get(i).add(newPair2(node, edgeWeight));
}
}
primBruteForce(adjacencyList, numberOfNodes); // Brute force Approach
primOptimised(adjacencyList, numberOfNodes); // Optimised Approach
}
// Prim's algorithm : Brute Force Approach
// TC : O(N^2)
publicstaticvoidprimBruteForce(ArrayList<ArrayList<Pair2>> adjacencyList, intnumberOfNodes){
// for storing the minimum distance
int[] key = newint[numberOfNodes+1];
Arrays.fill(key, Integer.MAX_VALUE);
key[1] = 0;
// for marking the nodes as visited
boolean[] mst = newboolean[numberOfNodes+1];
Arrays.fill(mst, false);
// for storing the parent of the nodes
int[] parent = newint[numberOfNodes+1];
Arrays.fill(parent, -1);
// Run the loop equal to number of edges because in MST there are (n-1) edges where n is number of nodes
for(inti=1 ; i<=numberOfNodes-1 ; i++){
intminDistance = Integer.MAX_VALUE; // for checking minimum distance node in key array
intnode = 0; // storing the index of key array which has minimum distance
// Run loop till numberOfNodes and check if the distance is minimum and the node is not visited
for(intj=1 ; j<=numberOfNodes ; j++){
if(!mst[j] && key[j] < minDistance){
minDistance = key[j];
node = j;
}
}
// now mark the node visited
mst[node] = true;
// check for adjacent nodes of the current node
for(Pair2adjacentNode : adjacencyList.get(node)){
// if the adjacent node is not visited and
// adjacent's edge weight < key[adjacent node] then store the adjacent's edge into the key[adjacent node]
// and store current node as parent node of adjacent node
if(!mst[adjacentNode.node] && adjacentNode.edgeWeight < key[adjacentNode.node]){
parent[adjacentNode.node] = node;
key[adjacentNode.node] = adjacentNode.edgeWeight;
}
}
}
System.out.println("\n\nMST of the given graph is (Brute Force method):");
for(intnode=1 ; node<=numberOfNodes ; node++){
if(parent[node] != -1) {
System.out.println("Parent of node " + node + " is : " + parent[node]);
}else{
System.out.println("Parent of node " + node + " is : 1");
}
}
}
// Prim's Algorithm : Optimised Approach
// TC : O(N * log N)
publicstaticvoidprimOptimised(ArrayList<ArrayList<Pair2>> adjacencyList, intnumberOfNodes){
// for storing the minimum distance
int[] key = newint[numberOfNodes+1];
Arrays.fill(key, Integer.MAX_VALUE);
key[1] = 0;
// for marking the nodes as visited
boolean[] mst = newboolean[numberOfNodes+1];
Arrays.fill(mst, false);
// for storing the parent of the nodes
int[] parent = newint[numberOfNodes+1];
Arrays.fill(parent, -1);
PriorityQueue<Pair2> minPQ = newPriorityQueue<>(numberOfNodes, newPair2());
minPQ.add(newPair2(1, key[1]));
// Run the loop equal to number of edges because in MST there are (n-1) edges where n is number of nodes
for(inti=1 ; i<=numberOfNodes-1 ; i++){
intnode = Objects.requireNonNull(minPQ.poll()).node;
// now mark the node visited
mst[node] = true;
// check for adjacent nodes of the current node
for(Pair2adjacentNode : adjacencyList.get(node)){
// if the adjacent node is not visited and
// adjacent's edge weight < key[adjacent node] then store the adjacent's edge into the key[adjacent node]
// and store current node as parent node of adjacent node
if(!mst[adjacentNode.node] && adjacentNode.edgeWeight < key[adjacentNode.node]){
parent[adjacentNode.node] = node;
key[adjacentNode.node] = adjacentNode.edgeWeight;
minPQ.add(newPair2(adjacentNode.node, key[adjacentNode.node]));
}
}
}
System.out.println("\n\nMST of the given graph is (Optimised method):");
for(intnode=1 ; node<=numberOfNodes ; node++){
if(parent[node] != -1) {
System.out.println("Parent of node " + node + " is : " + parent[node]);
}else{
System.out.println("Parent of node " + node + " is : 1");
}
}
}
}