Skip to content

Commit f0c416b

Browse files
author
Jiang Haifeng
committed
初步完成 邻接表-稀疏图
1 parent 8332e45 commit f0c416b

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

GraphTheory/src/DenseGraph.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public boolean hasEdge(int v, int w) {
5353
return g.get(v).get(w);
5454
}
5555

56+
public Iterable<Integer> pathTo(int v) {
57+
return null;
58+
}
5659

60+
// 暂废弃
5761
class adjIterator implements Iterator {
5862
private DenseGraph G;
5963
private int v;

GraphTheory/src/Main.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import java.util.Iterator;
21
import java.util.Random;
32

43
public class Main {
@@ -14,10 +13,15 @@ public static void main(String[] args) {
1413
int b = random.nextInt(N);
1514
sparseGraph.addEdge(a, b);
1615
}
16+
System.out.println(sparseGraph);
1717

1818
for (int v = 0; v < N; v++) {
19-
System.out.println(v + " : ");
20-
SparseGraph.AdjIterator iterator = new SparseGraph.AdjIterator(sparseGraph, v);
19+
System.out.print(v + " : ");
20+
SparseGraph.adjIterator iterator = sparseGraph.new adjIterator(sparseGraph, v);
21+
while (iterator.hasNext()) {
22+
System.out.print(iterator.next() + " ");
23+
}
24+
System.out.println();
2125
}
2226
}
2327
}

GraphTheory/src/SparseGraph.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java.util.ArrayList;
22
import java.util.Iterator;
33
import java.util.List;
4+
import java.util.Stack;
45

56
// 稀疏图 - 邻接表
67
public class SparseGraph {
@@ -57,20 +58,40 @@ public boolean hasEdge(int v, int w) {
5758
return false;
5859
}
5960

60-
public class AdjIterator implements Iterator {
61+
@Override
62+
public String toString() {
63+
StringBuilder stringBuilder = new StringBuilder();
64+
stringBuilder.append("SparseGraph\n");
65+
stringBuilder.append("-----------\n");
66+
for (int i = 0; i < g.size(); i++) {
67+
List<Integer> line = g.get(i);
68+
stringBuilder.append(i);
69+
stringBuilder.append(" : ");
70+
for (int j = 0; j < line.size(); j++) {
71+
int v = line.get(j);
72+
stringBuilder.append(v);
73+
if (j < line.size() - 1)
74+
stringBuilder.append(", ");
75+
}
76+
stringBuilder.append("\n");
77+
}
78+
return stringBuilder.toString();
79+
}
80+
81+
public class adjIterator implements Iterator {
6182
private SparseGraph G;
6283
private int v;
6384
int index;
6485

65-
public AdjIterator(SparseGraph graph, int v) {
86+
public adjIterator(SparseGraph graph, int v) {
6687
this.G = graph;
6788
this.v = v;
68-
this.index = 0;
89+
this.index = -1;
6990
}
7091

7192
@Override
7293
public boolean hasNext() {
73-
return index >= G.g.get(v).size();
94+
return index + 1 < G.g.get(v).size();
7495
}
7596

7697
@Override

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
* [红黑树_pending](./RedBlackTree/src/RBTree.java)
4848
- 【哈希表】
4949
- [HashTable](./HashTable/src/HashTable.java)
50+
- 【图论】
51+
- [稠密图 - 邻接矩阵](./GraphTheory/src/DenseGraph.java)
52+
- [稀疏图 - 邻接表](./GraphTheory/src/SparseGraph.java)
5053

5154

5255
#### 我的相关仓库

0 commit comments

Comments
 (0)