NAME

Graph::Easy::Weighted - A weighted graph implementation

VERSION

version 0.03

SYNOPSIS

use Graph::Easy::Weighted;

my $gw = Graph::Easy::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 ( $gw->vertices ) {
   printf "vertex: %s weight=%.2f\n",
       $vertex->name, $gw->get_cost($vertex);
   for my $edge ( $gw->edges ) {
       next if $edge->from->name ne $vertex->name;
       printf "\tedge to: %s weight=%.2f\n",
           $edge->to->name, $gw->get_cost($edge);
   }
}

my ($lightest, $heaviest) = $gw->vertex_span();
($lightest, $heaviest) = $gw->edge_span();

my $weight = $gw->path_cost(\@vertices);

my $attr = 'probability';
$gw = Graph::Easy::Weighted->new();
$gw->populate(
   {
       0 => { title => 'A', 1 => 0.4, 3 => 0.6 }, # Vertex A with 2 edges, weight 1
       1 => { title => 'B', 0 => 0.3, 2 => 0.7 }, # Vertex B "    2 "
       2 => { title => 'C', 0 => 0.5, 2 => 0.5 }, # Vertex C "    2 "
       3 => { title => 'D', 0 => 0.2, 1 => 0.8 }, # Vertex D "    2 "
   },
   $attr
);
for my $vertex ( $gw->vertices ) {
   printf "vertex: %s weight=%.2f\n",
       $vertex->name, $gw->get_cost($vertex, $attr);
   for my $edge ( $gw->edges ) {
       next if $edge->from->name ne $vertex->name;
       printf "\tedge to: %s weight=%.2f\n",
           $edge->to->name, $gw->get_cost($edge, $attr);
   }
}

DESCRIPTION

A Graph::Easy::Weighted object is a subclass of the Graph::Easy module with attribute handling. As such, all of the Graph::Easy methods may be used as documented, but with the addition of custom weighting.

METHODS

new()

my $gw = Graph::Easy::Weighted->new;

Return a new Graph::Easy::Weighted object.

Please see "new()" in Graph::Easy for the possible constructor arguments.

populate()

$gw->populate($matrix);
$gw->populate($matrix, $attribute);
$gw->populate(\@vectors);
$gw->populate(\@vectors, $attribute);
$gw->populate(\%data_points);
$gw->populate(\%data_points, $attribute);

Populate a graph with weighted nodes.

For arguments, the data can be 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 vertices in array reference form:

[]      1 vertex with no edges.
[0]     1 vertex with no edges.
[1]     1 vertex and 1 edge to itself, weight 1.
[0,1]   2 vertices and 1 edge, weight 1.
[1,0,9] 3 vertices and 2 edges having, weight 10.
[1,2,3] 3 vertices and 3 edges having, weight 6.

Multiple attributes may be applied to a graph, thereby layering and increasing the overall dimension.

get_cost()

$c = $gw->get_cost($vertex);
$c = $gw->get_cost($vertex, $attribute);
$c = $gw->get_cost($edge);
$c = $gw->get_cost($edge, $attribute);

Return the weight or named attribute value for the vertex or edge.

vertex_span()

my ($lightest, $heaviest) = $gw->vertex_span();
my ($lightest, $heaviest) = $gw->vertex_span($attr);

Return the lightest and heaviest vertices.

edge_span()

my ($lightest, $heaviest) = $gw->edge_span();
my ($lightest, $heaviest) = $gw->edge_span($attr);

Return the lightest to heaviest edges.

path_cost()

my $weight = $gw->path_cost(\@named_vertices);
my $weight = $gw->path_cost(\@named_vertices, $attr);

Return the summed weight (or given cost attribute) of the path edges.

SEE ALSO

Graph::Easy

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.