Skip to content

Commit c126be7

Browse files
committed
Chapter 07 section 07 new test case added. Java BFS bug fixed.
1 parent a5e5eb2 commit c126be7

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/main.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,33 @@ int main() {
1414
SparseGraph g = SparseGraph(7, false);
1515
ReadGraph<SparseGraph> readGraph(g, filename);
1616
g.show();
17-
cout<<endl;
1817

1918
// 比较使用深度优先遍历和广度优先遍历获得路径的不同
2019
// 广度优先遍历获得的是无权图的最短路径
2120
Path<SparseGraph> dfs(g,0);
22-
cout<<"DFS : ";
21+
cout << "DFS : ";
2322
dfs.showPath(6);
2423

2524
ShortestPath<SparseGraph> bfs(g,0);
26-
cout<<"BFS : ";
25+
cout << "BFS : ";
2726
bfs.showPath(6);
2827

28+
cout << endl;
29+
30+
filename = "testG1.txt";
31+
SparseGraph g2 = SparseGraph(13, false);
32+
ReadGraph<SparseGraph> readGraph2(g2, filename);
33+
g2.show();
34+
35+
// 比较使用深度优先遍历和广度优先遍历获得路径的不同
36+
// 广度优先遍历获得的是无权图的最短路径
37+
Path<SparseGraph> dfs2(g2,0);
38+
cout << "DFS : ";
39+
dfs2.showPath(3);
40+
41+
ShortestPath<SparseGraph> bfs2(g,0);
42+
cout << "BFS : ";
43+
bfs2.showPath(3);
44+
2945
return 0;
3046
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
13 13
2+
0 5
3+
4 3
4+
0 1
5+
9 12
6+
6 4
7+
5 4
8+
0 2
9+
11 12
10+
9 10
11+
0 6
12+
7 8
13+
9 11
14+
5 3

07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/Main.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public static void main(String[] args) {
99
SparseGraph g = new SparseGraph(7, false);
1010
ReadGraph readGraph = new ReadGraph(g, filename);
1111
g.show();
12-
System.out.println();
1312

1413
// 比较使用深度优先遍历和广度优先遍历获得路径的不同
1514
// 广度优先遍历获得的是无权图的最短路径
@@ -20,5 +19,22 @@ public static void main(String[] args) {
2019
ShortestPath bfs = new ShortestPath(g,0);
2120
System.out.print("BFS : ");
2221
bfs.showPath(6);
22+
23+
System.out.println();
24+
25+
filename = "testG1.txt";
26+
SparseGraph g2 = new SparseGraph(13, false);
27+
ReadGraph readGraph2 = new ReadGraph(g2, filename);
28+
g2.show();
29+
30+
// 比较使用深度优先遍历和广度优先遍历获得路径的不同
31+
// 广度优先遍历获得的是无权图的最短路径
32+
Path dfs2 = new Path(g2,0);
33+
System.out.print("DFS : ");
34+
dfs2.showPath(3);
35+
36+
ShortestPath bfs2 = new ShortestPath(g,0);
37+
System.out.print("BFS : ");
38+
bfs.showPath(3);
2339
}
2440
}

07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/ShortestPath.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Vector;
44
import java.util.Stack;
55
import java.util.LinkedList;
6+
import java.util.Queue;
67

78
public class ShortestPath {
89

@@ -31,16 +32,16 @@ public ShortestPath(Graph graph, int s){
3132
this.s = s;
3233

3334
// 无向图最短路径算法, 从s开始广度优先遍历整张图
34-
LinkedList<Integer> q = new LinkedList<Integer>();
35+
Queue<Integer> q = new LinkedList<Integer>();
3536

36-
q.push( s );
37+
q.add(s);
3738
visited[s] = true;
3839
ord[s] = 0;
3940
while( !q.isEmpty() ){
40-
int v = q.pop();
41+
int v = q.remove();
4142
for( int i : G.adj(v) )
4243
if( !visited[i] ){
43-
q.push(i);
44+
q.add(i);
4445
visited[i] = true;
4546
from[i] = v;
4647
ord[i] = ord[v] + 1;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
13 13
2+
0 5
3+
4 3
4+
0 1
5+
9 12
6+
6 4
7+
5 4
8+
0 2
9+
11 12
10+
9 10
11+
0 6
12+
7 8
13+
9 11
14+
5 3

0 commit comments

Comments
 (0)