edgegraph.builder.adjmatrix.load_adj_matrix#
- edgegraph.builder.adjmatrix.load_adj_matrix(matrix, vertices, linktype=<class 'edgegraph.structure.directededge.DirectedEdge'>)#
Loads 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
vxvalues areVertexinstances (or subclasses thereof). The given example will produce the following structure:Existing links between vertices are not checked or altered. If, in the above example,
v0was already linked tov2, 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
linktypeparam, 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!
- Parameters:
matrix (list[list[bool]]) – The adjacency matrix. Each individual “cell” is tested for truthy-ness – if
bool(x)would returnTrue, 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
Vertexobjects (or subclasses thereof).linktype (type) – Class of links to use in creation. May be any subclass of
TwoEndedLink; default isDirectedEdge.