Greedy · straightforward
Standing where it is, the raccoon scores every uneaten item and walks to the most attractive one, then
repeats. Attractiveness is Smith's ratio rule: calories lost per second of waiting ÷ seconds of
walking to it. It never looks more than one step ahead.
GREEDY-RACCOON(items, den, speed)
1 here ← den; clock ← 0
2 remaining ← all n items; route ← empty
3 while remaining is not empty
4 best ← NIL; best_value ← −∞
5 for each item j in remaining
6 travel ← distance(here, j) / speed
7 value ← w_j / travel
8 if value > best_value
9 best ← j; best_value ← value
10 walk to best; clock ← clock + travel
11 remove best from remaining; append to route
12 return route
- Time
- Θ(n²): lines 6–9 run n + (n−1) + … + 1 times
- Memory
- Θ(n)
Greedy + 2-opt · clever
Step 1: build exactly Greedy's route, but faster. All items of one food type share the
same w, so the best fish is just the nearest fish. One k-d tree per food type answers
"nearest uneaten fish?" in O(log n).
Step 2: repeatedly pick a stretch of the route and walk it backwards when that loses fewer
calories. Running totals of the stretch's walking time, weight and internal cost make each check O(1).
TWO-OPT-RACCOON(items, den, L, P)
1 route ← GREEDY-KD(items, den)
2 repeat at most P times
3 improved ← false
4 for i ← 1 to n
5 for j ← i+1 to min(n, i+L−1)
6 Δ ← change in spoilage if route[i..j]
is reversed ▹ O(1)
7 if Δ < 0
8 reverse route[i..j] ▹ Θ(n)
9 improved ← true
10 if not improved: stop
11 return route
- Time
- O(n log² n + P·n·L + M·n) expected, with L = 50 and P = 20 fixed and
M = flips accepted. On random maps M grows like 0.3·n, so in practice this is about n², the
same order as Greedy. 2-opt's advantage is the score, not asymptotic speed.
- Memory
- Θ(n)