Using Floyd-Warshall algorithm to count number of paths between 2 vertices

浪子不回头ぞ 提交于 2019-12-03 21:55:54
voidengine

In an undirected unweighted acylic graph there's at most 1 path between any two vertices. If there were more distinct paths, they would create a cycle. (not relevant after question was edited)

For directed graphs, I don't see a problem with your algorithm. The usage of modified Floyd-Warshall algorithm is actually mentioned in this paper. The reason it's not used widely is probably its complexity - O(n3) compared to O(m+n) of this simple approach

In the cyclic graph case, you can't do this with the straight Floyd-Warshall algorithm, because counting simple paths requires you to keep track of where you've been. Dynamic programming assumes that the state being computed is only a function of the states in the recurrence, which is not true in this case.

However, I don't see why this wouldn't work. But why use Floyd-Warshall to compute just two verticies (just use a DFS or BFS).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!