圖算法系列之計算圖中最短路徑
本文轉載自微信公眾號「貝塔學JAVA」,作者Silently9527。轉載本文請聯系貝塔學JAVA公眾號。
前言
在前面兩篇中我們通過深度優先搜索可以從圖中找出一條通過頂點v到頂點w的路徑,但是深度優先搜索與頂點的輸入有很大的關系,找出來的路徑也不一定是最短的,通常情況下我們很多時候需要找出圖中的最短路徑,比如:地圖功能。這里我們就需要使用到廣度優先搜索算法
廣度優先搜索
依然使用之前定義的尋找路徑的API
- public class Paths {
- Paths(Graph graph, int s);
- boolean hasPathTo(int v); //判斷出從s->v是否存在路徑
- Iterable<Integer> pathTo(int v); //如果存在路徑,返回路徑
- }
在廣度優先搜索中,為了找出最短路徑,我們需要按照起點的順序來遍歷所有的頂點,而不在是使用遞歸來實現;算法的思路:
- 使用隊列來保存已經被標記過但是鄰接表還未被遍歷過的頂點
- 取出隊列中的下一個頂點v并標記它
- 將v相鄰的所有未被標記的頂點加入到隊列
在該算法中,為了保存路徑,我們依然需要使用一個邊的數組edgeTo[],用一顆父鏈樹來表示根節點到所有連通頂點的最短路徑。
- public class BreadthFirstPaths {
- private boolean marked[];
- private int[] edgeTo;
- private int s;
- private Queue<Integer> queue = new LinkedListQueue<>();
- public BreadthFirstPaths(Graph graph, int s) {
- this.s = s;
- this.marked = new boolean[graph.V()];
- this.edgeTo = new int[graph.V()];
- bfs(graph, s);
- }
- private void bfs(Graph graph, int s) {
- this.marked[s] = true;
- this.queue.enqueue(s);
- while (!this.queue.isEmpty()) {
- Integer v = this.queue.dequeue();
- for (int w : graph.adj(v)) {
- if (!this.marked[w]) {
- this.marked[w] = true;
- this.edgeTo[w] = v;
- this.queue.enqueue(w);
- }
- }
- }
- }
- public boolean hasPathTo(int v) {
- return this.marked[v];
- }
- public Iterable<Integer> pathTo(int v) {
- if (!hasPathTo(v)) {
- throw new IllegalStateException("s不能到達v");
- }
- Stack<Integer> stack = new LinkedListStack<>();
- stack.push(v);
- while (edgeTo[v] != s) {
- stack.push(edgeTo[v]);
- v = edgeTo[v];
- }
- stack.push(s);
- return stack;
- }
- }
以下圖為列,來看看廣度優先搜索的運行軌跡
單元測試的代碼:
- @Test
- public void test() {
- Graph graph = new Graph(8);
- graph.addEdge(0, 1);
- graph.addEdge(0, 2);
- graph.addEdge(0, 5);
- graph.addEdge(1, 3);
- graph.addEdge(2, 4);
- graph.addEdge(4, 3);
- graph.addEdge(5, 3);
- graph.addEdge(6, 7);
- BreadthFirstPaths paths = new BreadthFirstPaths(graph, 0);
- System.out.println(paths.hasPathTo(5));
- System.out.println(paths.hasPathTo(2));
- System.out.println(paths.hasPathTo(6));
- paths.pathTo(5).forEach(System.out::print);
- System.out.println();
- paths.pathTo(4).forEach(System.out::print);
- System.out.println();
- paths.pathTo(2).forEach(System.out::print);
- System.out.println();
- paths.pathTo(3).forEach(System.out::print);
- }
執行的結果與我們分析的運行軌跡一致
符號圖
最近幾篇文章一起學習到的圖算法都是以數字作為了頂點,是因為數字來實現這些算法會非常的簡潔方便,但是在實際的場景中,通常都是使用的字符作為頂點而非數字,比如:地圖上的位置、電影與演員的關系。
為了滿足實際的場景,我們只需要在數字與字符串的關系做一個映射,此時我們會想到之前文章實現的map(通過二叉樹實現map、紅黑樹實現map、哈希表實現map等等,有興趣的同學可以去翻翻),使用Map來維護字符串和數字的映射關系。
- public interface SymbolGraph {
- boolean contains(String key); //判斷是否存在頂點
- int index(String key); //通過名稱返回對應的數字頂點
- String name(int v); //通過數字頂點返回對應的字符名稱
- Graph graph();
- }
實現的思路:
- 使用Map來保存字符串-數字的映射,key為字符串,value為數字
- 使用一個數組來反向映射數字-字符串,數組的下標對應數字頂點,值對應字符串名稱
- 假設構造圖的頂點與邊是通過字符串來表示的,如:a,b,c,d\nb,a,h,l,p\ng,s,z 使用\n分隔的每段第一個字符串表示頂點v,后面的表示與頂點v相連的相鄰頂點;
實際的過程可以根據具體情況來確定,不一定非得這種字符串,可以來源于數據庫,也可以來源于網路的請求。
代碼實現如下:
- public class SymbolGraph {
- private Map<String, Integer> map = new RedBlack23RedBlackTreeMap<>();
- private String[] keys;
- private Graph graph;
- public SymbolGraph(String text) {
- Arrays.stream(text.split("\n")).forEach(line -> {
- String[] split = line.split(",");
- for (int i = 0; i < split.length; i++) {
- map.put(split[i], i);
- }
- });
- this.keys = new String[map.size()];
- map.keys().forEach(key -> {
- this.keys[this.map.get(key)] = key;
- });
- this.graph = new Graph(this.keys.length);
- Arrays.stream(text.split("\n")).forEach(line -> {
- String[] split = line.split(",");
- int v = this.map.get(split[0]);
- for (int i = 1; i < split.length; i++) {
- this.graph.addEdge(v, this.map.get(split[i]));
- }
- });
- }
- public boolean contains(String key) {
- return map.contains(key);
- }
- public int index(String key) {
- return map.get(key);
- }
- public String name(int index) {
- return this.keys[index];
- }
- public Graph graph() {
- return this.graph;
- }
- public static void main(String[] args) {
- System.out.println(Arrays.toString("323\n2323".split("\n")));
- }
- }
文中所有源碼已放入到了github倉庫:https://github.com/silently9527/JavaCore