|
| 1 | +""" |
| 2 | + is_tree(g) |
| 3 | +
|
| 4 | +Returns true if g is a tree: that is, a simple, connected undirected graph, with nv-1 edges (nv = number of vertices). Trees are the minimal connected graphs; equivalently they have no cycles. |
| 5 | +
|
| 6 | +This function does not apply to directed graphs. Directed trees are sometimes called [polytrees](https://en.wikipedia.org/wiki/Polytree)). |
| 7 | +
|
| 8 | +""" |
| 9 | +function is_tree end |
| 10 | + |
| 11 | +@traitfn function is_tree(g::::(!IsDirected)) |
| 12 | + return ne(g) == nv(g) - 1 && is_connected(g) |
| 13 | +end |
| 14 | + |
| 15 | +function _is_prufer(c::AbstractVector{T}) where {T<:Integer} |
| 16 | + return isempty(c) || (minimum(c) >= 1 && maximum(c) <= length(c) + 2) |
| 17 | +end |
| 18 | + |
| 19 | +function _degree_from_prufer(c::Vector{T})::Vector{T} where {T<:Integer} |
| 20 | + """ |
| 21 | + Degree sequence from Prüfer code. |
| 22 | + Returns d such that d[i] = 1 + number of occurences of i in c |
| 23 | + """ |
| 24 | + n = length(c) + 2 |
| 25 | + d = ones(T, n) |
| 26 | + for value in c |
| 27 | + d[value] += 1 |
| 28 | + end |
| 29 | + return d |
| 30 | +end |
| 31 | + |
| 32 | +""" |
| 33 | + prufer_decode(code) |
| 34 | +
|
| 35 | +Returns the unique tree associated with the given (Prüfer) code. |
| 36 | +Each tree of size n is associated with a Prüfer sequence (a[1], ..., a[n-2]) with 1 ⩽ a[i] ⩽ n. The sequence is constructed recursively by the leaf removal algoritm. At step k, the leaf with smallest index is removed and its unique neighbor is added to the Prüfer sequence, giving a[k]. The decoding algorithm goes backward. The tree associated with the empty sequence is the 2-vertices tree with one edge. |
| 37 | +Ref: [Prüfer sequence on Wikipedia](https://en.wikipedia.org/wiki/Pr%C3%BCfer_sequence) |
| 38 | +
|
| 39 | +""" |
| 40 | +function prufer_decode(code::AbstractVector{T})::SimpleGraph{T} where {T<:Integer} |
| 41 | + !_is_prufer(code) && throw( |
| 42 | + ArgumentError("The code must have one dimension and must be a Prüfer sequence. "), |
| 43 | + ) |
| 44 | + isempty(code) && return path_graph(T(2)) # the empty Prüfer sequence codes for the one-edge tree |
| 45 | + |
| 46 | + n = length(code) + 2 |
| 47 | + d = _degree_from_prufer(code) |
| 48 | + L = BinaryMinHeap{T}(findall(==(1), d)) |
| 49 | + g = Graph{T}(n, 0) |
| 50 | + |
| 51 | + for i in 1:(n - 2) |
| 52 | + l = pop!(L) # extract leaf with priority rule (max) |
| 53 | + d[l] -= 1 # update degree sequence |
| 54 | + add_edge!(g, l, code[i]) # add edge |
| 55 | + d[code[i]] -= 1 # update degree sequence |
| 56 | + d[code[i]] == 1 && push!(L, code[i]) # add new leaf if any |
| 57 | + end |
| 58 | + |
| 59 | + add_edge!(g, pop!(L), pop!(L)) # add last leaf |
| 60 | + |
| 61 | + return g |
| 62 | +end |
| 63 | + |
| 64 | +""" |
| 65 | + prufer_encode(g::SimpleGraph) |
| 66 | +
|
| 67 | +Given a tree (a connected minimal undirected graph) of size n⩾3, returns the unique Prüfer sequence associated with this tree. |
| 68 | +
|
| 69 | +Each tree of size n ⩾ 2 is associated with a Prüfer sequence (a[1], ..., a[n-2]) with 1 ⩽ a[i] ⩽ n. The sequence is constructed recursively by the leaf removal algoritm. At step k, the leaf with smallest index is removed and its unique neighbor is added to the Prüfer sequence, giving a[k]. The Prüfer sequence of the tree with only one edge is the empty sequence. |
| 70 | +
|
| 71 | +Ref: [Prüfer sequence on Wikipedia](https://en.wikipedia.org/wiki/Pr%C3%BCfer_sequence) |
| 72 | +
|
| 73 | +""" |
| 74 | +function prufer_encode(G::SimpleGraph{T})::Vector{T} where {T<:Integer} |
| 75 | + (nv(G) < 2 || !is_tree(G)) && |
| 76 | + throw(ArgumentError("The graph must be a tree with n ⩾ 2 vertices. ")) |
| 77 | + |
| 78 | + n = nv(G) |
| 79 | + n == 2 && return Vector{T}() # empty Prüfer sequence |
| 80 | + g = copy(G) |
| 81 | + code = zeros(T, n - 2) |
| 82 | + d = degree(g) |
| 83 | + L = BinaryMinHeap(findall(==(1), d)) |
| 84 | + |
| 85 | + for i in 1:(n - 2) |
| 86 | + u = pop!(L) |
| 87 | + v = neighbors(g, u)[1] |
| 88 | + rem_edge!(g, u, v) |
| 89 | + d[u] -= 1 |
| 90 | + d[v] -= 1 |
| 91 | + d[v] == 1 && push!(L, v) |
| 92 | + code[i] = v |
| 93 | + end |
| 94 | + |
| 95 | + return code |
| 96 | +end |
0 commit comments