In [1]:
import numpy as np
from graph_tools import DiGraph
In [2]:
g0 = DiGraph([[0, 1], [1, 0]])
In [3]:
g0.period
Out[3]:
In [4]:
g1 = DiGraph([[1, 0], [0, 1]])
In [5]:
g1.period
In [6]:
g1.is_strongly_connected
Out[6]:
In [7]:
g1.strongly_connected_components
Out[7]:
In [8]:
g1.sink_strongly_connected_components
Out[8]:
In [9]:
A = np.zeros((11, 11))
A[0, [1, 5]] = 1
A[1, [2, 10]] = 1
A[2, 7] = 1
A[3, 4] = 1
A[4, 5] = 1
A[5, [2, 6]] = 1
A[6, [7, 8]] = 1
A[7, 4] = 1
A[8, 0] = 1
A[9, 4] = 1
A[10, [3, 8, 9]] = 1
In [10]:
A
Out[10]:
In [11]:
g2 = DiGraph(A)
In [12]:
g2.period
Out[12]:
In [13]:
cyclic_components = g2.cyclic_components
print cyclic_components
In [14]:
permutation = [i for cyclic_component in cyclic_components for i in cyclic_component]
print permutation
In [15]:
g2._cyclic_components_proj
Out[15]:
In [16]:
g2._cyclic_components_proj.argsort()
Out[16]:
In [17]:
# Cyclic normal form of A
A[permutation, :][:, permutation]
Out[17]:
In [18]:
# Make A be a stochastic matrix
B = A[permutation, :][:, permutation]
B /= np.sum(B, axis=1, keepdims=True)
In [19]:
np.set_printoptions(precision=2)
In [20]:
print B
In [21]:
print np.linalg.matrix_power(B, 5)
In [22]:
C = np.linalg.matrix_power(B, 8)
for k in range(5):
C = C.dot(B)
print 'B^{0} ='.format(9 + k)
print C, '\n'
In [23]:
g3 = DiGraph(B)
In [24]:
g3.csgraph
Out[24]:
In [25]:
print g3.csgraph.todense()
In [26]:
g4 = DiGraph(B, weighted=True)
In [27]:
g4.csgraph
Out[27]:
In [28]:
print g4.csgraph.todense()
In [28]: