-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdfs_find_maxflow.py
59 lines (57 loc) · 1.82 KB
/
dfs_find_maxflow.py
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
from graph import *
import Kmeans_rgb_modified
from skimage.io import imread, imsave
import numpy as np
def dfs(capacity_graph):
stack = []
res = []
stack.append(0)
visited = {}
stack_min = []
while stack:
cur = stack.pop()
visited[cur] = 1
if res:
tmp_min = capacity_graph.find_edge_value(res[-1], cur)
if not stack_min or tmp_min < stack_min[-1]:
stack_min.append(tmp_min)
else:
stack_min.append(stack_min[-1])
res.append(cur)
if cur == -1:
break
flag = False
cur_neighbor_list = capacity_graph.find_adjcent(cur)
for i in cur_neighbor_list:
if i not in visited:
flag = True
stack.append(i)
while res and stack and (stack[-1] not in capacity_graph.find_adjcent(res[-1])):
res.pop()
stack_min.pop()
return res, stack_min, visited
if __name__ == "__main__":
#image, m, n, likelihood_a, likelihood_b = Kmeans_rgb_modified.mainfunction("cow.jpg")
m = 2
n = 2
#likelihood_a = [0.1, 0.1, 0.1, 0.1, 0.1, 1, 1, 0.1, 0.1, 1, 1, 0.1, 0.1, 0.1, 0.1, 0.1]
#likelihood_b = [1, 1, 1, 1, 1, 0.1, 0.1, 1, 1, 0.1, 0.1, 1, 1, 1, 1, 1]
likelihood_a = [0.1, 0.1, 1, 1]
likelihood_b = [1, 1, 0.1, 0.1]
g = Graph(m, n, likelihood_a, likelihood_b, 0.2)
res, stack_min, visited = dfs(g)
total_flow = 0
while len(res) != 1:
total_flow += stack_min[-1]
print(total_flow)
g.update(res, stack_min[-1])
res, stack_min, visited = dfs(g)
cc = 0
'''
one_side = np.zeros_like(image)
for i in range(m):
for j in range(n):
if (i * n + j + 1) in visited:
one_side[i][j] = image[i][j]
imsave('foreground.jpg', one_side)
'''