'Write an algorithm in Python which takes a prufer code as input and returns the edge set of the tree

Write an algorithm in Python which takes a prufer code as input and returns the edge set of the tree. Input: a list named "p" (the prufer code, zero-indexed) Example:

p = [3,1,0,0,3,2,9,9,2,3]

(The prufer code can be defined within the code block. You do not need to write a function which takes user input) Output: a list named "edges" (the edge set_ Example:

print(edges)

[[3, 4], [1, 5], [0, 1], [0, 6], [3, 0], [2, 7], [9, 8], [9, 10], [2, 9], [3, 2], [3,11]]

I am having trouble with this. How can i get the values for "p" so that it prints the output in "edges"?



Solution 1:[1]

Connect the first vertex in (what remains of) the sequence to the lowest vertex that doesn't appear in (what remains of) the sequence. Delete the first vertex in the sequence and repeat. Connect the two remaining vertices.

def decode(p):
    p = list(p)
    vertices = set(range(len(p) + 2))
    while p:
        v = min(vertices - set(p))
        vertices.remove(v)
        yield p.pop(0), v
    yield min(vertices), max(vertices)


print(list(decode([3, 1, 0, 0, 3, 2, 9, 9, 2, 3])))

Output:

[(3, 4), (1, 5), (0, 1), (0, 6), (3, 0), (2, 7), (9, 8), (9, 10), (2, 9), (3, 2), (3, 11)]

Solution 2:[2]

The following algorithm (refer to this) can be used to construct the spanning tree of Kn on n labeled vertices, given the corresponding Prufer sequence of length n-2:

enter image description here

The next code snippet implements the above algorithm to generate a labeled tree (by computing the edges) given the Prufer sequence (also note that we have zero-based indexing in python):

def get_tree(S):
    n = len(S)
    L = set(range(n+2))
    tree_edges = []
    for i in range(n):
        u, v = S[0], min(L - set(S))
        S.pop(0)
        L.remove(v)
        tree_edges.append((u,v))
    tree_edges.append((L.pop(), L.pop()))
    return tree_edges

Invoking the above function on the input Prufer sequence will generate the following tree, as shown in the next animation:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aristide
Solution 2