Algorithms and Problem Solving · Lesson 3.5.1–3.5.4
Algorithm Design Techniques
Compare the four textbook approaches: divide and conquer, greedy algorithms, dynamic programming and backtracking.
01
Learning objectives
Explain divide-and-conquer by splitting a problem and combining subproblem results.
Describe greedy selection and identify when a locally optimal choice may fail.
Explain overlapping subproblems and memoisation in dynamic programming.
Trace backtracking as a systematic search that reverses choices when a path fails.
Compare the design techniques by correctness assumptions, time and memory trade-offs.
02
Lesson notes
Divide and conquer splits a large problem into smaller instances, solves those instances, and combines their results. Merge sort is an example: divide a list into halves, sort each half, and merge the results. It is appropriate when subproblems have a similar form and can be solved independently.
A greedy algorithm makes a locally optimal choice at each step. Coin change is a familiar example: repeatedly select the largest coin not exceeding the remaining amount. Greedy methods can be simple and efficient, but local choices do not guarantee a globally optimal answer for every coin system or problem. Correctness needs an argument about the problem structure.
Dynamic programming applies when subproblems overlap and their solutions can be reused. Rather than recomputing the same result, a program stores it in a table (tabulation) or cache (memoisation). Fibonacci numbers provide a simple example: a naive recursive version repeats work, whereas a stored-result approach computes each needed value once.
Backtracking builds a candidate solution one decision at a time. If a partial candidate cannot lead to a valid solution, the algorithm undoes the last choice and tries another. It is used in puzzles, constraint satisfaction and combinatorial search. Pruning can avoid exploring branches that already violate a constraint.
Choose a technique based on the structure of the problem. Divide and conquer relies on independently solvable pieces, dynamic programming reuses overlapping subproblems, greedy methods require safe local choices, and backtracking explores alternatives while rejecting impossible partial solutions.
03
Worked code examples
Worked example
Merge sort uses divide and conquer: split the input, recursively sort the parts, then combine them in order.
def merge_sort_count(values):
if len(values) <= 1:
return values
mid = len(values) // 2
left = merge_sort_count(values[:mid])
right = merge_sort_count(values[mid:])
result = []
while left and right:
result.append(left.pop(0) if left[0] <= right[0] else right.pop(0))
return result + left + right
print(merge_sort_count([8, 3, 5, 1]))
Output
[1, 3, 5, 8]
04
Key terminology
Pseudocode
A language-independent description of algorithm steps.
Flowchart
A graphical representation of steps and control flow.
Greedy strategy
A method that selects a locally optimal option at each step.
Divide and conquer
A strategy that splits a problem, solves subproblems and combines results.
05
Common misconceptions
Common mistake
An algorithm is the same thing as Python source code.
Correct understanding
An algorithm is the underlying procedure and can be expressed in pseudocode, a flowchart or code.
Common mistake
A greedy choice always produces a globally optimal answer.
Correct understanding
Greedy algorithms need a proof that local choices lead to a correct global solution.
06
Check your understanding
1. Which control structure repeats steps?
2. Which strategy divides input into smaller subproblems and combines results?