def test_incidence_matrix(self):
        g = DirectedGraph(8)
        g.add_random_edges(15)
        before = g.to_adjacency_list()
        g.from_incidence_matrix(g.to_incidence_matrix())
        after = g.to_adjacency_list()

        assert before == after
    def test_adjacency_list(self):
        g = DirectedGraph(8)
        g.add_random_edges(15)

        before = g.to_adjacency_list()
        g.from_adjacency_list(before)
        after = g.to_adjacency_list()

        assert before == after
Exemplo n.º 3
0
def _page_rank_random_walk(g: DirectedGraph, d) -> Dict[int, float]:
    adj_l = g.to_adjacency_list()
    visited = {n: 0 for n in g.nodes}
    current_node = random.choice(list(g.nodes))
    N = 0
    while N < 100000:
        if random.random() > d and len(adj_l[current_node]) != 0:
            current_node = random.choice(list(adj_l[current_node]))
        else:
            current_node = random.choice(list(g.nodes))

        visited[current_node] += 1
        N += 1
    return {n: v / sum(visited.values()) for n, v in visited.items()}