In graph-theory a Prüfer code is a unique sequence of integers that denotes a specific tree.
You can find the Prüfer code of a tree with the following algorithm taken from Wikipedia:
Consider a labeled tree T with vertices
{1, 2, ..., n}
. At step i, remove the leaf with the smallest label and set the ith element of the Prüfer sequence to be the label of this leaf's neighbor.
(Note that since it's a leaf it will only have one neighbor).
You should stop the iteration when only two vertices remain in the graph.
Task
Given a labeled tree as input output its Prüfer code. You may take input in any reasonable manner. Such as an adjacency matrix or your languages builtin graph representation. (You may not take input as a Prüfer code).
This is code-golf so you should aim to minimize the bytes in your source.
Test cases
Here are some inputs in ASCII with their outputs below. You do not need to support ASCII input like this.
3
|
1---2---4---6
|
5
{2,2,2,4}
1---4---3
|
5---2---6---7
|
8
{4,4,2,6,2,5}
5---1---4 6
| |
2---7---3
{1,1,2,7,3}
[[2,1],[2,3],[2,5],[2,4,6]]
for the first case? (i.e. each branch) \$\endgroup\$