File tree Expand file tree Collapse file tree 5 files changed +69
-8
lines changed
Course Code (C++)/07-BFS-and-Shortest-Path
Course Code (Java)/07-BFS-and-Shortest-Path Expand file tree Collapse file tree 5 files changed +69
-8
lines changed Original file line number Diff line number Diff line change @@ -14,17 +14,33 @@ int main() {
14
14
SparseGraph g = SparseGraph (7 , false );
15
15
ReadGraph<SparseGraph> readGraph (g, filename);
16
16
g.show ();
17
- cout<<endl;
18
17
19
18
// 比较使用深度优先遍历和广度优先遍历获得路径的不同
20
19
// 广度优先遍历获得的是无权图的最短路径
21
20
Path<SparseGraph> dfs (g,0 );
22
- cout<< " DFS : " ;
21
+ cout << " DFS : " ;
23
22
dfs.showPath (6 );
24
23
25
24
ShortestPath<SparseGraph> bfs (g,0 );
26
- cout<< " BFS : " ;
25
+ cout << " BFS : " ;
27
26
bfs.showPath (6 );
28
27
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
+
29
45
return 0 ;
30
46
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ public static void main(String[] args) {
9
9
SparseGraph g = new SparseGraph (7 , false );
10
10
ReadGraph readGraph = new ReadGraph (g , filename );
11
11
g .show ();
12
- System .out .println ();
13
12
14
13
// 比较使用深度优先遍历和广度优先遍历获得路径的不同
15
14
// 广度优先遍历获得的是无权图的最短路径
@@ -20,5 +19,22 @@ public static void main(String[] args) {
20
19
ShortestPath bfs = new ShortestPath (g ,0 );
21
20
System .out .print ("BFS : " );
22
21
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 );
23
39
}
24
40
}
Original file line number Diff line number Diff line change 3
3
import java .util .Vector ;
4
4
import java .util .Stack ;
5
5
import java .util .LinkedList ;
6
+ import java .util .Queue ;
6
7
7
8
public class ShortestPath {
8
9
@@ -31,16 +32,16 @@ public ShortestPath(Graph graph, int s){
31
32
this .s = s ;
32
33
33
34
// 无向图最短路径算法, 从s开始广度优先遍历整张图
34
- LinkedList <Integer > q = new LinkedList <Integer >();
35
+ Queue <Integer > q = new LinkedList <Integer >();
35
36
36
- q .push ( s );
37
+ q .add ( s );
37
38
visited [s ] = true ;
38
39
ord [s ] = 0 ;
39
40
while ( !q .isEmpty () ){
40
- int v = q .pop ();
41
+ int v = q .remove ();
41
42
for ( int i : G .adj (v ) )
42
43
if ( !visited [i ] ){
43
- q .push (i );
44
+ q .add (i );
44
45
visited [i ] = true ;
45
46
from [i ] = v ;
46
47
ord [i ] = ord [v ] + 1 ;
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments