Exemplo n.º 1
0
    def test_inverse(self):
        """Tests that the encoding and decoding functions are inverses."""
        for T in nx.nonisomorphic_trees(4):
            T2 = nx.from_prufer_sequence(nx.to_prufer_sequence(T))
            assert_nodes_equal(list(T), list(T2))
            assert_edges_equal(list(T.edges()), list(T2.edges()))

        for seq in product(range(4), repeat=2):
            seq2 = nx.to_prufer_sequence(nx.from_prufer_sequence(seq))
            assert list(seq) == seq2
Exemplo n.º 2
0
    def test_inverse(self):
        """Tests that the encoding and decoding functions are inverses.

        """
        for T in nx.nonisomorphic_trees(4):
            T2 = nx.from_prufer_sequence(nx.to_prufer_sequence(T))
            assert_equal(list(T), list(T2))
            assert_equal(list(T.edges()), list(T2.edges()))

        for seq in product(range(4), repeat=2):
            seq2 = nx.to_prufer_sequence(nx.from_prufer_sequence(seq))
            assert_equal(list(seq), seq2)
Exemplo n.º 3
0
    def test_encoding(self):
        """Tests for encoding a tree as a Prüfer sequence using the
        iterative strategy.

        """
        # Example from Wikipedia.
        tree = nx.Graph([(0, 3), (1, 3), (2, 3), (3, 4), (4, 5)])
        sequence = nx.to_prufer_sequence(tree)
        assert_equal(sequence, [3, 3, 3, 4])
Exemplo n.º 4
0
def task_q(spantree):
    new_labels = dict()
    i = 0
    for node in spantree.nodes:
        new_labels[node] = i
        i+=1
    spntr = nx.relabel_nodes(spantree, new_labels)
    prufer = nx.to_prufer_sequence(spntr)
    print("Prufer code: " + str(prufer))
Exemplo n.º 5
0
    def test_encoding(self):
        """Tests for encoding a tree as a Prüfer sequence using the
        iterative strategy.

        """
        # Example from Wikipedia.
        tree = nx.Graph([(0, 3), (1, 3), (2, 3), (3, 4), (4, 5)])
        sequence = nx.to_prufer_sequence(tree)
        assert sequence == [3, 3, 3, 4]
Exemplo n.º 6
0
def solve_q(G):
    mst = solve_o(G, show_the_tree=False)
    new_mst = nx.Graph()
    new_nodes = dict()

    i = 0
    for node in mst.nodes:
        new_nodes[node] = i
        i += 1
    for edge in mst.edges:
        new_mst.add_edge(new_nodes[edge[0]], new_nodes[edge[1]])
    prufer = nx.to_prufer_sequence(new_mst)
    correct_prufer = list()
    for number in prufer:
        correct_prufer.append(
            list(new_nodes.keys())[list(new_nodes.values()).index(number)])
    print(len(prufer))
    print(prufer)
    print(correct_prufer)
Exemplo n.º 7
0
def solve(n, m, edges):
    def find(v):
        nonlocal parent
        while (v != parent[v]):
            v = parent[v]
        return v

    def jopa(v1, v2):
        return find(v1) == find(v2)

    def union():
        nonlocal parent, rank, a, b, c, answer

        v1 = find(a)

        v2 = find(b)
        if rank[v1] > rank[v2]:
            v1, v2 = v2, v1
        rank[v2] += rank[v1]
        parent[v1] = v2

    parent = [i for i in range(n)]
    rank = [1] * n

    answer = 0
    answer_edges = []
    for i in range(m):
        a, b, c = edges[i]
        if not jopa(a, b):
            union()
            answer += c
            answer_edges.append([a, b])

    # Получим код Прюфера
    G_new = nx.Graph()
    G_new.add_edges_from(answer_edges)
    prufer_code = nx.to_prufer_sequence(G_new)

    # Переведём num вершин в name и посмотрим на них
    for i in prufer_code:
        print(country[i], end=', ')
Exemplo n.º 8
0
 def test_null_graph(self):
     nx.to_prufer_sequence(nx.null_graph())
Exemplo n.º 9
0
 def test_nontree(self):
     G = nx.cycle_graph(3)
     nx.to_prufer_sequence(G)
Exemplo n.º 10
0
 def test_trivial_graph(self):
     with pytest.raises(nx.NetworkXPointlessConcept):
         nx.to_prufer_sequence(nx.trivial_graph())
Exemplo n.º 11
0
 def test_bad_integer_labels(self):
     with pytest.raises(KeyError):
         T = nx.Graph(nx.utils.pairwise("abc"))
         nx.to_prufer_sequence(T)
Exemplo n.º 12
0
 def test_bad_integer_labels(self):
     T = nx.Graph(nx.utils.pairwise('abc'))
     nx.to_prufer_sequence(T)
Exemplo n.º 13
0
 def test_nontree(self):
     with pytest.raises(nx.NotATree):
         G = nx.cycle_graph(3)
         nx.to_prufer_sequence(G)
Exemplo n.º 14
0
 def test_trivial_graph(self):
     nx.to_prufer_sequence(nx.trivial_graph())
Exemplo n.º 15
0
 def test_null_graph(self):
     nx.to_prufer_sequence(nx.null_graph())
Exemplo n.º 16
0
 def test_nontree(self):
     G = nx.cycle_graph(3)
     nx.to_prufer_sequence(G)
Exemplo n.º 17
0
 def test_trivial_graph(self):
     nx.to_prufer_sequence(nx.trivial_graph())
Exemplo n.º 18
0
 def test_bad_integer_labels(self):
     T = nx.Graph(nx.utils.pairwise('abc'))
     nx.to_prufer_sequence(T)