Skip to content

Commit a36b32d

Browse files
author
Ubuntu
committed
refactory flowGraph
1 parent 22dc62f commit a36b32d

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

graph/flowGraph.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
package graph
22

33
import (
4-
"github.com/shady831213/algorithms/tree/disjointSetTree"
54
"math"
65
)
76

87
func augmentingPath(g flowGraph, s interface{}, t interface{}) (int, []edge) {
9-
set := make(map[interface{}]*disjointSetTree.DisjointSet)
8+
augmentingEdges := make([]edge, 0, 0)
9+
minRC := math.MaxInt32
1010
handler := newBFSVisitHandler()
1111
handler.EdgeHandler = func(start, end *bfsElement) {
12-
if _, ok := set[start.V]; !ok {
13-
set[start.V] = disjointSetTree.MakeSet(start)
14-
}
15-
if _, ok := set[end.V]; !ok {
16-
set[end.V] = disjointSetTree.MakeSet(end)
17-
}
18-
if g.RCap(edge{start.V, end.V}) > 0 {
19-
disjointSetTree.Union(set[start.V], set[end.V])
12+
if end.V == t {
13+
for v := end; v.P != nil; v = v.P {
14+
currentEdge := edge{v.P.V, v.V}
15+
augmentingEdges = append(augmentingEdges, currentEdge)
16+
if rc := g.RCap(currentEdge); rc < minRC {
17+
minRC = rc
18+
}
19+
}
2020
}
2121
}
2222

2323
bfsVisit(g, s, handler)
2424

25-
if _, ok := set[t]; ok && disjointSetTree.FindSet(set[t]) == disjointSetTree.FindSet(set[s]) {
26-
augmentingEdges := make([]edge, 0, 0)
27-
minRC := math.MaxInt32
28-
for v := set[t].Value.(*bfsElement); v.P != nil; v = v.P {
29-
currentEdge := edge{v.P.V, v.V}
30-
augmentingEdges = append(augmentingEdges, currentEdge)
31-
if rc := g.RCap(currentEdge); rc < minRC {
32-
minRC = rc
33-
}
34-
}
35-
return minRC, augmentingEdges
36-
}
37-
return 0, make([]edge, 0, 0)
25+
return minRC, augmentingEdges
3826
}
3927

4028
func updateFlow(rg, g flowGraph, rc int, edges []edge) {

0 commit comments

Comments
 (0)