It can be done using Dynamic Programming and topological sort as follows:
Topological sort the vertices, let the ordered vertices be v1,v2,...,vn
create new array of size t, let it be arr
init: arr[t] = 1
for i from t-1 to 1 (descending, inclusive):
arr[i] = 0
for each edge (v_i,v_j) such that i < j <= t:
arr[i] += arr[j]
When you are done, for each i
in [1,t]
, arr[i]
indicates the number of paths from vi
to vt
Now, proving the above claim is easy (comparing to your algorithm, which I have no idea if its correct and how to prove it), it is done by induction:
Base: arr[t] == 1
, and indeed there is a single path from t to t, the empty one.
Hypothesis: The claim is true for each k
in range m < k <= t
Proof: We need to show the claim is correct for m
.
Let's look at each out edge from vm
: (v_m,v_i)
.
Thus, the number of paths to vt
starting from v_m
that use this edge (v_m,v_i)
. is exactly arr[i]
(induction hypothesis). Summing all possibilities of out edges from v_m
, gives us the total number of paths from v_m
to v_t
- and this is exactly what the algorithm do.
Thus, arr[m] = #paths from v_m to v_t
QED
Time complexity:
The first step (topological sort) takes O(V+E)
.
The loop iterate all edges once, and all vertices once, so it is O(V+E)
as well.
This gives us total complexity of O(V+E)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…