Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Six Degrees via Shortest Path ?

by jkahn (Friar)
on Apr 16, 2003 at 20:03 UTC ( #251020=note: print w/replies, xml ) Need Help??

in reply to Six Degrees via Shortest Path ?

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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://251020]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-06-24 06:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.