From c126be773bc913f2f26bbd5951e72e5dbe46222a Mon Sep 17 00:00:00 2001 From: liuyubobobo Date: Tue, 1 May 2018 14:57:28 +0800 Subject: [PATCH] Chapter 07 section 07 new test case added. Java BFS bug fixed. --- .../07-BFS-and-Shortest-Path/main.cpp | 22 ++++++++++++++++--- .../07-BFS-and-Shortest-Path/testG1.txt | 14 ++++++++++++ .../src/bobo/algo/Main.java | 18 ++++++++++++++- .../src/bobo/algo/ShortestPath.java | 9 ++++---- .../07-BFS-and-Shortest-Path/testG1.txt | 14 ++++++++++++ 5 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/testG1.txt create mode 100644 07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/testG1.txt diff --git a/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/main.cpp b/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/main.cpp index e78fe90..2593c12 100644 --- a/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/main.cpp +++ b/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/main.cpp @@ -14,17 +14,33 @@ int main() { SparseGraph g = SparseGraph(7, false); ReadGraph readGraph(g, filename); g.show(); - cout< dfs(g,0); - cout<<"DFS : "; + cout << "DFS : "; dfs.showPath(6); ShortestPath bfs(g,0); - cout<<"BFS : "; + cout << "BFS : "; bfs.showPath(6); + cout << endl; + + filename = "testG1.txt"; + SparseGraph g2 = SparseGraph(13, false); + ReadGraph readGraph2(g2, filename); + g2.show(); + + // 比较使用深度优先遍历和广度优先遍历获得路径的不同 + // 广度优先遍历获得的是无权图的最短路径 + Path dfs2(g2,0); + cout << "DFS : "; + dfs2.showPath(3); + + ShortestPath bfs2(g,0); + cout << "BFS : "; + bfs2.showPath(3); + return 0; } \ No newline at end of file diff --git a/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/testG1.txt b/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/testG1.txt new file mode 100644 index 0000000..8a67676 --- /dev/null +++ b/07-Graph-Basics/Course Code (C++)/07-BFS-and-Shortest-Path/testG1.txt @@ -0,0 +1,14 @@ +13 13 +0 5 +4 3 +0 1 +9 12 +6 4 +5 4 +0 2 +11 12 +9 10 +0 6 +7 8 +9 11 +5 3 diff --git a/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/Main.java b/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/Main.java index 7d58feb..2887e82 100644 --- a/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/Main.java +++ b/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/Main.java @@ -9,7 +9,6 @@ public static void main(String[] args) { SparseGraph g = new SparseGraph(7, false); ReadGraph readGraph = new ReadGraph(g, filename); g.show(); - System.out.println(); // 比较使用深度优先遍历和广度优先遍历获得路径的不同 // 广度优先遍历获得的是无权图的最短路径 @@ -20,5 +19,22 @@ public static void main(String[] args) { ShortestPath bfs = new ShortestPath(g,0); System.out.print("BFS : "); bfs.showPath(6); + + System.out.println(); + + filename = "testG1.txt"; + SparseGraph g2 = new SparseGraph(13, false); + ReadGraph readGraph2 = new ReadGraph(g2, filename); + g2.show(); + + // 比较使用深度优先遍历和广度优先遍历获得路径的不同 + // 广度优先遍历获得的是无权图的最短路径 + Path dfs2 = new Path(g2,0); + System.out.print("DFS : "); + dfs2.showPath(3); + + ShortestPath bfs2 = new ShortestPath(g,0); + System.out.print("BFS : "); + bfs.showPath(3); } } \ No newline at end of file diff --git a/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/ShortestPath.java b/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/ShortestPath.java index c380c9c..f0d66b8 100644 --- a/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/ShortestPath.java +++ b/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/src/bobo/algo/ShortestPath.java @@ -3,6 +3,7 @@ import java.util.Vector; import java.util.Stack; import java.util.LinkedList; +import java.util.Queue; public class ShortestPath { @@ -31,16 +32,16 @@ public ShortestPath(Graph graph, int s){ this.s = s; // 无向图最短路径算法, 从s开始广度优先遍历整张图 - LinkedList q = new LinkedList(); + Queue q = new LinkedList(); - q.push( s ); + q.add(s); visited[s] = true; ord[s] = 0; while( !q.isEmpty() ){ - int v = q.pop(); + int v = q.remove(); for( int i : G.adj(v) ) if( !visited[i] ){ - q.push(i); + q.add(i); visited[i] = true; from[i] = v; ord[i] = ord[v] + 1; diff --git a/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/testG1.txt b/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/testG1.txt new file mode 100644 index 0000000..8a67676 --- /dev/null +++ b/07-Graph-Basics/Course Code (Java)/07-BFS-and-Shortest-Path/testG1.txt @@ -0,0 +1,14 @@ +13 13 +0 5 +4 3 +0 1 +9 12 +6 4 +5 4 +0 2 +11 12 +9 10 +0 6 +7 8 +9 11 +5 3