The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Graph::Weighted - A weighted graph implementation

VERSION

version 0.9101

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
    ]
 );
 $gw->dump;

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

 my $attr = 'probability';
 $gw = Graph::Weighted->new;
 $gw->populate(
    {
        0 => { label => 'A', 1=>0.4, 3=>0.6 },
        1 => { label => 'B', 0=>0.3, 2=>0.7 },
        2 => { label => 'C', 0=>0.5, 2=>0.5 },
        3 => { label => 'D', 0=>0.2, 1=>0.8 },
    },
    $attr
 );
 $gw->dump($attr);

 my $cost = $gw->get_cost( [0, 1], $attr );

 $cost = $gw->path_cost( [0, 3, 1, 2], $attr );

 my $tree = $gw->MST_Kruskal;
 my $sum = $gw->MST_edge_sum($tree);

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.

METHODS

new

  my $gw = Graph::Weighted->new;
  my $gw = Graph::Weighted->new(%arguments);

Return a new Graph::Weighted object.

Please see "Constructors" in Graph 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 and edges.

The data can be an arrayref of numeric vectors, a Math::Matrix object, a Math::MatrixReal object, or a hashref of node-edge values.

Data given as a hash reference may also contain node labels. Also, the keys need not be numeric, just unique.

The optional attribute argument is a string with the default weight.

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 named attribute value for the vertex or edge. If no attribute name is given, the string weight is used.

vertex_span

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

Return the lightest and heaviest vertices as array references.

edge_span

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

Return the lightest and heaviest edges as array references.

path_cost

 $c = $gw->path_cost(\@vertices);
 $c = $gw->path_cost(\@vertices, $attr);

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

MST_edge_sum

  $sum = $gw->MST_edge_sum($tree);

Compute the sum of the edges of a minimum-spanning-tree.

dump

  $gw->dump
  $gw->dump($attr)

Print out the graph showing vertices, edges and costs.

SEE ALSO

Graph, the parent of this module

Graph::Easy::Weighted, the sibling

The eg/* and t/* programs in this distribution

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020 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.