Skip to content

Commit 9af1c8e

Browse files
author
Hamid Gasmi
committed
#150 is completed with an DP exact algorithm
1 parent 25a33e9 commit 9af1c8e

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

4-np-complete-problems/2-coping_with_np_completeness/travelling_salesman_school_bus.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# python3
21
from itertools import permutations
32

43
INF = 10 ** 9
5-
64
# Analysis:
75
# Input data structure: a weighted graph
86
# ... Bus depot, children homes and the school are represented with vertices
@@ -75,7 +73,7 @@ def optimal_path(w):
7573
map_v_bit_position = dict()
7674
for i in range(n):
7775
map_v_bit_position[b] = i
78-
b *= 2
76+
b = b << 1
7977

8078
# C(S, i)
8179
C = dict()
@@ -87,8 +85,8 @@ def optimal_path(w):
8785
C_S = C[s]
8886

8987
i = 1
90-
while i * 2 <= s: # for all vertices i in s
91-
i *= 2
88+
while (i << 1) <= s: # for all vertices i in s
89+
i = i << 1
9290
v_i = map_v_bit_position[i]
9391

9492
if i & s == 0: # the vertice i isn't included in s
@@ -107,12 +105,12 @@ def optimal_path(w):
107105
while j <= s_minus_i: #for all vertices j in s (j != i):
108106

109107
if s_minus_i & j == 0: # the vertice j isn't included in s_minus_i
110-
j *= 2
108+
j = j << 1
111109
continue
112110

113111
v_j = map_v_bit_position[j]
114112
C_S[v_i] = min(C_S[v_i], C_S_minus_i[v_j] + w[v_j][v_i])
115-
j *= 2
113+
j = j << 1
116114

117115
return shortest_path(w, S, C)
118116

@@ -160,8 +158,6 @@ def read_data():
160158

161159
return graph
162160

163-
164-
165161
if __name__ == '__main__':
166162
print_answer(*optimal_path(read_data()))
167163
#print_answer(*optimal_path_naive(read_data()))

0 commit comments

Comments
 (0)