forked from pmatiello/python-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow.diff
More file actions
226 lines (223 loc) · 6.6 KB
/
flow.diff
File metadata and controls
226 lines (223 loc) · 6.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
Index: graph/algorithms/minmax.py
===================================================================
--- graph/algorithms/minmax.py (Revision 375)
+++ graph/algorithms/minmax.py (Arbeitskopie)
@@ -2,6 +2,7 @@
# Rhys Ulerich <[email protected]>
# Roy Smith <[email protected]>
# Salim Fadhley <[email protected]>
+# J. Reinhardt <[email protected]>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
@@ -34,6 +35,7 @@
from heapq import heappush, heappop
from exceptions import unreachable
+from graph.classes.Digraph import digraph
# Minimal spanning tree
@@ -246,3 +248,205 @@
while node is not None:
yield node
node = parents[ node ]
+
+#maximum flow/minimum cut
+#code by J. Reinhardt
+
+def maximum_flow(graph, source, sink, caps = None):
+ """
+ Finds a maximum flow and minimum cut of a directed graph by the Edmonds-Karp algorithm
+
+ @type graph: digraph
+ @param graph: Graph
+
+ @type source: node
+ @param source: Source of the flow
+
+ @type sink: node
+ @param sink: Sink of the flow
+
+ @type caps: dictionary
+ @param caps: Dictionary containing a maximum capacity for each edge. Defaults to the weights of the edges.
+
+ @rtype: tuple
+ @return: A tuple containing two dictionaries
+ 1. contains the flow through each edge for a maximal flow through the graph
+ 2. contains to which component of a minimum cut each node belongs
+ """
+
+ #handle optional argument
+ if caps == None:
+ caps = {}
+ for edge in graph.edges():
+ caps[edge] = graph.get_edge_weight(edge[0],edge[1])
+
+ #data structures to maintain
+ f = {}.fromkeys(graph.edges(),0)
+ label = {}.fromkeys(graph.nodes(),[])
+ label[source] = ['-',float('Inf')]
+ u = {}.fromkeys(graph.nodes(),False)
+ d = {}.fromkeys(graph.nodes(),float('Inf'))
+ #queue for labelling
+ q = [source]
+
+ finished = False
+ while not finished:
+ #choose first labelled vertex with u == false
+ for i in range(len(q)):
+ if not u[q[i]]:
+ v = q.pop(i)
+ break
+
+ #find augmenting path
+ for w in graph.neighbors(v):
+ if label[w] == [] and f[(v,w)] < caps[(v,w)]:
+ d[w] = min(caps[(v,w)] - f[(v,w)],d[v])
+ label[w] = [v,'+',d[w]]
+ q.append(w)
+ for w in graph.incidents(v):
+ if label[w] == [] and f[(w,v)] > 0:
+ d[w] = min(f[(w,v)],d[v])
+ label[w] = [v,'-',d[w]]
+ q.append(w)
+
+ u[v] = True
+
+ #extend flow by augmenting path
+ if label[sink] != []:
+ delta = label[sink][-1]
+ w = sink
+ while w != source:
+ v = label[w][0]
+ if label[w][1] == '-':
+ f[(w,v)] = f[(w,v)] - delta
+ else:
+ f[(v,w)] = f[(v,w)] + delta
+ w = v
+ #reset labels
+ label = {}.fromkeys(graph.nodes(),[])
+ label[source] = ['-',float('Inf')]
+ q = [source]
+ u = {}.fromkeys(graph.nodes(),False)
+ d = {}.fromkeys(graph.nodes(),float('Inf'))
+
+ #check whether finished
+ finished = True
+ for node in graph.nodes():
+ if label[node] != [] and u[node] == False:
+ finished = False
+
+ #find the two components of the cut
+ cut = {}
+ for node in graph.nodes():
+ if label[node] == []:
+ cut[node] = 1
+ else:
+ cut[node] = 0
+ return (f,cut)
+
+def cut_value(graph,flow,cut):
+ """
+ Calculates the value of a cut
+
+ @type graph: digraph
+ @param graph: Graph
+
+ @type flow: dictionary
+ @param flow: Dictionary containing a flow for each edge.
+
+ @type cut: dictionary
+ @type cut: Dictionary mapping each node to a subset index. The function only considers the flow between nodes with 0 and 1.
+
+ @rtype: float
+ @return: The value of the flow between the subsets 0 and 1
+ """
+ #max flow/min cut value calculation
+ S = []
+ T = []
+ for node in cut.keys():
+ if cut[node] == 0:
+ S.append(node)
+ elif cut[node] == 1:
+ T.append(node)
+ value = 0
+ for node in S:
+ for neigh in graph.neighbors(node):
+ if neigh in T:
+ value = value + flow[(node,neigh)]
+ for inc in graph.incidents(node):
+ if inc in T:
+ value = value - flow[(inc,node)]
+ return value
+
+def cut_tree(igraph, caps = None):
+ """
+ Constructs a Gomory-Hu cut tree by applying the algorithm of Gusfield
+
+ @type graph: graph
+ @param graph: Graph
+
+ @type caps: dictionary
+ @param caps: Dictionary containing a maximum capacity for each edge. Defaults to the weights of the edges.
+
+ @rtype: dictionary
+ @return: Gomory-Hu cut tree as a dictionary, where each edge is associated with its weight
+ """
+
+ #maximum flow needs a digraph, we get a graph
+ #I think this conversion relies on implementation details outside the api and may break in the future
+ graph = digraph()
+ graph.add_graph(igraph)
+
+ #handle optional argument
+ if caps == None:
+ caps = {}
+ for edge in graph.edges():
+ caps[edge] = graph.get_edge_weight(edge[0],edge[1])
+
+ #temporary flow variable
+ f = {}
+
+ #we use a numbering of the nodes for easier handling
+ n = {}
+ N = 0
+ for node in graph.nodes():
+ n[N] = node
+ N = N + 1
+
+ #predecessor function
+ p = {}.fromkeys(range(N),0)
+ p[0] = None
+
+ for s in range(1,N):
+ t = p[s]
+ S = []
+ Sstar = []
+ T = []
+ Tstar = []
+ #max flow calculation
+ (flow,cut) = maximum_flow(graph,n[s],n[t],caps)
+ for i in range(N):
+ if cut[n[i]] == 0:
+ S.append(i)
+
+ value = cut_value(graph,flow,cut)
+
+ f[s] = value
+
+ for i in range(N):
+ if i == s:
+ continue
+ if i in S and p[i] == t:
+ p[i] = s
+ if p[t] in S:
+ p[s] = p[t]
+ p[t] = s
+ f[s] = f[t]
+ f[t] = value
+
+ #cut tree is a dictionary, where each edge is associated with its weight
+ b = {}
+ for i in range(1,N):
+ b[(n[i],n[p[i]])] = f[i]
+ return b
+