NAME

Graph::Weighted - A weighted graph implementation

SYNOPSIS

use Graph::Weighted;
my $g = Graph::Weighted->new();
$g->populate( # weight
    [ [ 0, 1, 2, 0, 0 ], # Vertex with 2 edges of weight 3
      [ 1, 0, 3, 0, 0 ], # Vertex with 2 edges of weight 4
      [ 2, 3, 0, 0, 0 ], # Vertex with 2 edges of weight 5
      [ 0, 0, 1, 0, 0 ], # Vertex with 1 edge of weight 1
      [ 0, 0, 0, 0, 0 ], # Vertex with no edges of weight 0
    ]
);
$g->populate( # magnitude
    [ [ 0, 2, 1, 0, 0 ], # Vertex with 2 edges of weight 3
      [ 3, 0, 1, 0, 0 ], # Vertex with 2 edges of weight 4
      [ 3, 2, 0, 0, 0 ], # Vertex with 2 edges of weight 5
      [ 0, 0, 2, 0, 0 ], # Vertex with 1 edge of weight 2
      [ 1, 1, 1, 1, 0 ], # Vertex with 4 edges of weight 4
    ]
);
my $vertex = 0;
my $vertex_weight = $g->get_weight($vertex); # 3
my $vertex_magnitude = $g->get_attr($vertex, 'magnitude'); # 3
my $edge = [0, 1];
my $edge_weight = $g->get_weight($edge); # 1
my $edge_magnitude = $g->get_attr($edge, 'magnitude'); # 2

DESCRIPTION

A Graph::Weighted object is a subclass of Graph with weighted attributes.

This module is a streamlined 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, $method, $attribute)

data      => ARRAYREF of numeric vectors
method    => Optional CODEREF weighting function
attribute => Optional STRING

Populate a graph with weighted nodes.

Example of array reference nodes:

[] no vertices (vertex weight 0)
[0] 1 vertex and no edges (vertex weight 0)
[1] 1 vertex and 1 edge (vertex weight 1)
[0,1] 2 vertices and 1 edge (vertex weight 1)
[0,1,9] 3 vertices and 2 edges (vertex weight 10)

The default edge weighting function returns the value in the neighbor position. An alternative may be provided, which should accept arguments of 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 arguments of the current node weight, current weight total and the attribute 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, if the nodes have multiple attributes.

get_weight($vertex) and get_attr($vertex, $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.

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.