edgegraph.builder.adjmatrix.create_adj_matrix#
- edgegraph.builder.adjmatrix.create_adj_matrix(uni, sort_key)#
Create an adjacency matrix from a given universe.
This function creates and returns an adjacency matrix from a given already-formed graph. The structure and meaning of the matrix is the same as in
load_adj_matrix().Note that this function requires not only the universe graph container, but also a sort key. This sort key controls the order of rows and columns within the output matrix. Consider the following matrix, which clearly labels rows and columns:
v0
v1
v2
v3
v4
v5
v0
False
True
True
True
False
False
v1
False
False
True
True
True
False
v2
False
False
False
True
True
True
v3
False
False
False
True
False
False
v4
False
False
False
False
False
False
v5
False
False
False
False
False
False
Now, if this is the matrix you wish to get out of this function, you must tell it which vertex is v0, which is v1, which is v2, etc.. This is what the sort_key argument is for. It is passed directly to the builtin
sorted()’skeyargument.Note that if you are using a custom subclass of
Vertexwhich implements rich comparison methods, you may passNoneto this argument to relegate sorting to these rich comparisons.See also
Python rich comparison methods: https://docs.python.org/3/reference/datamodel.html#object.__lt__
sorted()builtin, which is used internally in this method
Note
In some scenarios, with the right arguments, this function and
load_adj_matrix()can perform exactly inverse operations. The requirements for this to occur are:The
sort_keyparameter given here excatly replicates the order of vertices given in theverticesargument toload_adj_matrix()Either:
Directed edges are used throughout the graph, or
Both:
Undirected edges are used throughout the graph
The matrix given to
load_adj_matrix()is symmetric over the diagonal
In all other cases, it is NOT GUARANTEED that these two functions perform precisely inverse operations. This may return matrices that are slightly different, especially when using undirected edges.
This is expected and desired behavior.
See also
The
load_adj_matrix()function is nearly the inverse of this one.- Parameters:
- Returns:
A square 2-dimensional array (matrix), representing adjacency within the graph. The elements are set to either
TrueorFalseto indicate a link.- Return type: