Skip to content

Commit 6a56375

Browse files
2 parents 4abd672 + d47da55 commit 6a56375

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

src/utilities.jl

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ The Kronecker delta.
9999
100100
### CONSTRUCTORS
101101
102-
103102
δk{T}(N::Int64) where {T <: Number}
104103
105104
Inner constructor that only requires `N` and the `eltype`.
@@ -109,7 +108,7 @@ mutable struct δk{T} <: AbstractVector{T}
109108
representation::Matrix{Int64}
110109

111110
# Inner constructor that only requires N and the eltype.
112-
111+
113112
function δk{T}(N::Int64) where {T<:Number}
114113
out = new{T}(N)
115114
representation = [out[h, k] for h in 1:N, k in 1:N]
@@ -120,13 +119,13 @@ end
120119

121120
"""
122121
δk(N::Int64)
123-
122+
124123
Outer constructor that only requires `N`.
125124
"""
126125
δk(N::Int64) = δk{Int64}(N)
127126

128127
"""
129-
getindex(d::δk{T}, h::Int64, k::Int64) where T
128+
getindex(d::δk{T}, h::Int64, k::Int64) where T
130129
131130
`getindex` dispatch that allows to easily construct the `representation` field of `δ_Ω` inside its inner constructor.
132131
"""
@@ -273,7 +272,7 @@ Base.size(d::δ_Ω{T}) where {T} = (d.N >= 3 ? 3 : d.N, d.N, d.N, d.N)
273272
"""
274273
function get_diagonal_elements(arr::Array{T,N}) where {T,N}
275274
output = similar(arr)
276-
275+
277276
for cart_idx in CartesianIndices(arr)
278277
vertices_indexes = Tuple(cart_idx)[1:2]
279278
layers_indexes = Tuple(cart_idx)[3:4]
@@ -359,11 +358,6 @@ Check whether `A` is symmetric (within `zero(T)`).
359358
"""
360359
isapproxsymmetric(A::Matrix{T}) where {T<:Integer} = all(abs.(A .- A') .<= zero(T))
361360

362-
#= """
363-
"""
364-
function Graphs.isgraphical(degree_sequence::Vector{<:Integer}; allow_self_loops::Bool)
365-
end =#
366-
367361
"""
368362
isdigraphical(indegree_sequence::Vector{<:Integer}, outdegree_sequence::Vector{<:Integer} )
369363
@@ -382,12 +376,12 @@ function isdigraphical(indegree_sequence::Vector{<:Integer}, outdegree_sequence:
382376

383377
n = length(indegree_sequence)
384378

385-
n == length(outdegree_sequence) || return false
379+
n == length(outdegree_sequence) || return false
386380

387381
sum(indegree_sequence) == sum(outdegree_sequence) || return false
388382

389383
_sortperm = sortperm(indegree_sequence, rev = true)
390-
384+
391385
sorted_indegree_sequence = indegree_sequence[_sortperm]
392386
sorted_outdegree_sequence = outdegree_sequence[_sortperm]
393387

@@ -409,7 +403,7 @@ function isdigraphical(indegree_sequence::Vector{<:Integer}, outdegree_sequence:
409403
# Similarly for `outdegree_min_sum`.
410404

411405
@inbounds for r = 1:(n - 1)
412-
406+
413407
indegree_sum += sorted_indegree_sequence[r]
414408
outdegree_min_sum = sum([min(sorted_outdegree_sequence[i], r-1) for i in 1:r])
415409
cum_min = sum([min(sorted_outdegree_sequence[i], r) for i in (1+r):n])
@@ -422,9 +416,8 @@ function isdigraphical(indegree_sequence::Vector{<:Integer}, outdegree_sequence:
422416
return true
423417
end
424418

425-
426419
"""
427-
_random_undirected_configuration(empty_mg::M, degree_sequence::Vector{ <: Integer}) where {T,U,M <: MultilayerGraph{T,U}}
420+
_random_undirected_configuration(empty_mg::M, degree_sequence::Vector{ <: Integer}) where {T,U,M <: MultilayerGraph{T,U}}
428421
429422
Internal function. Returns a `MultilayerEdge` list compatible with `empty_mg`, using a relatively inefficient algorithm.
430423
"""
@@ -448,8 +441,8 @@ function _random_undirected_configuration(empty_mg::M, degree_sequence::Vector{
448441
try
449442
dsts = nothing
450443
if allow_self_loops
451-
dsts = sample(collect(keys(mvs_degree_dict)), mvs_degree_dict[src]; replace = false) # This would be correct but we have no "isgraphical" function that takes into account self loops. This section of the code is thus disabled.
452-
else
444+
dsts = sample(collect(keys(mvs_degree_dict)), mvs_degree_dict[src]; replace = false) # This would be correct but we have no "isgraphical" function that takes into account self loops. This section of the code is thus disabled.
445+
else
453446
dsts = sample(collect(setdiff(keys(mvs_degree_dict), [src])), mvs_degree_dict[src]; replace = false)
454447
end
455448

@@ -462,7 +455,7 @@ function _random_undirected_configuration(empty_mg::M, degree_sequence::Vector{
462455
end
463456

464457
descriptor = get_subgraph_descriptor(empty_mg, src.layer, dst.layer)
465-
458+
466459
push!(edge_list, MultilayerEdge(src, dst, descriptor.default_edge_weight(src,dst), descriptor.default_edge_metadata(src,dst)))
467460
end
468461

@@ -476,7 +469,7 @@ function _random_undirected_configuration(empty_mg::M, degree_sequence::Vector{
476469
end
477470
else
478471
continue
479-
end
472+
end
480473
end
481474
success = length(mvs_degree_dict) == 0
482475
end
@@ -502,34 +495,34 @@ function _random_directed_configuration(empty_mg::M, indegree_sequence::Vector{
502495
@info "Looping through wirings to find one that works..."
503496
# Loop until a successful wiring is found
504497
while !success
505-
498+
506499
mvs_indegree_dict = Dict(mv => indeg for (mv,indeg) in zip(mvs,indegree_sequence) if indeg != 0)
507500
mvs_outdegree_dict = Dict(mv => outdeg for (mv,outdeg) in zip(mvs,outdegree_sequence) if outdeg != 0)
508501
edge_list = MultilayerEdge[]
509502
for src in mvs
510-
503+
511504
if src in keys(mvs_outdegree_dict)
512505
try
513506
dsts = nothing
514507
if allow_self_loops
515-
dsts = sample(collect(keys(mvs_indegree_dict)), mvs_outdegree_dict[src]; replace = false) # This would be correct but we have no "isgraphical" function that takes into account self loops. This section of the code is thus disabled.
516-
else
508+
dsts = sample(collect(keys(mvs_indegree_dict)), mvs_outdegree_dict[src]; replace = false) # This would be correct but we have no "isgraphical" function that takes into account self loops. This section of the code is thus disabled.
509+
else
517510
dsts = sample(collect(setdiff(keys(mvs_indegree_dict), [src])), mvs_outdegree_dict[src]; replace = false)
518511
end
519-
512+
520513
for dst in dsts
521-
514+
522515
mvs_indegree_dict[dst] = mvs_indegree_dict[dst] - 1
523-
516+
524517
if mvs_indegree_dict[dst] == 0
525518
delete!(mvs_indegree_dict, dst)
526519
end
527-
520+
528521
descriptor = get_subgraph_descriptor(empty_mg, src.layer, dst.layer)
529-
522+
530523
push!(edge_list, MultilayerEdge(src, dst, descriptor.default_edge_weight(src,dst), descriptor.default_edge_metadata(src,dst)))
531524
end
532-
525+
533526
delete!(mvs_outdegree_dict, src)
534527
catch e
535528
if cmp(e.msg,"Cannot draw more samples without replacement.") == 0
@@ -542,17 +535,14 @@ function _random_directed_configuration(empty_mg::M, indegree_sequence::Vector{
542535
continue
543536
end
544537
end
545-
success = length(mvs_indegree_dict) == 0 && length(mvs_outdegree_dict) == 0
538+
success = length(mvs_indegree_dict) == 0 && length(mvs_outdegree_dict) == 0
546539
end
547-
548-
549540
return edge_list
550-
551541
end
552542

553543

554544
"""
555-
cartIndexTovecIndex(cart_index::CartesianIndex, tensor_size::NTuple{N, <: Integer} ) where N
545+
cartIndexTovecIndex(cart_index::CartesianIndex, tensor_size::NTuple{N, <: Integer} ) where N
556546
557547
Internal function. Converts `cart_index` to an integer index such that it corresponds to the same element under flattening of the tensor whose size is `tensor_size`.
558548
"""
@@ -563,9 +553,9 @@ cartIndexTovecIndex(cart_index::Union{NTuple{N, Integer},CartesianIndex}, tensor
563553
havel_hakimi_(empty_graph::SimpleGraph, degree_sequence::Vector{<:Integer})
564554
565555
Returns a simple graph with a given finite degree sequence of non-negative integers generated via the Havel-Hakimi algorithm which works as follows:
566-
1. successively connect the node of highest degree to other nodes of highest degree;
556+
1. successively connect the node of highest degree to other nodes of highest degree;
567557
2. sort the remaining nodes by degree in decreasing order;
568-
3. repeat the procedure.
558+
3. repeat the procedure.
569559
570560
## References
571561
1. [Hakimi (1962)](https://doi.org/10.1137/0110037)
@@ -574,15 +564,15 @@ Returns a simple graph with a given finite degree sequence of non-negative integ
574564
function havel_hakimi_(empty_graph::SimpleGraph, degree_sequence::Vector{<:Integer}) # Please think about a decent name!
575565
# Check whether the given degree sequence contains only non-negative integers
576566
!any(degree -> degree < 0, degree_sequence) || throw(ArgumentError("The degree sequence (degree_sequence) is invalid: it must contain non-negative integers only."))
577-
# Check whether the given degree sequence is compatible with the given multilayer graph
578-
nv(empty_graph) == length(degree_sequence) || throw(ArgumentError("The degree sequence (degree_sequence) and the multilayer graph (empty_mg) are incompatible: the length of the degree sequence doesn't coincide with the number of vertices."))
579-
# Check whether the given degree sequence is graphical
580-
isgraphical(degree_sequence) || throw(ArgumentError("The degree sequence (degree_sequence) is invalid: it must be graphical (i.e. realizable in a simple graph)."))
581-
# Check whether the given multilayer graph is undirected
567+
# Check whether the given degree sequence is compatible with the given multilayer graph
568+
nv(empty_graph) == length(degree_sequence) || throw(ArgumentError("The degree sequence (degree_sequence) and the multilayer graph (empty_mg) are incompatible: the length of the degree sequence doesn't coincide with the number of vertices."))
569+
# Check whether the given degree sequence is graphical
570+
isgraphical(degree_sequence) || throw(ArgumentError("The degree sequence (degree_sequence) is invalid: it must be graphical (i.e. realizable in a simple graph)."))
571+
# Check whether the given multilayer graph is undirected
582572
!is_directed(empty_mg) || throw(ArgumentError("The multilayer graph (empty_mg) is invalid: it must be undirected."))
583-
# Get all the multilayer vertices from the empty multilayer graph
573+
# Get all the multilayer vertices from the empty multilayer graph
584574
##mvs = mv_vertices(empty_mg)
585575
# Get the length of the degree sequence
586576
##n = length(degree_sequence)
587577
##mvs_degree_dict = Dict(mv => deg for (mv, deg) in zip(mvs, degree_sequence) if deg > 0)
588-
end
578+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ all_interlayers = [interlayer_sg_swg, interlayer_swg_mg, interlayer_mg_vg, inter
143143
@testset "utilities" begin
144144
include("utilities.jl")
145145
end
146-
end
146+
end

0 commit comments

Comments
 (0)