Graph:...Matrix(3pm)
β¨ Graph::AdjacencyMatrix π the adjacency-matrix bestie β¨
okay so this is the Graph::AdjacencyMatrix page from the User Contributed Perl Documentation, section 3pm (perl module docs, you know the vibe π
). It's literally the girl whose whole job is making you a matrix of who's adjacent to who, plus maybe distances if you're feeling fancy. π¦
πΈ NAME πΈ
Graph::AdjacencyMatrix β bestie, it creates and queries the adjacency matrix of graph G. no cap. π
β SYNOPSIS β
the receipts, verbatim, don't touch:
use Graph::AdjacencyMatrix;
use Graph::Directed; # or Undirected
my $g = Graph::Directed->new;
$g->add_...(); # build $g
my $am = Graph::AdjacencyMatrix->new($g);
$am->is_adjacent($u, $v)
my $am = Graph::AdjacencyMatrix->new($g, distance_matrix => 1);
$am->distance($u, $v)
my $am = Graph::AdjacencyMatrix->new($g, attribute_name => 'length');
$am->distance($u, $v)
my $am = Graph::AdjacencyMatrix->new($g, ...);
my @V = $am->vertices();
$g = Graph->new(multiedged => 1);
$g->add_...(); # build $g
$am = Graph::AdjacencyMatrix->new($g, distance_matrix => 1);
$am->distance($u, $v) # returns hash-ref of ID => distance
likeβ¦ the whole plot in one code block. ate. π₯
π DESCRIPTION π
so you can use Graph::AdjacencyMatrix to compute the adjacency matrix of a graph, and optionally the distance matrix too. then afterwards you can vibe-check the adjacencyness between vertices with the is_adjacent() method, or ask for the distance between vertices with the distance() method. obsessed. β¨
by default the edge attribute used for distance is "w", but bestie you can change that in new(), see below π
β οΈ heads up: if you modify the graph after creating the adjacency matrix of it, the adjacency matrix and the distance matrix may become invalid. like, they don't auto-update. it's giving stale. π
π§ Methods
π«Ά Class Methods
new($g)β constructs the adjacency matrix of the graph$g. that's it, that's the girl. πnew($g, options)β same thing but with options passed as a hash. the known options are, and I'm listing every single one, periodt:distance_matrix => booleanβ by default only the adjacency matrix gets computed. to compute also the distance matrix, pass the attributedistance_matrixwith a true value to thenew()constructor. πattribute_name => attribute_nameβ by default the edge attribute used for distance is"w". you can change that by giving another attribute name with theattribute_nameattribute to thenew()constructor. and plot twist: using this attribute also implicitly causes the distance matrix to be computed. so it's a two-for-one, iconic. π
π Object Methods
is_adjacent($u, $v)β returns true if vertex$vis adjacent to vertex$u, or false if not. simple. clean. slay. βdistance($u, $v)β returns the distance between vertices$uand$v, orundefif the vertices are not adjacent.
if the underlying graph is multiedged, it returns a hash-ref of ID mapped to distance. if a given edge ID does not have the attribute defined, it will not be represented. and if no edge IDs have the attribute, undef will be returned. (iykyk π)
adjacency_matrixβ returns the adjacency matrix itself, which is a list of bitvector scalars. πverticesβ returns the list of vertices. super useful for indexing the adjacency matrix. π¦
π ALGORITHM π
the algorithm used to create the matrix is two nested loops, which is O(V**2) in time, and the returned matrices are O(V**2) in space. so likeβ¦ quadratic bestie, just so you know what you're signing up for. π
π SEE ALSO π
Graph::TransitiveClosureGraph::BitMatrix
go read those too, they're in the group chat. π¬
π AUTHOR AND COPYRIGHT π
Jarkko Hietaniemi jhi@iki.fi β the author, the moment, the legend. π
π LICENSE π
this module is licensed under the same terms as Perl itself.
and that's the whole page, bestie π«Άβ¨ this one's from perl v5.40.1, dated 2025-03-28, and it's the Graph:...Matrix(3pm) page β go forth and matrix responsibly π πΈ
π¦ 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