Graph:...Matrix(3pm)
π Graph::TransitiveClosure::Matrix β the manual page, but make it make sense π
okay bestie, welcome to Graph:...Matrix(3pm), a User Contributed Perl Documentation situation, and honestly? it's a bop. we're gonna build a transitive closure matrix and then interrogate it about who can reach whom. let's go π¦
β¨ NAME β¨
Graph::TransitiveClosure::Matrix β create and query transitive closure of graph
πΈ SYNOPSIS πΈ
use Graph::TransitiveClosure::Matrix;
use Graph::Directed; # or Undirected
my $g = Graph::Directed->new;
$g->add_...(); # build $g
# Compute the transitive closure matrix.
my $tcm = Graph::TransitiveClosure::Matrix->new($g);
# Being reflexive is the default,
# meaning that null transitions are included.
my $tcm = Graph::TransitiveClosure::Matrix->new($g, reflexive => 1);
$tcm->is_reachable($u, $v)
# is_reachable(u, v) is always reflexive.
$tcm->is_reachable($u, $v)
# The reflexivity of is_transitive(u, v) depends on the reflexivity
# of the transitive closure.
$tcg->is_transitive($u, $v)
my $tcm = Graph::TransitiveClosure::Matrix->new($g, path_length => 1);
my $n = $tcm->path_length($u, $v)
my $tcm = Graph::TransitiveClosure::Matrix->new($g, path_vertices => 1);
my @v = $tcm->path_vertices($u, $v)
my $tcm =
Graph::TransitiveClosure::Matrix->new($g,
attribute_name => 'length');
my $n = $tcm->path_length($u, $v)
my @v = $tcm->vertices
π DESCRIPTION π
so like. you can use Graph::TransitiveClosure::Matrix to compute the transitive closure matrix of a graph β and if you're feeling extra, you can also get the minimum paths (both the lengths and the actual vertices) between vertices. then afterwards you can query the transitiveness between vertices with the is_reachable() and is_transitive() methods, and query the paths with the path_length() and path_vertices() methods. it's giving full-service, no cap. π
β οΈ real talk bestie: if you modify the graph after computing its transitive closure, the transitive closure and minimum paths may become invalid. so compute, then query, then maybe don't touch anything. ππ
π§ Methods π§
β Class Methods β
new($g)β Construct the transitive closure matrix of the graph$g.new($g, options)β Construct the transitive closure matrix of the graph$gwith options as a hash. The known options are:"attribute_name" => attribute_nameβ By default the edge attribute used for distance isweight. You can change that by giving another attribute name with the"attribute_name"attribute to thenew()constructor.reflexive => booleanβ By default the transitive closure matrix is not reflexive: that is, the adjacency matrix has zeroes on the diagonal. To have ones on the diagonal, use true for the"reflexive"option.path => booleanβ If set true, sets"path_length"and"path_vertices". If either of those are true (and"path_vertices"is by default), then both are calculated.path_length => booleanβ By defaultfalse, but see above as overridden by default"path_vertices"being true. If calculated, they can be retrieved using thepath_length()method.path_vertices => booleanβ By default the paths are computed, with the boolean transitivity, they can be retrieved using thepath_vertices()method.path_count => booleanβ As an alternative to setting"path_length", if this is true then the matrix will store the quantity of paths between the two vertices. This is still retrieved using thepath_length()method. The path vertices will not be available. You should probably only use this on a DAG, and not with"reflexive".
π Object Methods π
is_reachable($u, $v)β Return true if the vertex$vis reachable from the vertex$u, or false if not.path_length($u, $v)β Return the minimum path length from the vertex$uto the vertex$v, orundefif there is no such path.path_vertices($u, $v)β Return the minimum path (as a list of vertices) from the vertex$uto the vertex$v, or an empty list if there is no such path, OR also return an empty list if$uequals$v.has_vertices($u, $v, ...)β Return true if the transitive closure matrix has all the listed vertices, false if not.is_transitive($u, $v)β Return true if the vertex$vis transitively reachable from the vertex$u, false if not.verticesβ Return the list of vertices in the transitive closure matrix.path_successor($u, $v)β Return the successor of vertex$uin the transitive closure path towards vertex$v.all_paths($u, $v)β Return list of array-refs with all the paths from$uto$v. Will ignore self-loops.
π₯ RETURN VALUES π₯
For path_length() the return value will be the sum of the appropriate attributes on the edges of the path, weight by default. If no attribute has been set, one (1) will be assumed. π
If you try to ask about vertices not in the graph, undefs and empty lists will be returned.
π ALGORITHM π
The transitive closure algorithm used is Warshall and Floyd-Warshall for the minimum paths, which is O(V**3) in time, and the returned matrices are O(V**2) in space. yes it's cubic, yes it's iconic, we don't discuss it. π
π SEE ALSO π
Graph::AdjacencyMatrix
π AUTHOR AND COPYRIGHT π
Jarkko Hietaniemi jhi@iki.fi
π LICENSE π
This module is licensed under the same terms as Perl itself.
and that's the whole page, bestie. π«Ά this one shipped as perl v5.40.1, dated 2025-03-28, and it lives in the Graph:...Matrix(3pm) corner of the manual β go forth and close some transitively, you've got this. π
β¨
π¦ 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