Graph::Easy::Weighted - A weighted graph implementation
version 0.0701
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 ] ); $gw->dump(); 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 => { attributes => { title => 'A' }, 1=>0.4, 3=>0.6 }, 1 => { attributes => { title => 'B' }, 0=>0.3, 2=>0.7 }, 2 => { attributes => { title => 'C' }, 0=>0.5, 2=>0.5 }, 3 => { attributes => { title => 'D' }, 0=>0.2, 1=>0.8 }, }, $attr, '%0.2f' ); $gw->dump();
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.
Graph::Easy::Weighted
$gw = Graph::Easy::Weighted->new;
Return a new Graph::Easy::Weighted object.
Please see "new()" in Graph::Easy for the possible constructor arguments.
$gw->populate($matrix); $gw->populate($matrix, $attribute); $gw->populate(\@vectors); $gw->populate(\@vectors, $attribute); $gw->populate(\%data); $gw->populate(\%data, $attribute); $gw->populate(\%data, $attribute, $format);
Populate a graph with weighted nodes.
The data can be an arrayref of numeric vectors, a Math::Matrix object, a Math::MatrixReal object, or a hashref of numeric edge values.
Math::Matrix
Math::MatrixReal
Data given as a hash reference may contain node attributes as shown in the SYNOPSIS. See Graph::Easy::Manual for the available attributes.
The optional edge attribute argument is a string, with the default "weight."
attribute
Multiple attributes may populate a single graph, thereby layering and increasing the overall dimension.
An optional sprintf format string may be provided for the edge label.
sprintf
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.
$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.
($lightest, $heaviest) = $gw->vertex_span(); ($lightest, $heaviest) = $gw->vertex_span($attr);
Return the lightest and heaviest vertices.
($lightest, $heaviest) = $gw->edge_span(); ($lightest, $heaviest) = $gw->edge_span($attr);
Return the lightest to heaviest edges.
$c = $gw->path_cost(\@named_vertices); $c = $gw->path_cost(\@named_vertices, $attr);
Return the summed weight (or given cost attribute) of the path edges.
For shortest paths and minimum spanning trees, please see "EXAMPLES" in Graph::Weighted.
$gw->dump() $gw->dump($attr)
Print out the graph showing vertices, edges and costs.
Graph::Easy, the parent of this module
Graph::Weighted, the sibling
The eg/* and t/* file sources
Gene Boggs <gene@cpan.org>
This software is copyright (c) 2022 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.
To install Graph::Easy::Weighted, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Graph::Easy::Weighted
CPAN shell
perl -MCPAN -e shell install Graph::Easy::Weighted
For more information on module installation, please visit the detailed CPAN module installation guide.