-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFordFulkerson.py
More file actions
executable file
·183 lines (156 loc) · 4.79 KB
/
FordFulkerson.py
File metadata and controls
executable file
·183 lines (156 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def find_path(capacity, path, source, sink, flow):
"""Находит увеличивающий путь"""
if source == sink:
return flow
path[source] = True
for i, v in enumerate(path):
if not v and capacity[source][i] > 0:
df = find_path(capacity,
path,
i,
sink,
min(flow, capacity[source][i]))
if df > 0:
capacity[source][i] -= df
capacity[i][source] += df
return df
return 0
def max_flow(capacity, source, sink):
"""Находит максимальный поток методом Форда-Фалкерсона"""
flow = 0
iteration_count = 1
while True:
path = [False for i in xrange(len(capacity))]
df = find_path(capacity, path, source, sink, sys.maxint)
flow += df
if df == 0:
return flow, iteration_count
iteration_count += 1
import sys
stdout = sys.stdout
f = open('FordFulkerson.txt', 'w+')
def test(msg, capacity):
sys.stdout = f
print '\n', msg,
sys.stdout = stdout
flow, iteration_count = max_flow(capacity, 0, len(capacity) - 1)
sys.stdout = f
print ':', iteration_count, 'iterations'
print 'Max flow = ', flow
sys.stdout = stdout
# вариант 1
capacity = [
# [s, 1, 2, 3, 4, 5, 6, t]
[0, 3, 2, 1, 0, 6, 0, 0], # s
[0, 0, 0, 1, 2, 0, 0, 0], # 1
[0, 0, 0, 1, 2, 4, 0, 0], # 2
[0, 0, 0, 0, 7, 5, 4, 1], # 3
[0, 0, 0, 0, 0, 0, 3, 2], # 4
[0, 0, 0, 0, 0, 0, 0, 4], # 5
[0, 0, 0, 0, 0, 3, 0, 5], # 6
[0, 0, 0, 0, 0, 0, 0, 0], # t
]
test('1', capacity)
# вариант 2, в методичке правильный поток, но неправильно посчитан
# там v = 12, должно быть v = 13
capacity = [
# [s, 1, 2, 3, 4, 5, 6, 7, t]
[0, 4, 1, 0, 1, 5, 2, 0, 0], # s
[0, 0, 0, 1, 0, 6, 0, 0, 0], # 1
[0, 0, 0, 0, 2, 0, 0, 0, 0], # 2
[0, 0, 6, 0, 0, 0, 3, 0, 0], # 3
[0, 0, 0, 0, 0, 4, 0, 3, 0], # 4
[0, 0, 0, 0, 0, 0, 1, 3, 6], # 5
[0, 0, 0, 0, 0, 0, 0, 4, 5], # 6
[0, 0, 0, 0, 0, 0, 0, 0, 4], # 7
[0, 0, 0, 0, 0, 0, 0, 0, 0], # t
]
test('2', capacity)
# вариант 3
capacity = [
# [s, 1, 2, 3, 4, 5, 6, 7, 8, t]
[0, 2, 1, 0, 2, 0, 0, 0, 0, 0], # s
[0, 0, 0, 2, 1, 0, 0, 0, 0, 0], # 1
[0, 0, 0, 0, 3, 2, 0, 0, 0, 0], # 2
[0, 0, 0, 0, 0, 0, 1, 2, 0, 0], # 3
[0, 0, 0, 0, 0, 1, 4, 3, 3, 0], # 4
[0, 0, 0, 0, 0, 0, 0, 2, 2, 0], # 5
[0, 0, 0, 0, 0, 0, 0, 5, 0, 3], # 6
[0, 0, 0, 0, 0, 0, 0, 0, 1, 4], # 7
[0, 0, 0, 0, 0, 0, 0, 0, 0, 3], # 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # t
]
test('3', capacity)
# вариант 4
capacity = [
# [s, 1, 2, 3, 4, 5, 6, t]
[0, 3, 6, 3, 2, 0, 0, 0], # s
[0, 0, 4, 1, 4, 0, 0, 0], # 1
[0, 0, 0, 0, 0, 0, 3, 2], # 2
[0, 0, 1, 0, 5, 0, 1, 2], # 3
[0, 0, 0, 0, 0, 1, 0, 0], # 4
[0, 0, 0, 3, 0, 0, 1, 0], # 5
[0, 0, 0, 0, 0, 0, 0, 4], # 6
[0, 0, 0, 0, 0, 0, 0, 0], # t
]
test('4', capacity)
# вариант 5
capacity = [
# [s, 1, 2, 3, 4, 5, 6, 7, t]
[0, 2, 0, 5, 0, 3, 0, 0, 0], # s
[0, 0, 0, 0, 0, 0, 0, 0, 0], # 1
[0, 4, 0, 0, 1, 0, 0, 3, 1], # 2
[0, 0, 2, 0, 2, 4, 5, 0, 0], # 3
[0, 4, 0, 0, 0, 4, 0, 0, 5], # 4
[0, 0, 0, 0, 0, 0, 1, 0, 0], # 5
[0, 0, 0, 0, 1, 0, 0, 4, 0], # 6
[0, 0, 0, 0, 3, 0, 0, 0, 0], # 7
[0, 0, 0, 0, 0, 0, 0, 0, 0], # t
]
test('5', capacity)
# вариант 6
capacity = [
# [s, 1, 2, 3, 4, 5, 6, t]
[0, 0, 0, 3, 6, 0, 0, 0], # s
[1, 0, 0, 4, 7, 0, 1, 0], # 1
[5, 5, 0, 0, 0, 0, 0, 0], # 2
[0, 0, 1, 0, 2, 2, 1, 3], # 3
[0, 0, 0, 0, 0, 4, 3, 0], # 4
[0, 0, 5, 0, 0, 0, 0, 2], # 5
[0, 0, 0, 0, 0, 7, 0, 0], # 6
[0, 0, 4, 0, 0, 0, 0, 0], # t
]
test('6', capacity)
# вариант 7
capacity = [
# [s, 1, 2, 3, 4, 5, 6, 7, t]
[0, 0, 0, 0, 4, 6, 2, 0, 0], # s
[3, 0, 2, 0, 7, 0, 4, 0, 0], # 1
[0, 0, 0, 0, 0, 0, 0, 0, 0], # 2
[0, 3, 5, 0, 2, 0, 4, 0, 2], # 3
[0, 0, 0, 0, 0, 1, 0, 0, 0], # 4
[0, 0, 0, 0, 0, 0, 0, 0, 7], # 5
[0, 0, 0, 0, 0, 1, 0, 0, 0], # 6
[0, 0, 3, 6, 0, 0, 4, 0, 1], # 7
[0, 0, 1, 0, 0, 0, 0, 0, 0], # t
]
test('7', capacity)
# вариант 8
capacity = [
# [s, 1, 2, 3, 4, 5, 6, 7, 8, t]
[0, 3, 3, 0, 0, 2, 1, 0, 0, 0], # s
[0, 0, 6, 0, 0, 0, 0, 0, 0, 0], # 1
[0, 0, 0, 1, 2, 4, 4, 0, 0, 0], # 2
[0, 0, 0, 0, 7, 0, 0, 0, 0, 4], # 3
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1], # 4
[0, 0, 0, 4, 7, 0, 0, 0, 0, 0], # 5
[0, 0, 0, 0, 0, 0, 0, 0, 0, 3], # 6
[2, 4, 0, 0, 0, 5, 6, 0, 2, 0], # 7
[0, 0, 0, 0, 0, 0, 0, 0, 0, 5], # 8
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # t
]
test('8', capacity)
f.close()