-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphAlgorithms.py
More file actions
executable file
·285 lines (209 loc) · 6.93 KB
/
Copy pathgraphAlgorithms.py
File metadata and controls
executable file
·285 lines (209 loc) · 6.93 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Discussed in section 9.
# Implements the random contraction algorithm
from adjacencyListGraph import *
import random
import sys
import Queue
import numpy as np
from copy import deepcopy
import heapq
def shortestPath(start, goal):
'''
Use breadth-first search and keep track of minimum distance of each
explored node from starting node. Terminate when goal node is reached.
Return: number of edges between node start and node goal.
If these nodes are unconnected, returns float('inf').
'''
vertexqueue = Queue.Queue()
vertexqueue.put(start)
exploredVerts = [start]
dist = {start: 0}
if start == goal:
return dist[start]
while vertexqueue.empty() != True:
v = vertexqueue.get()
vDVertices = v.getDirectVertices()
for vert in vDVertices:
if vert not in exploredVerts:
exploredVerts.append(vert)
vertexqueue.put(vert)
dist[vert] = dist[v] + 1
if vert == goal:
return dist[vert]
return float("inf")
def RandomContract(g):
'''
Takes a graph g and executes uniformly selected random cuts on it
iteratively, yielding the minimum cut with 1/n^2 probability.
'''
while len(g.getVertices()) > 2:
allEdges = g.getEdges()
try:
e = random.choice(allEdges)
except IndexError:
print "ERROR: All edges removed before vertex counted reached 2. "\
"This implies the graph was disconnected."
raise
g.mergeEdge(e)
def nRandomContracts(g, n=1000):
'''
Runs RandomContract on a copy of g, n times.
Returns the minimum cut found (lowest number of edges in the final graph.)
over all n runs.
'''
mincut = len(g.getEdges())
for i in range(n):
h = deepcopy(g)
RandomContract(h)
mincut = min([mincut, len(h.getEdges())])
return mincut
def DFS_loop(g):
'''
Driver for depth-first search finder of strongly connected components
(Kosaraju's Two-Pass Algorithm). Needs some work doing in global scope,
e.g. the following will print the top 5 most populated SSCs:
t = 0
s = 0
h = g.reverseDirectedGraph()
_, finish_time = DFS_loop(h)
del h
g = g.reorderVertices(finish_time)
leader, _ = DFS_loop(g)
N = len(g.getVertices())
counts = []
for i in range(N):
counts.append(leader.count(i))
counts.sort(reverse=True)
print counts[:5]
'''
global s
N = len(g.getVertices())
# List of explored nodes by IDX (value-1)
explored = np.zeros(N, dtype=bool)
leader = [-1 for i in range(N)]
finish_time = np.zeros(N, dtype=int)
for i in range(N - 1, -1, -1):
if not explored[i]:
s = i
DFS_it(g, i, explored, leader, finish_time)
return leader, finish_time
def DFS(g, i, explored, leader, finish_time):
''' Recursive implementation of DFS for finding SSCs
'''
global t
global s
explored[i] = True
leader[i] = s
tail_vert = g.getVertices()[i]
head_verts = tail_vert.getDirectVertices()
for head_vert in head_verts:
j = head_vert.getValue() - 1
if not explored[j]:
DFS(g, j, explored, leader, finish_time)
t += 1
finish_time[i] = t
def DFS_it(g, start_idx, explored, leader, finish_time):
''' Iterative implementation of DFS for finding SSCs
'''
global t
global s
vertices = g.getVertices()
stack = [vertices[start_idx]]
while len(stack) > 0:
j_vert = stack.pop()
j = j_vert.getValue() - 1
j_direct_verts = j_vert.getDirectVertices()
if finish_time[j] == 0:
if not explored[j]:
explored[j] = True
leader[j] = s
toappend = []
for j_direct_vert in j_direct_verts:
if not explored[(j_direct_vert.getValue() - 1)]:
toappend.append(j_direct_vert)
if len(toappend) == 0:
t += 1
finish_time[j] = t
else:
stack.append(j_vert)
stack += toappend
def Dijkstra_Shortest_Path(g, start_idx):
''' Heap implementation of Dijkstra's shortest path algorithm for exploring
graphs with weighted paths.
'''
vertices = g.getVertices()
N = len(vertices)
start = vertices[start_idx]
shortest = np.zeros([N], dtype=int)
# Initialise heap
h = []
# map vertex idx to entry on heap
vertex_finder = {}
for vert_idx in range(N):
vert_partners, lengths = vertices[vert_idx].getDirectVertices()
try:
dist = lengths[vert_partners.index(start)]
except ValueError:
dist = sys.maxint
entry = [dist, vert_idx]
heapq.heappush(h, entry)
vertex_finder[vert_idx] = entry
while h:
# Extract-min and add to shortest
nearest_dist, nearest_vert_idx = heapq.heappop(h)
if nearest_vert_idx != -1:
shortest[nearest_vert_idx] = nearest_dist
# Update heap
neighs = zip(*vertices[nearest_vert_idx].getDirectVertices())
for neigh_vert, neigh_length in neighs:
neigh_idx = neigh_vert.getValue() - 1
if shortest[neigh_idx] != 0:
continue
neigh_new_length = nearest_dist + neigh_length
entry = vertex_finder[neigh_idx]
neigh_old_length = entry[0]
entry[1] = -1
entry[0] = -1
if neigh_new_length < neigh_old_length:
new_entry = [neigh_new_length, neigh_idx]
else:
new_entry = [neigh_old_length, neigh_idx]
heapq.heappush(h, new_entry)
vertex_finder[neigh_idx] = new_entry
return shortest
if __name__ == "__main__":
try:
filename = sys.argv[1]
except IndexError:
print "Please pass the name of the file containing an adjacency list "\
"for a graph as the first argument."
raise
############################
# For depth-first search #
############################
# g = fromFileType2(filename)
# print "Read-in complete."
# t = 0
# s = 0
# h = g.reverseDirectedGraph()
# _, finish_time = DFS_loop(h)
# del h
# print "finish_time obtained."
# g = g.reorderVertices(finish_time)
# print "Calculating leader"
# leader, _ = DFS_loop(g)
# print leader
# N = len(g.getVertices())
# counts = []
# for i in range(N):
# counts.append(leader.count(i))
# counts.sort(reverse=True)
# print counts[:5]
###################################
# For shortest-path computation #
###################################
g = fromFileType3(filename)
print g
shortest = Dijkstra_Shortest_Path(g, 0)