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,
____________
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Calculating Shortest Paths with Graph::Directed
by Cine (Friar) on Jul 19, 2002 at 14:45 UTC | |
by arunhorne (Pilgrim) on Jul 19, 2002 at 16:05 UTC | |
by arunhorne (Pilgrim) on Jul 19, 2002 at 16:51 UTC | |
|
•Re: Calculating Shortest Paths with Graph::Directed
by merlyn (Sage) on Jul 19, 2002 at 14:47 UTC | |
by arunhorne (Pilgrim) on Jul 19, 2002 at 16:09 UTC | |
|
Re: Calculating Shortest Paths with Graph::Directed
by kvale (Monsignor) on Jul 19, 2002 at 16:31 UTC |