NAME
Graph::Weighted - A weighted graph implementation
VERSION
version 0.55
SYNOPSIS
use Graph::Weighted;
my $gw = Graph::Weighted->new();
$gw->populate(
[ [ 0, 1, 2, 0, 0 ], # Vertex 0 with 2 edges of weight 3
[ 1, 0, 3, 0, 0 ], # " 1 2 " 4
[ 2, 3, 0, 0, 0 ], # " 2 2 " 5
[ 0, 0, 1, 0, 0 ], # " 3 1 " 1
[ 0, 0, 0, 0, 0 ], # " 4 0 " 0
]
);
for my $vertex (sort { $a <=> $b } $gw->vertices) {
warn sprintf "vertex: %s weight=%.2f\n",
$vertex, $gw->get_weight($vertex);
for my $neighbor (sort { $a <=> $b } $gw->neighbors($vertex)) {
warn sprintf "\tedge to: %s weight=%.2f\n",
$neighbor, $gw->get_weight([$vertex, $neighbor]);
}
}
my ($heaviest, $lightest) = $gw->span();
my $gw = Graph::Weighted->new();
my $attr = 'probability';
$gw->populate(
{
0 => { 1 => 0.4, 3 => 0.6 }, # Vertex 0 with 2 edges of weight 1
1 => { 0 => 0.3, 2 => 0.7 }, # Vertex 1 " 2 "
2 => { 0 => 0.5, 2 => 0.5 }, # Vertex 2 " 2 "
3 => { 0 => 0.2, 1 => 0.8 }, # Vertex 3 " 2 "
},
$attr
);
for my $vertex (sort { $a <=> $b } $gw->vertices) {
warn sprintf "vertex: %s %s=%.2f\n",
$vertex, $attr, $gw->get_attr($vertex, $attr);
for my $neighbor (sort { $a <=> $b } $gw->neighbors($vertex)) {
warn sprintf "\tedge to: %s %s=%.2f\n",
$neighbor, $attr, $gw->get_attr([$vertex, $neighbor], $attr);
}
}
DESCRIPTION
A Graph::Weighted
object is a subclass of the Graph module with attribute handling. As such, all of the Graph methods may be used as documented, but with the addition of custom weighting.
METHODS
new()
my $gw = Graph::Weighted->new;
Return a new Graph::Weighted
object.
Please see Graph for the possible constructor arguments.
populate()
$gw->populate($matrix);
$gw->populate(\@vectors);
$gw->populate(\@vectors, $attribute);
$gw->populate(\%data_points, $attribute);
Populate a graph with weighted nodes.
For arguments, the data can be a numeric value ("terminal node"), an arrayref of numeric vectors, a Math::MatrixReal
object, or a hashref of numeric edge values. The attribute
is an optional string name, with the default "weight."
Examples of data
in array reference form:
[] 1 vertex with no edges.
[0] 1 vertex and 1 edge to node 0 having weight of 0.
[1] 1 vertex and 1 edge to node 0 weight 1.
[0,1] 2 vertices and 2 edges having edge weights 0,1 and vertex weight 1.
[0,1,9] 3 vertices and 3 edges having edge weights 0,1,9 and vertex weight 10.
Multiple attributes may be applied to a graph, thereby layering and increasing the overall dimension.
get_weight()
$w = $gw->get_weight($vertex);
$w = $gw->get_weight([$vertex, $neighbor]);
Return the weight for the vertex or edge.
A vertex is a numeric value. An edge is an array reference with 2 elements. If no value is found, zero is returned.
get_attr()
$w = $gw->get_attr($vertex, $attribute);
$w = $gw->get_attr(\@edge, $attribute);
Return the named attribute value for the vertex or edge.
span()
my ($lightest, $heaviest) = $gw->span();
my ($lightest, $heaviest) = $gw->span($attr);
Return the span of lightest to heaviest vertices.
TO DO
Find the total weight beneath a node.
SEE ALSO
The eg/* and t/* file sources.
AUTHOR
Gene Boggs <gene@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Gene Boggs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.