Debian Codegraph @

maxflow_test.go

raw symbols SCIP
1 package graph
2
3 import (
4 "math/rand"
5 "testing"
6 )
7
8 func TestMaxFlow(t *testing.T) {
9 g := New(1)
10 g.AddCost(0, 0, 8)
11 flow, res := MaxFlow(g, 0, 0)
12 if mess, diff := diff(flow, Max); diff {
13 t.Errorf("MaxFlow(0, 0) %s", mess)
14 }
15 if mess, diff := diff(String(res), "1 []"); diff {
16 t.Errorf("MaxFlow(0, 0) %s", mess)
17 }
18
19 g = New(2)
20 g.AddCost(0, 1, 5)
21 flow, res = MaxFlow(g, 0, 1)
22 if mess, diff := diff(flow, int64(5)); diff {
23 t.Errorf("MaxFlow(0, 0) %s", mess)
24 }
25 if mess, diff := diff(String(res), "2 [(0 1):5]"); diff {
26 t.Errorf("MaxFlow(0, 1) %s", mess)
27 }
28
29 g = New(6)
30 for _, e := range []struct {
31 v, w int
32 c int64
33 }{
34 {0, 1, 16}, {0, 2, 13}, {1, 2, 10}, {2, 1, 4},
35 {1, 3, 12}, {2, 4, 14}, {3, 2, 9}, {4, 3, 7},
36 {3, 5, 20}, {4, 5, 4},
37 } {
38 g.AddCost(e.v, e.w, e.c)
39 }
40 _, res = MaxFlow(g, 0, 5)
41 exp := "6 [(0 1):12 (0 2):11 (1 3):12 (2 4):11 (3 5):19 (4 3):7 (4 5):4]"
42 if mess, diff := diff(String(res), exp); diff {
43 t.Errorf("MaxFlow(0, 5) %s", mess)
44 }
45 _, res = MaxFlow(g, 0, 1)
46 exp = "6 [(0 1):16 (0 2):4 (2 1):4]"
47 if mess, diff := diff(String(res), exp); diff {
48 t.Errorf("MaxFlow(0, 1) %s", mess)
49 }
50 _, res = MaxFlow(g, 0, 2)
51 exp = "6 [(0 1):16 (0 2):13 (1 2):10 (1 3):6 (3 2):6]"
52 if mess, diff := diff(String(res), exp); diff {
53 t.Errorf("MaxFlow(0, 2) %s", mess)
54 }
55 _, res = MaxFlow(g, 0, 3)
56 exp = "6 [(0 1):12 (0 2):7 (1 3):12 (2 4):7 (4 3):7]"
57 if mess, diff := diff(String(res), exp); diff {
58 t.Errorf("MaxFlow(0, 3) %s", mess)
59 }
60 _, res = MaxFlow(g, 0, 4)
61 exp = "6 [(0 1):1 (0 2):13 (1 2):1 (2 4):14]"
62 if mess, diff := diff(String(res), exp); diff {
63 t.Errorf("MaxFlow(0, 4) %s", mess)
64 }
65 _, res = MaxFlow(g, 3, 1)
66 exp = "6 [(2 1):4 (3 2):4]"
67 if mess, diff := diff(String(res), exp); diff {
68 t.Errorf("MaxFlow(3, 1) %s", mess)
69 }
70 }
71
72 func BenchmarkMaxFlow(b *testing.B) {
73 n := 50
74 b.StopTimer()
75 g := New(n)
76 for i := 0; i < n; i++ {
77 for j := i; j < n; j++ {
78 g.AddCost(i, j, int64(rand.Int()))
79 }
80 }
81 b.StartTimer()
82 for i := 0; i < b.N; i++ {
83 _, _ = MaxFlow(g, 0, n-1)
84 }
85 }