Graph module and multiedge graphs

From: Jim Mozley (jim.mozley_at_exponential-e.com)
Date: 10/18/04


Date: Mon, 18 Oct 2004 09:18:25 +0100

I am trying to use the graph module (Graph-0.20105) to implement a
multigraph representing a computer network. Where I am after some help
is in implementing a multiedge.

I can add an edge using:

$G = $G->add_edge($u, $v)

But when I have multiple redundent edges between the same $u and $v
there seems to be no way to manipulate the edges as discrete objects.

The add_edge method returns a graph object, if it returned an edge
object I could add an attribute to it.

After looking at the module source I have tried following the example of:

$G->add_weighted_edge($u, $w, $v, $a)

( although $a doesn't seemed to be used in the source ?)

So I've used:

$G->add_edge($u, $v);
$G->set_attribute('id', $u, $v, $w);

However, there is no way to specify a particular edge if there is more
than one between two vertices.

For instance if I use this:

#!/usr/local/bin/perl

use strict;
use warnings;

use Graph::Undirected;

my $G = Graph::Undirected->new;

my $u = 'west';
my $v = 'east';

# create two vertices with four edges between
for ( 1..4 ) {
     $G = $G->add_edge( $u, $v );
     $G->set_attribute( 'id', $u, $v, $_);
}

my @edges = $G->edges;
while ( my( $u, $v ) = splice( @edges, 0 ,2) ) {
     print "ID: ", $G->get_attribute('id', $u, $v), "\n";
}

__END__

to add four interfaces with a unique ID I get the output:

ID: 4
ID: 4
ID: 4
ID: 4

Any suggestions as to how I can use the module to treat edges as unique?

Jim Mozley