-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdfs.py
39 lines (31 loc) ยท 899 Bytes
/
dfs.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
import sys
dfs = []
dist = {}
compare = 0
def DFS(current_station, sum, s, cost, station_info):
global compare
if not current_station in dist:
dist[current_station] = sys.maxsize
if dist[current_station] < sum:
return
else:
dist[current_station] = sum
if sum > cost:
return
compare += 1
for next_station in station_info[current_station]['time'].keys():
compare += 1
if not next_station in dfs:
dfs.append(next_station)
DFS(next_station,sum + station_info[current_station]['time'][next_station],s+next_station,cost,station_info)
dfs.remove(next_station)
def start(start, station_info):
global dfs
global dist
dfs = []
dist = {}
cost = sys.maxsize
dist[start] = cost
dfs.append(start)
DFS(start, 0, " ", cost, station_info)
return dist, compare