1from numpy import *
2
3hit_idx = (0,4)
4
5# Define a graph by edge list
6edges = [[0,1],[1,2],[2,3],[2,4]]
7
8# Create adj. matrix
9A = zeros((5,5))
10A[zip(*edges)] = 1
11# Undirected condition
12A += A.T
13
14# Make the final state an absorbing condition
15A[hit_idx[1],:] = 0
16A[hit_idx[1],hit_idx[1]] = 1
17
18# Make a proper Markov matrix by row normalizing
19A = (A.T/A.sum(axis=1)).T
20
21B = A.copy()
22Z = []
23for n in xrange(100):
24 Z.append( B[hit_idx] )
25 B = dot(B,A)
26
27from pylab import *
28plot(Z)
29xlabel("steps")
30ylabel("hit probability")
31show()