-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheapest Pair of Spanning Trees
300 lines (244 loc) · 9.63 KB
/
Cheapest Pair of Spanning Trees
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import java.io.*;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.lang.Math;
import java.util.Comparator;
class Main {
public static void main(String[] args) {
// Uncomment the following two lines if you want to read from a file
In.open("public/test1.in");
Out.compareTo("public/test1.out");
int n=In.readInt(); // number of vertices
int m=In.readInt(); // number of edges
// The following two arrays stores the information of edges
int[][] edge_array=new int[m][3];
// Read edges
for(int i=0;i<m;i++){
edge_array[i][0]=In.readInt(); // one endpoint
edge_array[i][1]=In.readInt(); // the other endpoint
edge_array[i][2]=In.readInt(); // weight
}
Graph G= new Graph(n, m, edge_array);
Out.println(G.Cheapest_Pair_of_Spanning_Trees());
// Uncomment the following line if you want to read from a file
In.close();
}
}
class Graph{
private int n; // number of vertices
private int m; // number of edges
private int[] degrees; // degrees[i]: the degree of vertex i
private int[][] edges; // edges[i][j]: the endpoint of the j-th edge of vertex i
private int[][] weights; // weights[i][j]: the weight of the j-th edge of vertex i
Graph(int n, int m, int[][] edge_array){
this.n=n;
this.m=m;
degrees=new int[n];
for(int i=0;i<n;i++){
degrees[i]=0;
}
for(int i=0;i<m;i++){
degrees[edge_array[i][0]]++;
degrees[edge_array[i][1]]++;
}
edges=new int[n][];
weights=new int[n][];
for(int i=0;i<n;i++){
if(degrees[i]!=0){
edges[i]=new int[degrees[i]];
weights[i]=new int[degrees[i]];
degrees[i]=0;
}
else{
edges[i]=null;
weights[i]=null;
}
}
for(int i=0;i<m;i++){
edges[edge_array[i][0]][degrees[edge_array[i][0]]]=edge_array[i][1];
edges[edge_array[i][1]][degrees[edge_array[i][1]]]=edge_array[i][0];
weights[edge_array[i][0]][degrees[edge_array[i][0]]]=edge_array[i][2];
weights[edge_array[i][1]][degrees[edge_array[i][1]]]=edge_array[i][2];
degrees[edge_array[i][0]]++;
degrees[edge_array[i][1]]++;
}
}
public int Cheapest_Pair_of_Spanning_Trees(){
// Please complete this Method
// Vertices u, v are supposed to be vertices 0, 1
//initialize two queues, add
PriorityQueue<Pair> heapU = new PriorityQueue<>(new CustomComparator());
PriorityQueue<Pair> heapV = new PriorityQueue<>(new CustomComparator());
//add all vertices to both queues, setting key to infinity, setting start vertices to zero
for (int i=0;i<n;i++){
//if vertex is u, add it with key 0 to heapU
if(i==0){
heapU.add(new Pair(i, 0));
heapV.add(new Pair(i, Integer.MAX_VALUE));
}
//if vertex is v, add it with key 0 to heapV
else if(i==1){
heapU.add(new Pair(i, Integer.MAX_VALUE));
heapV.add(new Pair(i, 0));
}
//else its just some normal vertex, add to both heaps with key = infinity
else {
heapU.add(new Pair(i, Integer.MAX_VALUE));
heapV.add(new Pair(i, Integer.MAX_VALUE));
}
}
//initialize two linked lists to store MSTu and MSTv, add start vertices to MSTu and MSTv
LinkedList<Pair> MSTu = new LinkedList<>();
LinkedList<Pair> MSTv = new LinkedList<>();
//array to store wether vertex i is in the MST or not
boolean[] inMST = new boolean[n];
//array to store parents of extracted vertices
int[] parentU = new int[n];
parentU[0]=0;
int[] parentV = new int[n];
parentV[1]=1;
//variable to return the tree size
int treeSize = 0;
while(heapU.peek()!=null && heapV.peek()!=null){
Out.println("Heap U size "+heapU.size());
Out.println("Heap V size "+heapV.size());
//if extract_min of heapU is smaller (or equal) than extract_min of heapV...
Out.println("extract_min heapU is "+heapU.peek().value+", extract_min heapV is "+heapV.peek().value);
if(heapU.peek().value<=heapV.peek().value){
//...extract it...
Pair toAddU = heapU.poll();
Out.println("vertex "+toAddU.index+" extracted from heapU, Heap U size "+heapU.size());
//...if its not in any MST yet, add it to U, set its inMST value to true, increase tree size, else do nothing...
if (!inMST[toAddU.index]){
MSTu.add(toAddU);
Out.println("vertex "+toAddU.index+" added to MST");
inMST[toAddU.index]=true;
for(int i=0;i<degrees[parentU[toAddU.index]];i++){
if(toAddU.index==edges[parentU[toAddU.index]][i]){
treeSize+=weights[parentU[toAddU.index]][i];
Out.println("weight added to MST from vertex "+parentU[toAddU.index]+" to vertex "+toAddU.index+" is "+ weights[parentU[toAddU.index]][i]);
}
}
//relax all edges (i.e. for all outgoing edges from toAdd, adjust value in heapU)
//1. convert heap into array to be traversed
Pair[] heapArrayU = heapU.toArray(new Pair[heapU.size()]);
//Out.println("heap converted to array");
//2. clear the heap
heapU.clear();
//Out.println("heap cleared");
//3.iterate over element of array, if its a neighbor of the extracted vertex, relax its edge
for (int i=0;i<heapArrayU.length;i++){
for(int j=0;j<degrees[toAddU.index];j++){
//if vertex in the queue is a neighbor of the vertex we extracted
if(heapArrayU[i].index==edges[toAddU.index][j]){
//relax its edge (IF relaing is smaller than value vertex already has)
if(heapArrayU[i].value>toAddU.value+weights[toAddU.index][j]){
heapArrayU[i].value=toAddU.value+weights[toAddU.index][j];
Out.println("vertex "+heapArrayU[i].index+" relaxed, new value is "+heapArrayU[i].value);
}
//set its parent to the vertex we extracted
parentU[i]=toAddU.index;
}
}
}
//4. add elements back into heap
for (int i=0;i<heapArrayU.length;i++){
heapU.add(heapArrayU[i]);
}
}
}
//if extract_min of heapV is smaller (or equal) than extract_min of heapU...
else{
//...extract it...
Pair toAddV = heapV.poll();
Out.println("vertex "+toAddV.index+" extracted from heapV, Heap V size "+heapV.size());
//...if its not in any MST yet, add it to V, set its inMST value to true, increase the treeSize by the weight else do nothing...
if (!inMST[toAddV.index]){
MSTv.add(toAddV);
Out.println("vertex "+toAddV.index+" added to MST");
inMST[toAddV.index]=true;
for(int i=0;i<degrees[parentV[toAddV.index]];i++){
if(toAddV.index==edges[parentV[toAddV.index]][i]){
treeSize+=weights[parentV[toAddV.index]][i];
}
}
//relax all edges (i.e. for all outgoing edges from toAdd, adjust value in heapU)
//1. convert heap into array to be traversed
Pair[] heapArrayV = heapV.toArray(new Pair[heapV.size()]);
//2. clear the heap
heapV.clear();
//3.iterate over element of array, if its a neighbor of the extracted vertex, relax its edge
for (int i=0;i<heapArrayV.length;i++){
for(int j=0;j<degrees[toAddV.index];j++){
//if vertex in the queue is a neighbor of the vertex we extracted
if(heapArrayV[i].index==edges[toAddV.index][j]){
//relax its edge
if(heapArrayV[i].value>toAddV.value+weights[toAddV.index][j]){
heapArrayV[i].value=toAddV.value+weights[toAddV.index][j];
Out.println("vertex "+heapArrayV[i].index+" relaxed, new value is "+heapArrayV[i].value);
}
//set its parent to the vertex we extracted
parentV[i]=toAddV.index;
}
}
}
//4. add elements back into heap
for (int i=0;i<heapArrayV.length;i++){
heapV.add(heapArrayV[i]);
}
}
}
}
//when algo is finished, we will have the cheapest pair of trees
//print treeV
Out.print("MSTv contains vertices [");
for (int i=0;i<MSTv.size();i++){
Out.print(MSTv.get(i).index+" ");
}
Out.print("]");
Out.println();
//print TreeU
Out.print("MSTu contains vertices [");
for (int i=0;i<MSTu.size();i++){
Out.print(MSTu.get(i).index+" ");
}
Out.print("]");
Out.println();
return treeSize;
}
}
class Pair implements Comparable<Pair> {
public int index;
public int value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
@Override
public int compareTo(Pair other) {
if(this.value < other.value){
return -1;
}
else if(this.value==other.value){
return 0;
}
else{
return 1;
}
}
}
class CustomComparator implements Comparator<Pair>{
public int compare(Pair pair1, Pair pair2){
int value = pair1.compareTo(pair2);
if (value>0){
return 1;
}
else if(value<0){
return -1;
}
else{
return 0;
}
}
}