Skip to content

Commit 3eadd0b

Browse files
author
Hamid Gasmi
committed
#150 test cases added
1 parent 325373d commit 3eadd0b

File tree

11 files changed

+53
-27
lines changed

11 files changed

+53
-27
lines changed

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

+37-22
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,38 @@ def shortest_path(w, S, C):
3838
#print("C: ", C)
3939
#print("S, C[S]: ", S, C[S])
4040

41-
path = []
42-
path.append(1)
43-
path_length = INF
44-
41+
shortest_path_length = INF
42+
for i in C[S]:
43+
if C[S][i] + w[i][0] >= shortest_path_length:
44+
continue
45+
shortest_path_length = C[S][i] + w[i][0]
46+
47+
if shortest_path_length == INF:
48+
return -1, []
49+
50+
shortest_path = []
4551
while S > 0:
46-
prev_v = 1
52+
prev_v = 0
53+
curr_v = -1
54+
path_length = INF
4755
for i in C[S]:
48-
if C[S][i] + w[i][1] >= path_length:
56+
if C[S][i] + w[i][prev_v] >= path_length:
4957
continue
58+
59+
path_length = C[S][i] + w[i][prev_v]
60+
curr_v = i
5061

51-
path_length = C[S][i] + w[i][v]
52-
v = -1
53-
54-
55-
56-
57-
return -1 if path_length == INF else path_length, path
62+
if path_length == INF:
63+
break
64+
65+
#print("C[S], curr_v, path_length: ", C[S], curr_v, path_length)
66+
shortest_path.append(curr_v + 1)
67+
S = S ^ (1 << curr_v)
68+
prev_v = curr_v
69+
#print("S', path: ", S, shortest_path)
70+
71+
shortest_path.append(1)
72+
return shortest_path_length, shortest_path[::-1]
5873

5974
def optimal_path(w):
6075

@@ -105,16 +120,16 @@ def optimal_path(w):
105120
C_S[v_i] = min(C_S[v_i], C_S_minus_i[v_j] + w[v_j][v_i])
106121
j *= 2
107122
#print(C)
108-
print(C[S])
109-
path_length = INF
110-
for i in C[S]:
111-
if C[S][i] + w[i][1] >= path_length:
112-
continue
123+
#print(C[S])
124+
#path_length = INF
125+
#for i in C[S]:
126+
# if C[S][i] + w[i][1] >= path_length:
127+
# continue
113128

114-
path_length = C[S][i] + w[i][0]
129+
# path_length = C[S][i] + w[i][0]
115130

116-
print(path_length)
117-
#return shortest_path(w, S, C)
131+
#print(path_length)
132+
return shortest_path(w, S, C)
118133

119134
def optimal_path_naive(graph):
120135
# This solution tries all the possible sequences of stops.
@@ -163,5 +178,5 @@ def read_data():
163178

164179

165180
if __name__ == '__main__':
166-
optimal_path(read_data())
181+
print_answer(*optimal_path(read_data()))
167182
#print_answer(*optimal_path_naive(read_data()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
97
2-
1 4 3 2
1+
40
2+
1 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
40
2+
1 2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
40
2-
1 2
3-
1+
97
2+
1 4 3 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
97
2+
1 4 3 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
16
2+
1 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
16
2+
1 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-1

0 commit comments

Comments
 (0)