Skip to content

Commit a1f8b6f

Browse files
committed
Merge branch 'master' into no_alloc_enumerate_paths
2 parents af30aad + 6130332 commit a1f8b6f

26 files changed

+89
-46
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Graphs"
22
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
3-
version = "1.12.0"
3+
version = "1.12.1"
44

55
[deps]
66
ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"
@@ -42,6 +42,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
4242
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
4343
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
4444
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
45+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
4546

4647
[targets]
47-
test = ["Aqua", "Base64", "DelimitedFiles", "Documenter", "JET", "JuliaFormatter", "LinearAlgebra", "Pkg", "Random", "SparseArrays", "StableRNGs", "Statistics", "Test"]
48+
test = ["Aqua", "Base64", "DelimitedFiles", "Documenter", "JET", "JuliaFormatter", "LinearAlgebra", "Pkg", "Random", "SparseArrays", "StableRNGs", "Statistics", "Test", "Unitful"]

src/Parallel/distance.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function eccentricity(
44
g::AbstractGraph, vs=vertices(g), distmx::AbstractMatrix{T}=weights(g)
5-
) where {T<:Real}
5+
) where {T<:Number}
66
vlen = length(vs)
77
eccs = SharedVector{T}(vlen)
88
@sync @distributed for i in 1:vlen

src/Parallel/shortestpaths/bellman-ford.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function bellman_ford_shortest_paths(
22
g::AbstractGraph{U},
33
sources::AbstractVector{<:Integer},
44
distmx::AbstractMatrix{T}=weights(g),
5-
) where {T<:Real} where {U<:Integer}
5+
) where {T<:Number} where {U<:Integer}
66
nvg = nv(g)
77
active = Set{U}()
88
sizehint!(active, nv(g))
@@ -30,7 +30,7 @@ function _loop_body!(
3030
dists::Vector{T},
3131
parents::Vector{U},
3232
active::Set{U},
33-
) where {T<:Real} where {U<:Integer}
33+
) where {T<:Number} where {U<:Integer}
3434
prev_dists = deepcopy(dists)
3535

3636
tmp_active = collect(active)
@@ -58,7 +58,7 @@ end
5858

5959
function has_negative_edge_cycle(
6060
g::AbstractGraph{U}, distmx::AbstractMatrix{T}
61-
) where {T<:Real} where {U<:Integer}
61+
) where {T<:Number} where {U<:Integer}
6262
try
6363
Parallel.bellman_ford_shortest_paths(g, vertices(g), distmx)
6464
catch e

src/Parallel/shortestpaths/dijkstra.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
An [`AbstractPathState`](@ref) designed for Parallel.dijkstra_shortest_paths calculation.
55
"""
6-
struct MultipleDijkstraState{T<:Real,U<:Integer} <: AbstractPathState
6+
struct MultipleDijkstraState{T<:Number,U<:Integer} <: AbstractPathState
77
dists::Matrix{T}
88
parents::Matrix{U}
99
end
@@ -18,7 +18,7 @@ traversal information.
1818
"""
1919
function dijkstra_shortest_paths(
2020
g::AbstractGraph{U}, sources=vertices(g), distmx::AbstractMatrix{T}=weights(g)
21-
) where {T<:Real} where {U}
21+
) where {T<:Number} where {U}
2222
n_v = nv(g)
2323
r_v = length(sources)
2424

src/Parallel/shortestpaths/floyd-warshall.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Helper function used due to performance bug in @threads.
22
function _loopbody!(
33
pivot::U, nvg::U, dists::Matrix{T}, parents::Matrix{U}
4-
) where {T<:Real} where {U<:Integer}
4+
) where {T<:Number} where {U<:Integer}
55
# Relax dists[u, v] = min(dists[u, v], dists[u, pivot]+dists[pivot, v]) for all u, v
66
@inbounds @threads for v in one(U):nvg
77
d = dists[pivot, v]
@@ -26,7 +26,7 @@ end
2626

2727
function floyd_warshall_shortest_paths(
2828
g::AbstractGraph{U}, distmx::AbstractMatrix{T}=weights(g)
29-
) where {T<:Real} where {U<:Integer}
29+
) where {T<:Number} where {U<:Integer}
3030
nvg = nv(g)
3131
dists = fill(typemax(T), (Int(nvg), Int(nvg)))
3232
parents = zeros(U, (Int(nvg), Int(nvg)))

src/Parallel/shortestpaths/johnson.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Compat
44

