-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerIteration.py
52 lines (42 loc) · 1.21 KB
/
PowerIteration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
def diagonal_eigval(eigen_val):
arr = [[0 for j in range(len(eigen_val))] for i in range(len(eigen_val))]
x=0
for i in eigen_val:
arr[x][x]=pow(i,20)
x+=1
if x>3:
break
return arr
def Google_Matrix(A, m):
N = A.shape[0]
v = np.ones(N)
# Calculate the degree of each node
KT = np.dot(A.T, v)
# Normalize the columns
for i in range(N):
A.T[i] = A.T[i]/KT[i]
# Add random links
S = np.ones((N, N))/N
G = (1-m)*A+m*S
return G
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
n = int(input("Enter the number of rows:"))
m = int(input("Enter the number of columns:"))
print("Enter the entries rowise (separated by space): ")
matrix=[]
for i in range(n):
a=[]
for j in range(m):
a.append(float(input()))
matrix.append(a)
A_Matrix=np.array(matrix)
# gives adjacency matrix when M is kept 0 in google matrix
# print(Google_Matrix(A_Matrix,0))
print("Result of Power Iteration Algorithm: ")
print(Power_Method(A_Matrix,100))