-
Notifications
You must be signed in to change notification settings - Fork 3
/
quick_point.py
128 lines (106 loc) ยท 4.28 KB
/
quick_point.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
import sys
import dijkstra
import SPFA
import dfs
import time
# ์ต์๊ฐ์ด ์ฌ๋ฌ๊ฐ์ผ ๋ ๋ชจ๋ ์ ์ฅํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํ
def dozen_min(args):
min_result = [] # ์ฌ๋ฌ ์ญ๋ค์ ์ ์ฅํ ๋ฐฐ์ด
minimum = min(args.values()) # value ์ต์๊ฐ ์ ์ฅ
for station in args:
if args[station] == minimum:
min_result.append(station)
return min_result # value๊ฐ ๊ฐ์ฅ ์์ ์ญ๋ค ๋ชจ๋ ์ ์ฅํ ๋ฐฐ์ด ๋ฐํ
# ๋์์ ์ถ๋ฐํ์ ๋ ๋ชจ๋ ๊ตฌ์ฑ์์ ๋์ฐฉ ์ง์ ์ด ๊ฐ์ฅ ๋น ๋ฅธ ์ง์
def find_middle_point(search_station, user_info, distance_result):
# ๊ฐ ์ญ๋ณ๋ก user๊น์ง ๊ฑฐ๋ฆฌ list ์์ฑ
# ex. '๊ฐ๋ฅ': [115, 19, 87, 107]
each_user_distance = {}
for station in search_station:
user_distance = []
for j in range(len(user_info)):
user_distance.append(distance_result[j][station])
each_user_distance[station] = user_distance
# ๊ฐ ์ญ๋ณ๋ก '์์์๊ฐ(user์ distance ์ค ๊ฐ์ฅ ํฐ ๊ฐ)' ๊ตฌํ๊ธฐ
# ex. '๊ฐ๋ฅ': 115
max_distance = {}
for station in search_station:
max_distance[station] = max(each_user_distance[station]) # ์์์๊ฐ
# ์์์๊ฐ์ด ๊ฐ์ฅ ์์ ์ญ๋ค ๊ตฌํ๊ธฐ
quick_station = dozen_min(max_distance)
all_sum = {}
# quick_station์ ์ ์ฅ๋ ๊ฐ๋ค ๋ชจ๋ ๋ํด์
for i in quick_station:
all_sum[i] = sum(each_user_distance[i])
print(all_sum)
min_sum = min(all_sum.values()) # ํฉ์ด ๊ฐ์ฅ ์์ ์ญ์ด ๋ง๋ ์ญ
for meet_station in all_sum:
if min_sum != all_sum[meet_station]:
continue
# ์ถ๋ ฅ
print("===============================")
print("์ค๊ฐ์ง์ ์ญ:", meet_station)
i = 0
for name in user_info.keys():
print(name + "(" + user_info[name] + ") :", each_user_distance[meet_station][i])
#for j in range(0, len(path_result[i][meet_station]) - 1):
# print(path_result[i][meet_station][len(path_result[i][meet_station]) - j - 1], end='->')
#print(path_result[i][meet_station][0] + "]")
i += 1
def dijkstra_quickpoint(station_info, user_info):
compare = 0
start = time.time()
# ํ์ํ ์ญ ์ด๋ฆ ์ ์ฅ
search_station = list(station_info.keys())
# user๋ณ dijkstra ์ํ
distance_result = []
#path_result = []
for i in user_info:
distance, path, search_station, compare = dijkstra.dijkstra(user_info[i], station_info, user_info, search_station, compare)
distance_result.append(distance)
#path_result.append(path)
# print(search_station)
find_middle_point(search_station, user_info, distance_result)
end = time.time()
print('dijkstra', end-start, 'sec')
print('dijkstra compare', compare)
def SPFA_quickpoint(station_info, user_info):
compare = 0
start = time.time()
# ํ์ํ ์ญ ์ด๋ฆ ์ ์ฅ
search_station = list(station_info.keys())
# user๋ณ SPFA ์ํ
distance_result = []
for i in user_info:
distance, search_station, compare = SPFA.SPFA(user_info[i], station_info, user_info, search_station, compare)
distance_result.append(distance)
# print(search_station)
find_middle_point(search_station, user_info, distance_result)
end = time.time()
print('SPFA', end-start, 'sec')
print('SPFA compare', compare)
def DFS_quickpoint(station_info, user_info):
total_compare = 0
start = time.time()
# ํ์ํ ์ญ ์ด๋ฆ ์ ์ฅ
search_station = list(station_info.keys())
# user๋ณ DFS ์ํ
distance_result = []
for i in user_info:
distance, compare = dfs.start(user_info[i], station_info)
distance_result.append(distance)
total_compare += compare
max = 0
for j in user_info:
if distance[user_info[j]] > max:
max = distance[user_info[j]]
new_search_station = []
for station_name in distance:
if distance[station_name] <= max:
if station_name in search_station:
new_search_station.append(station_name)
search_station = new_search_station
find_middle_point(search_station, user_info, distance_result)
end = time.time()
print('DFS', end-start, 'sec')
print('DFS compare', total_compare)