Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by BrowserUk (Patriarch)
on May 22, 2011 at 02:58 UTC ( [id://906110]=note: print w/replies, xml ) Need Help??


in reply to Re^14: 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

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. ...

Hm. That suggests that having transitioned from the A state to the B state, you cannot go backwards to the A state.

But then you have two sentences that have an overriding termination (or perhaps extension) criteria:

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

An earlier captured 'node-edge-node' should not be captured again, until the the path logically ends (without a possibility of further elongation).

And in your example, having transitioned from c-d, the reaction(s) can find their way back to the c state; and then leave via a different route.

Your path:A->B->C->D->G->H->C->J->K

So my question is, if the reaction can go from c-d once; and can find its way back to c; why can it not transition c-d a second time?

Which would make the path a-b-c-d-g-h-c-d-e an elongation of the path you've identified as legitimate: A->B->C->D->E

Thinking, I hope logically, about a subject I know next to nothing about, I considered the possibility that whatever enzymes are in the original mix, when the C state occures it preferentially reacts with whichever enzyme(s) are required to transition to state D.

And in doing so, depletes the mix of that(those) enzymes. Which means that when the mix arrives at the C state a second time, those enzymes are no longer present so it can now react with other enzymes that cause it to transition to state J.

Is that a viable, even if technically inaccurate, model of what goes on?

If so, I think an efficient solution is possible.


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^16: Finding All Paths From a Graph From a Given Source and End Node
by eMBR_chi (Acolyte) on May 22, 2011 at 08:18 UTC

    Oh Great!

    I appear to have used the word 'elongation' in a somewhat confusing manner. I didn't consider the fact that A->B->C->G->H->C->D->E is actually an elongation of A->B->C->D->E. But the intended meaning is that having arrived at E, if there is nothing else beyond E to which the path should go, then it ends

    And! The focus of the work is to list the possible paths from a given node, without getting stuck in a loop somewhere. This would mean that for the hypothetical example we are using here the following paths should result:

    a. A->B->C->D->E

    b. A->B->C->J->K

    c. A->B->C->D->G->H->C->J->K

    And! Yes. Your model of what is going on is so very viable.

    Of course we all would rather like to take any of the short routes. In nature however, the shortest routes sometimes are not the best and the objective here is to outline all the possible routes; and number them appropriately 'route1, route2, route3...', affording one the chance to look closely at each route and see its workability.

    Thanks a lot for your time. Its good to hear of a possible solution.

      In that case, try this:

      #! perl -slw use strict; sub _pathsFrom { my( $code, $graph, $start, $path, $seen ) = @_; return $code->( @$path, $start ) unless exists $graph->{ $start }; for my $next ( @{ $graph->{ $start } } ) { if( exists $seen->{ "$start-$next" } ) { return $code->( @$path, $start ); } else { _pathsFrom( $code, $graph, $next, [ @$path, $start ], { %$seen, "$start-$next", undef } ); } } } sub pathsFrom(&@) { _pathsFrom( @_, [], {} ) } my %graph = ( a => [ 'b' ], b => [ 'c' ], c => [ 'd', 'j' ], d => [ 'e', 'g' ], g => [ 'h' ], h => [ 'c' ], j => [ 'k' ], ); pathsFrom{ print join '->', @_; } \%graph, 'a'; __END__ c:\test>904729-2 a->b->c->d->e a->b->c->d->g->h->c->j->k a->b->c->j->k

      This version is the identical algorithm but somewhat more efficient:

      use enum qw[ CODE GRAPH START PATH SEEN ]; sub _pathsFrom2 { return $_[CODE]->( @{ $_[PATH] }, $_[START] ) unless exists $_[GRAPH]->{ $_[START] }; for ( @{ $_[GRAPH]->{ $_[START] } } ) { if( exists $_[SEEN]->{ $_[START] . "-$_" } ) { return $_[CODE]->( @{ $_[PATH] }, $_[START] ); } else { _pathsFrom2( @_[ CODE, GRAPH ], $_, [ @{ $_[PATH] }, $_[START] ], { %{ $_[SEEN] }, $_[START] . "-$_", undef } ); } } } sub pathsFrom(&@) { _pathsFrom2( @_, [], {} ) }

      BTW: If you have a dataset that forms a decent sized test I'd like to get a copy as I've found generating legal directed graphs quite difficult.


      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

        Thanks so much for your help. And sorry for the late response. I've tried to get some test as you requested. Its quite raw and needs to be processed into a hash listing but it does give an idea of the nature of the data I am dealing with.

        You will notice that in some cases no enzyme is supplied. In this case the relationship between substrate and product suffices for the graph connections. In some other cases like the flow from (S)-1-Phenylethanol -> Acetophenone (line 72 and 73), the reaction can be catalysed by two different enzymes. This should not necessarily result in two routes as I can embedd information on the two enzyme possibilities in the same edge.

        I hope that supplying an 800 line dataset like this is okay by you. Feel free to truncate as needed. Hopefully there will be enough of those to form connections. Given the relatively large size I have decided to paste it ouside of PerlMonks

        Please find it at http://codepaste.net/9gs3da. And note that it is tab delimited, with the enzyme catalyising each connection supplied in the middle column.

        Once more thanks a great lot. Cheers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found