Skip to content

Commit 04fc986

Browse files
author
Jiang Haifeng
committed
添加 测试用例
1 parent fc97547 commit 04fc986

File tree

1 file changed

+175
-2
lines changed

1 file changed

+175
-2
lines changed

GraphTheory/src/weight/Main.java

Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package weight;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import java.util.Random;
46

57
public class Main {
@@ -103,12 +105,102 @@ public void lazyPrimMst() {
103105
int M = 10;
104106
Random random = new Random();
105107

108+
109+
List<Edge> listEdge = new ArrayList<>();
110+
for (int i = 0; i < M; i++) {
111+
int a = random.nextInt(N);
112+
int b = random.nextInt(N);
113+
double weight = Math.random();
114+
listEdge.add(new Edge(a, b, weight));
115+
}
116+
106117
// weight.SparseGraph
107118
SparseGraph sparseGraph = new SparseGraph(N, false);
119+
for (Edge edge : listEdge) {
120+
int a = edge.v();
121+
int b = edge.w();
122+
double weight = edge.wt();
123+
sparseGraph.addEdge(a, b, weight);
124+
}
125+
System.out.println(sparseGraph);
126+
127+
128+
// O(E)
129+
// 打印所有边
130+
for (int v = 0; v < N; v++) {
131+
System.out.print(v + " : ");
132+
Iterable<Edge> iterator = sparseGraph.adj(v);
133+
for (Edge w : iterator) {
134+
System.out.print(w + " ");
135+
}
136+
System.out.println();
137+
}
138+
139+
// 打印最小生成树
140+
LazyPrimMST lazySparseGraphPrimMST = new LazyPrimMST(sparseGraph);
141+
Iterable<Edge> sparseGraphIterator = lazySparseGraphPrimMST.mstEdges();
142+
for (Edge w : sparseGraphIterator) {
143+
System.out.print(w + " ");
144+
}
145+
System.out.println();
146+
System.out.println(lazySparseGraphPrimMST.reuslt());
147+
148+
149+
System.out.println("----------分割线----------");
150+
151+
// weight.DenseGraph
152+
DenseGraph denseGraph = new DenseGraph(N, false);
153+
for (Edge edge : listEdge) {
154+
int a = edge.v();
155+
int b = edge.w();
156+
double weight = edge.wt();
157+
denseGraph.addEdge(a, b, weight);
158+
}
159+
System.out.println(denseGraph);
160+
161+
// O(V^2)
162+
// 打印所有边
163+
for (int v = 0; v < N; v++) {
164+
System.out.print(v + " : ");
165+
Iterable<Edge> iterator = denseGraph.adj(v);
166+
for (Edge w : iterator) {
167+
System.out.print(w + " ");
168+
}
169+
System.out.println();
170+
}
171+
172+
// 打印最小生成树
173+
LazyPrimMST lazyDenseGraphPrimMST = new LazyPrimMST(denseGraph);
174+
Iterable<Edge> denseGraphIterator = lazyDenseGraphPrimMST.mstEdges();
175+
for (Edge w : denseGraphIterator) {
176+
System.out.print(w + " ");
177+
}
178+
System.out.println();
179+
System.out.println(lazyDenseGraphPrimMST.reuslt());
180+
181+
}
182+
183+
184+
public void primMst() {
185+
int N = 10;
186+
int M = 10;
187+
Random random = new Random();
188+
189+
List<Edge> listEdge = new ArrayList<>();
108190
for (int i = 0; i < M; i++) {
109191
int a = random.nextInt(N);
110192
int b = random.nextInt(N);
111193
double weight = Math.random();
194+
listEdge.add(new Edge(a, b, weight));
195+
}
196+
197+
198+
// weight.SparseGraph
199+
SparseGraph sparseGraph = new SparseGraph(N, false);
200+
for (Edge edge : listEdge) {
201+
int a = edge.v();
202+
int b = edge.w();
203+
double weight = edge.wt();
112204
sparseGraph.addEdge(a, b, weight);
113205
}
114206
System.out.println(sparseGraph);
@@ -126,7 +218,7 @@ public void lazyPrimMst() {
126218
}
127219

128220
// 打印最小生成树
129-
LazyPrimMST lazySparseGraphPrimMST = new LazyPrimMST(sparseGraph);
221+
PrimMST lazySparseGraphPrimMST = new PrimMST(sparseGraph);
130222
Iterable<Edge> sparseGraphIterator = lazySparseGraphPrimMST.mstEdges();
131223
for (Edge w : sparseGraphIterator) {
132224
System.out.print(w + " ");
@@ -139,10 +231,89 @@ public void lazyPrimMst() {
139231

140232
// weight.DenseGraph
141233
DenseGraph denseGraph = new DenseGraph(N, false);
234+
for (Edge edge : listEdge) {
235+
int a = edge.v();
236+
int b = edge.w();
237+
double weight = edge.wt();
238+
denseGraph.addEdge(a, b, weight);
239+
}
240+
System.out.println(denseGraph);
241+
242+
// O(V^2)
243+
// 打印所有边
244+
for (int v = 0; v < N; v++) {
245+
System.out.print(v + " : ");
246+
Iterable<Edge> iterator = denseGraph.adj(v);
247+
for (Edge w : iterator) {
248+
System.out.print(w + " ");
249+
}
250+
System.out.println();
251+
}
252+
253+
// 打印最小生成树
254+
PrimMST lazyDenseGraphPrimMST = new PrimMST(denseGraph);
255+
Iterable<Edge> denseGraphIterator = lazyDenseGraphPrimMST.mstEdges();
256+
for (Edge w : denseGraphIterator) {
257+
System.out.print(w + " ");
258+
}
259+
System.out.println();
260+
System.out.println(lazyDenseGraphPrimMST.reuslt());
261+
262+
}
263+
264+
265+
public void kruskalMst() {
266+
int N = 10;
267+
int M = 10;
268+
Random random = new Random();
269+
List<Edge> listEdge = new ArrayList<>();
142270
for (int i = 0; i < M; i++) {
143271
int a = random.nextInt(N);
144272
int b = random.nextInt(N);
145273
double weight = Math.random();
274+
listEdge.add(new Edge(a, b, weight));
275+
}
276+
277+
// weight.SparseGraph
278+
SparseGraph sparseGraph = new SparseGraph(N, false);
279+
for (Edge edge : listEdge) {
280+
int a = edge.v();
281+
int b = edge.w();
282+
double weight = edge.wt();
283+
sparseGraph.addEdge(a, b, weight);
284+
}
285+
System.out.println(sparseGraph);
286+
287+
288+
// O(E)
289+
// 打印所有边
290+
for (int v = 0; v < N; v++) {
291+
System.out.print(v + " : ");
292+
Iterable<Edge> iterator = sparseGraph.adj(v);
293+
for (Edge w : iterator) {
294+
System.out.print(w + " ");
295+
}
296+
System.out.println();
297+
}
298+
299+
// 打印最小生成树
300+
KruskalMST lazySparseGraphPrimMST = new KruskalMST(sparseGraph);
301+
Iterable<Edge> sparseGraphIterator = lazySparseGraphPrimMST.mstEdges();
302+
for (Edge w : sparseGraphIterator) {
303+
System.out.print(w + " ");
304+
}
305+
System.out.println();
306+
System.out.println(lazySparseGraphPrimMST.reuslt());
307+
308+
309+
System.out.println("----------分割线----------");
310+
311+
// weight.DenseGraph
312+
DenseGraph denseGraph = new DenseGraph(N, false);
313+
for (Edge edge : listEdge) {
314+
int a = edge.v();
315+
int b = edge.w();
316+
double weight = edge.wt();
146317
denseGraph.addEdge(a, b, weight);
147318
}
148319
System.out.println(denseGraph);
@@ -159,7 +330,7 @@ public void lazyPrimMst() {
159330
}
160331

161332
// 打印最小生成树
162-
LazyPrimMST lazyDenseGraphPrimMST = new LazyPrimMST(denseGraph);
333+
KruskalMST lazyDenseGraphPrimMST = new KruskalMST(denseGraph);
163334
Iterable<Edge> denseGraphIterator = lazyDenseGraphPrimMST.mstEdges();
164335
for (Edge w : denseGraphIterator) {
165336
System.out.print(w + " ");
@@ -175,6 +346,8 @@ public static void main(String[] args) {
175346
// main.adjIteratorMain();
176347
// main.iteratorMain();
177348
main.lazyPrimMst();
349+
// main.primMst();
350+
// main.kruskalMst();
178351

179352
}
180353
}

0 commit comments

Comments
 (0)