The total number of paths in a directed acyclic graph containing a specific link

I am trying to copy an algorithm that takes a directed set of nodes (at the moment I am expressed as a sparse adjacency matrix), say A, B, C and D, which when called, gives me all possible paths that contain the given path (for example, AB or AD). A node cannot connect to itself, and all nodes will be directed to the stream from A to D.

I made an attempt to code a recursive python script with limited success so far - my graph theory is not strong (I actually have a zero background). Any help anyone can provide as far as I should go will be appreciated.

As a disclaimer, this is not homework (I'm just trying to process a large dataset and create a personal library for some research), and I have been looking at “similar questions” for several hours, but mostly not (except for the above recursive python script).

Thank.

+3
source share
2 answers

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

, .

?

+8

, networkx all_simple_paths.

, DAG s sink t. , a -> b, s a b t .

+1

All Articles