Graph::Traversal(3pm)
β¨ Graph::Traversal β the perl manual page, but make it cute β¨
okay so bestie, this is Graph::Traversal, a Perl module from the User Contributed Perl Documentation section (that's the 3pm part, iykyk). it's literally all about traversing graphs π¦ and it lives in the Perl v5.40.1 era. let's get into it π
π NAME π
Graph::Traversal β traverse graphs. that's it, that's the whole vibe. graphs? traversed. periodt. π
πΈ SYNOPSIS πΈ
okay so FIRST of all, do NOT use Graph::Traversal directly, bestie. like, don't do it. use Graph::Traversal::DFS or Graph::Traversal::BFS instead, fr fr. those are your girls.
use Graph;
my $g = Graph->new;
$g->add_edge(...);
use Graph::Traversal::...;
my $t = Graph::Traversal::...->new($g, %opt);
$t->...
see? use Graph, make a Graph->new, add_edge(...) to it, then pull in whichever Graph::Traversal::... flavor you want, build your traversal object with Graph::Traversal::...->new($g, %opt), and go off with $t->.... slay. π
π DESCRIPTION π
so basically you get to control how the graph gets traversed, using all these callback parameters in the %opt hash. iconic behavior honestly.
in the parameter descriptions below: $u and $v are vertices (the little guys π«Ά) and $self is the traversal object itself. it's giving main character energy, but for a graph.
π Callback parameters π
these are the callback parameters you've got available, bestie:
tree_edgeβ called when you're traversing an edge that belongs to the traversal tree. it gets called with arguments($u, $v, $self). tree edges only, no gatekeeping.non_tree_edgeβ called when you meet an edge that either leads back to the traversal tree (so that's either aback_edge, adown_edge, or across_edge). called with arguments($u, $v, $self). it's the catch-all for the edges that aren't tree edges πpre_edgeβ called for edges in preorder. called with arguments($u, $v, $self).post_edgeβ called for edges in postorder. called with arguments($u, $v, $self).back_edgeβ called for back edges. called with arguments($u, $v, $self).down_edgeβ called for down edges. called with arguments($u, $v, $self).cross_edgeβ called for cross edges. called with arguments($u, $v, $self).pre,pre_vertexβ called for vertices in preorder. called with arguments($v, $self). two names, one job, we love a flexible queen β¨post,post_vertexβ called for vertices in postorder. called with arguments($v, $self).first_rootβ called when choosing the first root (start) vertex for the traversal. called with arguments($self, $unseen)where$unseenis a hash reference with the unseen vertices as keys. if you don't supply it, it defaults to the same asnext_root. no thoughts, just defaults.next_rootβ called when choosing the next root (like, after the first one, duh) vertex for the traversal. super useful when the graph is not connected, because apparently some graphs just refuse to be one big happy family π called with arguments($self, $unseen)where$unseenis a hash reference with the unseen vertices as keys. oh and heads up: if you want only the first reachable subgraph to be processed, setnext_roottoundef. that's the move.startβ identical to definingfirst_rootand undefiningnext_root. a little shortcut, a little treat πnext_alphabeticβ set this to true if you want the vertices processed in alphabetic order (and leavefirst_root/next_rootundefined). ABC order bestie, alphabetize the graph.next_numericβ set this to true if you want the vertices processed in numeric order (and leavefirst_root/next_rootundefined). 1, 2, 3, math girlies rise πΈnext_successorβ called when choosing the next vertex to visit. called with arguments($self, $next)where$nextis a hash reference with the possible next vertices as keys. use this to provide a custom ordering for choosing vertices, as opposed tonext_numericornext_alphabetic. it's giving bespoke, it's giving artisanal vertex selection β¨
so the parametersfirst_rootandnext_successorhave a whole 'hierarchy' of how they get determined, and here's the tea: if they've been explicitly defined, use that value. if not, use the value ofnext_alphabetic, if that's been defined. if not, use the value ofnext_numeric, if that's been defined. and if not? the next vertex to be visited is chosen randomly π². chaos mode activated, we love to see it.
π Methods π
these methods are available, bestie, collect them all:
unseenβ return the unseen vertices in random order.seenβ return the seen vertices in random order.seeingβ return the active fringe vertices in random order.preorderβ return the vertices in preorder traversal order.postorderβ return the vertices in postorder traversal order.vertex_by_preorderβ return the ith (0..$V-1) vertex by preorder.
$v = $t->vertex_by_preorder($i)
preorder_by_vertexβ return the preorder index (0..$V-1) by vertex.
$i = $t->preorder_by_vertex($v)
vertex_by_postorderβ return the ith (0..$V-1) vertex by postorder.
$v = $t->vertex_by_postorder($i)
postorder_by_vertexβ return the postorder index (0..$V-1) by vertex.
$i = $t->postorder_by_vertex($v)
preorder_verticesβ return a hash with the vertices as the keys and their preorder indices as the values.postorder_verticesβ return a hash with the vertices as the keys and their postorder indices as the values.treeβ return the traversal tree as a graph. it grew a whole tree, we're so proud π³has_stateβ test whether the traversal has state's'attached to it.
$t->has_state('s')
get_stateβ get the state's'attached to the traversal (undefif there's none).
$t->get_state('s')
set_stateβ set the state's'attached to the traversal.
$t->set_state('s', $s)
delete_stateβ delete the state's'from the traversal.
$t->delete_state('s')
π¦ Special callbacks π¦
if, inside a callback, you call the special terminate method, the traversal is terminated β no more vertices are traversed. done. cancelled. it's over bestie, the traversal said "and that's on periodt" π
π₯ SEE ALSO π₯
Graph::Traversal::DFS, Graph::Traversal::BFS β go read those too, they're the ones you should actually be using, remember? π
π AUTHOR AND COPYRIGHT π
Jarkko Hietaniemi jhi@iki.fi β thank you for your service, king π«Ά
π LICENSE π
this module is licensed under the same terms as Perl itself.
and that's the whole page, bestie!! no cap, every flag, callback, method and default accounted for π this was Graph::Traversal(3pm) from perl v5.40.1, dated 2025-03-28 β mwah, kisses, go traverse something iconic β¨ππ¦
π¦ where this page comes from
- debian trixie: libgraph-perl 1:0.9735-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libgraph-perl 1:0.9735-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libgraph-perl 1:0.9735-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms