4
votes

I want to find all possible paths in a directed cyclic graph. I have written a program which does so, but I notice that if the number of nodes grow above 40 or 50, it starts taking infinite time.

Theoretically speaking how many paths are possible for a directed cyclic graph of N nodes. Is it like factorial(N) or something? Can you give me a guess for the following example with 119 nodes. Of course, I am going over loops only once, so you can ignore the cyclic paths.

Graph Image

2
I have tried Depth First search. - pythonic
At this moment I am interested in knowing the theoretical limit. - pythonic
Theoretical limit is based on the computer, architecture, and I could go on. Even up to compiler flags used. You need to analyze your algorithm in great detail. Start with small cases. - Drise

2 Answers

3
votes

Let's just take this common pattern that shows in your graph:

A ---> B
|     /|
|    / v
|   /  C
|  /   |
| /    |
vv    /
D <---

Excuse the ASCII art. So you have three paths here: A -> D, A -> B -> D, and A -> B -> C -> D.

Now say you have the exact same figure emanating from D to another node G:

D ---> E
|     /|
|    / v
|   /  F
|  /   |
| /    |
vv    /
G <---

You have the same analogous three paths as before: D -> G, D -> E -> G, and D -> E -> F -> G.

Now, how many paths are there from A to G?

To get from A to G, you have to get from A to D. You can do this in one of three ways. Then you have to get from D to G. You can do this in one of three ways. These two choices (A to D and D to G) are independent of each other. Thus you have 3 * 3 = 9 possible paths from A to G.

If you keep repeating the figure, you multiply the number of possible paths by 3 with each repetition. So with three figures, 27 paths; with four figures, 81 paths; etc.

That's exponential growth. Put differently: you'll have to find another way to do what it is you're doing, if you want to be efficient about it.

EDIT: To get a rough estimate: only counting those figures, not even looking at the complex jumbles in the middle, I get 3 * 3 * 3 * 3 * (2^8) * (4^8) * 3 * 3 * 2 * 3 = 73383542784 possible paths, through just those simple nodes.

EDIT: You seem to be doing code analysis. Without knowing exactly what you want to do, what I recommend is consolidating whatever information you're gathering along those nodes that must be reached (e.g. nodes A, D, and G in my example figures). Then do a search until you get to the next node that must be reached, and gather your info there as well. This will prevent exponential blow-up.

0
votes

You can try the following:

  • Find nodes in your graph that are part of every path. To do that, sequentially remove nodes from your graph. Each node that, if removed, makes it impossible to find a path is such a node. Your graph should have plenty of those.
  • I can't prove this, but it should be possible to find an order in which those nodes appear in every path.
  • Split the graph into chunks that consist of two consecutive forced nodes and all nodes between them
  • Calculate the number of paths for each chunk
  • Calculate the product of all path counts to get the full count. Remember to use something like GMP to avoid overflowing numbers