-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWGraph_DSTest.java
200 lines (180 loc) · 4.99 KB
/
WGraph_DSTest.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package tests;
import org.junit.jupiter.api.Test;
import ex1.src.WGraph_DS;
import ex1.src.node_info;
import ex1.src.weighted_graph;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
class WGraph_DSTest {
private static Random _rnd = null;
@Test
void nodeSize() {
weighted_graph g = new WGraph_DS();
g.addNode(0);
g.addNode(1);
g.addNode(1);
g.removeNode(2);
g.removeNode(1);
g.removeNode(1);
int s = g.nodeSize();
assertEquals(1,s);
}
@Test
void edgeSize() {
weighted_graph g = new WGraph_DS();
g.addNode(0);
g.addNode(1);
g.addNode(2);
g.addNode(3);
g.connect(0,1,1);
g.connect(0,2,2);
g.connect(0,3,3);
g.connect(0,1,1);
int e_size = g.edgeSize();
assertEquals(3, e_size);
double w03 = g.getEdge(0,3);
double w30 = g.getEdge(3,0);
assertEquals(w03, w30);
assertEquals(w03, 3);
}
@Test
void getV() {
weighted_graph g = new WGraph_DS();
g.addNode(0);
g.addNode(1);
g.addNode(2);
g.addNode(3);
g.connect(0,1,1);
g.connect(0,2,2);
g.connect(0,3,3);
g.connect(0,1,1);
Collection<node_info> v = g.getV();
Iterator<node_info> iter = v.iterator();
while (iter.hasNext()) {
node_info n = iter.next();
assertNotNull(n);
}
}
@Test
void hasEdge() {
int v = 10, e = v*(v-1)/2;
weighted_graph g = graph_creator(v,e,1);
for(int i=0;i<v;i++) {
for(int j=i+1;j<v;j++) {
boolean b = g.hasEdge(i,j);
assertTrue(b);
assertTrue(g.hasEdge(j,i));
}
}
}
@Test
void connect() {
weighted_graph g = new WGraph_DS();
g.addNode(0);
g.addNode(1);
g.addNode(2);
g.addNode(3);
g.connect(0,1,1);
g.connect(0,2,2);
g.connect(0,3,3);
g.removeEdge(0,1);
assertFalse(g.hasEdge(1,0));
g.removeEdge(2,1);
g.connect(0,1,1);
double w = g.getEdge(1,0);
assertEquals(w,1);
}
@Test
void removeNode() {
weighted_graph g = new WGraph_DS();
g.addNode(0);
g.addNode(1);
g.addNode(2);
g.addNode(3);
g.connect(0,1,1);
g.connect(0,2,2);
g.connect(0,3,3);
g.removeNode(4);
g.removeNode(0);
assertFalse(g.hasEdge(1,0));
int e = g.edgeSize();
assertEquals(0,e);
assertEquals(3,g.nodeSize());
}
@Test
void removeEdge() {
weighted_graph g = new WGraph_DS();
g.addNode(0);
g.addNode(1);
g.addNode(2);
g.addNode(3);
g.connect(0,1,1);
g.connect(0,2,2);
g.connect(0,3,3);
g.removeEdge(0,3);
double w = g.getEdge(0,3);
assertEquals(w,-1);
}
///////////////////////////////////
/**
* Generate a random graph with v_size nodes and e_size edges
* @param v_size
* @param e_size
* @param seed
* @return
*/
public static weighted_graph graph_creator(int v_size, int e_size, int seed) {
weighted_graph g = new WGraph_DS();
_rnd = new Random(seed);
for(int i=0;i<v_size;i++) {
g.addNode(i);
}
// Iterator<node_data> itr = V.iterator(); // Iterator is a more elegant and generic way, but KIS is more important
int[] nodes = nodes(g);
while(g.edgeSize() < e_size) {
int a = nextRnd(0,v_size);
int b = nextRnd(0,v_size);
int i = nodes[a];
int j = nodes[b];
double w = _rnd.nextDouble();
g.connect(i,j, w);
}
return g;
}
private static int nextRnd(int min, int max) {
double v = nextRnd(0.0+min, (double)max);
int ans = (int)v;
return ans;
}
private static double nextRnd(double min, double max) {
double d = _rnd.nextDouble();
double dx = max-min;
double ans = d*dx+min;
return ans;
}
/**
* Simple method for returning an array with all the node_data of the graph,
* Note: this should be using an Iterator<node_edge> to be fixed in Ex1
* @param g
* @return
*/
private static int[] nodes(weighted_graph g) {
int size = g.nodeSize();
Collection<node_info> V = g.getV();
node_info[] nodes = new node_info[size];
V.toArray(nodes); // O(n) operation
int[] ans = new int[size];
for(int i=0;i<size;i++) {ans[i] = nodes[i].getKey();}
Arrays.sort(ans);
return ans;
}
}