How to reconstruct paths from a multi-path Dijkstra?

后端 未结 2 1484
轮回少年
轮回少年 2021-01-21 07:25

I am currently writing a PHP library for graphs. I have already implemented a single-path Dijkstra\'s algorithm successfully, but now struggle with implementing a multi-path ver

2条回答
  •  被撕碎了的回忆
    2021-01-21 08:20

    Like this? (Pseudocode):

    function output_paths(source, dest, tail) {
    
        if source == dest:
            output([dest] + tail)
    
        for each node in prev[dest]:
            output_paths(source, node, [dest] + tail)
    }
    
    
    output_paths(source=A, dest=J, tail=[])
    

提交回复
热议问题