Skip to content

Commit d4d2eb7

Browse files
author
Ubuntu
committed
refactory flowGraph
1 parent d396892 commit d4d2eb7

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

graph/flowGraph.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,36 @@ import (
44
"math"
55
)
66

7-
func augmentingPath(g flowGraph, s interface{}, t interface{}) (int, []edge) {
7+
type residualGraph interface {
8+
flowGraph
9+
RCap(edge) int
10+
}
11+
12+
type adjacencyMatrixResidual struct {
13+
adjacencyMatrixWithFlow
14+
}
15+
16+
func (g *adjacencyMatrixResidual) init() *adjacencyMatrixResidual {
17+
g.adjacencyMatrixWithFlow.init()
18+
return g
19+
}
20+
21+
func (g *adjacencyMatrixResidual) AddEdgeWithFlow(e edge, f int) {
22+
g.adjacencyMatrixWithFlow.AddEdgeWithFlow(e, f)
23+
if g.RCap(e) == 0 {
24+
g.DeleteEdge(e)
25+
}
26+
}
27+
28+
func (g *adjacencyMatrixResidual) RCap(e edge) int {
29+
return g.Cap(e) - g.Flow(e)
30+
}
31+
32+
func newResidualGraph() residualGraph {
33+
return new(adjacencyMatrixResidual).init()
34+
}
35+
36+
func augmentingPath(g residualGraph, s interface{}, t interface{}) (int, []edge) {
837
augmentingEdges := make([]edge, 0, 0)
938
minRC := math.MaxInt32
1039
handler := newBFSVisitHandler()
@@ -25,28 +54,18 @@ func augmentingPath(g flowGraph, s interface{}, t interface{}) (int, []edge) {
2554
return minRC, augmentingEdges
2655
}
2756

28-
func updateFlow(rg, g flowGraph, rc int, edges []edge) {
29-
updateResidualGraph := func(g flowGraph, flow int, e edge) {
30-
g.AddEdgeWithFlow(e, flow)
31-
if g.RCap(e) == 0 {
32-
g.DeleteEdge(e)
33-
}
34-
re := edge{e.End, e.Start}
35-
g.AddEdgeWithFlow(re, 0-flow)
36-
if g.RCap(re) == 0 {
37-
g.DeleteEdge(re)
38-
}
39-
}
40-
57+
func updateFlow(rg residualGraph, g flowGraph, rc int, edges []edge) {
4158
for _, e := range edges {
4259
flow := g.Flow(e) + rc
4360
g.AddEdgeWithFlow(e, flow)
44-
updateResidualGraph(rg, flow, e)
61+
rg.AddEdgeWithFlow(e, flow)
62+
re := edge{e.End, e.Start}
63+
rg.AddEdgeWithFlow(re, 0-flow)
4564
}
4665
}
4766

4867
func edmondsKarp(g flowGraph, s interface{}, t interface{}) {
49-
residualG := newFlowGraph()
68+
residualG := newResidualGraph()
5069
for _, e := range g.AllEdges() {
5170
g.AddEdgeWithFlow(e, 0)
5271
residualG.AddEdgeWithCap(e, g.Cap(e))

graph/graph.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ type flowGraph interface {
203203
graph
204204
Cap(edge) int
205205
Flow(edge) int
206-
RCap(edge) int
207206
AddEdgeWithCap(edge, int)
208207
AddEdgeWithFlow(edge, int)
209208
}
@@ -214,7 +213,7 @@ type adjacencyMatrixWithFlow struct {
214213
flow map[edge]int
215214
}
216215

217-
func (g *adjacencyMatrixWithFlow) Init() *adjacencyMatrixWithFlow {
216+
func (g *adjacencyMatrixWithFlow) init() *adjacencyMatrixWithFlow {
218217
g.adjacencyMatrix.init()
219218
g.cap = make(map[edge]int)
220219
g.flow = make(map[edge]int)
@@ -245,10 +244,6 @@ func (g *adjacencyMatrixWithFlow) Flow(e edge) int {
245244
return g.flow[e]
246245
}
247246

248-
func (g *adjacencyMatrixWithFlow) RCap(e edge) int {
249-
return g.Cap(e) - g.Flow(e)
250-
}
251-
252247
func (g *adjacencyMatrixWithFlow) DeleteEdge(e edge) {
253248
g.adjacencyMatrix.DeleteEdge(e)
254249
delete(g.cap, e)
@@ -264,5 +259,5 @@ func (g *adjacencyMatrixWithFlow) DeleteEdgeBi(e edge) {
264259
}
265260

266261
func newFlowGraph() flowGraph {
267-
return new(adjacencyMatrixWithFlow).Init()
262+
return new(adjacencyMatrixWithFlow).init()
268263
}

heap/fibHeap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ type FibHeap struct {
148148
mixin FibHeapMixin
149149
}
150150

151-
//Init : Cross package
151+
//init : Cross package
152152
func (h *FibHeap) Init(mixin FibHeapMixin) *FibHeap {
153153
h.root = newFabHeapElementList(nil)
154154
h.min = nil

0 commit comments

Comments
 (0)