Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Calculating Shortest Paths with Graph::Directed

by arunhorne (Pilgrim)
on Jul 19, 2002 at 11:36 UTC ( [id://183192]=perlquestion: print w/replies, xml ) Need Help??

arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I have written the following code that creates a graph using the Graph::Directed package and generates a distance matrix accordingly:

use Graph::Directed; use warnings; use strict; # Instantiate my $G = new Graph::Directed; # Metabolite Create graph $G = $G->add_edge("E1", "E2"); $G = $G->add_edge("E2", "E3"); $G = $G->add_edge("E3", "E1"); $G = $G->add_edge("E3", "E2"); $G = $G->add_edge("E2", "E2"); $G = $G->add_edge("E3", "E3"); # Weight edges (constant) $G->set_attribute("weight", "E1", "E2", 1); $G->set_attribute("weight", "E2", "E3", 1); $G->set_attribute("weight", "E3", "E1", 1); $G->set_attribute("weight", "E3", "E2", 1); $G->set_attribute("weight", "E2", "E2", 1); $G->set_attribute("weight", "E3", "E3", 1); # Give meaningful names to edges $G->set_attribute("name", "E1", "E2", "C"); $G->set_attribute("name", "E2", "E3", "F"); $G->set_attribute("name", "E3", "E1", "A"); $G->set_attribute("name", "E3", "E2", "F"); $G->set_attribute("name", "E2", "E2", "C,E,F"); $G->set_attribute("name", "E3", "E3", "A,F"); # Print some info about graph print "GRAPH (", scalar $G->vertices, " vertices):\n", $G, "\n\n"; # All Pairs Shortest Path my $APSP = $G->APSP_Floyd_Warshall; print "ALL AVAILABLE PAIRS:\n", $APSP, "\n\n"; print "DISTANCE MATRIX:\n"; print "\t", join ("\t", $G->vertices), "\n"; foreach my $rowVtx ($G->vertices) { print "$rowVtx\t"; foreach my $colVtx ($G->vertices) { $APSP->get_attribute("weight", $rowVtx, $colVtx); print $APSP->get_attribute("weight", $rowVtx, $colVtx), "\t"; } print "\n"; }

The following output is produced:

GRAPH (3 vertices): E1-E2,E2-E2,E2-E3,E3-E1,E3-E2,E3-E3 ALL AVAILABLE PAIRS: E1-E1,E1-E2,E1-E3,E2-E1,E2-E2,E2-E3,E3-E1,E3-E2,E3-E3 DISTANCE MATRIX: E1 E2 E3 E1 0 1 2 E2 2 0 1 E3 1 1 0

The distance matrix is exactly correct apart from the diagonal (E1-E1, E2-E2, E3-E3). By correct I mean it isn't the result I want as it is correct in terms of graph theory. What I want is for the cell E1,E1 = 3, the cell E2,E2 = 1 and E3,E3 = 1. Obviously the distance from E1->E1 is zero as the start nodes and end nodes are equal - however, I wish to intorduce the constraint that the search must leave the start node before returning (a domain specific feature of the problem). The required matrix therfore is below:

DISTANCE MATRIX: E1 E2 E3 E1 3 1 2 E2 2 1 1 E3 1 1 1

Thus to get from E1->E1 in the above graph you must go E1->E2->E3->E1, the distance of which equals 3 (all arcs have a weight/cost of 1) as required. The corresponding paths for E2 and E3 have a distance of just 1 because they have self referencing arcs.

The question: can anyone think of a way of obtaining the required result (and thus placing the constraint on the algorithm) with the minimum effort - preferably whilst staying within the bounds of Graph::Directed.

Best wishes,

____________
Arun

Replies are listed 'Best First'.
Re: Calculating Shortest Paths with Graph::Directed
by Cine (Friar) on Jul 19, 2002 at 14:45 UTC
    Try removing
    if ( $i == $j ) { $APSP->add_weighted_edge( $iv, 0, $iv ); $APSP->set_attribute("path", $iv, $iv, [ $iv ]); next; }
    from the APSP_Floyd_Warshall sub in Base.pm in the Graph package. Completly untested and without any kind of guarantee!

    T I M T O W T D I

      That nearly does it... its adds the 2 missing 1's, i.e. the self-referencing nodes... couldn't handle the 3 (E1-E1) part... will look into that. Thanks

      ____________
      Arun

      I have managed to make all the necessary mods... more info here 183305

      ____________
      Arun
•Re: Calculating Shortest Paths with Graph::Directed
by merlyn (Sage) on Jul 19, 2002 at 14:47 UTC
    One idea comes to mind... (but I'm not a math geek, so beware).

    Duplicate the nodes that don't have a self-link, such as E1a and E1b. Add the same set of links to both E1a and E1b that you would have added to E1, but don't link E1a to E1b. Now look for a path from E1a to E1b, since it must go out and back first.

    -- Randal L. Schwartz, Perl hacker

      Very lateral thinking! I've a feeling it would work, but unfortunately my real-life data set has 3097 vertextes and many more edges...!! I think my workstation is already going to have trouble without duplicating the work. Thanks though :)

      ____________
      Arun
Re: Calculating Shortest Paths with Graph::Directed
by kvale (Monsignor) on Jul 19, 2002 at 16:31 UTC
    This constaint changes the algorithm. For the diagonal elements you want the shortest total path given that you have moved to an adjacent point.

    The diagonal distances can be computed from the APSP distance matrix. For E1, the only adjacent vertex is E2 and the distance from E2 back to E1 is 2, so the total distance is 3. For E2, adjacent vertices are a loop to E2 and E3 and the min distance is the loop at one. And so on.

    -Mark

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-24 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found