
Lecture 7 - Management Science
Master Baker’s Morning Dilemma:
“Every morning at 5:00, our delivery van leaves with fresh bread for 16 cafés across the city. Our driver currently takes much too long using his ‘intuition’ for the route. The fuel costs are killing us, and worse, some cafés get their bread late.”
Artisan Bakery’s daily logistics puzzle:
The Stakes: Poor routing costs plus reputation damage from late deliveries!
Last week we learned greedy algorithms for scheduling:
Question: Can we use the same greedy approach for routing?
Today: We’ll start greedy, then learn how to improve solutions with local search!
Visit all locations exactly once, minimize total distance.

How many unique tours exist? With depot, n!/2 unique tours.
If your computer checks 1 million routes per second:
The Reality: Exact approach would take longer than the universe has existed!
Why buying a faster computer won’t help:
TSP optimization where we find minimum cost tour → NP-Hard. This means no known algorithm can find the perfect solution quickly for large problems.
A graph \(G = (V, E)\) consists of:
For our bakery problem:
Different graph structures lead to different complexities:
Density dramatically affects both problem difficulty and solution approaches!
Mathematical foundation of the TSP:
Mathematical Definition: A tour \(T = (v_1, v_2, ..., v_n, v_1)\) where:
Goal: Find tour \(T^*\) such that \(w(T^*) = \min_{T \in \mathcal{H}} w(T)\)
The weight function can have different properties:
Common Distance Functions:
What happens if we pick cafés randomly?

Random selection creates routes with many crossings and inefficiencies.
Given these 8 cafés, which should we visit first?

Question: Using nearest neighbor, which café would you visit first?
Build a route by always visiting the closest unvisited location.
Intuition: Like picking low-hanging fruit - grab what’s easiest (nearest) first!
Let’s see how nearest neighbor builds the route step by step:

Farthest insertion builds a more balanced tour by establishing the outer structure first!
Same 8 cafés - but now we’ll use a different strategy:

The farthest point is our start!
Build a route by starting with distant points and filling in gaps:
Intuition: Build the “skeleton” of the route first with distant points, then fill in the gaps.

Notice how farthest insertion builds a more balanced tour by establishing the outer structure first, then filling in the gaps!
Let’s compare all three construction methods:
The better your starting point, the better your final result after local search!
This is also true for all other problems we are solving! A good initial heuristic to create a solution will help us later.
Often obvious inefficiencies in the resulting routes
Can we improve our greedy solutions?
Any problem can be solved with local search by defining:
The power of local search: The same “engine” works for routing, scheduling, or any combinatorial problem - just plug in different components!
Think of the solution space as a landscape:
How can we search this space?
Local search transforms “quick and dirty” solutions into “pretty good” ones!
Systematically improve routes by removing crossing paths.
The Idea: Take two edges and swap them (to uncross the route)

Let’s see exactly how 2-opt fixes a crossing in a real route:

The Key Insight: When you reverse a segment between two crossing edges, you automatically eliminate the crossing and create a shorter route!
improved = True
while improved:
improved = False
best_distance = calculate_route_distance(route, distances)
for i in range(len(route) - 1):
for j in range(i + 2, len(route)):
new_route = route[:i+1] + route[i+1:j+1][::-1] + route[j+1:]
new_distance = calculate_route_distance(new_route, distances)
if new_distance < best_distance:
route = new_route
best_distance = new_distance
improved = True
break
if improved:
breakThe [::-1] reverses the segment, eliminating crossings!
Let’s see how this changes the route!

Notice how 2-opt removed the crossing paths from our nearest neighbor solution, creating a more efficient route!
Debug these scenarios you’ll encounter:
Bug 1: Infinite Loop
Bug 2: Missing Return to Start
Bug 3: Invalid Segment Reversal
Fix: route[:i+1] + route[i+1:j+1][::-1] + route[j+1:]

