You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- TSP is a touchstone for many general heuristics devised for combinatorial optimization such as [genetic algorithms](https://en.wikipedia.org/wiki/Genetic_algorithm"Genetic algorithm"), [simulated annealing](https://en.wikipedia.org/wiki/Simulated_annealing"Simulated annealing"), [tabu search](https://en.wikipedia.org/wiki/Tabu_search"Tabu search"), [ant colony optimization](https://en.wikipedia.org/wiki/Ant_colony_optimization"Ant colony optimization"), [river formation dynamics](https://en.wikipedia.org/w/index.php?title=River_formation_dynamics&action=edit&redlink=1"River formation dynamics (page does not exist)") (see [swarm intelligence](https://en.wikipedia.org/wiki/Swarm_intelligence"Swarm intelligence")), and the [cross entropy method](https://en.wikipedia.org/wiki/Cross_entropy_method"Cross entropy method").
9
10
- DP (recursive)
10
11
- DP + Bit mask
11
-
- Held-Karp 알고리즘
12
+
- Held-Karp 알고리즘
12
13
- `dp[current][visited]`
13
14
- 현재 도시가 current이고, visited는 지금까지 방문한 도시의 집합
14
15
- visited는 비트마스크 (0 ~ 2ⁿ - 1)로 표현
15
16
- ```peusudo
16
-
function tsp(current, visited):
17
-
// 모든 조합이라는 것은 다 돌았다는 뜻
18
-
if visited == all_visited_mask((1 << N) - 1):
19
-
// 마지막에서 처음으로 가는 비용이 있다는 것은 돌아 갈 수 있다는 말
20
-
if cost[current][start] > 0:
21
-
return cost[current][start] // 돌아가는 비용
17
+
function TSP(u, mask):
18
+
# 의미: 현재 도시가 u, 이미 방문한 집합이 mask일 때
19
+
# 남은 도시들을 모두 방문하고 start로 귀환하는 "최소 추가 비용"
20
+
21
+
if mask == FULL:
22
+
# 모든 도시를 방문했다면 start로 귀환해야 함
23
+
if cost[u][start] < INF:
24
+
return cost[u][start]
22
25
else:
23
-
// 돌아가지 못함으로
24
-
return INF // 못 돌아감
26
+
return INF
25
27
26
-
// 이미 구한 값이 있으면 얼리 리턴 (not initial value ex: -1)
27
-
if dp[current][visited] is already computed:
28
-
return dp[current][visited]
28
+
if dp[u][mask] != UNKNOWN:
29
+
return dp[u][mask]
29
30
30
-
dp[current][visited] = INF
31
+
best = INF
32
+
best_next = NONE
31
33
32
-
// 모든 도시에 대해서
33
-
for next in 0 to N-1:
34
-
// 조합에 없는 도시만 비트마스크로 추출, 거리가 있다면 dp 없데이트
35
-
if city 'next' is not visited and cost[current][next] > 0:
0 commit comments