Syntactic Confectionery Delight PerlMonks

### 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;

# 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!

Comment on Re: Six Degrees via Shortest Path ?

Create A New User
Node Status?
node history
Node Type: note [id://251020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (19)
As of 2015-08-03 14:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?