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
to v
which doesn't pass through w
- Paths which go through
w
= number of paths from u
to w
times number of paths from w
to v
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. :)