def test_update_fundamental_matrix():
    prng = RandomState(20150101)
    P = compute_transition_matrix(karate_club_graph())
    n = P.shape[0]
    order = arange(P.shape[0])
    previous_index = prng.choice(order, 1)
    previous_node = order[previous_index]
    non_absorbing_nodes = chain(range(previous_index),
                                range(previous_index + 1, n))
    non_absorbing_nodes = list(non_absorbing_nodes)
    order = order[non_absorbing_nodes]
    F = compute_fundamental_matrix(
        P[non_absorbing_nodes, :][:, non_absorbing_nodes])
    absorbing_nodes = [previous_node]
    P_updated = P.copy()
    F_updated = F
    while P_updated.shape[0] >= 3:
        next_node = order[prng.choice(len(order), 1)]
        (P_updated, F_updated, order, previous_index) = \
            update_fundamental_matrix(P_updated, F_updated, next=next_node,
                                      previous=previous_node,
                                      previous_index=previous_index,
                                      node_order=order)
        previous_node = next_node
        absorbing_nodes.append(next_node)
        non_absorbing_nodes = [x for x in range(n) if x not in absorbing_nodes]
        F_slow = compute_fundamental_matrix(
            P[non_absorbing_nodes, :][:, non_absorbing_nodes])
        error_at_step = sum(sum(F_updated - F_slow).T)[0, 0]
        assert abs(error_at_step) < 1e-8, "Error is more than 1e-8."
def test_update_fundamental_matrix():
    prng = RandomState(20150101)
    P = compute_transition_matrix(karate_club_graph())
    n = P.shape[0]
    order = arange(P.shape[0])
    previous_index = prng.choice(order, 1)
    previous_node = order[previous_index]
    non_absorbing_nodes = chain(range(previous_index),
                                range(previous_index + 1, n))
    non_absorbing_nodes = list(non_absorbing_nodes)
    order = order[non_absorbing_nodes]
    F = compute_fundamental_matrix(P[non_absorbing_nodes, :]
                                   [:, non_absorbing_nodes])
    absorbing_nodes = [previous_node]
    P_updated = P.copy()
    F_updated = F
    while P_updated.shape[0] >= 3:
        next_node = order[prng.choice(len(order), 1)]
        (P_updated, F_updated, order, previous_index) = \
            update_fundamental_matrix(P_updated, F_updated, next=next_node,
                                      previous=previous_node,
                                      previous_index=previous_index,
                                      node_order=order)
        previous_node = next_node
        absorbing_nodes.append(next_node)
        non_absorbing_nodes = [x for x in range(n) if x not in absorbing_nodes]
        F_slow = compute_fundamental_matrix(P[non_absorbing_nodes, :]
                                            [:, non_absorbing_nodes])
        error_at_step = sum(sum(F_updated - F_slow).T)[0, 0]
        assert abs(error_at_step) < 1e-8, "Error is more than 1e-8."
def test_transition_matrix_with_supernode():
    karate_club = karate_club_graph()
    karate_club = canonical_relabel_nodes(keep_largest_component(karate_club))
    karate_club_super = add_supernode(karate_club, query=[10, 20, 30])
    P = compute_transition_matrix(karate_club_super)
    for v in [10, 20, 30]:
        assert P[-1, v - 1] == 1 / 3
def test_transition_matrix_with_supernode():
    karate_club = karate_club_graph()
    karate_club = canonical_relabel_nodes(keep_largest_component(karate_club))
    karate_club_super = add_supernode(karate_club, query=[10, 20, 30])
    P = compute_transition_matrix(karate_club_super)
    for v in [10, 20, 30]:
        assert P[-1, v - 1] == 1 / 3