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.
Let’s just take this common pattern that shows in your graph:
Excuse the ASCII art. So you have three paths here:
A -> D,A -> B -> D, andA -> B -> C -> D.Now say you have the exact same figure emanating from
Dto another nodeG:You have the same analogous three paths as before:
D -> G,D -> E -> G, andD -> E -> F -> G.Now, how many paths are there from
AtoG?To get from
AtoG, you have to get fromAtoD. You can do this in one of three ways. Then you have to get fromDtoG. You can do this in one of three ways. These two choices (AtoDandDtoG) are independent of each other. Thus you have3*3=9possible paths fromAtoG.If you keep repeating the figure, you multiply the number of possible paths by
3with 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=73383542784possible 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, andGin 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.