Graph::Weighted - An abstract, weighted graph implementation


NAME

Graph::Weighted - An abstract, weighted graph implementation


SYNOPSIS

  use Graph::Weighted;
  $g = Graph::Weighted->new(
      data => [
        [ 0, 1, 2, 0, 0 ],  # A vertex with two edges.
        [ 1, 0, 3, 0, 0 ],  # "
        [ 2, 3, 0, 0, 0 ],  # "
        [ 0, 0, 1, 0, 0 ],  # A vertex with one edge.
        [ 0, 0, 0, 0, 0 ]   # A vertex with no edges.
      ]
  );
  $g = Graph::Weighted->new(
      data => {
          weight => {
              a => { b => 1, c => 2 },  # A vertex with two edges.
              b => { a => 1, c => 3 },  # "
              c => { a => 2, b => 3 },  # "
              d => { c => 1 },          # A vertex with one edge.
              e => {}                   # A vertex with no edges.
          }
          foo => [
              [ 1, 2, 3 ],
              [ 4, 5, 6 ],
              [ 7, 8, 9 ]
          ],
     }
  );
  $g = Graph::Weighted->new(
      data => $Math_Matrix_object,
      retrieve_as => 'ARRAY',
  );
  $data = $g->weight_data;
  $w = $g->graph_weight;
  $w = $g->vertex_weight($v1);
  $w = $g->vertex_weight($v1, $w + 1);
  $w = $g->edge_weight($v1, $v2);
  $w = $g->edge_weight($v1, $v2, $w + 1);
  $vertices = $g->heaviest_vertices;
  $vertices = $g->lightest_vertices;
  $w = $g->max_weight;  # Weight of the largest vertices.
  $w = $g->min_weight;  # Weight of the smallest vertices.
  # Call the weight methods of the inherited Graph module.
  $x = $g->MST_Kruskal;
  $x = $g->APSP_Floyd_Warshall;
  $x = $g->MST_Prim($p);


DESCRIPTION

A Graph::Weighted object represents a subclass of Graph::Directed with weighted attributes that are taken from a two dimensional matrix of numerical values.

This module can use a standard array or hash reference for data. It can also load the matrix portions of Math::Matrix, Math::MatrixReal, and Math::MatrixBool objects.

Initially, the weights of the vertices are set to the sum of their outgoing edge weights. This is mutable, however, and can be reset to any value desired, after initialization, with the vertex_weight and edge_weight methods.

This module allows you to create a graph with edges that have values defined in a given matrix. You can have as many of these matrices as you like. Each one is referenced by an attribute name. For a weighted graph, this attribute is named "weight". For a capacity graph, this attribute is named "capacity". Each attribute corresponds to one matrix of values.


PUBLIC METHODS


PRIVATE METHODS


API METHODS

This section briefly describes the methods to use when creating your own, custom subclass of Graph::Weighted. Please see the Graph::Weighted::Capacity module for a simple example.

These are generic methods used in the public methods of Graph::Weighted and Graph::Weighted::Capacity. Primarily, they each accept an extra attribute argument and use the class default attribute, if none is provided.

Please remember that the default_attribute should probably be set, even though it is not required. Also, it is recommended that you specifically call your methods with an attribute (shown as [$ATTR] below), even though you may have already defined a default. This is to avoid the mixups that result in "multi-attributed" graphs, where the default may be something other than the data attribute of interest.

All the following methods are described in greater detail under the PUBLIC METHODS section, above.


SEE ALSO

the Graph::Base manpage

the Graph::Weighted::Capacity manpage


TO DO

Handle arbitrary string attribute values.

Handle algebraic expression attribute values (probably via Math::Symbolic). Lisp expressions come to mind also...

That is, use some sort of callback to update values, instead of addition and subtraction.


AUTHOR

Gene Boggs <gene@cpan.org>


COPYRIGHT AND LICENSE

Copyright 2003 by Gene Boggs

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

 Graph::Weighted - An abstract, weighted graph implementation