|
1 | 1 | package graph
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/shady831213/algorithms/tree/disjointSetTree" |
5 | 4 | "math"
|
6 | 5 | )
|
7 | 6 |
|
8 | 7 | 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 |
10 | 10 | handler := newBFSVisitHandler()
|
11 | 11 | 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 | + } |
20 | 20 | }
|
21 | 21 | }
|
22 | 22 |
|
23 | 23 | bfsVisit(g, s, handler)
|
24 | 24 |
|
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 |
38 | 26 | }
|
39 | 27 |
|
40 | 28 | func updateFlow(rg, g flowGraph, rc int, edges []edge) {
|
|
0 commit comments