-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskalTest.java
177 lines (149 loc) · 4.53 KB
/
KruskalTest.java
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
/* KruskalTest.java */
/**
* The KruskalTest class tests the Kruskal class.
*/
import graph.*;
import graphalg.*;
import java.util.*;
public class KruskalTest {
private static final int VERTICES = 10;
private static final int MAXINT = 100;
private static boolean tree = true;
private static boolean minTree = true;
public static void addRandomEdges(WUGraph g, Object[] vertArray) {
int i, j;
System.out.println("Adding random edges to graph.");
Random random = new Random(3); // Create a "Random" object with seed 0
for (i = 0; i < vertArray.length; i++) {
for (j = i; j < vertArray.length; j++) {
int r = random.nextInt() % MAXINT; // Between -99 and 99
if (r >= 0) {
g.addEdge(vertArray[i], vertArray[j], r);
}
}
}
}
public static void DFS(WUGraph t, DFSVertex current, DFSVertex prev,
int[] maxOnPath, int maxEdge) {
Neighbors neigh;
int i;
current.visited = true;
maxOnPath[current.number] = maxEdge;
neigh = t.getNeighbors(current);
if (neigh != null) {
for (i = 0; i < neigh.neighborList.length; i++) {
DFSVertex next = (DFSVertex) neigh.neighborList[i];
if (next.visited) {
if ((next != current) && (next != prev)) {
tree = false;
return;
}
} else if (neigh.weightList[i] > maxEdge) {
DFS(t, next, current, maxOnPath, neigh.weightList[i]);
} else {
DFS(t, next, current, maxOnPath, maxEdge);
}
if (!tree) {
return;
}
}
}
}
public static void DFSTest(WUGraph g, WUGraph t, DFSVertex[] vertArray) {
int[][] maxOnPath;
Neighbors neigh;
int i, j;
System.out.println("Testing the tree.");
maxOnPath = new int[VERTICES][VERTICES];
for (i = 0; i < VERTICES; i++) {
for (j = 0; j < VERTICES; j++) {
vertArray[j].visited = false;
}
DFS(t, vertArray[i], null, maxOnPath[i], -MAXINT);
for (j = 0; j < VERTICES; j++) {
if (!vertArray[j].visited) {
tree = false;
}
}
if (!tree) {
return;
}
}
// for (i = 0; i < vertArray.length; i++) {
// for (j = 0; j < vertArray.length; j++) {
// System.out.print(" " + maxOnPath[i][j]);
// }
// System.out.println();
// }
for (i = 0; i < VERTICES; i++) {
neigh = g.getNeighbors(vertArray[i]);
if (neigh != null) {
for (j = 0; j < neigh.neighborList.length; j++) {
int v = ((DFSVertex) neigh.neighborList[j]).number;
if (neigh.weightList[j] < maxOnPath[i][v]) {
minTree = false;
}
}
}
}
}
public static void main(String[] args) {
int i, j;
int score;
WUGraph g, t;
DFSVertex[] vertArray;
System.out.println("Running minimum spanning tree test.");
System.out.println("Creating empty graph.");
g = new WUGraph();
System.out.println("Adding " + VERTICES + " vertices.");
vertArray = new DFSVertex[VERTICES];
for (i = 0; i < VERTICES; i++) {
vertArray[i] = new DFSVertex();
vertArray[i].number = i;
g.addVertex(vertArray[i]);
}
addRandomEdges(g, vertArray);
// for (i = 0; i < vertArray.length; i++) {
// for (j = 0; j < vertArray.length; j++) {
// if (g.isEdge(vertArray[i], vertArray[j])) {
// System.out.print(" " + g.weight(vertArray[i], vertArray[j]));
// } else {
// System.out.print(" *");
// }
// }
// System.out.println();
// }
System.out.println("Finding the minimum spanning tree.");
t = Kruskal.minSpanTree(g);
// for (i = 0; i < vertArray.length; i++) {
// for (j = 0; j < vertArray.length; j++) {
// if (t.isEdge(vertArray[i], vertArray[j])) {
// System.out.print(" " + t.weight(vertArray[i], vertArray[j]));
// } else {
// System.out.print(" *");
// }
// }
// System.out.println();
// }
DFSTest(g, t, vertArray);
if (tree) {
System.out.println("One point for creating a tree.");
if (minTree) {
System.out.println("Two points for creating a minimum spanning tree.");
score = 3;
} else {
System.out.println("Not a minimum spanning tree.");
score = 1;
}
} else {
System.out.println("Not a tree.");
score = 0;
}
System.out.println("Your Kruskal test score is " + score + " out of 3.");
System.out.println(" (Be sure also to run WUGTest.java.)");
}
}
class DFSVertex {
boolean visited;
int number;
}