-
Notifications
You must be signed in to change notification settings - Fork 109
Implement Havel-Hakimi and Kleitman-Wang graph realization algorithms #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
763f4cf
c08f978
18b03a7
afa7abd
7d06acc
1e5d43c
4b5accd
19d0b8c
238bd39
ebc05a5
c2ca53e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -989,6 +989,144 @@ function uniform_tree(n::Integer; rng::Union{Nothing,AbstractRNG}=nothing) | |||||||||
return prufer_decode(random_code) | ||||||||||
end | ||||||||||
|
||||||||||
""" | ||||||||||
havel_hakimi_graph_generator(degree_sequence::AbstractVector{<:Integer}) | ||||||||||
|
||||||||||
Returns a simple graph with a given finite degree sequence of non-negative integers generated via the Havel-Hakimi algorithm which works as follows: | ||||||||||
1. successively connect the node of highest degree to other nodes of highest degree; | ||||||||||
2. sort the remaining nodes by degree in decreasing order; | ||||||||||
3. repeat the procedure. | ||||||||||
|
||||||||||
## References | ||||||||||
1. [Hakimi (1962)](https://doi.org/10.1137/0110037); | ||||||||||
2. [Wikipedia](https://en.wikipedia.org/wiki/Havel%E2%80%93Hakimi_algorithm). | ||||||||||
""" | ||||||||||
function havel_hakimi_graph_generator(degree_sequence::AbstractVector{<:Integer}) | ||||||||||
InterdisciplinaryPhysicsTeam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
# Check whether the degree sequence has only non-negative values | ||||||||||
all(degree_sequence .>= 0) || | ||||||||||
throw(ArgumentError("The degree sequence must contain non-negative integers only.")) | ||||||||||
|
||||||||||
# Instantiate an empty simple graph | ||||||||||
graph = SimpleGraph(length(degree_sequence)) | ||||||||||
# Create a (vertex, degree) ordered dictionary | ||||||||||
vertices_degrees_dict = OrderedDict( | ||||||||||
|
||||||||||
vertex => degree for (vertex, degree) in enumerate(degree_sequence) | ||||||||||
) | ||||||||||
# Havel-Hakimi algorithm | ||||||||||
while (any(values(vertices_degrees_dict) .!= 0)) | ||||||||||
InterdisciplinaryPhysicsTeam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
# Sort the new sequence in non-increasing order | ||||||||||
vertices_degrees_dict = OrderedDict( | ||||||||||
sort(collect(vertices_degrees_dict); by=last, rev=true) | ||||||||||
) | ||||||||||
|
vertices_degrees_dict = OrderedDict( | |
sort(collect(vertices_degrees_dict); by=last, rev=true) | |
) | |
sort!(vertices_degrees_dict, byvalues=true, rev=true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It throws an error https://github.com/JuliaGraphs/Graphs.jl/actions/runs/3919202599/jobs/6700055795.
So we reinstated the allocating line.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably more efficient to do this check, when set vertices_degrees_dict[vertex] -= 1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I inserted this check in the loop. But it may be temporary, depending on what comes out of this comment.
InterdisciplinaryPhysicsTeam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this function? Julia already implements lexicographical orders for tuples (but it also does that for tuples of different lengths. E.g.
julia> (1, 2) < (3, 4)
true
julia> (1, 2) < (1, 2)
false
julia> (1, 2) < (1, 2, 1)
true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we didn't think this functionality was implemented out of the box. We then removed lexicographical_order_ntuple
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if
randgraphs.jl
is the correct place for these functions, maybestaticgraphs.jl
is a better place, as generators are not random.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We moved it to
staticgraphs.jl
. Thanks for the suggestion.