点击查看代码
import numpy as np
matches = np.array([
[0, 1, 0, 1, 1, 1], # 1队
[0, 0, 0, 1, 1, 1], # 2队
[1, 1, 0, 1, 0, 0], # 3队
[0, 0, 0, 0, 1, 1], # 4队
[0, 0, 1, 0, 0, 1], # 5队
[0, 0, 1, 0, 0, 0] # 6队
], dtype=int)
n = matches.shape[0]
closure = matches.copy()
for k in range(n):
for i in range(n):
for j in range(n):
closure[i, j] = closure[i, j] or (closure[i, k] and closure[k, j])
strength = closure.sum(axis=1)
ranking = np.argsort(-strength)
for i, rank in enumerate(ranking):
print(f"{chr(65 + rank)}队 排名 {i + 1}")
import numpy as np
from scipy.sparse import csr_matrix
edges = [
(0, 1), (0, 3), (0, 4), (0, 5), # 1队胜
(1, 3), (1, 4), (1, 5), # 2队胜
(2, 0), (2, 1), (2, 3), # 3队胜
(3, 4), (3, 5), # 4队胜
(4, 2), (4, 5), # 5队胜
(5, 2) # 6队胜
]
num_teams = 6
row_ind = []
col_ind = []
data = []
for u, v in edges:
row_ind.append(u)
col_ind.append(v)
data.append(1)
adj_matrix = csr_matrix((data, (row_ind, col_ind)), shape=(num_teams, num_teams))
adj_matrix_T = adj_matrix.T
d = 0.85
out_degree = np.array(adj_matrix_T.sum(axis=1)).flatten()
out_degree[out_degree == 0] = 1
M = adj_matrix_T.multiply(1.0 / out_degree).tocsr()
M = M + (1 - d) / num_teams * csr_matrix(np.ones((num_teams, num_teams)))
R = np.ones(num_teams) / num_teams
num_iterations = 100
for _ in range(num_iterations):
R = R.dot(M.toarray())
pagerank_ranking = np.argsort(-R)
for i, rank in enumerate(pagerank_ranking):
print(f"{chr(65 + rank)}队 PageRank排名 {i + 1}")
print("学号:3004")