-
Notifications
You must be signed in to change notification settings - Fork 3
/
GraphUtils.py
65 lines (53 loc) · 2.16 KB
/
GraphUtils.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
60
61
62
63
64
65
from Graph import *
from Path import *
# djikstra's algorithm starting at "source"; Returns a list of Path objects where list[i] is the shortest path from source to vertex i
def djikstra(graph, source):
q = set() # set of unvisited nodes
dist = dict() # dist[i] is distance from source to vertex i
prev = dict() # prev[i] is the previous node in the optimal path from source to i
# foreach vertex in the graph
for vertex in range(0, graph.num_vertices):
dist[vertex] = math.inf
prev[vertex] = None
q.add(vertex)
dist[source] = 0 # duh
while len(q) > 0:
min_vertex = None
for vertex in q:
if min_vertex is None:
min_vertex = vertex
elif dist[vertex] < dist[min_vertex]:
min_vertex = vertex
if min_vertex is None:
break
q.remove(min_vertex)
for neighbor in graph.get_neighbors(min_vertex):
alternate = dist[min_vertex] + graph.get_weight(min_vertex, neighbor)
if alternate < dist[neighbor]:
dist[neighbor] = alternate
prev[neighbor] = min_vertex
# at this point, we will have discovered the shortest paths from source to every other node
# now we'll walk backwards along each path to construct the Path objects
shortest_paths = dict()
for vertex in range(0, graph.num_vertices):
path_to_vertex = []
node_along_path = vertex
while prev[node_along_path] is not None:
path_to_vertex.insert(0, prev[node_along_path])
node_along_path = prev[node_along_path]
path = Path(graph)
for node in path_to_vertex:
path.extend(node)
path.extend(vertex)
shortest_paths[vertex] = path
return shortest_paths
# breadth-first search over the specified graph starting at vertex with index "start"
def bfs(graph, start):
visited, queue = set(), [start]
while queue:
vertex = queue.pop(0)
if vertex not in visited:
visited.add(vertex)
neighbors = graph.get_neighbors(vertex)
queue.extend(neighbors - visited)
return visited