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 attribute distance_matrix with a true value to the new() 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 the attribute_name attribute to the new() 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 $v is adjacent to vertex $u, or false if not. simple. clean. slay. βœ…
  • distance($u, $v) β€” returns the distance between vertices $u and $v, or undef if 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::TransitiveClosure
  • Graph::BitMatrix

go read those too, they're in the group chat. πŸ’¬


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