-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstras_faster
More file actions
200 lines (146 loc) · 3.91 KB
/
dijkstras_faster
File metadata and controls
200 lines (146 loc) · 3.91 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
import time
#!/usr/bin/python
#
# Part 1: Read file, assign to giant list
#
# each list element contains a vertex number,
listSize = 1000
file = open("1000.txt") #open the text file
line = file.readline() #discard the 1st line
line = file.readline() #this is the 1st valid line
print "Setting up for Dijkstras, timing doesn't start" \
" until entry to algorithm"
Q = [[i] for i in range(listSize)]
R = [i for i in range(listSize)]
#create a list of length 1000
def retreiveindex(x):
return R[x]
y = 0
for i in Q:
if i[0] == x:
return y
y += 1
return -1
for x in range(listSize):
Q[x].append(-1)
Q[x].append(None)
while 1:
if not line:
#print "EOF"
break #check for EOF
elif line.strip(): #check for empty line
#print "not an empty line"
#print r.strip()
while line.find(" ") != 0:
#must be a vertex u
x = int(line.strip())
# print x
line = file.readline()
#load the next token
while (line.find(" ") == 0) and (line.strip() != ' '):
#
y= line.split()
y[0],y[1]= int(y[0]), int(y[1])
Q[int(x)].append(y)
line = file.readline()
line = file.readline()
#loading line for next iter
break
Q[0][1] = 0
for i in Q:
if len(i)>3:
for x in i[3:]:
if x[0] > i[0]:
Q[x[0]].append([i[0],x[1]])
#print list
#
# End Part 1
#
def dijkstras(Q): #queue is already assigned
print "going into dijkstras, timing started now"
s = []
def rchild(i):
return (i+i) + 2
def lchild(i):
return (i+i) + 1
def parent(i):
return (i - 1)/2
def heapDecrKey(i, key):
if key == -1:
print "error, key is -1"
if key > Q[i][1] != -1:
print "key is larger than current value"
return
Q[i][1] = key
p = parent(i)
while (i > 0) and (Q[p][1] > Q[i][1] or Q[p][1] == -1):
R[Q[i][0]],R[Q[p][0]] = R[Q[p][0]], R[Q[i][0]]
Q[i], Q[p] = Q[p], Q[i]
i = p
p = parent(i)
def extractMin(): #extract root, exchange for bottom element, sift down
if len(Q) < 1:
"error"
min = Q[0]
R[Q[len(Q)-1][0]]=0
Q[0], Q[len(Q) - 1] = Q[len(Q) - 1], Q[0]
del Q[len(Q) - 1]
minHeapify(Q, 0)
return min
def relax(u, v, w):
if (w == -1) or u[1] == -1:
return
if (v[1] == -1) or (v[1] > u[1] + w):
v[2] = u[0]
r=retreiveindex(v[0])
if r != -1: heapDecrKey(r, u[1] + w)
def minHeapify(A, i):
l = lchild(i)
r = rchild(i)
if (l < len(A)):
if A[l][1] < A[i][1]:
if A[l][1] != -1:
smallest = l
else: smallest = i
elif A[i][1] == -1 and A[l][1] != -1:
smallest = l
else: smallest = i
else:
smallest = i
if (r < len(A)):
if A[r][1] < A[smallest][1]:
if A[r][1] != -1:
smallest = r
else: None
elif A[smallest][1] == -1 and A[r][1] != -1:
smallest = r
else: None
else:
None
if i != smallest:
R[A[i][0]], R[A[smallest][0]] = R[A[smallest][0]], R[A[i][0]]
A[i], A[smallest] = A[smallest], A[i]
minHeapify(A, smallest)
while len(Q) > 0:
u = extractMin()
R[u[0]] = -1
s.append(u)
if len(u) > 3:
i = 3
for v in u[3:]:
r=retreiveindex(v[0])
if r !=-1: relax(u, Q[r], u[i][1])
i += 1
return s
start = time.time()
s=dijkstras(Q)
i=0
z =0
for x in s:
i+=x[1]
#print x[0], x[1], z
#z+=1
end =time.time() - start
print end
print i
#for x in s: None