NAME

Graph::Weighted - A weighted graph implementation

SYNOPSIS

use Graph::Weighted;
my $g = Graph::Weighted->new();
$g->populate(
  [
      [ 0, 1, 2, 0, 0 ], # Vertex with 5 edges of weight 3
      [ 1, 0, 3, 0, 0 ], #               "               4
      [ 2, 3, 0, 0, 0 ], #               "               5
      [ 0, 0, 1, 0, 0 ], #               "               1
      [ 0, 0, 0, 0, 0 ], #               "               0
  ]
);
$g->populate(
  {
      0 => { 1=>2, 2=>1 },             # Vertex with 2 edges of weight 3
      1 => { 0=>3, 2=>1 },             #      "      2        "        4
      2 => { 0=>3, 1=>2 },             #      "      2        "        5
      3 => { 1=>2 },                   #      "      1        "        2
      4 => { 0=>1, 1=>1, 2=>1, 3=>1 }, #      "      4        "        4
  },
  'magnitude'
);
for my $v ($g->vertices) {
  my $w = $g->get_weight($v) || 0;
  warn "V: $v = $w\n";
}
for my $e ($g->edges) {
  my $w = $g->get_weight($e) || 0;
  my $m = $g->get_attr($e, 'magnitude') || 0;
  warn "E: @$e = $w $m\n";
}

DESCRIPTION

A Graph::Weighted object is a subclass of Graph with weighted attributes. As such, all of the Graph methods may be used as documented. This module is a streamlined, simplified version of the weight based accessors provided by the Graph module.

METHODS

new(%arguments)

Return a new Graph::Weighted object.

See Graph for the possible constructor arguments.

populate($data, $attribute, $method)

$g->populate(\@vectors)
$g->populate(
  { a => { b=>2, c=>1 },
    b => { a=>3, c=>1 },
    c => { a=>3, b=>2 },
    d => { b=>2 },
    e => { a=>1, b=>1, c=>1, d=>1 },
  },
);

Arguments:

data      => Array ref of numeric vectors or HASH ref of numeric edge values
attribute => Optional string name
method    => Optional code ref weighting function

Populate a graph with weighted nodes.

Example of array reference nodes:

[]      no edges
[0]     1 vertex and 1 edge to node 0 (weight 0)
[1]     1 vertex and 1 edge to node 0 (vertex & edge weights 1)
[0,1]   2 vertices and 2 edges (edge weights 0,1; vertex weight 1)
[0,1,9] 3 vertices and 3 edges (edge weights 0,1,9; vertex weight 10)

The default edge weighting function returns the value in the neighbor position. An alternative may be provided, as a subroutine reference, which should accept the current edge weight and the attribute to update. For example:

sub edge_weight_function {
  my ($weight, attribute);
  return $current_weight_total / $current_node_weight;
}

The default vertex weighting function is a simple sum of the neighbor weight values. An alternative may be provided, which should accept of the current node weight, current weight total and the attribute as arguments to update. For example:

sub vertex_weight_function {
  my ($current_node_weight, $current_weight_total, attribute);
  return $current_weight_total / $current_node_weight;
}

The attribute is named 'weight' by default but may be anything of your choosing. This method can be called multiple times on the same graph, for nodes of different attributes.

get_weight($vertex), get_weight($edge), get_attr($vertex, $attribute), get_attr($edge, $attribute)

$g->get_weight($vertex);
$g->get_attr($vertex, $attribute);
$g->get_weight($edge);
$g->get_attr($edge, $attribute);

Return the attribute value for the vertex or edge. A vertex is a numeric value. An edge is an array reference with 2 elements.

TO DO

Accept hashrefs and Matrix::* objects instead of just LoLs.

Make subroutines for finding the heaviest and lightest nodes.

Make subroutines for finding the total weight beneath a node.

SEE ALSO

Graph

The t/* sources.

AUTHOR

Gene Boggs, <gene at cpan.org>

LICENSE AND COPYRIGHT

Copyright 2003-2012 Gene Boggs.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.