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