Skip to content

Commit

Permalink
Merge branch 'JuliaGraphs:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebeggs authored Oct 11, 2022
2 parents f99bc29 + 7a2cc82 commit ced6e7b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Graphs"
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
version = "1.7.3"
version = "1.7.4"

[deps]
ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"
Expand Down
12 changes: 6 additions & 6 deletions src/SimpleGraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function SimpleGraph{T}(
maxe = div(Int(nv) * (nv - 1), 2)
@assert(ne <= maxe, "Maximum number of edges for this graph is $maxe")
rng = rng_from_rng_or_seed(rng, seed)
ne > div((2 * maxe), 3) && return complement(SimpleGraph(tnv, maxe - ne, rng=rng, seed=seed))
ne > div((2 * maxe), 3) && return complement(SimpleGraph(tnv, maxe - ne, rng=rng))

g = SimpleGraph(tnv)

Expand Down Expand Up @@ -70,7 +70,7 @@ function SimpleDiGraph{T}(
maxe = Int(nv) * (nv - 1)
@assert(ne <= maxe, "Maximum number of edges for this graph is $maxe")
rng = rng_from_rng_or_seed(rng, seed)
ne > div((2 * maxe), 3) && return complement(SimpleDiGraph{T}(tnv, maxe - ne; rng=rng, seed=seed))
ne > div((2 * maxe), 3) && return complement(SimpleDiGraph{T}(tnv, maxe - ne; rng=rng))
g = SimpleDiGraph(tnv)
while g.ne < ne
source = rand(rng, one(T):tnv)
Expand Down Expand Up @@ -262,7 +262,7 @@ where each vertex has exacly `div(k, 2)` neighbors on each side (i.e., `k` or
connected to some vertex `d`, chosen uniformly at random from the entire
graph, excluding `s` and its neighbors. (Note that `t` is a valid candidate.)
For `β = 1`, the graph will remain a 1-lattice, and for `β = 0`, all edges will
For `β = 0`, the graph will remain a 1-lattice, and for `β = 1`, all edges will
be rewired randomly.
### Optional Arguments
Expand Down Expand Up @@ -789,7 +789,7 @@ function random_regular_graph(
end
rng = rng_from_rng_or_seed(rng, seed)
if (k > n / 2) && iseven(n * (n - k - 1))
return complement(random_regular_graph(n, n - k - 1; rng=rng, seed=seed))
return complement(random_regular_graph(n, n - k - 1; rng=rng))
end

edges = _try_creation(n, k, rng)
Expand Down Expand Up @@ -876,7 +876,7 @@ function random_regular_digraph(
end
rng = rng_from_rng_or_seed(rng, seed)
if (k > n / 2) && iseven(n * (n - k - 1))
return complement(random_regular_digraph(n, n - k - 1; dir=dir, rng=rng, seed=seed))
return complement(random_regular_digraph(n, n - k - 1; dir=dir, rng=rng))
end
cs = collect(2:n)
i = 1
Expand Down Expand Up @@ -962,7 +962,7 @@ function stochastic_block_model(

m = a == b ? div(n[a] * (n[a] - 1), 2) : n[a] * n[b]
p = a == b ? n[a] * c[a, b] / (2m) : n[a] * c[a, b] / m
nedg = randbn(m, p; rng=rng, seed=seed)
nedg = randbn(m, p; rng=rng)
rb = (cum[b] + 1):cum[b + 1]
i = 0
while i < nedg
Expand Down
2 changes: 1 addition & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ julia> all_neighbors(g, 3)
2-element Array{Int64,1}:
1
2
```
```
"""
function all_neighbors end
@traitfn all_neighbors(g::::IsDirected, v::Integer) =
Expand Down
23 changes: 21 additions & 2 deletions src/traversals/dfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@
Return `true` if graph `g` contains a cycle.
### Implementation Notes
Uses DFS.
The algorithm uses a DFS. Self-loops are counted as cycles.
"""
function is_cyclic end
@traitfn is_cyclic(g::::(!IsDirected)) = ne(g) > 0
@enum Vertex_state unvisited visited
@traitfn function is_cyclic(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
visited = falses(nv(g))
for v in vertices(g)
visited[v] && continue
visited[v] = true
S = [(v,v)]
while !isempty(S)
parent, w = pop!(S)
for u in neighbors(g, w)
u == w && return true # self-loop
u == parent && continue
visited[u] && return true
visited[u] = true
push!(S, (w, u))
end
end
end
return false
end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function is_cyclic(g::AG::IsDirected) where {T, AG<:AbstractGraph{T}}
vcolor = zeros(UInt8, nv(g))
Expand Down
5 changes: 5 additions & 0 deletions test/simplegraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
@test erdos_renyi(5, 1.0, is_directed=true, rng=rng) == complete_digraph(5)
@test erdos_renyi(5, 2.1, rng=rng) == complete_graph(5)
@test erdos_renyi(5, 2.1, is_directed=true, rng=rng) == complete_digraph(5)

# issue #173
er = erdos_renyi(4, 6, seed=1)
@test nv(er) == 4
@test ne(er) == 6
end

@testset "expected degree" begin
Expand Down
33 changes: 30 additions & 3 deletions test/traversals/dfs.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
@testset "DFS" begin

gnodes_directed=SimpleDiGraph(4)
gnodes_undirected=SimpleGraph(4)
gloop_directed=SimpleDiGraph(1)
add_edge!(gloop_directed, 1, 1);
gloop_undirected=SimpleGraph(1)
add_edge!(gloop_undirected, 1, 1);
g5 = SimpleDiGraph(4)
add_edge!(g5, 1, 2); add_edge!(g5, 2, 3); add_edge!(g5, 1, 3); add_edge!(g5, 3, 4)
gx = cycle_digraph(3)

gcyclic = SimpleGraph([0 1 1 0 0; 1 0 1 0 0; 1 1 0 0 0; 0 0 0 0 1; 0 0 0 1 0])
gtree = SimpleGraph([0 1 1 1 0 0 0; 1 0 0 0 0 0 0; 1 0 0 0 0 0 0; 1 0 0 0 1 1 1; 0 0 0 1 0 0 0; 0 0 0 1 0 0 0; 0 0 0 1 0 0 0])
@testset "dfs_tree" begin
for g in testdigraphs(g5)
z = @inferred(dfs_tree(g, 1))
Expand All @@ -26,8 +32,29 @@

@testset "is_cyclic" begin
for g in testgraphs(path_graph(2))
@test !@inferred(is_cyclic(g))
@test !@inferred(is_cyclic(zero(g)))
end
for g in testgraphs(gcyclic)
@test @inferred(is_cyclic(g))
end
for g in testgraphs(gtree)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(g5)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(gnodes_directed)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(gnodes_undirected)
@test !@inferred(is_cyclic(g))
end
for g in testgraphs(gloop_directed)
@test @inferred(is_cyclic(g))
end
for g in testgraphs(gloop_undirected)
@test @inferred(is_cyclic(g))
@test @inferred(!is_cyclic(zero(g)))
end
end

Expand Down

0 comments on commit ced6e7b

Please sign in to comment.