Convert graph to matrix form

These methods convert a graph to a matrix representation .

class PGraph.PGraph(arg=None, metric=None, heuristic=None, verbose=False)[source]
Laplacian()[source]

Laplacian matrix for the graph

Returns:

Laplacian matrix

Return type:

NumPy ndarray

g.Laplacian() is the Laplacian matrix (NxN) of the graph where N is the number of vertices.

Note

  • Laplacian is always positive-semidefinite.

  • Laplacian has at least one zero eigenvalue.

  • The number of zero-valued eigenvalues is the number of connected

    components in the graph.

Seealso:

adjacency() incidence() degree()

adjacency()[source]

Adjacency matrix of graph

Returns:

adjacency matrix

Return type:

ndarray(N,N)

The elements of the adjacency matrix [i,j] are 1 if vertex i is connected to vertex j, else 0.

Note

  • vertices are numbered in their order of creation. A vertex index can be resolved to a vertex reference by graph[i].

  • for an undirected graph the matrix is symmetric

  • Eigenvalues of A are real and are known as the spectrum of the graph.

  • The element A[i,j] can be considered the number of walks of length one edge from vertex i to vertex j (either zero or one).

  • If Ak = A ** k the element Ak[i,j] is the number of walks of length k from vertex i to vertex j.

Seealso:

Laplacian() incidence() degree()

degree()[source]

Degree matrix of graph

Returns:

degree matrix

Return type:

ndarray(N,N)

This is a diagonal matrix where element [i,i] is the number of edges connected to vertex id i.

Seealso:

adjacency() incidence() laplacian()

distance()[source]

Distance matrix of graph

Returns:

distance matrix

Return type:

ndarray(n,n)

The elements of the distance matrix D[i,j] is the edge cost of moving from vertex i to vertex j. It is zero if the vertices are not connected.

incidence()[source]

Incidence matrix of graph

Returns:

incidence matrix

Return type:

ndarray(n,ne)

The elements of the incidence matrix I[i,j] are 1 if vertex i is connected to edge j, else 0.

Note

  • vertices are numbered in their order of creation. A vertex index can be resolved to a vertex reference by graph[i].

  • edges are numbered in the order they appear in graph.edges().

Seealso:

Laplacian() adjacency() degree()