import numpy as np import matplotlib.pyplot as plt filename = "celegans.txt" N = 297 A = np.zeros((N,N)) # Question 1 for line in open(filename): node_i, node_j = line.strip().split() node_i = int(node_i) node_j = int(node_j) A[node_i, node_j] = 1 v = np.ones(N) # Question 2 K_i = np.dot(A, v) plt.hist(K_i, max(K_i)) plt.xlabel('K') plt.ylabel('N(K)') plt.savefig('NK.png') plt.close() print "Connectivite' moyenne:", np.mean(K_i) # Question 3 def Google_Matrix(A, m): N = A.shape[0] v = np.ones(N) KT = np.dot(A.T, v) for i in range(N): A.T[i] = A.T[i]/KT[i] S = np.ones((N,N))/N G = (1-m)*A+m*S return G # Question 4 def Power_Method(G, iter): N = G.shape[0] x0 = np.ones(N)/N for i in range(iter): x0 = np.dot(G,x0) return x0 m = 0.15 iter = 1000 # Question 5 G = Google_Matrix(A, m) x0 = Power_Method(G, iter) plt.plot(x0) plt.xlabel('node') plt.ylabel('PageRank') plt.savefig('PageRank.png') plt.close()