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 a back_edge, a down_edge, or a cross_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 $unseen is a hash reference with the unseen vertices as keys. if you don't supply it, it defaults to the same as next_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 $unseen is a hash reference with the unseen vertices as keys. oh and heads up: if you want only the first reachable subgraph to be processed, set next_root to undef. that's the move.
  • start β€” identical to defining first_root and undefining next_root. a little shortcut, a little treat πŸ“
  • next_alphabetic β€” set this to true if you want the vertices processed in alphabetic order (and leave first_root/next_root undefined). ABC order bestie, alphabetize the graph.
  • next_numeric β€” set this to true if you want the vertices processed in numeric order (and leave first_root/next_root undefined). 1, 2, 3, math girlies rise 🌸
  • next_successor β€” called when choosing the next vertex to visit. called with arguments ($self, $next) where $next is a hash reference with the possible next vertices as keys. use this to provide a custom ordering for choosing vertices, as opposed to next_numeric or next_alphabetic. it's giving bespoke, it's giving artisanal vertex selection ✨
so the parameters first_root and next_successor have 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 of next_alphabetic, if that's been defined. if not, use the value of next_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 (undef if 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? πŸ‘‘


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