def test_adjacency_matrix(self): g = DirectedGraph(8) g.add_random_edges(15) before = g.to_adjacency_matrix() g.from_adjacency_matrix(before) after = g.to_adjacency_matrix() assert before == after
def test_incidence_matrix_file(self): g = DirectedGraph(8) g.add_random_edges(15) before = g.to_adjacency_matrix() g.save("test", "im") g.load("test.im") after = g.to_adjacency_matrix() assert before == after
def _page_rank_matrix(g: DirectedGraph, d) -> Dict[int, float]: n = len(g.nodes) P = np.zeros((n, n)) A = np.array(g.to_adjacency_matrix()) for i in range(n): for j in range(n): P[i][j] = (1.0 - d) * A[i][j] / g.node_degree(i + 1) + d / float(n) p0 = np.full(n, 1 / n) p1 = np.zeros(n) i = 0 err = float("inf") while err > 1e-11: p1 = p0.dot(P) diff = p1 - p0 err = sum(x**2 for x in diff) p0 = p1 i += 1 print(f"Zbieżność uzyskano po {i} iteracjach.") return {k: p1[k - 1] for k in range(1, n + 1)}