-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialTreeList.java
More file actions
281 lines (252 loc) · 7 KB
/
PartialTreeList.java
File metadata and controls
281 lines (252 loc) · 7 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
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
package app;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import structures.Arc;
import structures.Graph;
import structures.MinHeap;
import structures.PartialTree;
import structures.Vertex;
/**
* Stores partial trees in a circular linked list
*
*/
public class PartialTreeList implements Iterable<PartialTree> {
/**
* Inner class - to build the partial tree circular linked list
*
*/
public static class Node {
/**
* Partial tree
*/
public PartialTree tree;
/**
* Next node in linked list
*/
public Node next;
/**
* Initializes this node by setting the tree part to the given tree,
* and setting next part to null
*
* @param tree Partial tree
*/
public Node(PartialTree tree) {
this.tree = tree;
next = null;
}
}
/**
* Pointer to last node of the circular linked list
*/
private Node rear;
/**
* Number of nodes in the CLL
*/
private int size;
/**
* Initializes this list to empty
*/
public PartialTreeList() {
rear = null;
size = 0;
}
/**
* Adds a new tree to the end of the list
*
* @param tree Tree to be added to the end of the list
*/
public void append(PartialTree tree) {
Node ptr = new Node(tree);
if (rear == null) {
ptr.next = ptr;
} else {
ptr.next = rear.next;
rear.next = ptr;
}
rear = ptr;
size++;
}
/**
* Initializes the algorithm by building single-vertex partial trees
*
* @param graph Graph for which the MST is to be found
* @return The initial partial tree list
*/
public static PartialTreeList initialize(Graph graph) {
/* COMPLETE THIS METHOD */
PartialTreeList partialList = new PartialTreeList();
for(int j = 0; j < graph.vertices.length; j++) {
PartialTree parTree = new PartialTree(graph.vertices[j]);
//Node initialize = new Node(indTree);
Vertex vertex1 = graph.vertices[j];
Vertex.Neighbor neighborsV = graph.vertices[j].neighbors;
while(neighborsV != null) {
Vertex neighborOfV = neighborsV.vertex;
int weight = neighborsV.weight;
Arc arcV = new Arc(vertex1, neighborOfV, weight);
parTree.getArcs().insert(arcV);
neighborsV = neighborsV.next;
}
partialList.append(parTree);
}
return partialList;
}
/**
* Executes the algorithm on a graph, starting with the initial partial tree list
* for that graph
*
* @param ptlist Initial partial tree list
* @return Array list of all arcs that are in the MST - sequence of arcs is irrelevant
*/
public static ArrayList<Arc> execute(PartialTreeList ptlist) {
/* COMPLETE THIS METHOD */
ArrayList<Arc> arcs = new ArrayList<Arc>();
while(ptlist.size > 1) {
Arc arc = null;
Vertex vertex1 = null;
Vertex vertex2 = null;
boolean check = true;
PartialTree parTree = ptlist.remove();
//System.out.println("First In List: " + firstParTree.toString());
while(check) {
arc = parTree.getArcs().deleteMin(); // (A, C, 1) // E D
vertex1 = arc.getv1(); // A E
vertex2 = arc.getv2(); // C D
check = rootCheck(vertex1, vertex2);
}
if(!check) {
arcs.add(arc);
}
PartialTree secParTree = ptlist.removeTreeContaining(vertex2);
System.out.println("Here: " + secParTree.toString());
parTree.merge(secParTree);
ptlist.append(parTree);
}
return arcs;
}
private static boolean rootCheck(Vertex v1, Vertex v2) {
if(v1.getRoot().equals(v2.getRoot())) {
return true; // if true v2 is there
}
return false;
}
/**
* Removes the tree that is at the front of the list.
*
* @return The tree that is removed from the front
* @throws NoSuchElementException If the list is empty
*/
public PartialTree remove()
throws NoSuchElementException {
if (rear == null) {
throw new NoSuchElementException("list is empty");
}
PartialTree ret = rear.next.tree;
if (rear.next == rear) {
rear = null;
} else {
rear.next = rear.next.next;
}
size--;
return ret;
}
/**
* Removes the tree in this list that contains a given vertex.
*
* @param vertex Vertex whose tree is to be removed
* @return The tree that is removed
* @throws NoSuchElementException If there is no matching tree
*/
public PartialTree removeTreeContaining(Vertex vertex)
throws NoSuchElementException {
/* COMPLETE THIS METHOD */
Node ptr = this.rear.next;
Node prev = this.rear;
PartialTree treeS = null;
do {
/*System.out.println("Remove: " + front.tree.toString());
System.out.println("Check: " + front.tree.getRoot().parent);
System.out.println("Name: " + vertex + " Parent: " + vertex.parent);*/
if(rear.next == rear && rootCheck(rear.tree.getRoot(), vertex)) {
//System.out.println("I am here rear");
treeS = rear.tree;
rear = null;
size--;
return treeS;
}
//System.out.println("Name: " + front.tree.getRoot().name);
if(rootCheck(ptr.tree.getRoot(), vertex)) {
treeS = ptr.tree;
if(ptr == rear) {
rear = prev;
}
prev.next = ptr.next;
size--;
return treeS;
}
prev = ptr;
ptr = ptr.next;
}while(ptr != this.rear.next);
throw new NoSuchElementException("No Such Element Found");
}
/*private static boolean check(Vertex parent, Vertex v2) { // B D
System.out.println("Hello");
if(parent.equals(v2)) {
System.out.println("returning true");
return true;
}
Vertex curr = v2;
while(!(curr.equals(curr.parent))) {
System.out.println("Inside");
curr = curr.parent;
if(curr.equals(parent)) {
System.out.println("returning true");
return true;
}
}
return false;
}*/
/**
* Gives the number of trees in this list
*
* @return Number of trees
*/
public int size() {
return size;
}
/**
* Returns an Iterator that can be used to step through the trees in this list.
* The iterator does NOT support remove.
*
* @return Iterator for this list
*/
public Iterator<PartialTree> iterator() {
return new PartialTreeListIterator(this);
}
private class PartialTreeListIterator implements Iterator<PartialTree> {
private PartialTreeList.Node ptr;
private int rest;
public PartialTreeListIterator(PartialTreeList target) {
rest = target.size;
ptr = rest > 0 ? target.rear.next : null;
}
public PartialTree next()
throws NoSuchElementException {
if (rest <= 0) {
throw new NoSuchElementException();
}
PartialTree ret = ptr.tree;
ptr = ptr.next;
rest--;
return ret;
}
public boolean hasNext() {
return rest != 0;
}
public void remove()
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
}