Skip to content

Commit

Permalink
Fix formatting for file vf2.jl and remove ignore in test (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored Jan 10, 2024
1 parent 34b44d0 commit 5878e7b
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 73 deletions.
4 changes: 3 additions & 1 deletion benchmark/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ end

suite["core"]["has_edge"] = BenchmarkGroup(["graphs", "digraphs"])
suite["core"]["has_edge"]["graphs"] = @benchmarkable [all_has_edge(g) for (n, g) in $GRAPHS]
suite["core"]["has_edge"]["digraphs"] = @benchmarkable [all_has_edge(g) for (n, g) in $DIGRAPHS]
suite["core"]["has_edge"]["digraphs"] = @benchmarkable [
all_has_edge(g) for (n, g) in $DIGRAPHS
]
2 changes: 1 addition & 1 deletion src/Experimental/vf2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ function all_isomorph(
edge_relation::Union{Nothing,Function}=nothing,
)::Channel{Vector{Tuple{eltype(g1),eltype(g2)}}}
T = Vector{Tuple{eltype(g1),eltype(g2)}}
!could_have_isomorph(g1, g2) && return Channel(_ -> nothing, ctype = T) # TODO: fix problem with JuliaFormatter, right now the whole file is ignored
!could_have_isomorph(g1, g2) && return Channel(_ -> nothing, ctype = T)
make_callback(c) = vmap -> (put!(c, collect(zip(vmap, 1:length(vmap)))), return true)
ch::Channel{T} = Channel(; ctype=T) do c
vf2(
Expand Down
4 changes: 3 additions & 1 deletion src/Parallel/centrality/stress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function threaded_stress_centrality(g::AbstractGraph, vs=vertices(g))::Vector{In
@sync for (t, task_range) in enumerate(Iterators.partition(1:k, task_size))
Threads.@spawn for s in @view(vs[task_range])
if degree(g, s) > 0 # this might be 1?
state = Graphs.dijkstra_shortest_paths(g, s; allpaths=true, trackvertices=true)
state = Graphs.dijkstra_shortest_paths(
g, s; allpaths=true, trackvertices=true
)
Graphs._stress_accumulate_basic!(local_stress[t], state, g, s)
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/SimpleGraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ function newman_watts_strogatz(
rng::Union{Nothing,AbstractRNG}=nothing,
seed::Union{Nothing,Integer}=nothing,
)
return watts_strogatz(n, k, β; is_directed=is_directed, remove_edges=false, rng=rng, seed=seed)
return watts_strogatz(
n, k, β; is_directed=is_directed, remove_edges=false, rng=rng, seed=seed
)
end

function _suitable(edges::Set{SimpleEdge{T}}, potential_edges::Dict{T,T}) where {T<:Integer}
Expand Down
4 changes: 3 additions & 1 deletion src/SimpleGraphs/simpleedge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ function ==(e1::AbstractSimpleEdge, e2::AbstractSimpleEdge)
return (src(e1) == src(e2) && dst(e1) == dst(e2))
end
hash(e::AbstractSimpleEdge, h::UInt) = hash(src(e), hash(dst(e), h))
isless(e1::AbstractSimpleEdge, e2::AbstractSimpleEdge) = (src(e1) < src(e2)) || ((src(e1) == src(e2)) && (dst(e1) < dst(e2)))
function isless(e1::AbstractSimpleEdge, e2::AbstractSimpleEdge)
return (src(e1) < src(e2)) || ((src(e1) == src(e2)) && (dst(e1) < dst(e2)))
end
4 changes: 3 additions & 1 deletion src/centrality/stress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function stress_centrality(
rng::Union{Nothing,AbstractRNG}=nothing,
seed::Union{Nothing,Integer}=nothing,
)
return stress_centrality(g, sample(collect_if_not_vector(vertices(g)), k; rng=rng, seed=seed))
return stress_centrality(
g, sample(collect_if_not_vector(vertices(g)), k; rng=rng, seed=seed)
)
end

function _stress_accumulate_basic!(
Expand Down
1 change: 0 additions & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ end

degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs]


"""
Δout(g)
Expand Down
6 changes: 3 additions & 3 deletions src/graphcut/normalized_cut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ end
function _recursive_normalized_cut(W, thres, num_cuts)
m, n = size(W)
(m <= 1) && return ones(Int, m) # trivial
D = Diagonal(vec(sum(W, dims=2)))
D = Diagonal(vec(sum(W; dims=2)))

# check that the diagonal is not degenerated as otherwise invDroot errors
dnz = abs.(diag(D)) .>= 1E-16
Expand All @@ -137,7 +137,7 @@ function _recursive_normalized_cut(W, thres, num_cuts)
# At least some versions of ARPACK have a bug, this is a workaround
invDroot = sqrt.(inv(D)) # equal to Cholesky factorization for diagonal D
if n > 12
_, Q = eigs(invDroot' * (D - W) * invDroot, nev=12, which=SR())
_, Q = eigs(invDroot' * (D - W) * invDroot; nev=12, which=SR())
(size(Q, 2) <= 1) && return collect(1:m) # no 2nd eigenvector
ret = convert(Vector, real(view(Q, :, 2)))
else
Expand All @@ -149,7 +149,7 @@ function _recursive_normalized_cut(W, thres, num_cuts)
min_cost = Inf
best_thres = -1
vmin, vmax = extrema(v)
for t in range(vmin, stop=vmax, length=num_cuts)
for t in range(vmin; stop=vmax, length=num_cuts)
cut = v .> t
cost = _normalized_cut_cost(cut, W, D)
if cost < min_cost
Expand Down
2 changes: 1 addition & 1 deletion src/linalg/LinAlg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function eigs(A; kwargs...)
vals, vectors = partialeigen(schr[1])
reved = (kwargs[:which] == LR() || kwargs[:which] == LM())
k = min(get(kwargs, :nev, length(vals))::Int, length(vals))
perm = sortperm(vals, by=real, rev=reved)[1:k]
perm = sortperm(vals; by=real, rev=reved)[1:k]
λ = vals[perm]
Q = vectors[:, perm]
return λ, Q
Expand Down
6 changes: 3 additions & 3 deletions src/traversals/dfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end
end
else
pop!(vertex_stack)
if vcolor[u] == 1
if vcolor[u] == 1
vcolor[u] = 2
end
end
Expand Down Expand Up @@ -107,10 +107,10 @@ function topological_sort_by_dfs end
push!(vertex_stack, n)
end
end
else
else
pop!(vertex_stack)
# if vcolor[u] == 2, the vertex was already explored and added to verts
if vcolor[u] == 1
if vcolor[u] == 1
vcolor[u] = 2
pushfirst!(verts, u)
end
Expand Down
18 changes: 11 additions & 7 deletions src/traversals/eulerian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cycle, the path starts _and_ ends at vertex `u`.
"""
function eulerian(g::AbstractGraph{T}, u::T=first(vertices(g))) where {T}
is_directed(g) && error("`eulerian` is not yet implemented for directed graphs")

_check_eulerian_input(g, u) # perform basic sanity checks

g′ = SimpleGraph{T}(nv(g)) # copy `g` (mutated in `_eulerian!`)
Expand All @@ -28,7 +28,7 @@ function eulerian(g::AbstractGraph{T}, u::T=first(vertices(g))) where {T}
return _eulerian!(g′, u)
end

@traitfn function _eulerian!(g::AG::(!IsDirected), u::T) where {T, AG<:AbstractGraph{T}}
@traitfn function _eulerian!(g::AG::(!IsDirected), u::T) where {T,AG<:AbstractGraph{T}}
# TODO: This uses Fleury's algorithm which is O(|E|²) in the number of edges |E|.
# Hierholzer's algorithm [https://en.wikipedia.org/wiki/Eulerian_path#Hierholzer's_algorithm]
# is presumably faster, running in O(|E|) time, but requires needing to keep track
Expand Down Expand Up @@ -67,7 +67,7 @@ end
end
end
end
error("unreachable reached")
return error("unreachable reached")
end

@inline function _excludes_edge(u, w, e::AbstractEdge)
Expand All @@ -82,22 +82,26 @@ function _check_eulerian_input(g, u)
end

# special case: if any vertex has degree zero
if any(x->degree(g, x) == 0, vertices(g))
if any(x -> degree(g, x) == 0, vertices(g))
error("some vertices have degree zero (are isolated) and cannot be reached")
end

# vertex degree checks
du = degree(g, u)
if iseven(du) # cycle: start (u) == stop (v) - all nodes must have even degree
if any(x -> isodd(degree(g, x)), vertices(g))
error("starting vertex has even degree but there are other vertices with odd degree: a eulerian cycle does not exist")
error(
"starting vertex has even degree but there are other vertices with odd degree: a eulerian cycle does not exist",
)
end
else # isodd(du) # trail: start (u) != stop (v) - all nodes, except u and v, must have even degree
if count(x -> iseven(degree(g, x)), vertices(g)) != 2
error("starting vertex has odd degree but the total number of vertices of odd degree is not equal to 2: a eulerian trail does not exist")
error(
"starting vertex has odd degree but the total number of vertices of odd degree is not equal to 2: a eulerian trail does not exist",
)
end
end

# to reduce cost, the graph connectivity check is performed in `_eulerian!` rather
# than through `is_connected(g)`
end
end
16 changes: 11 additions & 5 deletions src/traversals/maxadjvisit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ weight of the cut that makes this partition. An optional `distmx` matrix
of non-negative weights may be specified; if omitted, edge distances are
assumed to be 1.
"""
@traitfn function mincut(g::::(!IsDirected), distmx::AbstractMatrix{T}=weights(g)) where {T <: Real}

@traitfn function mincut(
g::::(!IsDirected), distmx::AbstractMatrix{T}=weights(g)
) where {T<:Real}
nvg = nv(g)
U = eltype(g)

Expand All @@ -31,12 +32,17 @@ assumed to be 1.
# We need to mutate the weight matrix,
# and we need it clean (0 for non edges)
w = zeros(T, nvg, nvg)
size(distmx) != (nvg, nvg) && throw(ArgumentError("Adjacency / distance matrix size should match the number of vertices"))
size(distmx) != (nvg, nvg) && throw(
ArgumentError(
"Adjacency / distance matrix size should match the number of vertices"
),
)
@inbounds for e in edges(g)
d = distmx[src(e), dst(e)]
(d < 0) && throw(DomainError(w, "weigths should be non-negative"))
w[src(e), dst(e)] = d
(d != distmx[dst(e), src(e)]) && throw(ArgumentError("Adjacency / distance matrix must be symmetric"))
(d != distmx[dst(e), src(e)]) &&
throw(ArgumentError("Adjacency / distance matrix must be symmetric"))
w[dst(e), src(e)] = d
end
# we also need to mutate neighbors when merging vertices
Expand All @@ -58,7 +64,7 @@ assumed to be 1.
pq[v] = zero(T)
end
for v in fadjlist[u]
(is_merged[v] || v == u ) && continue
(is_merged[v] || v == u) && continue
pq[v] = w[u, v]
cutweight += w[u, v]
end
Expand Down
2 changes: 1 addition & 1 deletion test/biconnectivity/articulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

hint = blockdiag(wheel_graph(5), wheel_graph(5))
add_edge!(hint, 5, 6)
for h in test_generic_graphs(hint, eltypes=[Int, UInt8, Int16])
for h in test_generic_graphs(hint; eltypes=[Int, UInt8, Int16])
@test @inferred(articulation(h)) == [5, 6]
end
end
4 changes: 2 additions & 2 deletions test/centrality/betweenness.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@testset "Betweenness" begin
rng = StableRNG(1)
# self loops
s1 = GenericGraph(SimpleGraph(Edge.([(1,2), (2,3), (3, 3)])))
s2 = GenericDiGraph(SimpleDiGraph(Edge.([(1,2), (2,3), (3, 3)])))
s1 = GenericGraph(SimpleGraph(Edge.([(1, 2), (2, 3), (3, 3)])))
s2 = GenericDiGraph(SimpleDiGraph(Edge.([(1, 2), (2, 3), (3, 3)])))

g3 = GenericGraph(path_graph(5))

Expand Down
6 changes: 4 additions & 2 deletions test/cycles/limited_length.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
# This is probably because it uses induced_subgraph which fails on that graph type.
octalengths, _ = simplecycleslength(octadg)
for k in 1:6
@test sum(octalengths[1:k]) == length(simplecycles_limited_length(GenericGraph(octag), k))
@test sum(octalengths[1:k]) == length(simplecycles_limited_length(GenericDiGraph(octadg), k))
@test sum(octalengths[1:k]) ==
length(simplecycles_limited_length(GenericGraph(octag), k))
@test sum(octalengths[1:k]) ==
length(simplecycles_limited_length(GenericDiGraph(octadg), k))
end
end
end
1 change: 0 additions & 1 deletion test/degeneracy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@test @inferred(k_crust(g, 2)) == k_crust(g, 2; corenum=corenum) == [9:21;]
@test @inferred(k_crust(g, 4, corenum=corenum)) == [1:21;]
end

end

# TODO k_corona does not work yet with generic graphs
Expand Down
8 changes: 6 additions & 2 deletions test/edit_distance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
edge_delete_cost = e -> 5.0
edge_subst_cost = (e1, e2) -> 6.0

@testset "undirected edit_distance" for G1 in test_generic_graphs(g1), G2 in test_generic_graphs(g2)
@testset "undirected edit_distance" for G1 in test_generic_graphs(g1),
G2 in test_generic_graphs(g2)

d, λ = @inferred(edit_distance(G1, G2))
@test d == 2.0
d, λ = @inferred(
Expand Down Expand Up @@ -44,7 +46,9 @@
add_edge!(g2, e)
end

@testset "directed edit_distance" for G1 in test_generic_graphs(g1), G2 in test_generic_graphs(g2)
@testset "directed edit_distance" for G1 in test_generic_graphs(g1),
G2 in test_generic_graphs(g2)

d, λ = @inferred(edit_distance(G1, G2))
@test d == 4.0
end
Expand Down
19 changes: 13 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ end
@testset "Code quality (JET.jl)" begin
if VERSION >= v"1.9"
@assert get_pkg_version("JET") >= v"0.8.4"
JET.test_package(Graphs; target_defined_modules=true, ignore_missing_comparison=true)
JET.test_package(
Graphs; target_defined_modules=true, ignore_missing_comparison=true
)
end
end

Expand All @@ -40,7 +42,7 @@ end
end

@testset verbose = true "Code formatting (JuliaFormatter.jl)" begin
@test format(Graphs; verbose=false, overwrite=false, ignore="vf2.jl") # TODO: remove ignore kwarg once the file is formatted correctly
@test format(Graphs; verbose=false, overwrite=false)
end

doctest(Graphs)
Expand Down Expand Up @@ -70,7 +72,7 @@ function test_generic_graphs(g; eltypes=[UInt8, Int16], skip_if_too_large::Bool=
SG = is_directed(g) ? SimpleDiGraph : SimpleGraph
GG = is_directed(g) ? GenericDiGraph : GenericGraph
result = GG[]
for T in eltypes
for T in eltypes
if skip_if_too_large && nv(g) > typemax(T)
continue
end
Expand All @@ -79,10 +81,15 @@ function test_generic_graphs(g; eltypes=[UInt8, Int16], skip_if_too_large::Bool=
return result
end

test_generic_graphs(gs...; kwargs...) = vcat((test_generic_graphs(g; kwargs...) for g in gs)...)

test_large_generic_graphs(g; skip_if_too_large::Bool=false) = test_generic_graphs(g; eltypes=[UInt16, Int32], skip_if_too_large=skip_if_too_large)
function test_generic_graphs(gs...; kwargs...)
return vcat((test_generic_graphs(g; kwargs...) for g in gs)...)
end

function test_large_generic_graphs(g; skip_if_too_large::Bool=false)
return test_generic_graphs(
g; eltypes=[UInt16, Int32], skip_if_too_large=skip_if_too_large
)
end

tests = [
"simplegraphs/runtests",
Expand Down
8 changes: 6 additions & 2 deletions test/shortestpaths/astar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
@inferred(a_star(g, 1, 4, d2))
@test isempty(@inferred(a_star(dg, 4, 1)))
end
@testset "GenericGraph and GenricDiGraph with SimpleEdge" for g in test_generic_graphs(g3), dg in test_generic_graphs(g4)
@testset "GenericGraph and GenricDiGraph with SimpleEdge" for g in
test_generic_graphs(g3),
dg in test_generic_graphs(g4)

zero_heuristic = n -> 0
Eg = SimpleEdge{eltype(g)}
Edg = SimpleEdge{eltype(dg)}
Expand All @@ -30,5 +33,6 @@
s::Int
d::Int
end
@test eltype(a_star(GenericGraph(g), 1, 4, w, n -> 0, MyFavoriteEdgeType)) == MyFavoriteEdgeType
@test eltype(a_star(GenericGraph(g), 1, 4, w, n -> 0, MyFavoriteEdgeType)) ==
MyFavoriteEdgeType
end
16 changes: 9 additions & 7 deletions test/shortestpaths/dijkstra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@
end

#maximum distance setting limits paths found
G = cycle_graph(6)
add_edge!(G, 1, 3)
m = float([0 2 2 0 0 1; 2 0 1 0 0 0; 2 1 0 4 0 0; 0 0 4 0 1 0; 0 0 0 1 0 1; 1 0 0 0 1 0])
G = cycle_graph(6)
add_edge!(G, 1, 3)
m = float(
[0 2 2 0 0 1; 2 0 1 0 0 0; 2 1 0 4 0 0; 0 0 4 0 1 0; 0 0 0 1 0 1; 1 0 0 0 1 0]
)

for g in test_generic_graphs(G)
ds = @inferred(dijkstra_shortest_paths(g, 3, m;maxdist=3.0))
@test ds.dists == [2, 1, 0, Inf, Inf, 3]
end
for g in test_generic_graphs(G)
ds = @inferred(dijkstra_shortest_paths(g, 3, m; maxdist=3.0))
@test ds.dists == [2, 1, 0, Inf, Inf, 3]
end
end
12 changes: 6 additions & 6 deletions test/shortestpaths/yen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@
w[1, 4] = 3
w[4, 1] = 3
for g in testdigraphs(G)
ds = @inferred(yen_k_shortest_paths(g, 1, 6, w, 100))
ds = @inferred(yen_k_shortest_paths(g, 1, 6, w, 100))
@test ds.dists == [4.0, 5.0, 7.0, 7.0, 8.0, 8.0, 8.0, 11.0, 11.0]
ds = @inferred(yen_k_shortest_paths(g, 1, 6, w, 100, maxdist=7.))

ds = @inferred(yen_k_shortest_paths(g, 1, 6, w, 100, maxdist=7.0))
@test ds.dists == [4.0, 5.0, 7.0, 7.0]
end
end

# Test that no paths are returned if every path is longer than maxdist
# Test that no paths are returned if every path is longer than maxdist
for g in testdigraphs(cycle_digraph(10))
ds = @inferred(yen_k_shortest_paths(g, 2, 1, weights(g), 2, maxdist=2))
@test isempty(ds.paths)
end
end
end
Loading

0 comments on commit 5878e7b

Please sign in to comment.