Add %ifempty and variants
[nasm/autotest.git] / perllib / Graph.pod
blob9452d51d261ed249c3536c65939589bd66e86d9d
1 =pod
3 =head1 NAME
5 Graph - graph data structures and algorithms
7 =head1 SYNOPSIS
9         use Graph;
10         my $g0 = Graph->new;             # A directed graph.
12         use Graph::Directed;
13         my $g1 = Graph::Directed->new;   # A directed graph.
15         use Graph::Undirected;
16         my $g2 = Graph::Undirected->new; # An undirected graph.
18         $g->add_edge(...);
19         $g->has_edge(...)
20         $g->delete_edge(...);
22         $g->add_vertex(...);
23         $g->has_vertex(...);
24         $g->delete_vertex(...);
26         $g->vertices(...)
27         $g->edges(...)
29         # And many, many more, see below.
31 =head1 DESCRIPTION
33 =head2 Non-Description
35 This module is not for B<drawing> any sort of I<graphics>, business or
36 otherwise.
38 =head2 Description
40 Instead, this module is for creating I<abstract data structures>
41 called graphs, and for doing various operations on those.
43 =head2 Perl 5.6.0 minimum
45 The implementation depends on a Perl feature called "weak references"
46 and Perl 5.6.0 was the first to have those.
48 =head2 Constructors
50 =over 4
52 =item new
54 Create an empty graph.
56 =item Graph->new(%options)
58 The options are a hash with option names as the hash keys and the option
59 values as the hash values.
61 The following options are available:
63 =over 8
65 =item *
67 directed
69 A boolean option telling that a directed graph should be created.
70 Often somewhat redundant because a directed graph is the default
71 for the Graph class or one could simply use the C<new()> constructor
72 of the Graph::Directed class.
74 You can test the directness of a graph with $g->is_directed() and
75 $g->is_undirected().
77 =item *
79 undirected
81 A boolean option telling that an undirected graph should be created.
82 One could also use the C<new()> constructor the Graph::Undirected class
83 instead.
85 Note that while often it is possible to think undirected graphs as
86 bidirectional graphs, or as directed graphs with edges going both ways,
87 in this module directed graphs and undirected graphs are two different
88 things that often behave differently.
90 You can test the directness of a graph with $g->is_directed() and
91 $g->is_undirected().
93 =item *
95 refvertexed
97 If you want to use references (including Perl objects) as vertices.
99 =item *
101 unionfind
103 If the graph is undirected, you can specify the C<unionfind> parameter
104 to use the so-called union-find scheme to speed up the computation of
105 I<connected components> of the graph (see L</is_connected>,
106 L</connected_components>, L</connected_component_by_vertex>,
107 L</connected_component_by_index>, and L</same_connected_components>).
108 If C<unionfind> is used, adding edges (and vertices) becomes slower,
109 but connectedness queries become faster.  You can test a graph for
110 "union-findness" with
112 =over 8
114 =item has_union_find
116     has_union_find
118 =back
120 =item *
122 vertices
124 An array reference of vertices to add.
126 =item *
128 edges
130 An array reference of array references of edge vertices to add.
132 =back
134 =item copy
136 =item copy_graph
138     my $c = $g->copy_graph;
140 Create a shallow copy of the structure (vertices and edges) of the graph.
141 If you want a deep copy that includes attributes, see L</deep_copy>.
142 The copy will have the same directedness as the original.
144 =item deep_copy
146 =item deep_copy_graph
148     my $c = $g->deep_copy_graph;
150 Create a deep copy of the graph (vertices, edges, and attributes) of
151 the graph.  If you want a shallow copy that does not include attributes,
152 see L</copy>.  (Uses Data::Dumper behind the scenes.  Note that copying
153 code references only works with Perls 5.8 or later, and even then only
154 if B::Deparse can reconstruct your code.)
156 =item undirected_copy
158 =item undirected_copy_graph
160     my $c = $g->undirected_copy_graph;
162 Create an undirected shallow copy (vertices and edges) of the directed graph
163 so that for any directed edge (u, v) there is an undirected edge (u, v).
165 =item directed_copy
167 =item directed_copy_graph
169     my $c = $g->directed_copy_graph;
171 Create a directed shallow copy (vertices and edges) of the undirected graph
172 so that for any undirected edge (u, v) there are two directed edges (u, v)
173 and (v, u).
175 =item transpose
177 =item transpose_graph
179     my $t = $g->transpose_graph;
181 Create a directed shallow transposed copy (vertices and edges) of the
182 directed graph so that for any directed edge (u, v) there is a directed
183 edge (v, u).
185 You can also transpose a single edge with
187 =over 8
189 =item transpose_edge
191     $g->transpose_edge($u, $v)
193 =back
195 =item complete_graph
197 =item complete
199     my $c = $g->complete_graph;
201 Create a complete graph that has the same vertices as the original graph.
202 A complete graph has an edge between every pair of vertices.
204 =item complement_graph
206 =item complement
208     my $c = $g->complement_graph;
210 Create a complement graph that has the same vertices as the original graph.
211 A complement graph has an edge (u,v) if and only if the original
212 graph does not have edge (u,v).
214 =back
216 See also L</random_graph> for a random constructor.
218 =head2 Basics
220 =over 4
222 =item add_vertex
224     $g->add_vertex($v)
226 Add the vertex to the graph.  Returns the graph.
228 By default idempotent, but a graph can be created I<countvertexed>.
230 A vertex is also known as a I<node>.
232 Adding C<undef> as vertex is not allowed.
234 Note that unless you have isolated vertices (or I<countvertexed>
235 vertices), you do not need to explicitly use C<add_vertex> since
236 L</add_edge> will implicitly add its vertices.
238 =item add_edge
240     $g->add_edge($u, $v)
242 Add the edge to the graph.  Implicitly first adds the vertices if the
243 graph does not have them.  Returns the graph.
245 By default idempotent, but a graph can be created I<countedged>.
247 An edge is also known as an I<arc>.
249 =item has_vertex
251     $g->has_vertex($v)
253 Return true if the vertex exists in the graph, false otherwise.
255 =item has_edge
257     $g->has_edge($u, $v)
259 Return true if the edge exists in the graph, false otherwise.
261 =item delete_vertex
263     $g->delete_vertex($v)
265 Delete the vertex from the graph.  Returns the graph, even
266 if the vertex did not exist in the graph.
268 If the graph has been created I<multivertexed> or I<countvertexed>
269 and a vertex has been added multiple times, the vertex will require
270 at least an equal number of deletions to become completely deleted.
272 =item delete_vertices
274     $g->delete_vertices($v1, $v2, ...)
276 Delete the vertices from the graph.  Returns the graph.
278 If the graph has been created I<multivertexed> or I<countvertexed>
279 and a vertex has been added multiple times, the vertex will require
280 at least an equal number of deletions to become completely deleteted.
282 =item delete_edge
284     $g->delete_edge($u, $v)
286 Delete the edge from the graph.  Returns the graph, even
287 if the edge did not exist in the graph.
289 If the graph has been created I<multivertexed> or I<countedged>
290 and an edge has been added multiple times, the edge will require
291 at least an equal number of deletions to become completely deleted.
293 =item delete_edges
295     $g->delete_edges($u1, $v1, $u2, $v2, ...)
297 Delete the edges from the graph.  Returns the graph.
299 If the graph has been created I<multivertexed> or I<countedged>
300 and an edge has been added multiple times, the edge will require
301 at least an equal number of deletions to become completely deleted.
303 =back
305 =head2 Displaying
307 Graphs have stringification overload, so you can do things like
309     print "The graph is $g\n"
311 One-way (directed, unidirected) edges are shown as '-', two-way
312 (undirected, bidirected) edges are shown as '='.  If you want to,
313 you can call the stringification via the method
315 =over 4
317 =item stringify
319 =back
321 =head2 Comparing
323 Testing for equality can be done either by the overloaded C<eq>
324 operator
326     $g eq "a-b,a-c,d"
328 or by the method
330 =over 4
332 =item eq
334     $g->eq("a-b,a-c,d")
336 =back
338 The equality testing compares the stringified forms, and therefore it
339 assumes total equality, not isomorphism: all the vertices must be
340 named the same, and they must have identical edges between them.
342 For unequality there are correspondingly the overloaded C<ne>
343 operator and the method
345 =over 4
347 =item ne
349     $g->ne("a-b,a-c,d")
351 =back
353 See also L</Isomorphism>.
355 =head2 Paths and Cycles
357 Paths and cycles are simple extensions of edges: paths are edges
358 starting from where the previous edge ended, and cycles are paths
359 returning back to the start vertex of the first edge.
361 =over 4
363 =item add_path
365    $g->add_path($a, $b, $c, ..., $x, $y, $z)
367 Add the edges $a-$b, $b-$c, ..., $x-$y, $y-$z to the graph.
368 Returns the graph.
370 =item has_path
372    $g->has_path($a, $b, $c, ..., $x, $y, $z)
374 Return true if the graph has all the edges $a-$b, $b-$c, ..., $x-$y, $y-$z,
375 false otherwise.
377 =item delete_path
379    $g->delete_path($a, $b, $c, ..., $x, $y, $z)
381 Delete all the edges edges $a-$b, $b-$c, ..., $x-$y, $y-$z
382 (regardless of whether they exist or not).  Returns the graph.
384 =item add_cycle
386    $g->add_cycle($a, $b, $c, ..., $x, $y, $z)
388 Add the edges $a-$b, $b-$c, ..., $x-$y, $y-$z, and $z-$a to the graph.
389 Returns the graph.
391 =item has_cycle
393    $g->has_cycle($a, $b, $c, ..., $x, $y, $z)
395 Return true if the graph has all the edges $a-$b, $b-$c, ..., $x-$y, $y-$z,
396 and $z-$a, false otherwise.
398 B<NOTE:> This does not I<detect> cycles, see L</has_a_cycle> and
399 L</find_a_cycle>.
401 =item delete_cycle
403    $g->delete_cycle($a, $b, $c, ..., $x, $y, $z)
405 Delete all the edges edges $a-$b, $b-$c, ..., $x-$y, $y-$z, and $z-$a
406 (regardless of whether they exist or not).  Returns the graph.
408 =item has_a_cycle
410    $g->has_a_cycle
412 Returns true if the graph has a cycle, false if not.
414 =item find_a_cycle
416    $g->find_a_cycle
418 Returns a cycle if the graph has one (as a list of vertices), an empty
419 list if no cycle can be found.
421 Note that this just returns the vertices of I<a cycle>: not any
422 particular cycle, just the first one it finds.  A repeated call
423 might find the same cycle, or it might find a different one, and
424 you cannot call this repeatedly to find all the cycles.
426 =back
428 =head2 Graph Types
430 =over 4
432 =item is_simple_graph
434     $g->is_simple_graph
436 Return true if the graph has no multiedges, false otherwise.
438 =item is_pseudo_graph
440     $g->is_pseudo_graph
442 Return true if the graph has any multiedges or any self-loops,
443 false otherwise.
445 =item is_multi_graph
447     $g->is_multi_graph
449 Return true if the graph has any multiedges but no self-loops,
450 false otherwise.
452 =item is_directed_acyclic_graph
454 =item is_dag
456     $g->is_directed_acyclic_graph
457     $g->is_dag
459 Return true if the graph is directed and acyclic, false otherwise.
461 =item is_cyclic
463     $g->is_cyclic
465 Return true if the graph is cyclic (contains at least one cycle).
466 (This is identical to C<has_a_cycle>.)
468 To find at least that one cycle, see L</find_a_cycle>.
470 =item is_acyclic
472 Return true if the graph is acyclic (does not contain any cycles).
474 =back
476 To find a cycle, use L<find_a_cycle>.
478 =head2 Transitivity
480 =over 4
482 =item is_transitive
484     $g->is_transitive
486 Return true if the graph is transitive, false otherwise.
488 =item TransitiveClosure_Floyd_Warshall
490 =item transitive_closure
492     $tcg = $g->TransitiveClosure_Floyd_Warshall
494 Return the transitive closure graph of the graph.
496 =back
498 You can query the reachability from $u to $v with
500 =over 4
502 =item is_reachable
504     $tcg->is_reachable($u, $v)
506 =back
508 See L<Graph::TransitiveClosure> for more information about creating
509 and querying transitive closures.
511 With
513 =over 4
515 =item transitive_closure_matrix
517    $tcm = $g->transitive_closure_matrix;
519 =back
521 you can (create if not existing and) query the transitive closure
522 matrix that underlies the transitive closure graph.  See
523 L<Graph::TransitiveClosure::Matrix> for more information.
525 =head2 Mutators
527 =over 4
529 =item add_vertices
531     $g->add_vertices('d', 'e', 'f')
533 Add zero or more vertices to the graph.
535 =item add_edges
537     $g->add_edges(['d', 'e'], ['f', 'g'])
538     $g->add_edges(qw(d e f g));
540 Add zero or more edges to the graph.  The edges are specified as
541 a list of array references, or as a list of vertices where the
542 even (0th, 2nd, 4th, ...) items are start vertices and the odd
543 (1st, 3rd, 5th, ...) are the corresponding end vertices.
545 =back
547 =head2 Accessors
549 =over 4
551 =item is_directed
553 =item directed
555     $g->is_directed()
556     $g->directed()
558 Return true if the graph is directed, false otherwise.
560 =item is_undirected
562 =item undirected
564     $g->is_undirected()
565     $g->undirected()
567 Return true if the graph is undirected, false otherwise.
569 =item is_refvertexed
571 =item refvertexed
573 Return true if the graph can handle references (including Perl objects)
574 as vertices.
576 =item vertices
578     my $V = $g->vertices
579     my @V = $g->vertices
581 In scalar context, return the number of vertices in the graph.
582 In list context, return the vertices, in no particular order.
584 =item has_vertices
586     $g->has_vertices()
588 Return true if the graph has any vertices, false otherwise.
590 =item edges
592     my $E = $g->edges
593     my @E = $g->edges
595 In scalar context, return the number of edges in the graph.
596 In list context, return the edges, in no particular order.
597 I<The edges are returned as anonymous arrays listing the vertices.>
599 =item has_edges
601     $g->has_edges()
603 Return true if the graph has any edges, false otherwise.
605 =item is_connected
607     $g->is_connected
609 For an undirected graph, return true is the graph is connected, false
610 otherwise.  Being connected means that from every vertex it is possible
611 to reach every other vertex.
613 If the graph has been created with a true C<unionfind> parameter,
614 the time complexity is (essentially) O(V), otherwise O(V log V).
616 See also L</connected_components>, L</connected_component_by_index>,
617 L</connected_component_by_vertex>, and L</same_connected_components>,
618 and L</biconnectivity>.
620 For directed graphs, see L</is_strongly_connected>
621 and L</is_weakly_connected>.
623 =item connected_components
625     @cc = $g->connected_components()
627 For an undirected graph, returns the vertices of the connected
628 components of the graph as a list of anonymous arrays.  The ordering
629 of the anonymous arrays or the ordering of the vertices inside the
630 anonymous arrays (the components) is undefined.
632 For directed graphs, see L</strongly_connected_components>
633 and L</weakly_connected_components>.
635 =item connected_component_by_vertex
637     $i = $g->connected_component_by_vertex($v)
639 For an undirected graph, return an index identifying the connected
640 component the vertex belongs to, the indexing starting from zero.
642 For the inverse, see L</connected_component_by_index>.
644 If the graph has been created with a true C<unionfind> parameter,
645 the time complexity is (essentially) O(1), otherwise O(V log V).
647 See also L</biconnectivity>.
649 For directed graphs, see L</strongly_connected_component_by_vertex>
650 and L</weakly_connected_component_by_vertex>.
652 =item connected_component_by_index
654     @v = $g->connected_component_by_index($i)
656 For an undirected graph, return the vertices of the ith connected
657 component, the indexing starting from zero.  The order of vertices is
658 undefined, while the order of the connected components is same as from
659 connected_components().
661 For the inverse, see L</connected_component_by_vertex>.
663 For directed graphs, see L</strongly_connected_component_by_index>
664 and L</weakly_connected_component_by_index>.
666 =item same_connected_components
668     $g->same_connected_components($u, $v, ...)
670 For an undirected graph, return true if the vertices are in the same
671 connected component.
673 If the graph has been created with a true C<unionfind> parameter,
674 the time complexity is (essentially) O(1), otherwise O(V log V).
676 For directed graphs, see L</same_strongly_connected_components>
677 and L</same_weakly_connected_components>.
679 =item connected_graph
681     $cg = $g->connected_graph
683 For an undirected graph, return its connected graph.
685 =item connectivity_clear_cache
687     $g->connectivity_clear_cache
689 See L</"Clearing cached results">.
691 See L</"Connected Graphs and Their Components"> for further discussion.
693 =item biconnectivity
695     my ($ap, $bc, $br) = $g->biconnectivity
697 For an undirected graph, return the various biconnectivity components
698 of the graph: the articulation points (cut vertices), biconnected
699 components, and bridges.
701 Note: currently only handles connected graphs.
703 =item is_biconnected
705    $g->is_biconnected
707 For an undirected graph, return true if the graph is biconnected
708 (if it has no articulation points, also known as cut vertices).
710 =item is_edge_connected
712    $g->is_edge_connected
714 For an undirected graph, return true if the graph is edge-connected
715 (if it has no bridges).
717 =item is_edge_separable
719    $g->is_edge_separable
721 For an undirected graph, return true if the graph is edge-separable
722 (if it has bridges).
724 =item articulation_points
726 =item cut_vertices
728    $g->articulation_points
730 For an undirected graph, return the articulation points (cut vertices)
731 of the graph as a list of vertices.  The order is undefined.
733 =item biconnected_components
735    $g->biconnected_components
737 For an undirected graph, return the biconnected components of the
738 graph as a list of anonymous arrays of vertices in the components.
739 The ordering of the anonymous arrays or the ordering of the vertices
740 inside the anonymous arrays (the components) is undefined.  Also note
741 that one vertex can belong to more than one biconnected component.
743 =item biconnected_component_by_vertex
745    $i = $g->biconnected_component_by_index($v)
747 For an undirected graph, return an index identifying the biconnected
748 component the vertex belongs to, the indexing starting from zero.
750 For the inverse, see L</connected_component_by_index>.
752 For directed graphs, see L</strongly_connected_component_by_index>
753 and L</weakly_connected_component_by_index>.
755 =item biconnected_component_by_index
757    @v = $g->biconnected_component_by_index($i)
759 For an undirected graph, return the vertices in the ith biconnected
760 component of the graph as an anonymous arrays of vertices in the
761 component.  The ordering of the vertices within a component is
762 undefined.  Also note that one vertex can belong to more than one
763 biconnected component.
765 =item same_biconnected_components
767     $g->same_biconnected_components($u, $v, ...)
769 For an undirected graph, return true if the vertices are in the same
770 biconnected component.
772 =item biconnected_graph
774     $bcg = $g->biconnected_graph
776 For an undirected graph, return its biconnected graph.
778 See L</"Connected Graphs and Their Components"> for further discussion.
780 =item bridges
782    $g->bridges
784 For an undirected graph, return the bridges of the graph as a list of
785 anonymous arrays of vertices in the bridges.  The order of bridges and
786 the order of vertices in them is undefined.
788 =item biconnectivity_clear_cache
790     $g->biconnectivity_clear_cache
792 See L</"Clearing cached results">.
794 =item strongly_connected
796 =item is_strongly_connected
798     $g->is_strongly_connected
800 For a directed graph, return true is the directed graph is strongly
801 connected, false if not.
803 See also L</is_weakly_connected>.
805 For undirected graphs, see L</is_connected>, or L</is_biconnected>.
807 =item strongly_connected_component_by_vertex
809     $i = $g->strongly_connected_component_by_vertex($v)
811 For a directed graph, return an index identifying the strongly
812 connected component the vertex belongs to, the indexing starting from
813 zero.
815 For the inverse, see L</strongly_connected_component_by_index>.
817 See also L</weakly_connected_component_by_vertex>.
819 For undirected graphs, see L</connected_components> or
820 L</biconnected_components>.
822 =item strongly_connected_component_by_index
824     @v = $g->strongly_connected_component_by_index($i)
826 For a directed graph, return the vertices of the ith connected
827 component, the indexing starting from zero.  The order of vertices
828 within a component is undefined, while the order of the connected
829 components is the as from strongly_connected_components().
831 For the inverse, see L</strongly_connected_component_by_vertex>.
833 For undirected graphs, see L</weakly_connected_component_by_index>.
835 =item same_strongly_connected_components
837     $g->same_strongly_connected_components($u, $v, ...)
839 For a directed graph, return true if the vertices are in the same
840 strongly connected component.
842 See also L</same_weakly_connected_components>.
844 For undirected graphs, see L</same_connected_components> or
845 L</same_biconnected_components>.
847 =item strong_connectivity_clear_cache
849     $g->strong_connectivity_clear_cache
851 See L</"Clearing cached results">.
853 =item weakly_connected
855 =item is_weakly_connected
857     $g->is_weakly_connected
859 For a directed graph, return true is the directed graph is weakly
860 connected, false if not.
862 Weakly connected graph is also known as I<semiconnected> graph.
864 See also L</is_strongly_connected>.
866 For undirected graphs, see L</is_connected> or L</is_biconnected>.
868 =item weakly_connected_components
870     @wcc = $g->weakly_connected_components()
872 For a directed graph, returns the vertices of the weakly connected
873 components of the graph as a list of anonymous arrays.  The ordering
874 of the anonymous arrays or the ordering of the vertices inside the
875 anonymous arrays (the components) is undefined.
877 See also L</strongly_connected_components>.
879 For undirected graphs, see L</connected_components> or
880 L</biconnected_components>.
882 =item weakly_connected_component_by_vertex
884     $i = $g->weakly_connected_component_by_vertex($v)
886 For a directed graph, return an index identifying the weakly connected
887 component the vertex belongs to, the indexing starting from zero.
889 For the inverse, see L</weakly_connected_component_by_index>.
891 For undirected graphs, see L</connected_component_by_vertex>
892 and L</biconnected_component_by_vertex>.
894 =item weakly_connected_component_by_index
896     @v = $g->weakly_connected_component_by_index($i)
898 For a directed graph, return the vertices of the ith weakly connected
899 component, the indexing starting zero.  The order of vertices within
900 a component is undefined, while the order of the weakly connected
901 components is same as from weakly_connected_components().
903 For the inverse, see L</weakly_connected_component_by_vertex>.
905 For undirected graphs, see L<connected_component_by_index>
906 and L<biconnected_component_by_index>.
908 =item same_weakly_connected_components
910     $g->same_weakly_connected_components($u, $v, ...)
912 Return true if the vertices are in the same weakly connected component.
914 =item weakly_connected_graph
916     $wcg = $g->weakly_connected_graph
918 For a directed graph, return its weakly connected graph.
920 For undirected graphs, see L</connected_graph> and L</biconnected_graph>.
922 =item strongly_connected_components
924    my @scc = $g->strongly_connected_components;
926 For a directed graph, return the strongly connected components as a
927 list of anonymous arrays.  The elements in the anonymous arrays are
928 the vertices belonging to the strongly connected component; both the
929 elements and the components are in no particular order.
931 See also L</weakly_connected_components>.
933 For undirected graphs, see L</connected_components>,
934 or see L</biconnected_components>.
936 =item strongly_connected_graph
938    my $scg = $g->strongly_connected_graph;
940 See L</"Connected Graphs and Their Components"> for further discussion.
942 Strongly connected graphs are also known as I<kernel graphs>.
944 See also L</weakly_connected_graph>.
946 For undirected graphs, see L</connected_graph>, or L</biconnected_graph>.
948 =item is_sink_vertex
950     $g->is_sink_vertex($v)
952 Return true if the vertex $v is a sink vertex, false if not.  A sink
953 vertex is defined as a vertex with predecessors but no successors:
954 this definition means that isolated vertices are not sink vertices.
955 If you want also isolated vertices, use is_successorless_vertex().
957 =item is_source_vertex
959     $g->is_source_vertex($v)
961 Return true if the vertex $v is a source vertex, false if not.  A source
962 vertex is defined as a vertex with successors but no predecessors:
963 the definition means that isolated vertices are not source vertices.
964 If you want also isolated vertices, use is_predecessorless_vertex().
966 =item is_successorless_vertex
968     $g->is_successorless_vertex($v)
970 Return true if the vertex $v has no succcessors (no edges
971 leaving the vertex), false if it has.
973 Isolated vertices will return true: if you do not want this,
974 use is_sink_vertex().
976 =item is_successorful_vertex
978     $g->is_successorful_vertex($v)
980 Return true if the vertex $v has successors, false if not.
982 =item is_predecessorless_vertex
984     $g->is_predecessorless_vertex($v)
986 Return true if the vertex $v has no predecessors (no edges
987 entering the vertex), false if it has.
989 Isolated vertices will return true: if you do not want this,
990 use is_source_vertex().
992 =item is_predecessorful_vertex
994     $g->is_predecessorful_vertex($v)
996 Return true if the vertex $v has predecessors, false if not.
998 =item is_isolated_vertex
1000     $g->is_isolated_vertex($v)
1002 Return true if the vertex $v is an isolated vertex: no successors
1003 and no predecessors.
1005 =item is_interior_vertex
1007     $g->is_interior_vertex($v)
1009 Return true if the vertex $v is an interior vertex: both successors
1010 and predecessors.
1012 =item is_exterior_vertex
1014     $g->is_exterior_vertex($v)
1016 Return true if the vertex $v is an exterior vertex: has either no
1017 successors or no predecessors, or neither.
1019 =item is_self_loop_vertex
1021     $g->is_self_loop_vertex($v)
1023 Return true if the vertex $v is a self loop vertex: has an edge
1024 from itself to itself.
1026 =item sink_vertices
1028     @v = $g->sink_vertices()
1030 Return the sink vertices of the graph.
1031 In scalar context return the number of sink vertices.
1032 See L</is_sink_vertex> for the definition of a sink vertex.
1034 =item source_vertices
1036     @v = $g->source_vertices()
1038 Return the source vertices of the graph.
1039 In scalar context return the number of source vertices.
1040 See L</is_source_vertex> for the definition of a source vertex.
1042 =item successorful_vertices
1044     @v = $g->successorful_vertices()
1046 Return the successorful vertices of the graph.
1047 In scalar context return the number of successorful vertices.
1049 =item successorless_vertices
1051     @v = $g->successorless_vertices()
1053 Return the successorless vertices of the graph.
1054 In scalar context return the number of successorless vertices.
1056 =item successors
1058     @s = $g->successors($v)
1060 Return the immediate successor vertices of the vertex.
1062 =item neighbors
1064 =item neighbours
1066 Return the neighbo(u)ring vertices.  Also known as the I<adjacent vertices>.
1068 =item predecessorful_vertices
1070     @v = $g->predecessorful_vertices()
1072 Return the predecessorful vertices of the graph.
1073 In scalar context return the number of predecessorful vertices.
1075 =item predecessorless_vertices
1077     @v = $g->predecessorless_vertices()
1079 Return the predecessorless vertices of the graph.
1080 In scalar context return the number of predecessorless vertices.
1082 =item predecessors
1084     @s = $g->predecessors($v)
1086 Return the immediate predecessor vertices of the vertex.
1088 =item isolated_vertices
1090     @v = $g->isolated_vertices()
1092 Return the isolated vertices of the graph.
1093 In scalar context return the number of isolated vertices.
1094 See L</is_isolated_vertex> for the definition of an isolated vertex.
1096 =item interior_vertices
1098     @v = $g->interior_vertices()
1100 Return the interior vertices of the graph.
1101 In scalar context return the number of interior vertices.
1102 See L</is_interior_vertex> for the definition of an interior vertex.
1104 =item exterior_vertices
1106     @v = $g->exterior_vertices()
1108 Return the exterior vertices of the graph.
1109 In scalar context return the number of exterior vertices.
1110 See L</is_exterior_vertex> for the definition of an exterior vertex.
1112 =item self_loop_vertices
1114     @v = $g->self_loop_vertices()
1116 Return the self-loop vertices of the graph.
1117 In scalar context return the number of self-loop vertices.
1118 See L</is_self_loop_vertex> for the definition of a self-loop vertex.
1120 =back
1122 =head2 Connected Graphs and Their Components
1124 In this discussion I<connected graph> refers to any of
1125 I<connected graphs>, I<biconnected graphs>, and I<strongly
1126 connected graphs>.
1128 B<NOTE>: if the vertices of the original graph are Perl objects,
1129 (in other words, references, so you must be using C<refvertexed>) 
1130 the vertices of the I<connected graph> are NOT by default usable
1131 as Perl objects because they are blessed into a package with
1132 a rather unusable name.
1134 By default, the vertex names of the I<connected graph> are formed from
1135 the names of the vertices of the original graph by (alphabetically
1136 sorting them and) concatenating their names with C<+>.  The vertex
1137 attribute C<subvertices> is also used to store the list (as an array
1138 reference) of the original vertices.  To change the 'supercomponent'
1139 vertex names and the whole logic of forming these supercomponents
1140 use the C<super_component>) option to the method calls:
1142   $g->connected_graph(super_component => sub { ... })
1143   $g->biconnected_graph(super_component => sub { ... })
1144   $g->strongly_connected_graph(super_component => sub { ... })
1146 The subroutine reference gets the 'subcomponents' (the vertices of the
1147 original graph) as arguments, and it is supposed to return the new
1148 supercomponent vertex, the "stringified" form of which is used as the
1149 vertex name.
1151 =head2 Degree
1153 A vertex has a degree based on the number of incoming and outgoing edges.
1154 This really makes sense only for directed graphs.
1156 =over 4
1158 =item degree
1160 =item vertex_degree
1162     $d = $g->degree($v)
1163     $d = $g->vertex_degree($v)
1165 For directed graphs: the in-degree minus the out-degree at the vertex.
1166 For undirected graphs: the number of edges at the vertex.
1168 =item in_degree
1170     $d = $g->in_degree($v)
1172 The number of incoming edges at the vertex.
1174 =item out_degree
1176     $o = $g->out_degree($v)
1178 The number of outgoing edges at the vertex.
1180 =item average_degree
1182    my $ad = $g->average_degree;
1184 Return the average degree taken over all vertices.
1186 =back
1188 Related methods are
1190 =over 4
1192 =item edges_at
1194     @e = $g->edges_at($v)
1196 The union of edges from and edges to at the vertex.
1198 =item edges_from
1200     @e = $g->edges_from($v)
1202 The edges leaving the vertex.
1204 =item edges_to
1206     @e = $g->edges_to($v)
1208 The edges entering the vertex.
1210 =back
1212 See also L</average_degree>.
1214 =head2 Counted Vertices
1216 I<Counted vertices> are vertices with more than one instance, normally
1217 adding vertices is idempotent.  To enable counted vertices on a graph,
1218 give the C<countvertexed> parameter a true value
1220     use Graph;
1221     my $g = Graph->new(countvertexed => 1);
1223 To find out how many times the vertex has been added:
1225 =over 4
1227 =item get_vertex_count
1229     my $c = $g->get_vertex_count($v);
1231 Return the count of the vertex, or undef if the vertex does not exist.
1233 =back
1235 =head2 Multiedges, Multivertices, Multigraphs
1237 I<Multiedges> are edges with more than one "life", meaning that one
1238 has to delete them as many times as they have been added.  Normally
1239 adding edges is idempotent (in other words, adding edges more than
1240 once makes no difference).
1242 There are two kinds or degrees of creating multiedges and multivertices.
1243 The two kinds are mutually exclusive.
1245 The weaker kind is called I<counted>, in which the edge or vertex has
1246 a count on it: add operations increase the count, and delete
1247 operations decrease the count, and once the count goes to zero, the
1248 edge or vertex is deleted.  If there are attributes, they all are
1249 attached to the same vertex.  You can think of this as the graph
1250 elements being I<refcounted>, or I<reference counted>, if that sounds
1251 more familiar.
1253 The stronger kind is called (true) I<multi>, in which the edge or vertex
1254 really has multiple separate identities, so that you can for example
1255 attach different attributes to different instances.
1257 To enable multiedges on a graph:
1259     use Graph;
1260     my $g0 = Graph->new(countedged => 1);
1261     my $g0 = Graph->new(multiedged => 1);
1263 Similarly for vertices
1265     use Graph;
1266     my $g1 = Graph->new(countvertexed => 1);
1267     my $g1 = Graph->new(multivertexed => 1);
1269 You can test for these by
1271 =over 4
1273 =item is_countedged
1275 =item countedged
1277     $g->is_countedged
1278     $g->countedged
1280 Return true if the graph is countedged.
1282 =item is_countvertexed
1284 =item countvertexed
1286     $g->is_countvertexed
1287     $g->countvertexed
1289 Return true if the graph is countvertexed.
1291 =item is_multiedged
1293 =item multiedged
1295     $g->is_multiedged
1296     $g->multiedged
1298 Return true if the graph is multiedged.
1300 =item is_multivertexed
1302 =item multivertexed
1304     $g->is_multivertexed
1305     $g->multivertexed
1307 Return true if the graph is multivertexed.
1309 =back
1311 A multiedged (either the weak kind or the strong kind) graph is
1312 a I<multigraph>, for which you can test with C<is_multi_graph()>.
1314 B<NOTE>: The various graph algorithms do not in general work well with
1315 multigraphs (they often assume I<simple graphs>, that is, no
1316 multiedges or loops), and no effort has been made to test the
1317 algorithms with multigraphs.
1319 vertices() and edges() will return the multiple elements: if you want
1320 just the unique elements, use
1322 =over 4
1324 =item unique_vertices
1326 =item unique_edges
1328     @uv = $g->unique_vertices; # unique
1329     @mv = $g->vertices;        # possible multiples
1330     @ue = $g->unique_edges;
1331     @me = $g->edges;
1333 =back
1335 If you are using (the stronger kind of) multielements, you should use
1336 the I<by_id> variants:
1338 =over 4
1340 =item add_vertex_by_id
1342 =item has_vertex_by_id
1344 =item delete_vertex_by_id
1346 =item add_edge_by_id
1348 =item has_edge_by_id
1350 =item delete_edge_by_id
1352 =back
1354     $g->add_vertex_by_id($v, $id)
1355     $g->has_vertex_by_id($v, $id)
1356     $g->delete_vertex_by_id($v, $id)
1358     $g->add_edge_by_id($u, $v, $id)
1359     $g->has_edge_by_id($u, $v, $id)
1360     $g->delete_edge_by_id($u, $v, $id)
1362 When you delete the last vertex/edge in a multivertex/edge, the whole
1363 vertex/edge is deleted.  You can use add_vertex()/add_edge() on
1364 a multivertex/multiedge graph, in which case an id is generated
1365 automatically.  To find out which the generated id was, you need
1366 to use
1368 =over 4
1370 =item add_vertex_get_id
1372 =item add_edge_get_id
1374 =back
1376     $idv = $g->add_vertex_get_id($v)
1377     $ide = $g->add_edge_get_id($u, $v)
1379 To return all the ids of vertices/edges in a multivertex/multiedge, use
1381 =over 4
1383 =item get_multivertex_ids
1385 =item get_multiedge_ids
1387 =back
1389     $g->get_multivertex_ids($v)
1390     $g->get_multiedge_ids($u, $v)
1392 The ids are returned in random order.
1394 To find out how many times the edge has been added (this works for
1395 either kind of multiedges):
1397 =over 4
1399 =item get_edge_count
1401     my $c = $g->get_edge_count($u, $v);
1403 Return the count (the "countedness") of the edge, or undef if the
1404 edge does not exist.
1406 =back
1408 The following multi-entity utility functions exist, mirroring
1409 the non-multi vertices and edges:
1411 =over 4
1413 =item add_weighted_edge_by_id
1415 =item add_weighted_edges_by_id
1417 =item add_weighted_path_by_id
1419 =item add_weighted_vertex_by_id
1421 =item add_weighted_vertices_by_id
1423 =item delete_edge_weight_by_id
1425 =item delete_vertex_weight_by_id
1427 =item get_edge_weight_by_id
1429 =item get_vertex_weight_by_id
1431 =item has_edge_weight_by_id
1433 =item has_vertex_weight_by_id
1435 =item set_edge_weight_by_id
1437 =item set_vertex_weight_by_id
1439 =back
1441 =head2 Topological Sort
1443 =over 4
1445 =item topological_sort
1447 =item toposort
1449     my @ts = $g->topological_sort;
1451 Return the vertices of the graph sorted topologically.  Note that
1452 there may be several possible topological orderings; one of them
1453 is returned.
1455 If the graph contains a cycle, a fatal error is thrown, you
1456 can either use C<eval> to trap that, or supply the C<empty_if_cyclic>
1457 argument with a true value
1459     my @ts = $g->topological_sort(empty_if_cyclic => 1);
1461 in which case an empty array is returned if the graph is cyclic.
1463 =back
1465 =head2 Minimum Spanning Trees (MST)
1467 Minimum Spanning Trees or MSTs are tree subgraphs derived from an
1468 undirected graph.  MSTs "span the graph" (covering all the vertices)
1469 using as lightly weighted (hence the "minimum") edges as possible.
1471 =over 4
1473 =item MST_Kruskal
1475     $mstg = $g->MST_Kruskal;
1477 Returns the Kruskal MST of the graph.
1479 =item MST_Prim
1481     $mstg = $g->MST_Prim(%opt);
1483 Returns the Prim MST of the graph.
1485 You can choose the first vertex with $opt{ first_root }.
1487 =item MST_Dijkstra
1489 =item minimum_spanning_tree
1491     $mstg = $g->MST_Dijkstra;
1492     $mstg = $g->minimum_spanning_tree;
1494 Aliases for MST_Prim.
1496 =back
1498 =head2 Single-Source Shortest Paths (SSSP)
1500 Single-source shortest paths, also known as Shortest Path Trees
1501 (SPTs).  For either a directed or an undirected graph, return a (tree)
1502 subgraph that from a single start vertex (the "single source") travels
1503 the shortest possible paths (the paths with the lightest weights) to
1504 all the other vertices.  Note that the SSSP is neither reflexive (the
1505 shortest paths do not include the zero-length path from the source
1506 vertex to the source vertex) nor transitive (the shortest paths do not
1507 include transitive closure paths).  If no weight is defined for an
1508 edge, 1 (one) is assumed.
1510 =over 4
1512 =item SPT_Dijkstra
1514     $sptg = $g->SPT_Dijkstra($root)
1515     $sptg = $g->SPT_Dijkstra(%opt)
1517 Return as a graph the the single-source shortest paths of the graph
1518 using Dijkstra's algorithm.  The graph cannot contain negative edges
1519 (negative edges cause the algorithm to abort with an error message
1520 C<Graph::SPT_Dijkstra: edge ... is negative>).
1522 You can choose the first vertex of the result with either a single
1523 vertex argument or with $opt{ first_root }, otherwise a random vertex
1524 is chosen.
1526 B<NOTE>: note that all the vertices might not be reachable from the
1527 selected (explicit or random) start vertex.
1529 The start vertex is be available as the graph attribute
1530 C<SPT_Dijkstra_root>).
1532 The result weights of vertices can be retrieved from the result graph by
1534         my $w = $sptg->get_vertex_attribute($v, 'weight');
1536 The predecessor vertex of a vertex in the result graph
1537 can be retrieved by
1539         my $u = $sptg->get_vertex_attribute($v, 'p');
1541 ("A successor vertex" cannot be retrieved as simply because a single
1542 vertex can have several successors.  You can first find the
1543 C<neighbors()> vertices and then remove the predecessor vertex.)
1545 If you want to find the shortest path between two vertices,
1546 see L</SP_Dijkstra>.
1548 =item SSSP_Dijkstra
1550 =item single_source_shortest_paths
1552 Aliases for SPT_Dijkstra.
1554 =item SP_Dijkstra
1556     @path = $g->SP_Dijkstra($u, $v)
1558 Return the vertices in the shortest path in the graph $g between the
1559 two vertices $u, $v.  If no path can be found, an empty list is returned.
1561 Uses SPT_Dijkstra().
1563 =item SPT_Dijkstra_clear_cache
1565     $g->SPT_Dijkstra_clear_cache
1567 See L</"Clearing cached results">.
1569 =item SPT_Bellman_Ford
1571     $sptg = $g->SPT_Bellman_Ford(%opt)
1573 Return as a graph the single-source shortest paths of the graph using
1574 Bellman-Ford's algorithm.  The graph can contain negative edges but
1575 not negative cycles (negative cycles cause the algorithm to abort
1576 with an error message C<Graph::SPT_Bellman_Ford: negative cycle exists/>).
1578 You can choose the start vertex of the result with either a single
1579 vertex argument or with $opt{ first_root }, otherwise a random vertex
1580 is chosen.
1582 B<NOTE>: note that all the vertices might not be reachable from the
1583 selected (explicit or random) start vertex.
1585 The start vertex is be available as the graph attribute
1586 C<SPT_Bellman_Ford_root>).
1588 The result weights of vertices can be retrieved from the result graph by
1590         my $w = $sptg->get_vertex_attribute($v, 'weight');
1592 The predecessor vertex of a vertex in the result graph
1593 can be retrieved by
1595         my $u = $sptg->get_vertex_attribute($v, 'p');
1597 ("A successor vertex" cannot be retrieved as simply because a single
1598 vertex can have several successors.  You can first find the
1599 C<neighbors()> vertices and then remove the predecessor vertex.)
1601 If you want to find the shortes path between two vertices,
1602 see L</SP_Bellman_Ford>.
1604 =item SSSP_Bellman_Ford
1606 Alias for SPT_Bellman_Ford.
1608 =item SP_Bellman_Ford
1610     @path = $g->SP_Bellman_Ford($u, $v)
1612 Return the vertices in the shortest path in the graph $g between the
1613 two vertices $u, $v.  If no path can be found, an empty list is returned.
1615 Uses SPT_Bellman_Ford().
1617 =item SPT_Bellman_Ford_clear_cache
1619     $g->SPT_Bellman_Ford_clear_cache
1621 See L</"Clearing cached results">.
1623 =back
1625 =head2 All-Pairs Shortest Paths (APSP)
1627 For either a directed or an undirected graph, return the APSP object
1628 describing all the possible paths between any two vertices of the
1629 graph.  If no weight is defined for an edge, 1 (one) is assumed.
1631 =over 4
1633 =item APSP_Floyd_Warshall
1635 =item all_pairs_shortest_paths
1637     my $apsp = $g->APSP_Floyd_Warshall(...);
1639 Return the all-pairs shortest path object computed from the graph
1640 using Floyd-Warshall's algorithm.  The length of a path between two
1641 vertices is the sum of weight attribute of the edges along the
1642 shortest path between the two vertices.  If no weight attribute name
1643 is specified explicitly
1645     $g->APSP_Floyd_Warshall(attribute_name => 'height');
1647 the attribute C<weight> is assumed.
1649 B<If an edge has no defined weight attribute, the value of one is
1650 assumed when getting the attribute.>
1652 Once computed, you can query the APSP object with
1654 =over 8
1656 =item path_length
1658     my $l = $apsp->path_length($u, $v);
1660 Return the length of the shortest path between the two vertices.
1662 =item path_vertices
1664     my @v = $apsp->path_vertices($u, $v);
1666 Return the list of vertices along the shortest path.
1668 =item path_predecessor
1670    my $u = $apsp->path_predecessor($v);
1672 Returns the predecessor of vertex $v in the all-pairs shortest paths.
1674 =back
1676 =over 8
1678 =item average_path_length
1680     my $apl = $g->average_path_length; # All vertex pairs.
1682     my $apl = $g->average_path_length($u); # From $u.
1683     my $apl = $g->average_path_length($u, undef); # From $u.
1685     my $apl = $g->average_path_length($u, $v); # From $u to $v.
1687     my $apl = $g->average_path_length(undef, $v); # To $v.
1689 Return the average (shortest) path length over all the vertex pairs of
1690 the graph, from a vertex, between two vertices, and to a vertex.
1692 =item longest_path
1694     my @lp = $g->longest_path;
1695     my $lp = $g->longest_path;
1697 In scalar context return the I<longest shortest> path length over all
1698 the vertex pairs of the graph.  In list context return the vertices
1699 along a I<longest shortest> path.  Note that there might be more than
1700 one such path; this interfaces return a random one of them.
1702 =item diameter
1704 =item graph_diameter
1706     my $gd = $g->diameter;
1708 The longest path over all the vertex pairs is known as the
1709 I<graph diameter>.
1711 =item shortest_path
1713     my @sp = $g->shortest_path;
1714     my $sp = $g->shortest_path;
1716 In scalar context return the shortest length over all the vertex pairs
1717 of the graph.  In list context return the vertices along a shortest
1718 path.  Note that there might be more than one such path; this
1719 interface returns a random one of them.
1721 =item radius
1723     my $gr = $g->radius;
1725 The I<shortest longest> path over all the vertex pairs is known as the
1726 I<graph radius>.  See also L</diameter>.
1728 =item center_vertices
1730 =item centre_vertices
1732     my @c = $g->center_vertices;
1733     my @c = $g->center_vertices($delta);
1735 The I<graph center> is the set of vertices for which the I<vertex
1736 eccentricity> is equal to the I<graph radius>.  The vertices are
1737 returned in random order.  By specifying a delta value you can widen
1738 the criterion from strict equality (handy for non-integer edge weights).
1740 =item vertex_eccentricity
1742     my $ve = $g->vertex_eccentricity($v);
1744 The longest path to a vertex is known as the I<vertex eccentricity>.
1745 If the graph is unconnected, returns Inf.
1747 =back
1749 You can walk through the matrix of the shortest paths by using
1751 =over 4
1753 =item for_shortest_paths
1755     $n = $g->for_shortest_paths($callback)
1757 The number of shortest paths is returned (this should be equal to V*V).
1758 The $callback is a sub reference that receives four arguments:
1759 the transitive closure object from Graph::TransitiveClosure, the two
1760 vertices, and the index to the current shortest paths (0..V*V-1).
1762 =back
1764 =back
1766 =head2 Clearing cached results
1768 For many graph algorithms there are several different but equally valid
1769 results.  (Pseudo)Randomness is used internally by the Graph module to
1770 for example pick a random starting vertex, and to select random edges
1771 from a vertex.
1773 For efficiency the computed result is often cached to avoid
1774 recomputing the potentially expensive operation, and this also gives
1775 additional determinism (once a correct result has been computed, the
1776 same result will always be given).
1778 However, sometimes the exact opposite is desireable, and the possible
1779 alternative results are wanted (within the limits of the pseudorandomness:
1780 not all the possible solutions are guaranteed to be returned, usually only
1781 a subset is retuned).  To undo the caching, the following methods are
1782 available:
1784 =over 4
1786 =item *
1788 connectivity_clear_cache
1790 Affects L</connected_components>, L</connected_component_by_vertex>,
1791 L</connected_component_by_index>, L</same_connected_components>,
1792 L</connected_graph>, L</is_connected>, L</is_weakly_connected>,
1793 L</weakly_connected_components>, L</weakly_connected_component_by_vertex>,
1794 L</weakly_connected_component_by_index>, L</same_weakly_connected_components>,
1795 L</weakly_connected_graph>.
1797 =item *
1799 biconnectivity_clear_cache
1801 Affects L</biconnected_components>,
1802 L</biconnected_component_by_vertex>,
1803 L</biconnected_component_by_index>, L</is_edge_connected>,
1804 L</is_edge_separable>, L</articulation_points>, L</cut_vertices>,
1805 L</is_biconnected>, L</biconnected_graph>,
1806 L</same_biconnected_components>, L</bridges>.
1808 =item *
1810 strong_connectivity_clear_cache
1812 Affects L</strongly_connected_components>,
1813 L</strongly_connected_component_by_vertex>,
1814 L</strongly_connected_component_by_index>,
1815 L</same_strongly_connected_components>, L</is_strongly_connected>,
1816 L</strongly_connected>, L</strongly_connected_graph>.
1818 =item *
1820 SPT_Dijkstra_clear_cache
1822 Affects L</SPT_Dijkstra>, L</SSSP_Dijkstra>, L</single_source_shortest_paths>,
1823 L</SP_Dijkstra>.
1825 =item *
1827 SPT_Bellman_Ford_clear_cache
1829 Affects L</SPT_Bellman_Ford>, L</SSSP_Bellman_Ford>, L</SP_Bellman_Ford>.
1831 =back
1833 Note that any such computed and cached results are of course always
1834 automatically discarded whenever the graph is modified.
1836 =head2 Random
1838 You can either ask for random elements of existing graphs or create
1839 random graphs.
1841 =over 4
1843 =item random_vertex
1845     my $v = $g->random_vertex;
1847 Return a random vertex of the graph, or undef if there are no vertices.
1849 =item random_edge
1851     my $e = $g->random_edge;
1853 Return a random edge of the graph as an array reference having the
1854 vertices as elements, or undef if there are no edges.
1856 =item random_successor
1858     my $v = $g->random_successor($v);
1860 Return a random successor of the vertex in the graph, or undef if there
1861 are no successors.
1863 =item random_predecessor
1865     my $u = $g->random_predecessor($v);
1867 Return a random predecessor of the vertex in the graph, or undef if there
1868 are no predecessors.
1870 =item random_graph
1872     my $g = Graph->random_graph(%opt);
1874 Construct a random graph.  The I<%opt> B<must> contain the C<vertices>
1875 argument
1877     vertices => vertices_def
1879 where the I<vertices_def> is one of
1881 =over 8
1883 =item *
1885 an array reference where the elements of the array reference are the
1886 vertices
1888 =item *
1890 a number N in which case the vertices will be integers 0..N-1
1892 =back
1894 =back
1896 The %opt may have either of the argument C<edges> or the argument
1897 C<edges_fill>.  Both are used to define how many random edges to
1898 add to the graph; C<edges> is an absolute number, while C<edges_fill>
1899 is a relative number (relative to the number of edges in a complete
1900 graph, C).  The number of edges can be larger than C, but only if the
1901 graph is countedged.  The random edges will not include self-loops.
1902 If neither C<edges> nor C<edges_fill> is specified, an C<edges_fill>
1903 of 0.5 is assumed.
1905 If you want repeatable randomness (what is an oxymoron?)
1906 you can use the C<random_seed> option:
1908     $g = Graph->random_graph(vertices => 10, random_seed => 1234);
1910 As this uses the standard Perl srand(), the usual caveat applies:
1911 use it sparingly, and consider instead using a single srand() call
1912 at the top level of your application.
1914 The default random distribution of edges is flat, that is, any pair of
1915 vertices is equally likely to appear.  To define your own distribution,
1916 use the C<random_edge> option:
1918     $g = Graph->random_graph(vertices => 10, random_edge => \&d);
1920 where C<d> is a code reference receiving I<($g, $u, $v, $p)> as
1921 parameters, where the I<$g> is the random graph, I<$u> and I<$v> are
1922 the vertices, and the I<$p> is the probability ([0,1]) for a flat
1923 distribution.  It must return a probability ([0,1]) that the vertices
1924 I<$u> and I<$v> have an edge between them.  Note that returning one
1925 for a particular pair of vertices doesn't guarantee that the edge will
1926 be present in the resulting graph because the required number of edges
1927 might be reached before that particular pair is tested for the
1928 possibility of an edge.  Be very careful to adjust also C<edges>
1929 or C<edges_fill> so that there is a possibility of the filling process
1930 terminating.
1932 =head2 Attributes
1934 You can attach free-form attributes (key-value pairs, in effect a full
1935 Perl hash) to each vertex, edge, and the graph itself.
1937 Note that attaching attributes does slow down some other operations
1938 on the graph by a factor of three to ten.  For example adding edge
1939 attributes does slow down anything that walks through all the edges.
1941 For vertex attributes:
1943 =over 4
1945 =item set_vertex_attribute
1947     $g->set_vertex_attribute($v, $name, $value)
1949 Set the named vertex attribute.
1951 If the vertex does not exist, the set_...() will create it, and the
1952 other vertex attribute methods will return false or empty.
1954 B<NOTE: any attributes beginning with an underscore/underline (_)
1955 are reserved for the internal use of the Graph module.>
1957 =item get_vertex_attribute
1959     $value = $g->get_vertex_attribute($v, $name)
1961 Return the named vertex attribute.
1963 =item has_vertex_attribute
1965     $g->has_vertex_attribute($v, $name)
1967 Return true if the vertex has an attribute, false if not.
1969 =item delete_vertex_attribute
1971     $g->delete_vertex_attribute($v, $name)
1973 Delete the named vertex attribute.
1975 =item set_vertex_attributes
1977     $g->set_vertex_attributes($v, $attr)
1979 Set all the attributes of the vertex from the anonymous hash $attr.
1981 B<NOTE>: any attributes beginning with an underscore (C<_>) are
1982 reserved for the internal use of the Graph module.
1984 =item get_vertex_attributes
1986     $attr = $g->get_vertex_attributes($v)
1988 Return all the attributes of the vertex as an anonymous hash.
1990 =item get_vertex_attribute_names
1992     @name = $g->get_vertex_attribute_names($v)
1994 Return the names of vertex attributes.
1996 =item get_vertex_attribute_values
1998     @value = $g->get_vertex_attribute_values($v)
2000 Return the values of vertex attributes.
2002 =item has_vertex_attributes
2004     $g->has_vertex_attributes($v)
2006 Return true if the vertex has any attributes, false if not.
2008 =item delete_vertex_attributes
2010     $g->delete_vertex_attributes($v)
2012 Delete all the attributes of the named vertex.
2014 =back
2016 If you are using multivertices, use the I<by_id> variants:
2018 =over 4
2020 =item set_vertex_attribute_by_id
2022 =item get_vertex_attribute_by_id
2024 =item has_vertex_attribute_by_id
2026 =item delete_vertex_attribute_by_id
2028 =item set_vertex_attributes_by_id
2030 =item get_vertex_attributes_by_id
2032 =item get_vertex_attribute_names_by_id
2034 =item get_vertex_attribute_values_by_id
2036 =item has_vertex_attributes_by_id
2038 =item delete_vertex_attributes_by_id
2040     $g->set_vertex_attribute_by_id($v, $id, $name, $value)
2041     $g->get_vertex_attribute_by_id($v, $id, $name)
2042     $g->has_vertex_attribute_by_id($v, $id, $name)
2043     $g->delete_vertex_attribute_by_id($v, $id, $name)
2044     $g->set_vertex_attributes_by_id($v, $id, $attr)
2045     $g->get_vertex_attributes_by_id($v, $id)
2046     $g->get_vertex_attribute_values_by_id($v, $id)
2047     $g->get_vertex_attribute_names_by_id($v, $id)
2048     $g->has_vertex_attributes_by_id($v, $id)
2049     $g->delete_vertex_attributes_by_id($v, $id)
2051 =back
2053 For edge attributes:
2055 =over 4
2057 =item set_edge_attribute
2059     $g->set_edge_attribute($u, $v, $name, $value)
2061 Set the named edge attribute.
2063 If the edge does not exist, the set_...() will create it, and the other
2064 edge attribute methods will return false or empty.
2066 B<NOTE>: any attributes beginning with an underscore (C<_>) are
2067 reserved for the internal use of the Graph module.
2069 =item get_edge_attribute
2071     $value = $g->get_edge_attribute($u, $v, $name)
2073 Return the named edge attribute.
2075 =item has_edge_attribute
2077     $g->has_edge_attribute($u, $v, $name)
2079 Return true if the edge has an attribute, false if not.
2081 =item delete_edge_attribute
2083     $g->delete_edge_attribute($u, $v, $name)
2085 Delete the named edge attribute.
2087 =item set_edge_attributes
2089     $g->set_edge_attributes($u, $v, $attr)
2091 Set all the attributes of the edge from the anonymous hash $attr.
2093 B<NOTE>: any attributes beginning with an underscore (C<_>) are
2094 reserved for the internal use of the Graph module.
2096 =item get_edge_attributes
2098     $attr = $g->get_edge_attributes($u, $v)
2100 Return all the attributes of the edge as an anonymous hash.
2102 =item get_edge_attribute_names
2104     @name = $g->get_edge_attribute_names($u, $v)
2106 Return the names of edge attributes.
2108 =item get_edge_attribute_values
2110     @value = $g->get_edge_attribute_values($u, $v)
2112 Return the values of edge attributes.
2114 =item has_edge_attributes
2116     $g->has_edge_attributes($u, $v)
2118 Return true if the edge has any attributes, false if not.
2120 =item delete_edge_attributes
2122     $g->delete_edge_attributes($u, $v)
2124 Delete all the attributes of the named edge.
2126 =back
2128 If you are using multiedges, use the I<by_id> variants:
2130 =over 4
2132 =item set_edge_attribute_by_id
2134 =item get_edge_attribute_by_id
2136 =item has_edge_attribute_by_id
2138 =item delete_edge_attribute_by_id
2140 =item set_edge_attributes_by_id
2142 =item get_edge_attributes_by_id
2144 =item get_edge_attribute_names_by_id
2146 =item get_edge_attribute_values_by_id
2148 =item has_edge_attributes_by_id
2150 =item delete_edge_attributes_by_id
2152     $g->set_edge_attribute_by_id($u, $v, $id, $name, $value)
2153     $g->get_edge_attribute_by_id($u, $v, $id, $name)
2154     $g->has_edge_attribute_by_id($u, $v, $id, $name)
2155     $g->delete_edge_attribute_by_id($u, $v, $id, $name)
2156     $g->set_edge_attributes_by_id($u, $v, $id, $attr)
2157     $g->get_edge_attributes_by_id($u, $v, $id)
2158     $g->get_edge_attribute_values_by_id($u, $v, $id)
2159     $g->get_edge_attribute_names_by_id($u, $v, $id)
2160     $g->has_edge_attributes_by_id($u, $v, $id)
2161     $g->delete_edge_attributes_by_id($u, $v, $id)
2163 =back
2165 For graph attributes:
2167 =over 4
2169 =item set_graph_attribute
2171     $g->set_graph_attribute($name, $value)
2173 Set the named graph attribute.
2175 B<NOTE>: any attributes beginning with an underscore (C<_>) are
2176 reserved for the internal use of the Graph module.
2178 =item get_graph_attribute
2180     $value = $g->get_graph_attribute($name)
2182 Return the named graph attribute.
2184 =item has_graph_attribute
2186     $g->has_graph_attribute($name)
2188 Return true if the graph has an attribute, false if not.
2190 =item delete_graph_attribute
2192     $g->delete_graph_attribute($name)
2194 Delete the named graph attribute.
2196 =item set_graph_attributes
2198     $g->get_graph_attributes($attr)
2200 Set all the attributes of the graph from the anonymous hash $attr.
2202 B<NOTE>: any attributes beginning with an underscore (C<_>) are
2203 reserved for the internal use of the Graph module.
2205 =item get_graph_attributes
2207     $attr = $g->get_graph_attributes()
2209 Return all the attributes of the graph as an anonymous hash.
2211 =item get_graph_attribute_names
2213     @name = $g->get_graph_attribute_names()
2215 Return the names of graph attributes.
2217 =item get_graph_attribute_values
2219     @value = $g->get_graph_attribute_values()
2221 Return the values of graph attributes.
2223 =item has_graph_attributes
2225     $g->has_graph_attributes()
2227 Return true if the graph has any attributes, false if not.
2229 =item delete_graph_attributes
2231     $g->delete_graph_attributes()
2233 Delete all the attributes of the named graph.
2235 =back
2237 =head2 Weighted
2239 As convenient shortcuts the following methods add, query, and
2240 manipulate the attribute C<weight> with the specified value to the
2241 respective Graph elements.
2243 =over 4
2245 =item add_weighted_edge
2247     $g->add_weighted_edge($u, $v, $weight)
2249 =item add_weighted_edges
2251     $g->add_weighted_edges($u1, $v1, $weight1, ...)
2253 =item add_weighted_path
2255     $g->add_weighted_path($v1, $weight1, $v2, $weight2, $v3, ...)
2257 =item add_weighted_vertex
2259     $g->add_weighted_vertex($v, $weight)
2261 =item add_weighted_vertices
2263     $g->add_weighted_vertices($v1, $weight1, $v2, $weight2, ...)
2265 =item delete_edge_weight
2267     $g->delete_edge_weight($u, $v)
2269 =item delete_vertex_weight
2271     $g->delete_vertex_weight($v)
2273 =item get_edge_weight
2275     $g->get_edge_weight($u, $v)
2277 =item get_vertex_weight
2279     $g->get_vertex_weight($v)
2281 =item has_edge_weight
2283     $g->has_edge_weight($u, $v)
2285 =item has_vertex_weight
2287     $g->has_vertex_weight($v)
2289 =item set_edge_weight
2291     $g->set_edge_weight($u, $v, $weight)
2293 =item set_vertex_weight
2295     $g->set_vertex_weight($v, $weight)
2297 =back
2299 =head2 Isomorphism
2301 Two graphs being I<isomorphic> means that they are structurally the
2302 same graph, the difference being that the vertices might have been
2303 I<renamed> or I<substituted>.  For example in the below example $g0
2304 and $g1 are isomorphic: the vertices C<b c d> have been renamed as
2305 C<z x y>.
2307         $g0 = Graph->new;
2308         $g0->add_edges(qw(a b a c c d));
2309         $g1 = Graph->new;
2310         $g1->add_edges(qw(a x x y a z));
2312 In the general case determining isomorphism is I<NP-hard>, in other
2313 words, really hard (time-consuming), no other ways of solving the problem
2314 are known than brute force check of of all the possibilities (with possible
2315 optimization tricks, of course, but brute force still rules at the end of
2316 the day).
2318 A B<very rough guess> at whether two graphs B<could> be isomorphic
2319 is possible via the method
2321 =over 4
2323 =item could_be_isomorphic
2325     $g0->could_be_isomorphic($g1)    
2327 =back
2329 If the graphs do not have the same number of vertices and edges, false
2330 is returned.  If the distribution of I<in-degrees> and I<out-degrees>
2331 at the vertices of the graphs does not match, false is returned.
2332 Otherwise, true is returned.
2334 What is actually returned is the maximum number of possible isomorphic
2335 graphs between the two graphs, after the above sanity checks have been
2336 conducted.  It is basically the product of the factorials of the
2337 absolute values of in-degrees and out-degree pairs at each vertex,
2338 with the isolated vertices ignored (since they could be reshuffled and
2339 renamed arbitrarily).  Note that for large graphs the product of these
2340 factorials can overflow the maximum presentable number (the floating
2341 point number) in your computer (in Perl) and you might get for example
2342 I<Infinity> as the result.
2344 =head2 Miscellaneous
2346 The "expect" methods can be used to test a graph and croak if the
2347 graph is not as expected.
2349 =over 4
2351 =item expect_acyclic
2353 =item expect_dag
2355 =item expect_directed
2357 =item expect_multiedged
2359 =item expect_multivertexed
2361 =item expect_non_multiedged
2363 =item expect_non_multivertexed
2365 =item expect_undirected
2367 =back
2369 In many algorithms it is useful to have a value representing the
2370 infinity.  The Graph provides (and itself uses):
2372 =over 4
2374 =item Infinity
2376 (Not exported, use Graph::Infinity explicitly)
2378 =back
2380 =head2 Size Requirements
2382 A graph takes up at least 1172 bytes of memory.
2384 A vertex takes up at least 100 bytes of memory.
2386 An edge takes up at least 400 bytes of memory.
2388 (A Perl scalar value takes 16 bytes, or 12 bytes if it's a reference.)
2390 These size approximations are B<very> approximate and optimistic
2391 (they are based on total_size() of Devel::Size).  In real life many
2392 factors affect these numbers, for example how Perl is configured.
2393 The numbers are for a 32-bit platform and for Perl 5.8.8.
2395 Roughly, the above numbers mean that in a megabyte of memory you can
2396 fit for example a graph of about 1000 vertices and about 2500 edges.
2398 =head2 Hyperedges, hypervertices, hypergraphs
2400 B<BEWARE>: this is a rather thinly tested feature, and the theory
2401 is even less so.  Do not expect this to stay as it is (or at all)
2402 in future releases.
2404 B<NOTE>: most usual graph algorithms (and basic concepts) break
2405 horribly (or at least will look funny) with these hyperthingies.
2406 Caveat emptor.
2408 Hyperedges are edges that connect a number of vertices different
2409 from the usual two.
2411 Hypervertices are vertices that consist of a number of vertices
2412 different from the usual one.
2414 Note that for hypervertices there is an asymmetry: when adding
2415 hypervertices, the single vertices are also implicitly added.
2417 Hypergraphs are graphs with hyperedges.
2419 To enable hyperness when constructing Graphs use the C<hyperedged>
2420 and C<hypervertexed> attributes:
2422    my $h = Graph->new(hyperedged => 1, hypervertexed => 1);
2424 To add hypervertexes, either explicitly use more than one vertex (or,
2425 indeed, I<no> vertices) when using add_vertex()
2427    $h->add_vertex("a", "b")
2428    $h->add_vertex()
2430 or implicitly with array references when using add_edge()
2432    $h->add_edge(["a", "b"], "c")
2433    $h->add_edge()
2435 Testing for existence and deletion of hypervertices and hyperedges
2436 works similarly.
2438 To test for hyperness of a graph use the
2440 =over 4
2442 =item is_hypervertexed
2444 =item hypervertexed
2446     $g->is_hypervertexed
2447     $g->hypervertexed
2449 =item is_hyperedged
2451 =item hyperedged
2453     $g->is_hyperedged
2454     $g->hyperedged
2456 =back
2458 Since hypervertices consist of more than one vertex:
2460 =over 4
2462 =item vertices_at
2464     $g->vertices_at($v)
2466 =back
2468 Return the vertices at the vertex.  This may return just the vertex
2469 or also other vertices.
2471 To go with the concept of undirected in normal (non-hyper) graphs,
2472 there is a similar concept of omnidirected I<(this is my own coinage,
2473 "all-directions")> for hypergraphs, and you can naturally test for it by
2475 =over 4
2477 =item is_omnidirected
2479 =item omnidirected
2481 =item is_omniedged
2483 =item omniedged
2485    $g->is_omniedged
2487    $g->omniedged
2489    $g->is_omnidirected
2491    $g->omnidirected
2493 Return true if the graph is omnidirected (edges have no direction),
2494 false if not.
2496 =back
2498 You may be wondering why on earth did I make up this new concept, why
2499 didn't the "undirected" work for me?  Well, because of this:
2501    $g = Graph->new(hypervertexed => 1, omnivertexed => 1);
2503 That's right, vertices can be omni, too - and that is indeed the
2504 default.  You can turn it off and then $g->add_vertex(qw(a b)) no
2505 more means adding also the (hyper)vertex qw(b a).  In other words,
2506 the "directivity" is orthogonal to (or independent of) the number of
2507 vertices in the vertex/edge.
2509 =over 4
2511 =item is_omnivertexed
2513 =item omnivertexed
2515 =back
2517 Another oddity that fell out of the implementation is the uniqueness
2518 attribute, that comes naturally in C<uniqedged> and C<uniqvertexed>
2519 flavours.  It does what it sounds like, to unique or not the vertices
2520 participating in edges and vertices (is the hypervertex qw(a b a) the
2521 same as the hypervertex qw(a b), for example).  Without too much
2522 explanation:
2524 =over 4
2526 =item is_uniqedged
2528 =item uniqedged
2530 =item is_uniqvertexed
2532 =item uniqvertexed
2534 =back
2536 =head2 Backward compatibility with Graph 0.2
2538 The Graph 0.2 (and 0.2xxxx) had the following features
2540 =over 4
2542 =item *
2544 vertices() always sorted the vertex list, which most of the time is
2545 unnecessary and wastes CPU.
2547 =item *
2549 edges() returned a flat list where the begin and end vertices of the
2550 edges were intermingled: every even index had an edge begin vertex,
2551 and every odd index had an edge end vertex.  This had the unfortunate
2552 consequence of C<scalar(@e = edges)> being twice the number of edges,
2553 and complicating any algorithm walking through the edges.
2555 =item *
2557 The vertex list returned by edges() was sorted, the primary key being
2558 the edge begin vertices, and the secondary key being the edge end vertices.
2560 =item *
2562 The attribute API was oddly position dependent and dependent
2563 on the number of arguments.  Use ..._graph_attribute(),
2564 ..._vertex_attribute(), ..._edge_attribute() instead.
2566 =back
2568 B<In future releases of Graph (any release after 0.50) the 0.2xxxx
2569 compatibility will be removed.  Upgrade your code now.>
2571 If you want to continue using these (mis)features you can use the
2572 C<compat02> flag when creating a graph:
2574     my $g = Graph->new(compat02 => 1);
2576 This will change the vertices() and edges() appropriately.  This,
2577 however, is not recommended, since it complicates all the code using
2578 vertices() and edges().  Instead it is recommended that the
2579 vertices02() and edges02() methods are used.  The corresponding new
2580 style (unsorted, and edges() returning a list of references) methods
2581 are called vertices05() and edges05().
2583 To test whether a graph has the compatibility turned on
2585 =over 4
2587 =item is_compat02
2589 =item compat02
2591     $g->is_compat02
2592     $g->compat02
2594 =back
2596 The following are not backward compatibility methods, strictly
2597 speaking, because they did not exist before.
2599 =over 4
2601 =item edges02
2603 Return the edges as a flat list of vertices, elements at even indices
2604 being the start vertices and elements at odd indices being the end
2605 vertices.
2607 =item edges05
2609 Return the edges as a list of array references, each element
2610 containing the vertices of each edge.  (This is not a backward
2611 compatibility interface as such since it did not exist before.)
2613 =item vertices02
2615 Return the vertices in sorted order.
2617 =item vertices05
2619 Return the vertices in random order.
2621 =back
2623 For the attributes the recommended way is to use the new API.
2625 Do not expect new methods to work for compat02 graphs.
2627 The following compatibility methods exist:
2629 =over 4
2631 =item has_attribute
2633 =item has_attributes
2635 =item get_attribute
2637 =item get_attributes
2639 =item set_attribute
2641 =item set_attributes
2643 =item delete_attribute
2645 =item delete_attributes
2647 Do not use the above, use the new attribute interfaces instead.
2649 =item vertices_unsorted
2651 Alias for vertices() (or rather, vertices05()) since the vertices()
2652 now always returns the vertices in an unsorted order.  You can also
2653 use the unsorted_vertices import, but only with a true value (false
2654 values will cause an error).
2656 =item density_limits
2658     my ($sparse, $dense, $complete) = $g->density_limits;
2660 Return the "density limits" used to classify graphs as "sparse" or "dense".
2661 The first limit is C/4 and the second limit is 3C/4, where C is the number
2662 of edges in a complete graph (the last "limit").
2664 =item density
2666     my $density = $g->density;
2668 Return the density of the graph, the ratio of the number of edges to the
2669 number of edges in a complete graph.
2671 =item vertex
2673     my $v = $g->vertex($v);
2675 Return the vertex if the graph has the vertex, undef otherwise.
2677 =item out_edges
2679 =item in_edges
2681 =item edges($v)
2683 This is now called edges_at($v).
2685 =back
2687 =head2 DIAGNOSTICS
2689 =over 4
2691 =item *
2693 Graph::...Map...: arguments X expected Y ...
2695 If you see these (more user-friendly error messages should have been
2696 triggered above and before these) please report any such occurrences,
2697 but in general you should be happy to see these since it means that an
2698 attempt to call something with a wrong number of arguments was caught
2699 in time.
2701 =item *
2703 Graph::add_edge: graph is not hyperedged ...
2705 Maybe you used add_weighted_edge() with only the two vertex arguments.
2707 =item *
2709 Not an ARRAY reference at lib/Graph.pm ...
2711 One possibility is that you have code based on Graph 0.2xxxx that
2712 assumes Graphs being blessed hash references, possibly also assuming
2713 that certain hash keys are available to use for your own purposes.
2714 In Graph 0.50 none of this is true.  Please do not expect any
2715 particular internal implementation of Graphs.  Use inheritance
2716 and graph/vertex/edge attributes instead.
2718 Another possibility is that you meant to have objects (blessed
2719 references) as graph vertices, but forgot to use C<refvertexed>
2720 (see L</refvertexed>) when creating the graph.
2722 =back
2724 =head2 POSSIBLE FUTURES
2726 A possible future direction is a new graph module written for speed:
2727 this may very possibly mean breaking or limiting some of the APIs or
2728 behaviour as compared with this release of the module.
2730 What definitely won't happen in future releases is carrying over
2731 the Graph 0.2xxxx backward compatibility API.
2733 =head1 ACKNOWLEDGEMENTS
2735 All bad terminology, bugs, and inefficiencies are naturally mine, all
2736 mine, and not the fault of the below.
2738 Thanks to Nathan Goodman and Andras Salamon for bravely betatesting my
2739 pre-0.50 code.  If they missed something, that was only because of my
2740 fiendish code.
2742 The following literature for algorithms and some test cases:
2744 =over 4
2746 =item *
2748 Algorithms in C, Third Edition, Part 5, Graph Algorithms, Robert Sedgewick, Addison Wesley
2750 =item *
2752 Introduction to Algorithms, First Edition, Cormen-Leiserson-Rivest, McGraw Hill
2754 =item *
2756 Graphs, Networks and Algorithms, Dieter Jungnickel, Springer
2758 =back
2760 =head1 AUTHOR AND COPYRIGHT
2762 Jarkko Hietaniemi F<jhi@iki.fi>
2764 =head1 LICENSE
2766 This module is licensed under the same terms as Perl itself.
2768 =cut