-
Notifications
You must be signed in to change notification settings - Fork 61
Added DigraphMinimumCutSet
#879
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
base: main
Are you sure you want to change the base?
Changes from all commits
9d1c20d
1ff11a2
00fe48c
7932db5
4cc0df5
f05eaf3
5c5f5e8
81a7065
d3dac89
51a3664
c9f28ef
35033ec
02a088c
e1eb03e
b088fa8
4415aa0
bc3d557
e62fc7e
1414081
733cc5f
5ef192f
2964fab
851b036
d3dfdcd
73afcc5
6c6ab19
a136f62
dfa8fa9
4e4b80e
81e49ee
272d230
7e9bd13
5e8e5bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -638,7 +638,7 @@ function(D, source) | |||||||||||||
| end); | ||||||||||||||
|
|
||||||||||||||
| ############################################################################# | ||||||||||||||
| # 5. Maximum Flow | ||||||||||||||
| # 5. Maximum Flow and Minimum Cut | ||||||||||||||
| ############################################################################# | ||||||||||||||
|
|
||||||||||||||
| InstallMethod(DigraphMaximumFlow, "for an edge weighted digraph", | ||||||||||||||
|
|
@@ -772,6 +772,52 @@ function(D, start, destination) | |||||||||||||
| return flows; | ||||||||||||||
| end); | ||||||||||||||
|
|
||||||||||||||
| InstallMethod(DigraphMinimumCut, "for an edge weighted digraph", | ||||||||||||||
| [IsDigraph and HasEdgeWeights, IsPosInt, IsPosInt], | ||||||||||||||
| function(D, s, t) | ||||||||||||||
| local weights, outs, vertices, flow, residuals, u, v, S, T, Q; | ||||||||||||||
|
|
||||||||||||||
| # Extract important data | ||||||||||||||
| weights := EdgeWeights(D); | ||||||||||||||
| outs := OutNeighbours(D); | ||||||||||||||
| vertices := DigraphVertices(D); | ||||||||||||||
|
|
||||||||||||||
| # Check input | ||||||||||||||
| if s < 1 or s > Length(vertices) then | ||||||||||||||
| ErrorNoReturn("<s> must be a vertex of <D>,"); | ||||||||||||||
| elif t < 1 or t > Length(vertices) then | ||||||||||||||
| ErrorNoReturn("<t> must be a vertex of <D>,"); | ||||||||||||||
| elif s = t then | ||||||||||||||
| ErrorNoReturn("<s> and <t> must be distinct"); | ||||||||||||||
| fi; | ||||||||||||||
|
|
||||||||||||||
| # Find the residual edge capacities under the maximum flow | ||||||||||||||
| flow := DigraphMaximumFlow(D, s, t); | ||||||||||||||
| residuals := weights - flow; | ||||||||||||||
|
|
||||||||||||||
| # Carry out a BFS to find all the vertices in the residual | ||||||||||||||
| # network which are reachable from s. This gives the minimum | ||||||||||||||
| # cut by the max-flow min-cut theorem. | ||||||||||||||
|
|
||||||||||||||
| S := [s]; | ||||||||||||||
| Q := [s]; | ||||||||||||||
| while not IsEmpty(Q) do | ||||||||||||||
| u := Q[1]; | ||||||||||||||
| Remove(Q, 1); | ||||||||||||||
|
Comment on lines
+804
to
+806
Collaborator
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.
Suggested change
Removing an element from the front of a list is an Its a rather minor optimization since almost certainly most of the time spent by this function will be taken by the |
||||||||||||||
| for v in [1 .. Length(outs[u])] do | ||||||||||||||
| if residuals[u][v] > 0 then | ||||||||||||||
| if not outs[u][v] in S then | ||||||||||||||
| Add(Q, outs[u][v]); | ||||||||||||||
| Add(S, outs[u][v]); | ||||||||||||||
|
Comment on lines
+809
to
+811
Collaborator
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.
Suggested change
Another minor optimization: you are adding to the list GAP provides the However, if you used a boolean list (blist) Same as before, minor optimization but worth making. |
||||||||||||||
| fi; | ||||||||||||||
| fi; | ||||||||||||||
| od; | ||||||||||||||
| od; | ||||||||||||||
|
|
||||||||||||||
| T := Difference(vertices, S); | ||||||||||||||
| return [S, T]; | ||||||||||||||
| end); | ||||||||||||||
|
|
||||||||||||||
| ############################################################################# | ||||||||||||||
| # 6. Random edge weighted digraphs | ||||||||||||||
| ############################################################################# | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.