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"; } #### 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 #### DISTANCE MATRIX: E1 E2 E3 E1 3 1 2 E2 2 1 1 E3 1 1 1