-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
55 lines (48 loc) · 1.44 KB
/
matrix.py
File metadata and controls
55 lines (48 loc) · 1.44 KB
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
53
54
55
import numpy as np
# Step 1: Define the graph
edges = [
(1, 3), (1, 4), (1, 5), (1, 6),
(2, 3), (2, 4), (2, 5),
(3, 1), (3, 4), (3, 6),
(4, 5), (4, 6), (4, 7),
(5, 7), (5, 8),
(6, 5), (6, 7), (6, 9),
(7, 8), (7, 9),
(8, 9),
(9, 8)
]
n = 9
A = np.zeros((n, n), dtype=float)
for u, v in edges:
A[u - 1][v - 1] = 1
# Step 2: Power Iteration to compute Perron eigenvalue
def perron_lambda(A, tol=1e-9, max_iter=1000):
n = A.shape[0]
b = np.random.rand(n)
b = b / np.linalg.norm(b)
for _ in range(max_iter):
b_new = A @ b
b_new_norm = np.linalg.norm(b_new)
if b_new_norm == 0:
return 0
b_new = b_new / b_new_norm
if np.linalg.norm(b_new - b) < tol:
break
b = b_new
lambda_perron = b.T @ A @ b
return lambda_perron
# Step 3: Compute λ1
lambda1 = perron_lambda(A)
print(f"Perron eigenvalue λ1 ≈ {lambda1:.4f}")
# Step 4: Compute Influence
if lambda1 >= 1:
omega = 1 / (1 - lambda1) # May be negative if λ1 > 1
r = np.sum(A, axis=1)
I = (1 + omega) * r
max_node = np.argmax(I) + 1
print("Row Sum Vector r:", r)
print(f"omega(λ1): {omega:.4f}")
print("Influence Vector I:", I)
print(f"Most Influential Node: Node {max_node} with value {I[max_node - 1]:.4f}")
else:
print("λ1 < 1, influence vector may not be meaningful with this model.")