These are provisional lecture notes, expect changes.
the inductive approach to algorithm design
reduce the problem to a very simple form (base case)
show how to get the next solution by a small step from the previous
solution (inductive step)
advantages
both the base and inductive steps are simple,
although it may be difficult to find simple base and inductive steps
recursion, which can be unfolded if necessary (but that could be
complex)
example - polynomial evaluation
evaluate Pn(x) =
anxn +
an - 1xn - 1 + . . . +
a1x + a0 for some x
first cut
hyp - H(n) = I can compute Pn(x)
bas - H(0) = a0
ind - Suppose H(j) for j >= 0 and consider H(j +
1)
I can compute Pj + 1(x)
I can compute aj + 1xj + 1 +
Pj(x)
repeatedly exponentiating x is expensive
second cut
try a different, stronger, hypothesis
hyp - H(n) = I can compute Pn(x) and
xn
bas - H(0) = a0 and x0 = 1
ind - Suppose H(j) for j >= 0 and consider H(j +
1)
I can compute Pj + 1(x) and
xj + 1
I can compute aj + 1xj + 1 +
Pj(x) and xjx
2n multiplications
third cut
try a different order - (anxn +
an - 1xn - 1 + . . . +
a1x) + a0 instead of
anxn +
(an - 1xn - 1 + . . . +
a1x + a0)
hyp - H(j) = I can compute xPj - 1(x) + an - j
bas - H(0) = P0(x) = an - 0 = an
ind - Suppose H(j) for j >= 0 and consider
H(j + 1)
=
xPj - 1 + 1(x) + an - (j + 1)
=
xPj(x) +
an - (j + 1)
n multiplications - Horner's rule
example - induced subgraphs
a graph G, and induced subgraph of G
problem - find the largest k-degree induced subgraph of G.
solution - how about greed
if a vertex of G has degree less than k, it cannot be in
the induced subgraph
greed works, but is the result exact; that is, is the result
the largest possible k-degree induced subgraph; does order of removal
make any difference
hyp - H(n) = greed finds a largest k-degree induced
subgraph of any graph with n vertices
bas - H(i) for i <= k - there is no such largest,
k-degree induced subgraph and greed removes all vertices
bas - H(k + 1) - the graph itself is a k-degree
induced subgraph, it is largest, and greed removes no vertices.
ind - assume H(j) holds for j >= 0 and consider
H(j + 1). Let G be a graph with j + 1 vertices. There
are two cases to consider:
If G has no vertices of degree less than k, G is the
largest, k-degree induced subgraph and greed removes no vertices.
If G has a vertex with degree less than k, greed will
remove it. But then G is a graph with j vertices and, because
H(j) is true, greed will find the largest, k-degree induced
subgraph.
In either case greed works on a j + 1 vertex graph and H(j +
1) holds
example - one-to-one mappings
mapping a set onto itself
one-to-one mappings
find the largest subset containing a 1-1 mapping
hyp - H(n) = can find maximal 1-1 mappings for n-element
sets
bas - H(1) is true
ind - let H(j) be true for j >= 1 and consider H(j
+ 1)
how to reduce to H(j)?
chose i with in-degree zero - it can't be in the maximal 1-1
set
no such i - the whole set's 1-1
example - finding a celebrity
everyone knows a celebrity, the celebrity knows nobody
in a directed graph, a sink pointed to by all other nodes
problem - find the celebrity (or sink)
hyp - H(n) = can find the celebrity in an n-node graph
bas - H(2) is true
ind - let H(j) be true and consider H(j + 1)
how to get to H(j) from H(j + 1)
pick an arbitrary n - it's the celebrity or not
pick a non-celebrity - it's easy with one question
example - divide and conquer
reducing a problem by one each time may lead to an
O(n2) algorithm
reducing by half each time may be better, if the combination is not
expensive
example - balance factors
a root's balance factor - the difference between the heights of the left and
right subtrees
hyp - H(n) = can find the balance factor for an n-node tree
bas - H(1) is easy
ind - assume H(j) is true for j >= 1 and consider
H(h + 1)
but, you can't compute a parent's balance from the children's balance
- you need their heights
example - maximum consecutive subsequence
the sum of consecutive numbers - find the largest such sum
hyp - H(n) = can find the mcs in an n-number sequence
bas - H(1), yup
ind - assume H(j) is true for j >= 1 and consider
H(h + 1)