Start by solving a simpler task: given the two points A and B in the DAG, can you count all the paths starting with A and ending with B? (A "path" is defined as a list of edges, where the end of the node of one of them is equal to the beginning of the node of the next.)
That sounds hard. Can we simplify it?
, - , A B node. , .
, . WOLOG , A C D, B. A B C B, D B.
: A n , B, B .
A B, A- > B.
-, .
, , , , . :
A
/ \
C D
\ /
E
/ \
F G
\ /
H
/ \
I J
\ /
B
- A B C B D B. .
- C B E B.
- D B E B.
... E B . , , H B , . , , 2 n times, n !
, memoizer, , , .
, , , memoized , .
. A B? ab. :
ab = cb + db, but we don't know them...
cb = eb, but we don't know it...
eb = fb + gb, but we don't know them...
fb = hb, but we don't know it...
hb = ib + jb, but we don't know them...
ib = 1
jb = 1
therefore hb = 2
therefore fb = 2
gb = hb, but we already know that is 2
therefore eb = 4
therefore cb = 4
db = eb, but we already know that is 4
therefore ab is 8
.
, , . , A B, E-G, A E, G B.
.
ae = ce + de
ce = 1
de = 1
so ae = 2
gb = hb
hb = ib + jb
ib = 1
jb = 1
so hb = 2
so gb = 2
ae * gb = 4 A B, EG. .
AC-CE-EG-GH-HI-IB
AC-CE-EG-GH-HI-JB
AD-DE-EG-GH-HI-IB
AD-DE-EG-GH-HI-JB
, .
?