-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSC162_LAB16_SBK.py
123 lines (96 loc) · 2.85 KB
/
CSC162_LAB16_SBK.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
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
# sarah kingan
# CSC162, summer2015
# Lab 16
#
# Using breadth first search write an algorithm that can
# determine the shortest path from each vertex to every
# other vertex. This is called the all pairs shortest path
# problem.
#
# some code modified from Problem Solving with Algorithms
# and Data Structures, By Brad Miller and David Ranum
#
# http://www.codeskulptor.org/#user40_x9mT4tViDU_5.py
#
class Vertex:
def __init__(self,key):
self.id = key
self.connectedTo = {}
def addNeighbor(self,nbr,weight=0):
self.connectedTo[nbr] = weight
def __str__(self):
return str(self.id)
# return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo])
def getConnections(self):
return self.connectedTo.keys()
def getId(self):
return self.id
def getWeight(self,nbr):
return self.connectedTo[nbr]
class Graph:
def __init__(self):
self.vertList = {}
self.numVertices = 0
def addVertex(self,key):
self.numVertices = self.numVertices + 1
newVertex = Vertex(key)
self.vertList[key] = newVertex
return newVertex
def getVertex(self,n):
if n in self.vertList:
return self.vertList[n]
else:
return None
def __contains__(self,n):
return n in self.vertList
def addEdge(self,f,t,cost=0):
if f not in self.vertList:
nv = self.addVertex(f)
if t not in self.vertList:
nv = self.addVertex(t)
self.vertList[f].addNeighbor(self.vertList[t], cost)
def getVertices(self):
return self.vertList.keys()
def __iter__(self):
return (x for x in self.vertList.values())
def FloydWarshall(graph):
M = weightMatrix(graph)
V = len(M[0])
for k in range(V):
for i in range (V):
for j in range (V):
if M[i][j] > M[i][k] + M[k][j]:
M[i][j] = M[i][k] + M[k][j]
return M
def weightMatrix(graph):
# initialize matrix with infinity edges
V = len(graph.getVertices())
M = [[float("inf") for i in range(V)] for j in range(V)]
# add 0 for self-self
for i in range(V):
for j in range(V):
if i == j:
M[i][j] = 0
# import weights from graph object
for v in graph:
for adj in v.getConnections():
M[int(v.getId())][int(adj.getId())] = v.getWeight(adj)
return M
# make graph
g = Graph()
for i in range(6):
g.addVertex(i)
g.addEdge(0,1,5)
g.addEdge(0,5,2)
g.addEdge(1,2,4)
g.addEdge(2,3,9)
g.addEdge(3,5,3)
g.addEdge(3,4,7)
g.addEdge(4,0,1)
g.addEdge(5,4,8)
g.addEdge(5,2,1)
# compute minimum pairwise distances with Floyd Warshall algorith
dist = FloydWarshall(g)
# print distance matrix
for i in range(len(dist[0])):
print dist[i]