I have a directed graph, what algorithm can i use to find the number of distinct acyclic paths between 2 particular vertices, and count the maximum times any path is used in these distinct paths? Two paths are distinct if they either visit a different number of vertices or visit vertices in a different order.
A:
Seriously, when asking about every possible path, only a brute-force way trying out every possible path will give you a result.
Joey
2009-10-29 07:54:22
A:
If you follow a slightly modified Dijkstra's algorithm, you can have an all-pair solution.
Explanation: Paths from u
to v
is the sum of the following:
- Paths from
u
tov
which doesn't pass throughw
- Paths which go through
w
= number of paths fromu
tow
times number of paths fromw
tov
Initialise the matrix with zeros except when there is an edge from i
to j
(which is 1).
Then the following algorithm will give you the result (all-pair-path-count)
for i = 1 to n:
for j = 1 to n:
for k = 1 to n:
paths[i][i] += paths[i][k] * paths[k][j]
Needless to say : O(n^3)
Eager to read a single pair solution. :)
ThisIsMeMoony
2009-10-29 09:08:38
This solution doesn't deal correctly with the requirement that the paths must have no cycles.
MISSINGNO
2010-04-12 23:52:56
+1
A:
Found the solution to an identical problem on this link. It uses DFS. Have implemented it and it works well.
Pranav
2009-10-30 06:53:58