55
function johnson_shortest_paths(
66
g::AbstractGraph{U}, distmx::AbstractMatrix{T}=weights(g)
7-
) where {T<:Real} where {U<:Integer}
7+
) where {T<:Number} where {U<:Integer}
88
nvg = nv(g)
99
type_distmx = typeof(distmx)
1010
# Change when parallel implementation of Bellman Ford available

src/connectivity.jl

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -798,24 +798,49 @@ function isgraphical(degs::AbstractVector{<:Integer})
798798
!isempty(degs) || return true
799799
# Check whether the sum of degrees is even
800800
iseven(sum(degs)) || return false
801-
# Check that all degrees are non negative and less than n-1
801+
# Compute the length of the degree sequence
802802
n = length(degs)
803+
# Check that all degrees are non negative and less than n-1
803804
all(0 .<= degs .<= n - 1) || return false
804805
# Sort the degree sequence in non-increasing order
805806
sorted_degs = sort(degs; rev=true)
806-
# Compute the length of the degree sequence
807+
# Initialise a sum variable
807808
cur_sum = zero(UInt64)
808-
# Compute the minimum of each degree and the corresponding index
809-
mindeg = Vector{UInt64}(undef, n)
810-
@inbounds for i in 1:n
811-
mindeg[i] = min(i, sorted_degs[i])
812-
end
809+
right_deg_sum = zero(UInt64)
810+
# Initalise a pointer to track the smallest index with degree greater than r
811+
ptr = n
813812
# Check if the degree sequence satisfies the Erdös-Gallai condition
814-
cum_min = sum(mindeg)
815813
@inbounds for r in 1:(n - 1)
816814
cur_sum += sorted_degs[r]
817-
cum_min -= mindeg[r]
818-
cond = cur_sum <= (r * (r - 1) + cum_min)
815+
# Calculate the sum of the minimum of r and the degrees of the vertices
816+
min_idx = r + 1
817+
while ptr >= min_idx
818+
if sorted_degs[ptr] <= r
819+
# left_deg_sum = sum_{ptr+1}^n d_i
820+
right_deg_sum += sorted_degs[ptr]
821+
# move pointer to the 1-slot left
822+
ptr -= 1
823+
else
824+
# the ptr points to the degree greater than r
825+
break
826+
end
827+
end
828+
# calculate min_deg_sum: sum_{r+1}^n min(r, d_i)
829+
if ptr < min_idx
830+
# all required degrees are less than r
831+
# ptr is min_idx - 1
832+
min_deg_sum = right_deg_sum
833+
# prepare for the next iteration
834+
# shift ptr to the right
835+
ptr += 1
836+
# reduce right_deg_sum
837+
right_deg_sum -= sorted_degs[ptr]
838+
else
839+
# d_i with i between ptr and min_idx are greater than r
840+
min_deg_sum = (ptr - r) * r + right_deg_sum
841+
end
842+
# Check the Erdös-Gallai condition
843+
cond = cur_sum <= (r * (r - 1) + min_deg_sum)
819844
cond || return false
820845
end
821846
return true

src/cycles/karp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Discrete Mathematics, 1978, 23, 309 - 311
44
function _karp_minimum_cycle_mean(
55
g::AbstractGraph, distmx::AbstractMatrix{T}, component::Vector{U}
6-
) where {T<:Real} where {U<:Integer}
6+
) where {T<:Number} where {U<:Integer}
77
v2j = Dict{U,Int}()
88
for (j, v) in enumerate(component)
99
v2j[v] = j

src/distance.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ julia> eccentricity(g, [1; 2], [0 2 0; 0.5 0 0.5; 0 2 0])
7373
"""
7474
function eccentricity(
7575
g::AbstractGraph, v::Integer, distmx::AbstractMatrix{T}=weights(g)
76-
) where {T<:Real}
76+
) where {T<:Number}
7777
e = maximum(dijkstra_shortest_paths(g, v, distmx).dists)
7878
e == typemax(T) && @warn("Infinite path length detected for vertex $v")
7979

src/graphcut/normalized_cut.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,6 @@ function normalized_cut(
201201
thres::Real,
202202
W::AbstractMatrix{T}=adjacency_matrix(g),
203203
num_cuts::Int=10,
204-
) where {T<:Real}
204+
) where {T<:Number}
205205
return _recursive_normalized_cut(W, thres, num_cuts)
206206
end

0 commit comments

Comments
 (0)