edgegraph.builder.adjmatrix.create_adj_matrix

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()’s key argument.

Note that if you are using a custom subclass of Vertex which implements rich comparison methods, you may pass None to this argument to relegate sorting to these rich comparisons.

See also

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:

  1. The sort_key parameter given here excatly replicates the order of vertices given in the vertices argument to load_adj_matrix()

  2. Either:

    1. Directed edges are used throughout the graph, or

    2. Both:

      1. Undirected edges are used throughout the graph

      2. 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:
  • uni (Universe) – The Universe graph container to analyze.

  • sort_key (Callable[[Vertex], int]) – A function of one argument which specifies a comparison key for each vertex in the given universe. It may be set to None if the vertices are of a custom subtype which implements rich comparison operators.

Returns:

A square 2-dimensional array (matrix), representing adjacency within the graph. The elements are set to either True or False to indicate a link.

Return type:

list[list[bool]]