forked from sbromberger/LightGraphs.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement bridge computation * added tests * fix reference * fix docstring * change to Edge * simplify test * fix tests * fix tests for real * just add one edge * remove unnecessary lines * implement suggestions * rename to bridges * added check for undirectedness * use dispatch on trait IsDirected * update docs * Update bridge.jl add newlines between signature and start of doc
- Loading branch information
1 parent
b1e95f8
commit ee0db34
Showing
5 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ condensation | |
neighborhood | ||
neighborhood_dists | ||
articulation | ||
bridges | ||
period | ||
isgraphical | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
Bridges{T} | ||
A state type for the depth-first search that finds bridges in a graph. | ||
""" | ||
mutable struct Bridges{T<:Integer} | ||
low::Vector{T} | ||
depth::Vector{T} | ||
bridges::Vector{Edge{T}} | ||
id::T | ||
end | ||
|
||
function Bridges(g::AbstractGraph) | ||
n = nv(g) | ||
T = eltype(g) | ||
return Bridges(zeros(T, n), zeros(T, n), Edge{T}[], zero(T)) | ||
end | ||
|
||
""" | ||
visit!(state, g, u, v) | ||
Perform a depth first search storing the depth (in `depth`) and low-points | ||
(in `low`) of each vertex. | ||
""" | ||
function visit!(state::Bridges, g::AbstractGraph, u::Integer, v::Integer) | ||
state.id += 1 | ||
state.depth[v] = state.id | ||
state.low[v] = state.depth[v] | ||
|
||
@inbounds for w in outneighbors(g, v) | ||
if state.depth[w] == 0 | ||
visit!(state, g, v, w) | ||
|
||
state.low[v] = min(state.low[v], state.low[w]) | ||
if state.low[w] > state.depth[v] | ||
edge = v < w ? Edge(v, w) : Edge(w, v) | ||
push!(state.bridges, edge) | ||
end | ||
|
||
elseif w != u | ||
state.low[v] = min(state.low[v], state.depth[w]) | ||
end | ||
end | ||
end | ||
|
||
""" | ||
bridges(g) | ||
Compute the [bridges](https://en.m.wikipedia.org/wiki/Bridge_(graph_theory)) | ||
of a connected graph `g` and return an array containing all bridges, i.e edges | ||
whose deletion increases the number of connected components of the graph. | ||
# Examples | ||
```jldoctest | ||
julia> using LightGraphs | ||
julia> bridges(StarGraph(5)) | ||
8-element Array{LightGraphs.SimpleGraphs.SimpleEdge{Int64},1}: | ||
Edge 1 => 2 | ||
Edge 1 => 3 | ||
Edge 1 => 4 | ||
Edge 1 => 5 | ||
julia> bridges(PathGraph(5)) | ||
8-element Array{LightGraphs.SimpleGraphs.SimpleEdge{Int64},1}: | ||
Edge 4 => 5 | ||
Edge 3 => 4 | ||
Edge 2 => 3 | ||
Edge 1 => 2 | ||
``` | ||
""" | ||
function bridges end | ||
|
||
@traitfn function bridges(g::AG::(!IsDirected)) where {T<:Integer, AG<:AbstractGraph{T}} | ||
state = Bridges(g) | ||
for u in vertices(g) | ||
if state.depth[u] == 0 | ||
visit!(state, g, u, u) | ||
end | ||
end | ||
return state.bridges | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
@testset "Bridge" begin | ||
gint = SimpleGraph(13) | ||
add_edge!(gint, 1, 7) | ||
add_edge!(gint, 1, 2) | ||
add_edge!(gint, 1, 3) | ||
add_edge!(gint, 12, 13) | ||
add_edge!(gint, 10, 13) | ||
add_edge!(gint, 10, 12) | ||
add_edge!(gint, 12, 11) | ||
add_edge!(gint, 5, 4) | ||
add_edge!(gint, 6, 4) | ||
add_edge!(gint, 8, 9) | ||
add_edge!(gint, 6, 5) | ||
add_edge!(gint, 1, 6) | ||
add_edge!(gint, 7, 5) | ||
add_edge!(gint, 7, 3) | ||
add_edge!(gint, 7, 8) | ||
add_edge!(gint, 7, 10) | ||
add_edge!(gint, 7, 12) | ||
|
||
for g in testgraphs(gint) | ||
brd = @inferred(bridges(g)) | ||
ans = [ | ||
Edge(1, 2), | ||
Edge(8, 9), | ||
Edge(7, 8), | ||
Edge(11, 12), | ||
] | ||
@test brd == ans | ||
end | ||
for level in 1:6 | ||
btree = LightGraphs.BinaryTree(level) | ||
for tree in [btree, Graph{UInt8}(btree), Graph{Int16}(btree)] | ||
brd = @inferred(bridges(tree)) | ||
ans = collect(edges(tree)) | ||
@test Set(brd) == Set(ans) | ||
end | ||
end | ||
|
||
hint = blockdiag(WheelGraph(5), WheelGraph(5)) | ||
add_edge!(hint, 5, 6) | ||
for h in (hint, Graph{UInt8}(hint), Graph{Int16}(hint)) | ||
@test @inferred(bridges(h)) == [ | ||
Edge(5, 6), | ||
] | ||
end | ||
|
||
dir = SimpleDiGraph(10, 10) | ||
@test_throws MethodError bridges(dir) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters