Skiena, Chapter 1

Introduction to Algorithm Design

Why heuristics fail, why proofs matter, and how to think about correctness before you write a single line of code.

Prerequisites: Basic programming. That's it.
9
Chapters
5+
Simulations
9
Quizzes

Chapter 0: The Problem with Heuristics

You have a problem. It looks simple. You come up with a reasonable strategy that seems obvious. You implement it. It works on your test cases. You ship it. Then it fails spectacularly on someone else's input.

This chapter is about why that happens. The fundamental lesson of algorithm design is that reasonable-looking algorithms can easily be incorrect. An algorithm that works on the examples you tried is not the same as an algorithm that works on every possible input.

The difference between an algorithm and a heuristic is the difference between a guarantee and a hope. An algorithm always produces the correct result. A heuristic usually does a good job but provides no guarantee. Both have their place, but you need to know which one you're using.

The core distinction: An algorithm always produces a correct result. A heuristic may usually do a good job but without providing any guarantee. The gap between "usually works" and "always works" is where bugs live.
Heuristic vs. Optimal

Click "Nearest Neighbor" to see a greedy tour, then "Optimal" to see the best possible tour. Drag points to rearrange them. Notice how the heuristic can produce wildly different tour lengths.

Click a strategy
What is the key difference between an algorithm and a heuristic?

Chapter 1: The Robot Tour Problem

Imagine a robot arm soldering contact points on a circuit board. It must visit every point and return to where it started. You want to minimize travel time. This is the Traveling Salesman Problem (TSP), and it comes up everywhere: delivery routes, circuit design, DNA sequencing.

The most natural idea is the nearest-neighbor heuristic: from wherever you are, walk to the closest unvisited point. It sounds perfect. Why waste time going far when there's something close? But this greedy logic has a fatal flaw.

The nearest-neighbor trap: Consider points spaced along a line: -21, -5, -1, 0, 1, 3, 11. Starting from 0, nearest-neighbor might jump left-right-left-right, zigzagging across the line. The optimal tour just walks left to right (or right to left).

A second idea: the closest-pair heuristic. Repeatedly connect the two closest endpoints that won't create a premature cycle. This avoids the zigzag problem above, but it too can fail. When points form two rows slightly closer together than they are spaced within each row, closest-pair connects across the gap instead of around the boundary, producing a tour over 20% longer than optimal.

Both heuristics can look at the same points and produce dramatically different (and wrong) tours. The only way to guarantee the shortest tour is to try all n! orderings. But 20! is already 2.4 quintillion. There must be a better way. Spoiler: for TSP, finding the provably optimal solution efficiently remains one of computer science's greatest open problems.

Nearest-Neighbor Pathology

Points on a line. Watch nearest-neighbor zigzag while optimal just sweeps. Click "Step" to see each move.

Ready
Why does the nearest-neighbor heuristic fail on points along a line?

Chapter 2: The Scheduling Problem

You're a sought-after actor with offers for n movies. Each movie has a start day and end day. You can't film two movies at the same time. You want to star in as many movies as possible. How do you choose?

First instinct: Earliest Start First. Accept the movie that starts soonest. But what if that movie is "War and Peace" and it runs for six months, blocking everything else? One long movie kills your schedule.

Second instinct: Shortest Job First. Accept the shortest movie, since banging them out quickly maximizes throughput. But a short movie in the middle can block two jobs that don't overlap each other. You lose half your potential.

The trick: Think about which job finishes first. The job with the earliest completion date is safe to accept because it blocks out the least future time. Any overlapping job that finishes later would block at least as much. This gives the optimal greedy algorithm.
Interval Scheduling

Watch three strategies compete. Green bars = selected jobs. The "Earliest Finish" strategy always finds the maximum set.

Pick a strategy
Which greedy criterion always produces the optimal schedule for interval scheduling?

Chapter 3: Counterexamples

How do you prove an algorithm is wrong? You find a single input where it gives the wrong answer. That input is called a counterexample, and it instantly kills any algorithm's credibility.

Good counterexamples have two properties. First, verifiability: you can compute what the algorithm gives and show a better answer exists. Second, simplicity: boil away all unnecessary details until the failure is crystal clear.

Hunting strategies:
Think small. Most algorithms fail on instances with 3-6 elements, not 100.
Think exhaustively. For n=2, there are only a few possible configurations. Try them all.
Hunt for weakness. If the algorithm says "always take the biggest," think about when biggest is wrong.
Go for a tie. Give the algorithm identical-sized options so it has no basis for its greedy choice.
Seek extremes. Mix huge and tiny, near and far.
Build a Counterexample

Click to place points. The nearest-neighbor heuristic builds a tour (orange). Can you arrange points so the tour is clearly suboptimal? Press "Show Optimal" to compare.

Click to place points
What makes a good counterexample?

Chapter 4: Proof by Induction

A proof by induction works like a chain of dominoes. You show the first domino falls (the base case). Then you show that if any domino falls, the next one must fall too (the inductive step). Together, these guarantee every domino falls.

