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

In reply to Calculating Shortest Paths with Graph::Directed by arunhorne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.