Files
  • main.py
main.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
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
import sys
import os,copy

class PriorityQueue:
    def __init__(self):
        self.heapArray = [(0,0)]
        self.currentSize = 0

    def buildHeap(self,alist):
        self.currentSize = len(alist)
        self.heapArray = [(0,0)]
        for i in alist:
            self.heapArray.append(i)
        i = len(alist) // 2
        while (i > 0):
            self.percDown(i)
            i = i - 1

    def percDown(self,i):
        while (i * 2) <= self.currentSize:
            mc = self.minChild(i)
            if self.heapArray[i][0] > self.heapArray[mc][0]:
                tmp = self.heapArray[i]
                self.heapArray[i] = self.heapArray[mc]
                self.heapArray[mc] = tmp
            i = mc

    def minChild(self,i):
        if i*2 > self.currentSize:
            return -1
        else:
            if i*2 + 1 > self.currentSize:
                return i*2
            else:
                if self.heapArray[i*2][0] < self.heapArray[i*2+1][0]:
                    return i*2
                else:
                    return i*2+1

    def percUp(self,i):
        while i // 2 > 0:
            if self.heapArray[i][0] < self.heapArray[i//2][0]:
               tmp = self.heapArray[i//2]
               self.heapArray[i//2] = self.heapArray[i]
               self.heapArray[i] = tmp
            i = i//2

    def add(self,k):
        self.heapArray.append(k)
        self.currentSize = self.currentSize + 1
        self.percUp(self.currentSize)

    def delMin(self):
        retval = self.heapArray[1][1]
        self.heapArray[1] = self.heapArray[self.currentSize]
        self.currentSize = self.currentSize - 1
        self.heapArray.pop()
        self.percDown(1)
        return retval

    def isEmpty(self):
        if self.currentSize == 0:
            return True
        else:
            return False

    def decreaseKey(self,val,amt):
        # this is a little wierd, but we need to find the heap thing to decrease by
        # looking at its value
        done = False
        i = 1
        myKey = 0
        while not done and i <= self.currentSize:
            if self.heapArray[i][1] == val:
                done = True
                myKey = i
            else:
                i = i + 1
        if myKey > 0:
            self.heapArray[myKey] = (amt,self.heapArray[myKey][1])
            self.percUp(myKey)

    def __contains__(self,vtx):
        for pair in self.heapArray:
            if pair[1] == vtx:
                return True
        return False

class Graph:
    def __init__(self):
        self.vertices = {}
        self.numVertices = 0

    def addVertex(self,key):
        self.numVertices = self.numVertices + 1
        newVertex = Vertex(key)
        self.vertices[key] = newVertex
        return newVertex

    def addVertexName(self,key,name):
        self.vertices[key].name = name

    def getVertexName(self,key):
        return (self.vertices[key].name)

    def getVertex(self,n):
        if n in self.vertices:
            return self.vertices[n]
        else:
            return None

    def __contains__(self,n):
        return n in self.vertices

    def addEdge(self,f,t,cost=0):
            if f not in self.vertices:
                nv = self.addVertex(f)
            if t not in self.vertices:
                nv = self.addVertex(t)
            self.vertices[f].addNeighbor(self.vertices[t],cost)

    def getVertices(self):
        return list(self.vertices.keys())

    def __iter__(self):
        return iter(self.vertices.values())

class Vertex:
    def __init__(self,num):
        self.id = num
        self.connectedTo = {}
        self.color = 'white'
        self.dist = sys.maxsize
        self.pred = None
        self.disc = 0
        self.fin = 0
        self.name = ''

    # def __lt__(self,o):
    #     return self.id < o.id

    def addNeighbor(self,nbr,weight=0):
        self.connectedTo[nbr] = weight

    def setColor(self,color):
        self.color = color

    def setDistance(self,d):
        self.dist = d

    def setPred(self,p):
        self.pred = p

    def setDiscovery(self,dtime):
        self.disc = dtime

    def setFinish(self,ftime):
        self.fin = ftime

    def getFinish(self):
        return self.fin

    def getDiscovery(self):
        return self.disc

    def getPred(self):
        return self.pred

    def getDistance(self):
        return self.dist

    def getColor(self):
        return self.color

    def getConnections(self):
        return self.connectedTo.keys()

    def getWeight(self,nbr):
        return self.connectedTo[nbr]

    def __str__(self):
        return str(self.id) + ":color " + self.color + ":disc " + str(self.disc) + ":fin " + str(self.fin) + ":dist " + str(self.dist) + ":pred \n\t[" + str(self.pred)+ "]\n"

    def getId(self):
        return self.id

def dijkstra(aGraph,start,end,node_cst=None):
    pq = PriorityQueue()
    start.setDistance(0)
    pq.buildHeap([(v.getDistance(),v) for v in aGraph])
    while not pq.isEmpty():
        currentVert = pq.delMin()
        if currentVert.id == end.id:
            break
        for nextVert in currentVert.getConnections():
            newDist = currentVert.getDistance() + currentVert.getWeight(nextVert)
            if newDist < nextVert.getDistance() and (currentVert.id != node_cst.id) :
                nextVert.setDistance( newDist )
                nextVert.setPred(currentVert.id)
                pq.decreaseKey(nextVert,newDist)

    path = []
    pred = end.pred
    path.insert(0,end.name)
    while True:
        path.insert(0,aGraph.getVertexName(pred))
        if pred == start.id:
            return list(path)
            break
        pred = aGraph.getVertex(pred).pred


g=Graph()

for i in range (16):
    g.addVertex(i)

g.addEdge(0,1,10)
g.addEdge(1,0,10)
g.addEdge(0,1,10)
g.addEdge(1,0,10)
g.addEdge(0,8,10)
g.addEdge(8,0,10)
g.addEdge(0,5,10)
g.addEdge(5,0,10)
g.addEdge(5,6,10)
g.addEdge(6,5,10)
g.addEdge(6,8,10)
g.addEdge(8,6,10)
g.addEdge(8,9,10)
g.addEdge(9,8,10)
g.addEdge(8,11,10)
g.addEdge(11,8,10)
g.addEdge(11,13,10)
g.addEdge(13,11,10)
g.addEdge(13,9,10)
g.addEdge(9,13,10)
g.addEdge(13,14,10)
g.addEdge(14,13,10)
g.addEdge(1,9,100)
g.addEdge(9,1,100)
g.addEdge(1,2,100)
g.addEdge(2,1,100)
g.addEdge(9,14,100)
g.addEdge(14,9,100)
g.addEdge(2,9,10)
g.addEdge(9,2,10)
g.addEdge(9,10,10)
g.addEdge(10,9,10)
g.addEdge(2,3,10)
g.addEdge(3,2,10)
g.addEdge(3,4,10)
g.addEdge(4,3,10)
g.addEdge(2,7,10)
g.addEdge(7,2,10)
g.addEdge(3,7,10)
g.addEdge(7,3,10)
g.addEdge(4,7,10)
g.addEdge(7,4,10)
g.addEdge(7,10,10)
g.addEdge(10,7,10)
g.addEdge(12,10,10)
g.addEdge(10,12,10)
g.addEdge(14,12,10)
g.addEdge(12,14,10)
g.addEdge(12,15,10)
g.addEdge(15,12,10)
g.addEdge(10,15,10)
g.addEdge(15,10,10)
g.addVertexName(0,'SEA')
g.addVertexName(1,'MIN')
g.addVertexName(2,'CHI')
g.addVertexName(3,'ALB')
g.addVertexName(4,'BOS')
g.addVertexName(5,'POR')
g.addVertexName(6,'SFC')
g.addVertexName(7,'NYC')
g.addVertexName(8,'SJC')
g.addVertexName(9,'KCY')
g.addVertexName(10,'WDC')
g.addVertexName(11,'LAX')
g.addVertexName(12,'ATL')
g.addVertexName(13,'SAN')
g.addVertexName(14,'HST')
g.addVertexName(15,'MIA')

print (dijkstra(g,g.vertices[0],g.vertices[15],g.vertices[9]))