For algorithms, induction appears as recursion. Insertion sort can be proved correct by induction: a one-element array is sorted (base case). If we can sort n-1 elements correctly, then inserting the nth element into its proper position gives a sorted n-element array (inductive step).

Induction = Recursion. Every recursive algorithm has an inductive correctness proof hiding inside it. The base case of the recursion is the base case of the induction. The recursive call is the inductive hypothesis. If you can prove these two pieces, the algorithm is correct.

The most common mistake in inductive proofs is using too weak an inductive hypothesis. Consider trying to prove that a tree on n nodes has n-1 edges. The hypothesis "every tree on n-1 nodes has n-2 edges" doesn't obviously help, because removing a node from a tree might disconnect it. You need the stronger hypothesis "every tree with fewer than n nodes has one fewer edge than node."

Induction on Insertion Sort

Watch insertion sort prove itself correct by induction. At each step k, the first k elements are sorted (the inductive invariant, shown in teal).

k=1: base case
In a proof by induction, what are the two things you must show?

Chapter 5: Summations

Analyzing algorithms means counting operations. And counting operations usually means evaluating summations. Two formulas cover most of what you'll ever need.

i=1n i = n(n+1)/2

This arithmetic series tells you that 1+2+3+...+n grows like n2/2. It's why two nested loops over n elements give O(n2) work.

i=0n ai = (an+1 − 1) / (a − 1) for a ≠ 1

This geometric series is dominated by its largest term when a > 1. The total of 1 + 2 + 4 + 8 + ... + 2n = 2n+1 − 1 ≈ 2 · 2n. This explains why algorithms that halve the problem at each step (like binary search) run in O(log n) time.

The key insight: If a geometric series has ratio > 1, the sum is roughly twice the largest term. If ratio < 1, the sum converges. This fact shows up constantly in divide-and-conquer analysis.
Summation Visualizer

Arithmetic series (left) vs. geometric series (right). Drag n to see how they grow.

n 8
What is 1 + 2 + 4 + 8 + ... + 2n approximately equal to?

Chapter 6: Modeling

Modeling is the art of translating a messy real-world problem into a clean mathematical structure that an algorithm can attack. It is arguably the single most important skill in algorithm design. The right model makes the problem easy. The wrong model makes it impossible.

Most problems can be modeled using one of these fundamental structures:

StructureModelsExample
PermutationsArrangements, orderings, toursTSP tour, scheduling sequence
SubsetsSelections, committees, clustersKnapsack, vertex cover
TreesHierarchies, dominance, ancestryParse trees, search trees
GraphsRelationships, networksRoad maps, social networks
PointsLocations, data recordsNearest neighbor, clustering
PolygonsShapes, boundaries, regionsConvex hull, motion planning
StringsText, sequences, patternsDNA matching, search engines
The modeling question: When faced with a new problem, ask: "Is this really a graph problem? A sorting problem? A string problem?" If you can map your problem to a well-studied structure, you inherit centuries of algorithmic wisdom.
You want to find the best driving route between two cities. Which combinatorial structure best models this?

Chapter 7: TSP Explorer

Time to put it all together. This is the showcase: an interactive Traveling Salesman explorer. Place cities, try heuristics, watch them fail, then see how close they get to optimal on small instances.

Traveling Salesman Explorer

Click to place up to 10 cities. Compare three strategies: Nearest Neighbor, Closest Pair, and Brute-Force Optimal (only for ≤10 cities). Drag cities to explore new configurations.

NN: -- CP: -- Opt: --
Try this: Place 6-8 points in a roughly circular pattern. Nearest-neighbor will do reasonably well. Now place them in two tight clusters far apart. Watch nearest-neighbor bounce between clusters. The pathology is always a mismatch between the heuristic's local greed and the problem's global structure.
Why can't we just use brute-force optimal for all TSP instances?

Chapter 8: Connections

This chapter introduced the philosophy of algorithm design. Here is how the key ideas connect to the rest of the book:

ConceptWhere It Leads
Correctness proofsChapter 2: Algorithm Analysis formalizes efficiency
Greedy heuristicsChapter 6: some greedy algorithms (Prim's, Dijkstra's) are provably optimal
Exhaustive searchChapter 7: backtracking makes exhaustive search practical for small instances
ModelingChapters 5-6: graph models unlock BFS, DFS, shortest paths
TSPChapter 9: TSP is NP-complete, one of the hardest problems in CS
Skiena's take-home lessons from Chapter 1:
• There is a fundamental difference between algorithms (guaranteed correct) and heuristics (usually good).
• Reasonable-looking algorithms can easily be incorrect. Correctness must be carefully demonstrated.
• The heart of any algorithm is an idea. If your idea isn't clear, you're using too low-level a notation.
• Narrow the set of allowable instances until there is a correct and efficient algorithm.
• Searching for counterexamples is the best way to disprove correctness.
Before writing code, what should you do first according to this chapter?