Where 1-opt DOES work: In problems like knapsack (swap 1 item), assignment (reassign 1 person), or facility location (relocate 1 facility).
Moves sequence of 1-3 consecutive cities to different position.

Moving a sequence can satisfy time constraints without breaking tour structure!
The k-opt family of improvements:
2-opt
3-opt
Or-opt
Start with 2-opt (fast), use 3-opt if you have time! As k increases, solutions improve but computation time grows exponentially.
When does local search stop? Why might it get stuck?
Convergence:
The Local Optimum Problem:
Local search for improvement but does not guarantee global optimality.
Imagine you’re a hiker dropped in foggy mountains at night…
Question: What happens when you reach the top of a small hill?
You’re stuck! Every step is downhill, but you might be on a tiny hill while a much larger moutain is nearby. This is the local optimum trap!

Here, the local minimum is already quite good, but we likely won’t reach the global optimum from here.
Real problems often have thousands of local optima!

The probability of finding the global optimum with simple local search is nearly zero!
Depending on the problem: Multi-Start Strategy!
No Free Lunch Theorem: There’s no universal “best” algorithm for all problems. What works great for routing might fail for scheduling. Always match your tool to your problem!
Industry usage for delivery optimization
| Method | Industry Use | |
|---|---|---|
| Human intuition | Still common! | |
| Start + 2-opt | Common practice | |
| Advanced Meta | Sometimes practice | |
| Exact (if possible) | Mostly research |
A 10% improvement = millions in savings for large logistics companies. Even a 2-opt implementation could literally save a lot of money if not used yet!
Remember our bakery? Some cafés open earlier than others!
Artisan Bakery’s Morning Schedule:
Question: Can we just find the shortest route?
The shortest route might deliver to early cafés last. Feasibility first, optimization second!
Each location has a delivery time window:
Key Concepts:
Arrival Time = Previous departure + Travel time
Modify greedy construction to prioritize early deadlines:
unvisited = set(range(len(locations))); route = []; current_time = start_time
while unvisited:
# Find feasible neighbors (can reach before deadline)
feasible = [i for i in unvisited
if current_time + travel_time(current, i)
<= time_windows[i]['latest']]
if not feasible:
return None # No feasible route exists!
# Among feasible, choose most urgent
next_stop = min(feasible, key=lambda i: (time_windows[i]['latest']))
# Update state
route.append(next_stop)
unvisited.remove(next_stop)
current_time += travel_time(current, next_stop) + service_timeProblem: 2-opt can break time feasibility!

Early2 now arrives at 7:12 AM. Missed its 6:45 deadline by 27 minutes!
Only accept swaps that maintain feasibility!
improved = True
while improved:
improved = False
for i in range(len(route) - 1):
for j in range(i + 2, len(route)):
new_route = route[:i+1] + route[i+1:j+1][::-1] + route[j+1:]
# Check feasibility FIRST
if not is_feasible(new_route, time_windows, start_time):
continue # Skip infeasible swaps
# Among feasible swaps, take if shorter
if calculate_distance(new_route) < calculate_distance(route):
route = new_route
improved = True
break
if improved:
breakFeasibility is a hard constraint, distance is the objective.
Different situations call for different approaches:
| Situation | Best Approach | Why |
|---|---|---|
| Solution now | Nearest Neighbor | Lightning fast |
| Have seconds | NN + 2-opt | Good balance |
| Have minutes | Multi-start + 2-opt | Explore more options |
| Time windows | NN (early) + Or-opt | Preserves feasibility |
| Benchmark | 3-opt or meta | Best solutions |
Competition? Choose whatever you are comfortable with.
Common bugs that cost you time.
Forgetting return to bakery:
When local search gets stuck, we need clever escapes:
Advanced Techniques Coming:
Today’s local search foundation makes advanced methods possible!
Key Takeaways:
Take 20 minutes, then we start the practice notebook
Next up: You’ll become Bean Counter’s route planner
Then: The Bakery competition
Better Routing | Dr. Tobias Vlćek | Home