I was inspired to revisit this by a discussion in the chatterbox with gohaku, tye and Enlil.

Looks like your second guess was right, Anonymous Monk.

I can't for the life of me figure out what's going on in Graph::Traversal, which is the base class for Graph::BFS. The POD reads %param documentation to be written, which is a little disappointing.

However, I was able to address the problem you're trying to solve using SSSP_Dijkstra(), which also is a bit short on the POD:

#!/usr/bin/perl -w use strict; use Graph::Directed; # Construct the graph # This is a diagram of the graph we'll construct # note there are two possible paths from D to J # we want D->M->J since it's shorter than D->M->P->J # D -> M -> P # | | # \ v # -> J my $G = new Graph::Directed; $G = $G->add_edge("dave","mark"); $G = $G->add_edge("mark","paul"); $G = $G->add_edge("mark","john"); $G = $G->add_edge("paul","john"); # use the built-in SSSP_Dijkstra method # creates a new graph, rooted at "dave" my $SSP = $G->SSSP_Dijkstra("dave"); # this returns $SSP, which is another graph with every vertex # reachable from "dave". # Note that the "path" attribute for each vertex is a listref # indicating the shortest way to reach that path (using Dijkstra's # algorithm) from 'dave'. This is not documented in the # Graph::Base documentation. # find path to "john" my @path2John = @{$SSP->get_attribute('path', "john")}; for (@path2John) { print; print "\n"; } __END__ dave mark john
This issues a bunch of warnings about Use of uninitialized value in addition (+), which is because of some sloppy code in Graph::Base, but it gets the right answer. (they need the Perl 6 // operator!). Hope that helps!

In reply to Re: Six Degrees via Shortest Path ? by jkahn
in thread Six Degrees via Shortest Path ? by Anonymous Monk

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.