-
Notifications
You must be signed in to change notification settings - Fork 59
Add VertexConnectivity
#94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2118f0
attr: add VertexConnectivity
ffloresbrito 3fddbcc
Reimplement method with maximum flow + single doubled digraph computa…
reiniscirpons e52f205
Add more tests
reiniscirpons 8e92704
Update doc
reiniscirpons 10beabb
Modify code to have an assert instead of unreachable if
reiniscirpons 949d62e
Incorporate feedback from code review and add some more tests.
reiniscirpons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -3358,6 +3358,152 @@ function(D) | |
return Union(M, DIGRAPHS_MateToMatching(D, mateD)); | ||
end); | ||
|
||
InstallMethod(DigraphVertexConnectivity, "for a digraph", [IsDigraph], | ||
function(D) | ||
local doubled_D_adj, doubled_D, max_flow, u, v, i, j, | ||
neighbours_v, kappa, kappa_min, is_multi, has_loops, is_nonsymm; | ||
|
||
# As per Wikipedia: | ||
# "A graph is said to be k-vertex-connected if it contains at least k + 1 | ||
# vertices, but does not contain a set of k − 1 vertices whose removal | ||
# disconnects the graph." | ||
# https://en.wikipedia.org/wiki/Connectivity_(graph_theory) | ||
# The knock-on effect is that the singleton graph has vertex connectivity 0. | ||
# This is discussed in the documentation in more detail. | ||
if DigraphNrVertices(D) <= 1 or not IsConnectedDigraph(D) then | ||
return 0; | ||
fi; | ||
|
||
# Remove multiple edges, loops and symmetrize, if necessary | ||
is_multi := IsMultiDigraph(D); | ||
has_loops := DigraphHasLoops(D); | ||
is_nonsymm := not IsSymmetricDigraph(D); | ||
if is_multi or has_loops or is_nonsymm then | ||
D := DigraphMutableCopy(D); | ||
if is_multi then | ||
DigraphRemoveAllMultipleEdges(D); | ||
fi; | ||
if has_loops then | ||
DigraphRemoveLoops(D); | ||
fi; | ||
if is_nonsymm then | ||
DigraphSymmetricClosure(D); | ||
fi; | ||
# NOTE: D is mutable following this operation. This should not cause issues | ||
# or slow down the computations. | ||
fi; | ||
|
||
# Special case complete digraph since no set of vertices disconnects it. | ||
if IsCompleteDigraph(D) then | ||
return DigraphNrVertices(D) - 1; | ||
fi; | ||
|
||
# Construct "doubled" digraph as per Theorem 6.4 of | ||
# Even S. Applications of Network Flow Techniques. | ||
# In: Even G, ed. Graph Algorithms. | ||
# Cambridge University Press; 2011:117-145. | ||
# https://doi.org/10.1017/CBO9781139015165 | ||
# Doubles the vertices of the digraph `D`. For every vertex v, 2*v-1 is | ||
# called the "in"-vertex and 2*v is called the "out"-vertex. For every edge | ||
# (v, u) in `D`, the doubled digraph contains an edge (2*v, 2*u-1) from | ||
# the out-vertex of `v` to the in-vertex of `u`. Additionally, there is an | ||
# edge (2*v-1, 2*v) for every vertex v in `D`. | ||
doubled_D_adj := List([1 .. 2 * DigraphNrVertices(D)], x -> []); | ||
for v in DigraphVertices(D) do | ||
for u in OutNeighborsOfVertex(D, v) do | ||
Add(doubled_D_adj[2 * v], 2 * u - 1); | ||
od; | ||
Add(doubled_D_adj[2 * v - 1], 2 * v); | ||
od; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be also possible to compute degs with |
||
doubled_D := EdgeWeightedDigraph( | ||
doubled_D_adj, | ||
List(doubled_D_adj, x -> ListWithIdenticalEntries(Length(x), 1))); | ||
|
||
# The resulting graph, `doubled_D` is bipartite, and, additionally, | ||
# there is a correspondence between paths in `D` and `doubled_D` | ||
# given by mapping the path (v_1, v_2, ... v_n) in `D` to the path | ||
# (2*v_1, 2*v_2 - 1, 2*v_2, ..., 2*v_{n-1} - 1, 2*v_{n-1}, 2*v_n - 1) in | ||
# `doubled_D`. An conversely, any path starting with an even vertex and | ||
# ending with an odd vertex in `doubled_D` is of the form | ||
# (2*v_1, 2*v_2 - 1, 2*v_2, ..., 2*v_{n-1} - 1, 2*v_{n-1}, 2*v_n - 1) for | ||
# some path (v_1, v_2, ... v_n) in `D`. | ||
|
||
# The local vertex connectivity for a pair of vertices u, v is the | ||
# size of the least set S that contain u and v | ||
# such that any (u, v)-path (that is, a path with source u and target v) | ||
# passes through S. If two vertices are adjacent, then the local connectivity | ||
# is infinity by convention. The minimum cut with source u and target v is | ||
# the least number of edges that need to be removed so that there is no | ||
# longer a (u, v)-path. | ||
|
||
# Let u and v be non-adjacent vertices in `D`. Because of the | ||
# correspondence of paths in `D` and `doubled_D`, any such set S | ||
# for `D` corresponds to a set of edges E_S (obtained by replacing the | ||
# vertex w by the edge (2*w-1, 2*w)) in `doubled_D` whose removal | ||
# disconnects 2*u from 2*v-1. | ||
# Conversely, it can be shown that the smallest set of edges disconnecting | ||
# 2*u from 2*v-1 has the same cardinality as E_S. This is because, whenever | ||
# we remove any edge (2*s, 2*t-1) from `doubled_D` with the goal of | ||
# disconnecting 2*u from 2*v-1, it is always just as | ||
# efficient or more efficient to remove the edge (2*s-1, 2*s) instead, since | ||
# any path from 2*u to 2*v-1 utilizing (2*s, 2*t-1) in `doubled_D` must | ||
# pass through (2*s-1, 2*s) by construction. Note that u and v are | ||
# non-adjacent by assumption, so it cannot be the case that | ||
# (2*s, 2*t-1) = (2*u, 2*v-1). | ||
# It follows that, for non-adjacent vertices, the local connectivity of u and | ||
# v in `D` is equal to the minimum cut with source 2*u-1 and target 2*v | ||
# in `doubled_D`. | ||
|
||
# By the max-flow min-cut theorem, the size of the minimum cut with source u | ||
# and target v equals the maximum flow between u and v see Wikipedia below: | ||
# https://en.wikipedia.org/wiki/Max-flow_min-cut_theorem | ||
# Hence we can compute local vertex connectivity by repeated calls to the | ||
# max_flow function below: | ||
max_flow := {digraph, source, target} -> | ||
Sum(DigraphMaximumFlow(digraph, source, target)[source]); | ||
|
||
# The vertex connectivity is the minimum of the local vertex connectivity | ||
# over all pairs of vertices. However, it can be computed a bit more | ||
# cleverly by utilizing some theory to reduce the number of pairs considered. | ||
# In this function we implement Algorithm 11 from Abdol-Hossein | ||
# Esfahanian's ``Connectivity Algorithms'' which can be found at | ||
# https://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf | ||
# In particular, we reduce the number of local vertex connectivity | ||
# computations to n-d-1 + d*(d-1)/2 where n is the total number of vertices | ||
# and d is the minimum degree of any vertex. | ||
v := PositionMinimum(OutDegrees(D)); | ||
neighbours_v := OutNeighboursOfVertex(D, v); | ||
kappa_min := fail; | ||
for u in DigraphVertices(D) do | ||
if u <> v and not IsDigraphEdge(D, v, u) then | ||
kappa := max_flow(doubled_D, 2 * u, 2 * v - 1); | ||
if kappa_min = fail or kappa < kappa_min then | ||
kappa_min := kappa; | ||
fi; | ||
fi; | ||
od; | ||
|
||
for i in [1 .. Length(neighbours_v)] do | ||
for j in [i + 1 .. Length(neighbours_v)] do | ||
u := neighbours_v[i]; | ||
v := neighbours_v[j]; | ||
if not IsDigraphEdge(D, v, u) then | ||
kappa := max_flow(doubled_D, 2 * u, 2 * v - 1); | ||
if kappa_min = fail or kappa < kappa_min then | ||
kappa_min := kappa; | ||
fi; | ||
fi; | ||
od; | ||
od; | ||
|
||
# We can only be here if every vertex is adjacent to a vertex of minimum | ||
# degree u and every pair of vertices v, w adjacent to u are also adjacent | ||
# to each other. In other words, `D` is a complete graph, but we | ||
# deal with these at the start. So this assert should pass. | ||
Assert(1, kappa_min <> fail); | ||
return kappa_min; | ||
end); | ||
|
||
# The following function is a transliteration from python to GAP of | ||
# the function find_nonsemimodular_pair | ||
# in sage/src/sage/combinat/posets/hasse_diagram.py | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.