-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_theory.py
More file actions
417 lines (355 loc) · 13.6 KB
/
graph_theory.py
File metadata and controls
417 lines (355 loc) · 13.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from enum import Enum,unique
import numpy as np
from collections import deque
@unique
class Color(Enum):
white = 0
gray = 1
black = 2
class Node(object):
"""node in graph store node id,node parent 'pi' node color for detect search node distance for a node to given source"""
def __init__(self,idx):
self._id = idx
self.pi = None
self.color =None
self.depth = None
class Graph(object):
"graph is a logical of G<V,E> graph = vertex + edge"
def __init__(self,node_nums):
self.nodes_dict =dict([(idx,Node(idx)) for idx in range(node_nums)])
self.adj_matrix = np.zeros(shape=(node_nums,node_nums),dtype=np.int32)
def add_edge(self,*args):
""""""
for pair in args:
i,j = pair
self.adj_matrix[i,j]=1
self.adj_matrix[j,i]=1
def add_direct_edge(self,*args):
for pair in args:
i,j = pair
self.adj_matrix[i,j]=1
def broad_first_search(self):
for idx,node in self.nodes_dict.items():
node.pi = None
node.color = Color.white
node.depth =0
que = deque()
que.append(self.nodes_dict[0])
self.nodes_dict[0].color = Color.gray
self.nodes_dict[0].depth = 0
while que:
node = que.popleft()
n_id = node._id
for idx,link in enumerate(self.adj_matrix[n_id]):
if link>0 and idx != n_id:
if self.nodes_dict[idx].color == Color.white:
self.nodes_dict[idx].color = Color.gray
self.nodes_dict[idx].pi = n_id
self.nodes_dict[idx].depth = node.depth+1
que.append(self.nodes_dict[idx])
node.color = Color.black
print("tag black",node._id)
print("queue",[node._id for node in que])
def deep_first_search(self):
self.time = 0
for idx,node in self.nodes_dict.items():
node.pi = None
node.color = Color.white
node.depth =0
def Visit(node):
node.color = Color.gray
print("detect node {0},time {1}".format(node._id,self.time))
node.d_time = self.time
self.time +=1
for idx,link in enumerate(self.adj_matrix[node._id]):
if link>0 and self.nodes_dict[idx].color == Color.white:
self.nodes_dict[idx].pi = node._id
Visit(self.nodes_dict[idx])
node.color = Color.black
node.f_time = self.time
self.time +=1
print("node:{0},color:{1},time:{2},{3},".format(node._id,node.color,node.d_time,node.f_time))
for idx, node in self.nodes_dict.items():
if node.color == Color.white:
print("root visit",node._id)
Visit(node)
def top_sort(self):
self.deep_first_search()
node_list = [v for k,v in self.nodes_dict.items()]
node_list = sorted(node_list,key=lambda x:x.f_time)
print([node._id for node in node_list])
def __str__(self):
return "the matrix of adjcent is \n %s"%(str(self.adj_matrix))
class GraphWeight(object):
def __init__(self,nodes):
self.node_list =[Node(idx) for idx in range(nodes)]
self.adj_list = [[] for i in range(nodes)]
def add_node_weight(self,*args):
for w_link in args:
i,j,w = w_link
self.adj_list[i].append((j,w))
self.adj_list[j].append((i,w))
def build_weight_list(self):
self.weight_link = []
for entry_id,link_list in enumerate(self.adj_list):
for linked_id,wt in link_list:
if linked_id>entry_id:
self.weight_link.append((entry_id,linked_id,wt))
def kruskal_msp(self):
self.build_weight_list()
for node in self.node_list:
node.pi = node._id
def root_node(node):
p = node
while p.pi !=p._id:
p = self.node_list[p.pi]
return p
def make_union(u,v):
#set u.root to v root
root_node(u).pi = root_node(v).pi
sorted_weight_link = sorted(self.weight_link,key=lambda x:x[2])
A = []
msp_w = 0
for wt_link in sorted_weight_link:
i,j,w = wt_link
u,v = self.node_list[i],self.node_list[j]
if root_node(u) != root_node(v):
make_union(u,v)
A.append((i,j))
msp_w += w
return A,msp_w
def prim_msp(self,r=0):
#default generate from node 0
print("--------------------prim alg")
for v in self.node_list:
v.l_w = np.inf
v.pi = None
self.node_list[0].l_w=0
self.node_list[0].pi = None
Q = self.node_list[:] #G-V
A = []
cost = 0
while Q:
Q = sorted(Q,key=lambda x:x.l_w)
Q_ids = [item._id for item in Q]
u = Q.pop(0) # the first item
A.append((u.pi,u._id))
cost += u.l_w
for w_linked in self.adj_list[u._id]:
j,w = w_linked
if self.node_list[j].l_w>w and j in Q_ids:
self.node_list[j].pi = u._id
self.node_list[j].l_w = w
return A,cost
class Graph_Direct_Weight(object):
def __init__(self,nodes):
self.node_list = [Node(i) for i in range(nodes)]
self.adj_list = [[] for i in range(nodes)]
def add_dw_edge(self,*args):
for wlink in args:
s,t,w = wlink
self.adj_list[s].append((t,w))
def bellman_ford(self,s):
""" s is the source id"""
for node in self.node_list:
node.dis = np.inf
node.pi = None
self.node_list[s].dis = 0
edges = [(i,j,w) for i,node_link in enumerate(self.adj_list) for j,w in node_link ]
def relax(u,v,w):
if self.node_list[v].dis>self.node_list[u].dis+w:
self.node_list[v].dis = self.node_list[u].dis +w
self.node_list[v].pi = u
for _ in self.node_list:
for wedge in edges:
i,j,w = wedge
relax(i,j,w)
for e in edges:
u,v,w = e
if self.node_list[v].dis >self.node_list[u].dis +w:
return False
return True
def path(self,v):
" to find the v distance and path"
#print path from target to source
path = deque()
node= self.node_list[v]
path.append(node._id)
while not node.pi is None:
node=self.node_list[node.pi]
path.appendleft(node._id)
return path
def simple_dag_shortestpath(self,s):
# $24.2 page 364
def relax(u,v,w):
if self.node_list[v].dis>self.node_list[u].dis+w:
self.node_list[v].dis = self.node_list[u].dis +w
self.node_list[v].pi = u
for node in self.node_list:
node.pi = None
node.dis = np.inf
self.node_list[s].dis = 0
for node in self.node_list:
for edge in self.adj_list[node._id]:
t,w = edge
relax(node._id,t,w)
dist = [node.dis for node in self.node_list]
return dist
def dijkstra(self,s):
# $24,3 Dijkstra alg
for node in self.node_list:
node.pi = None
node.dis = np.inf
self.node_list[s].dis =0
Q = self.node_list[:]
S = []
while Q:
Q = sorted(Q,key=lambda x:x.dis)
u = Q.pop(0)
S.append(u)
i = u._id
for j,w in self.adj_list[i]:
if self.node_list[j].dis > self.node_list[i].dis +w:
self.node_list[j].dis = self.node_list[i].dis+w
self.node_list[j].pi = self.node_list[i]._id
dist = [node.dis for node in self.node_list]
return dist
class Graph_Mat(object):
def __init__(self,nodes):
self.node_list = [Node(i) for i in range(nodes)]
self.adj_wmat = np.ones(shape=(nodes,nodes),dtype=np.int32)*np.inf
for i in range(nodes):
self.adj_wmat[i,i]=0
def add_wd_edge(self,*args):
for pair in args:
i,j,w = pair
self.adj_wmat[i,j]=w
def slow_all_pair_shortest_path(self):
# use dynamci plan to calculate the full path matrix
L = self.adj_wmat[:]
n = self.adj_wmat.shape[0]
def extend_shortest_path(L,w):
n = L.shape[0]
C = np.ones_like(L)*np.inf
for i in range(n):
for j in range(n):
for k in range(n):
C[i,j]= min(C[i,j],L[i,k]+w[k,j])
return C
print("before cal distance ,the matrix is \n ",self.adj_wmat)
for i in range(2,n):
L = extend_shortest_path(L,self.adj_wmat)
return L
def fast_all_pair_shortest_path(self):
pass
class Graph_MaxFlow(object):
def __init__(self,nodes):
self.node_list = [Node(i) for i in range(nodes)]
self.cap_matrix = np.zeros(shape=(nodes,nodes),dtype=np.int32)
self.flow = np.zeros_like(self.cap_matrix)
def add_cap_edge(self,*args):
for pair in args:
i,j,cap = pair
self.cap_matrix[i,j]= cap
def bfs_path(self,s,t):
"find the path and the f"
for node in self.node_list:
node.color = Color.white
node.pi = None
node.depth = 0
q = []
q.append(self.node_list[s])
self.node_list[s].color = Color.gray
while q:
node = q.pop(0)
i = node._id
for j,w in enumerate(self.cap_matrix[node._id]):
if self.cap_matrix[i,j]>0 and self.node_list[j].color == Color.white:
self.node_list[j].color = Color.gray
self.node_list[j].pi = i
self.node_list[j].depth = node.depth +1
q.append(self.node_list[j])
node.color = Color.black
#search finished
path = deque()
node = self.node_list[t]
c = np.inf
path.append(node._id)
while not node.pi is None:
npi = self.node_list[node.pi]
path.appendleft(npi._id)
c = min(c,self.cap_matrix[npi._id,node._id])
node =npi
if path[0] != s:
return None,0
else:
return path,c
def maxflow_ford_fulkerson(self,i,j):
# $ maxflow-mincut 26.5 example
path,c = self.bfs_path(0,3)
while path:
for i in range((len(path)-1)):
u,v = path[i],path[i+1]
self.flow[u,v] += c
self.flow[v,u] -= c
self.cap_matrix[u,v] -=c
self.cap_matrix[v,u] +=c
path, c = self.bfs_path(0, 3)
maxflow = sum(self.flow[0])
flow_matrix = self.flow
return maxflow,flow_matrix
if __name__=='__main__':
# test broad first search
graph = Graph(8)
graph.add_edge((0,1),(0,4),(1,5),(2,6),(3,7),(2,3),(5,6),(6,7),(2,5),(3,6))
print(graph)
graph.broad_first_search()
# test deep first search
graph = Graph(6)
graph.add_direct_edge((0,1),(0,3),(3,1),(4,3),(1,4),(2,4),(2,5),(5,5))
graph.deep_first_search()
# test toplogy sort
graph.top_sort()
# test kruskal_minmum_span tree
graph = GraphWeight(9)
graph.add_node_weight((0,1,4),(1,2,8),(2,3,7),(3,4,9),(4,5,10),(5,6,2),(6,7,1),(7,8,7),(7,0,8))
graph.add_node_weight((1,7,11),(2,8,2),(2,5,4),(3,5,14),(6,8,6))
A,w = graph.kruskal_msp()
print(A,w)
# test prim minum_span tree
graph2 = graph
A,cost = graph2.prim_msp()
print("output of prim algrithm")
print(A,cost)
# test bellman ford single source min distance
print("test the path search in weighted graph")
graph = Graph_Direct_Weight(5)
graph.add_dw_edge((0,1,6),(0,4,7),(1,2,5),(1,3,-4),(1,4,8),(2,1,-2),(3,2,7),(3,0,2),(4,2,-3),(4,3,9))
graph.bellman_ford(0)
print("cal dis")
path_id = graph.path(3)
print("get path")
print(path_id)
# test directed no cycle single source shortest path
print("test the simple single source no cycle shortest path")
graph = Graph_Direct_Weight(6)
graph.add_dw_edge((0,1,5),(0,2,3),(1,2,2),(1,3,6),(2,4,4),(2,5,2),(3,4,-1),(3,5,1),(4,5,-2))
dist = graph.simple_dag_shortestpath(1)
print(dist)
# test dijkstra alg
print("test the dijkstra alg")
graph = Graph_Direct_Weight(5)
graph.add_dw_edge((0,1,10),(0,4,5),(1,2,1),(1,4,2),(2,3,4),(3,0,7),(3,2,6),(4,1,3),(4,3,9),(4,3,2))
dist = graph.dijkstra(0)
print(dist)
# test the full path shortest search
print("test the full path shortest search")
graph = Graph_Mat(5)
graph.add_wd_edge((0,1,3),(0,2,8),(0,4,-4),(1,3,1),(1,4,7),(2,1,4),(3,2,-5),(3,0,2),(4,3,6))
L = graph.slow_all_pair_shortest_path()
print(L)
# test the maxflow alg
graph = Graph_MaxFlow(6)
graph.add_cap_edge((0,1,16),(0,5,13),(1,2,12),(1,5,10),(2,5,9),(2,3,20),(4,2,7),(4,3,4),(5,4,14),(5,1,4))
maxflow,flow_matrix = graph.maxflow_ford_fulkerson(0,3)
print("max flow \n",maxflow,"\n",flow_matrix)