Параметры max_iter
контролируют только максимальное количество итераций для мощной итерации. Вы хотите прервать итерацию и получить текущий результат. Для этого вы можете легко изменить существующую реализацию nx.hits
:
import networkx as nx
def hits(graph, iterations, tolarance=1.0e-8):
hubs = dict.fromkeys(graph, 1.0 / graph.number_of_nodes())
authorities = {}
# power iteration, which stops after given iterations or reaching tolerance
for _ in range(iterations):
last_hubs = hubs
hubs = dict.fromkeys(last_hubs.keys(), 0)
authorities = dict.fromkeys(last_hubs.keys(), 0)
for node in hubs:
for neighbor in graph[node]:
authorities[neighbor] += last_hubs[node] * graph[node][neighbor].get('weight', 1)
for node in hubs:
for neighbor in graph[node]:
hubs[node] += authorities[neighbor] * graph[node][neighbor].get('weight', 1)
scaling = 1.0 / max(hubs.values())
for node in hubs:
hubs[node] *= scaling
scaling = 1.0 / max(authorities.values())
for node in authorities:
authorities[node] *= scaling
err = sum([abs(hubs[node] - last_hubs[node]) for node in hubs])
if err < tolarance:
break
return hubs, authorities
G1 = nx.DiGraph()
G1.add_edges_from([('A', 'B'),
('A', 'C'),
('D', 'C'),
('D', 'C'),
('C', 'A'),
('B', 'C')
])
print(hits(G1, 5))
# ({'A': 1.0, 'B': 0.7071428571428572, 'C': 0.0017857142857142852, 'D': 0.7071428571428572}, {'A': 0.0025252525252525246, 'B': 0.4141414141414141, 'C': 1.0, 'D': 0.0})