edgegraph.builder.adjmatrix.load_adj_matrix

edgegraph.builder.adjmatrix.load_adj_matrix#

edgegraph.builder.adjmatrix.load_adj_matrix(matrix, vertices, linktype=<class 'edgegraph.structure.directededge.DirectedEdge'>)#

Load an adjacency matrix to create a graph structure.

The input structure is expected to be a list of list of booleans. A “side array” is also required, to denote the vertices. Inputting of the following matrix is given:

v0

v1

v2

v3

v4

v5

v0

0

1

1

1

0

0

v1

0

0

1

1

1

0

v2

0

0

0

1

1

1

v3

0

0

0

1

0

0

v4

0

0

0

0

0

0

v5

0

0

0

0

0

0

 1# define the "side array"
 2vertices = [v0, v1, v2, v3, v4, v5]
 3
 4# and the matrix
 5matrix = [
 6    [0, 1, 1, 1, 0, 0],
 7    [0, 0, 1, 1, 1, 0],
 8    [0, 0, 0, 1, 1, 1],
 9    [0, 0, 0, 1, 0, 0],
10    [0] * 6,
11    [0] * 6
12    ]
13
14universe = load_adj_matrix(matrix, vertices)

where all vx values are Vertex instances (or subclasses thereof). The given example will produce the following structure:

object v0
object v1
object v2
object v3
object v4
object v5

v0 --> v1
v0 --> v2
v0 --> v3
v1 --> v2
v1 --> v3
v1 --> v4
v2 --> v3
v2 --> v4
v2 --> v5
v3 --> v3

Existing links between vertices are not checked or altered. If, in the above example, v0 was already linked to v2, this function would create another link between those vertices.

Attention

This process has side effects on the vertices that are a part of the adjacency dictionary! They are all added to a new universe and linked to the other vertices given.

Note

If you select an un-directed edge type for the linktype param, a graph algorithms textbook would have you believe these matrices should be symmetrical across the diagonal. This is great in theory – but here, EITHER of the cells \(a_{ij}\) or \(a_{ji}\) being truthy will set the link.

This is an implementation detail, not a part of the API specification, and may be changed without notice!

See also

create_adj_matrix() is more-or-less the inverse of this function, producing an adjacency matrix form an already-formed graph.

Parameters:
  • matrix (list[list[bool]]) – The adjacency matrix. Each individual “cell” is tested for truthy-ness – if bool(x) would return True, a link is created.

  • vertices (list[Vertex]) – The “side array” defining the vertices that run along the sides of the matrix. Must be an iterable object containing Vertex objects (or subclasses thereof).

  • linktype (type) – Class of links to use in creation. May be any subclass of TwoEndedLink; default is DirectedEdge.