Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^11: Finding All Paths From a Graph From a Given Source and End Node

by BrowserUk (Pope)
on Nov 02, 2010 at 17:09 UTC ( #869048=note: print w/replies, xml ) Need Help??


in reply to Re^10: Finding All Paths From a Graph From a Given Source and End Node
in thread Finding All Paths From a Graph From a Given Source and End Node

UPDATE: I expected this part { %$seen } to be quite expensive .... does it even make sense?

Well, it does no harm :)

Um. Do you mean:

  • Does the syntax make sense?

    It just makes a copy (anonymous reference) of the seen hash(ref).

  • Or; why make a copy?

    I make a copy because I want any changes made to it at deeper levels of recursion, to be unwound as the recursion unwinds.

    It's purpose is obviously to prevent the algorithm from going in circles when it re-encounters a node it has already visited. But when we visit a node a deeper level we don't to permanently prevent it from visiting that node again if it reaches it via different path earlier in the recursion but later in the iteration.

    Hm. Not sure that description works?

Essentially, the hash(ref) $seen and array(ref) $path contain the same information, but arrays are no good for lookup, and hashes are unordered which is no good for paths. Hence using both.

There is a potential optimisation there. I could just build the hash from the array at each level instead of passing it through. In fact, I'll try that later.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^12: Finding All Paths From a Graph From a Given Source and End Node
by eMBR_chi (Acolyte) on May 13, 2011 at 21:07 UTC

    Hi,

    Even though this thread is about 6 months old I find it really very useful, as I am currently working on a similar project and have struggled with it, being quite a young programmer.I have read through it all and feel that I might be able to get the help that I need.

    I have a network containing about 2500 edges (directed) and I am looking at being able to work out all the routes downstream from a given beginning node. The difference from neversaint's problem is that both beginning and end nodes are supplied in his.My initial concerns was that it'd result in very large output that may not be manageable but considering that it is currently not a very large network and that I really need to identify all the routes i am willing to give it a try.

    I will be happy to have your inputs and thoughts

    Thank you very much in advance

      I have a network containing about 2500 edges (directed) and I am looking at being able to work out all the routes downstream from a given beginning node.

      Hm. What is your terminating condition?

      A directed graph can contain loops:

      a->b->c ^ | | v e<-d

      If your starting node is (a), when do you decide you've reach the (an) end?

      Every time around the b->c->d->e->b loop, could be seen as another route. The only logical basis I can see for breaking that impasse is a rule along the lines of "Stop when there is no possibility of reaching a so far unvisited node. And that could be quite difficult to program efficiently.

      As so often, more info required?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Hi, BrowserUK,

        Sorry for the delayed reply. Have been inundated with work these two weeks. I just got some breathing space.

        To the question of terminating condition: The network is built from biodegradation reactions where, in A->B, B is the result of some reaction from A and the edge represents enzymes catalysing such a reaction. The source of the information is also such that most of the reaction paths have logical end points.

        The terminating situation will be when the path ends and there is no possibility of futher elongation along the path of interest.

        Example: J->K ^ | A->B->C->D->E ^ | | v H<-G

        A->B->C->D->E or A->B->C->D->G->H->C->J->K, would be correctly admissible in the resulting path.

        The edge between C and D should not be allowed again (as in that loop) seeing that in real life that enzyme would already be in the system and need not be captured again. However C as a node could be captured again (eg C->J). The important factor in the resulting pathways should be the edges. An earlier captured 'node-edge-node' should not be captured again, until the the path logically ends (without a possibility of further elongation).

        I hope the information supplied is sufficiently clear and helpful.

        Thank you so very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://869048]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2021-01-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?