Escolar Documentos
Profissional Documentos
Cultura Documentos
) (y
) (z
)
For any boolean formula and any literal x, let [x (pronounced sigh given eks) denote the
simpler boolean formula obtained by assuming x is true. Its not hard to prove by induction (hint,
hint) that x = x [x, which implies that
= (x
[x) (y
[y) (z
[z).
Thus, in any satisfying assignment for , either x is true and
[y in the second
step, we can check the even simpler formula
[ xy. Similarly, if
[ x yz in the
third step.
3SAT():
if =
return TRUE
(x y z)
if 3SAT([x)
return TRUE
if 3SAT([ xy)
return TRUE
return 3SAT([ x yz)
7
Algorithms Lecture 1: Recursion
The running time off this algorithm obeys the recurrence
T(n) = T(n 1) +T(n 2) +T(n 3) + poly(n),
where poly(n) denotes the polynomial time required to simplify boolean formulas, handle control
ow, move stuff into and out of the recursion stack, and so on. The annihilator method gives us the
solution
T(n) = O(
n
poly(n)) = O(1.83928675522
n
)
where 1.83928675521 . . . is the largest root of the characteristic polynomial r
3
r
2
r 1.
(Notice that we cleverly eliminated the polynomial noise by increasing the base of the exponent
ever so slightly.)
We can improve this algorithm further by eliminating pure literals from the formula before
recursing. A literal x is pure in if it appears in the formula but its negation x does not. Its not
hard to prove (hint, hint) that if has a satisfying assignment, then it has a satisfying assignment
where every pure literal is true. If = (xy z)
for some literals u and v (each of which might be y, y, z, or z). It follows that the rst recursive
formula [x has contains the clause (u v). We can recursively eliminate the variables u and v just
as we tested the variables y and x in the second and third cases of our previous algorithm:
[x = (u v)
[x = (u
[xu) (v
[x uv).
Here is our new faster algorithm:
3SAT():
if =
return TRUE
if has a pure literal x
return 3SAT([x)
(x y z) ( x u v)
if 3SAT([xu)
return TRUE
if 3SAT([x uv)
return TRUE
if 3SAT([ xy)
return TRUE
return 3SAT([ x yz)
The running time T(n) of this new algorithm satises the recurrence
T(n) = 2T(n 2) + 2T(n 3) + poly(n),
and the annihilator method implies that
T(n) = O(
n
poly(n)) = O(1.76929235425
n
)
where 1.76929235424 . . . is the largest root of the characteristic polynomial r
3
2r 2.
Naturally, this approach can be extended much further. As of 2004, the fastest (deterministic)
algorithm for 3SAT runs in O(1.473
n
) time
6
, but there is absolutely no reason to believe that this is
the best possible.
6
Tobias Brueggemann and Walter Kern. An improved deterministic local search algorithm for 3-SAT. Theoretical
Computer Science 329(13):303313, 2004.
8
Algorithms Lecture 1: Recursion
*1.5 Maximum Independent Set
This section assumes you are already familiar with graphs and NP-completeness.
Finally, suppose we are asked to nd the largest independent set in an undirected graph G. Once
again, we have an obvious, trivial algorithm: Try every subset of nodes, and return the largest
subset with no edges. Expressed recursively, the algorithm might look like this.
MAXIMUMINDSETSIZE(G):
if G =
return 0
else
v any node in G
withv 1 + MAXIMUMINDSETSIZE(G N(v))
withoutv MAXIMUMINDSETSIZE(G v)
return maxwithv, withoutv.
Here, N(v) denotes the neighborhood of v: the set containing v and all of its neighbors. Our
algorithm is exploiting the fact that if an independent set contains v, then by denition it contains
none of vs neighbors. In the worst case, v has no neighbors, so G v = G N(v). Thus, the
running time of this algorithm satises the recurrence T(n) = 2T(n1)+poly(n) = O(2
n
poly(n)).
Surprise, surprise.
This algorithm is mirroring a crude recursive upper bound for the number of maximal indepen-
dent sets in a graph. If the graph is non-empty, then every maximal independent set either includes
or excludes each vertex. Thus, the number of maximal independent sets satises the recurrence
M(n) 2M(n 1), with base case M(1) = 1. The annihilator method gives us M(n) 2
n
1.
The only subset that we arent counting with this upper bound is the empty set!
We can improve this upper bound by more carefully examining the worst case of the recurrence.
If v has no neighbors, then N(v) = v, and both recursive calls consider a graph with n1 nodes.
But in this case, v is in every maximal independent set, so one of the recursive calls is redundant.
On the other hand, if v has at least one neighbor, then G N(v) has at most n 2 nodes. So now
we have the following recurrence.
M(n) max
M(n 1)
M(n 1) +M(n 2)
= O(1.61803398875
n
)
The upper bound is derived by solving each case separately using the annihilator method and taking
the worst of the two cases. The rst case gives us M(n) = O(1); the second case yields our old
friends the Fibonacci numbers.
We can improve this bound even more by examining the new worst case: v has exactly one
neighbor w. In this case, either v or w appears in any maximal independent set. Thus, instead of
recursively searching in G v, we should recursively search in G N(w), which has at most n1
nodes. On the other hand, if G has no nodes with degree 1, then G N(v) has at most n3 nodes.
M(n) max
M(n 1)
2M(n 2)
M(n 1) +M(n 3)
= O(1.46557123188
n
)
The base of the exponent is the largest root of the characteristic polynomial r
3
r
2
1. The second
case implies a bound of O(
2
n
) = O(1.41421356237
n
), which is smaller.
We can apply this improvement technique one more time. If G has a node v with degree 3 or
more, then G N(v) has at most n 4 nodes. Otherwise (since we have already considered nodes
9
Algorithms Lecture 1: Recursion
of degree 0 and 1), every node in the graph has degree 2. Let u, v, w be a path of three nodes in G
(possibly with u adjacent to w). In any maximal independent set, either v is present and u, w are
absent, or u is present and its two neighbors are absent, or w is present and its two neighbors are
absent. In all three cases, we recursively count maximal independent sets in a graph with n 3
nodes.
M(n) max
M(n 1)
2M(n 2)
M(n 1) +M(n 4)
3M(n 3)
= O(3
n/3
) = O(1.44224957031
n
)
The third case implies a bound of O(1.3802775691
n
), where the base is the largest root of r
4
r
3
1.
Unfortunately, we cannot apply the same improvement trick again. A graph consisting of n/3
triangles (cycles of length three) has exactly 3
n/3
maximal independent sets, so our upper bound is
tight in the worst case.
Now from this recurrence, we can derive an efcient algorithm to compute the largest indepen-
dent set in G in O(3
n/3
poly(n)) = O(1.44224957032
n
) time.
MAXIMUMINDSETSIZE(G):
if G =
return 0
else if G has a node v with degree 0
return 1 + MAXIMUMINDSETSIZE(G v) n 1))
else if G has a node v with degree 1
w vs neighbor
withv 1 + MAXIMUMINDSETSIZE(G N(v)) n 2))
withw 1 + MAXIMUMINDSETSIZE(G N(w)) n 2))
return maxwithv, withw
else if G has a node v with degree greater than 2
withv 1 + MAXIMUMINDSETSIZE(G N(v)) n 4))
withoutv MAXIMUMINDSETSIZE(G v) n 1))
return maxwithv, withoutv
else every node in G has degree 2))
v any node; u, w vs neighbors
withu 1 + MAXIMUMINDSETSIZE(G N(u)) n 3))
withv 1 + MAXIMUMINDSETSIZE(G N(v)) n 3))
withw 1 + MAXIMUMINDSETSIZE(G N(w)) n 3))
return maxwithu, withv, withw
1.6 Generalities
Recursion is reduction from a problem to one or more simpler instances of the same problem.
Almost every recursive algorithm (and inductive proof) closely follows a recursive denition for
the object being computed. Here are a few simple recursive denitions that can be used to derive
recursive algorithms:
A natural number is either 0, or the successor of a natural number.
A sequence is either empty, or an atom followed by a sequence.
A sequence is either empty, an atom, or the concatenation of two shorter sequences.
A set is either empty, or the union of a set and an atom.
10
Algorithms Lecture 1: Recursion
A nonempty set either is a singleton, or the union of two nonempty sets.
A rooted tree is either nothing, or a node pointing to zero or more rooted trees.
A binary tree is either nothing, or a node pointing to two binary trees.
A triangulated polygon is nothing, or a triangle glued to a triangulated polygon [not obvious!]
i=1
a
i
=
0 if n = 0
n1
i=1
a
i
+a
n
otherwise
i=1
a
i
=
0 if n = 0
a
1
if n = 1
k
i=1
a
i
+
n
i=k+1
a
i
otherwise, for some 1 k n 1
11
Algorithms Lecture 1: Recursion
Exercises
1. (a) George has a 26-node binary tree T, with each node labeled by a unique letter of the
alphabet. The preorder and postorder sequences of nodes in T are as follows:
preorder: M N H C R S K W T G D X I Y A J P O E Z V B U L Q F
postorder: C W T K S G R H D N A O E P J Y Z I B Q L F U V X M
Draw Georges binary tree.
(b) Describe and analyze a recursive algorithm for reconstructing a binary tree, given its
preorder and postorder node sequences.
(c) Describe and analyze a recursive algorithm for reconstructing a binary tree, given its
preorder and inorder node sequences.
2. Suppose you are given a stack of n pancakes of different sizes. You want to sort the pancakes
so that smaller pancakes are on top of larger pancakes. The only operation you can perform
is a ipinsert a spatula under the top k pancakes, for some integer k between 1 and n, and
ip them all over.
Flipping the top three pancakes.
(a) Describe an algorithm to sort an arbitrary stack of n pancakes using as few ips as
possible. Exactly how many ips does your algorithm perform in the worst case?
(b) Now suppose one side of each pancake is burned. Describe an algorithm to sort an
arbitrary stack of n pancakes, so that the burned side of every pancake is facing down,
using as few ips as possible. Exactly how many ips does your algorithm perform in the
worst case?
3. Prove that the recursive Tower of Hanoi algorithm is exactly equivalent to each of the following
non-recursive algorithms; in other words, prove that all three algorithms move the same
disks, to and from the same needles, in the same order. The needles are labeled 0, 1, and 2,
and our problem is to move a stack of n disks from needle 0 to needle 2 (as shown on page 3).
(a) Follow these four rules:
Never move the same disk twice in a row.
If n is even, always move the smallest disk forward ( 0 1 2 0 ).
If n is odd, always move the smallest disk backward ( 0 2 1 0 ).
When there is no move that satises the other rules, the puzzle is solved.
12
Algorithms Lecture 1: Recursion
(b) Let (n) denote the smallest integer k such that n/2
k
is not an integer. For example,
(42) = 2, because 42/2
1
is an integer but 42/2
2
is not. (Equivalently, (n) is one more
than the position of the least signicant 1 bit in the binary representation of n.) The
function (n) is sometimes called the ruler function, because its behavior resembles the
marks on a ruler:
1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 6, 1, 2, 1, 3, 1, . . . .
Heres the non-recursive algorithm in one line:
In step i, move disk (i) forward if n i is even, backward if n i is odd.
When this rule requires us to move disk n + 1, the algorithm ends.
4. Consider the following restricted variants of the Tower of Hanoi puzzle. In each problem, the
needles are numbered 0, 1, and 2, as in problem 3, and your task is to move a stack of n disks
from needle 1 to needle 2.
(a) Suppose you are forbidden to move any disk directly between needle 1 and needle 2;
every move must involve needle 0. Describe an algorithm to solve this version of the
puzzle in as few moves as possible. Exactly how many moves does your algorithm make?
(b) Suppose you are only allowed to move disks from needle 0 to needle 2, from needle 2 to
needle 1, or from needle 1 to needle 0. Equivalently, Suppose the needles are arranged
in a circle and numbered in clockwise order, and you are only allowed to move disks
counterclockwise. Describe an algorithm to solve this version of the puzzle in as few
moves as possible. Exactly how many moves does your algorithm make?
1 0 3 2 4
6 5 8 7
A top view of the rst eight moves in a counterclockwise Towers of Hanoi solution
(c) Finally, suppose your only restriction is that you may never move a disk directly from
needle 1 to needle 2. Describe an algorithm to solve this version of the puzzle in as
few moves as possible. How many moves does your algorithm make? [Hint: This is
considerably harder to analyze than the other two variants.]
13
Algorithms Lecture 1: Recursion
5. The median of a set of size n is its n/2|th largest element, that is, the element that is as close
as possible to the middle of the set in sorted order. It?s quite easy to nd the median of a set
in O(nlog n) timejust sort the set and look in the middlebut you (correctly!) think that
you can do better.
During your lifelong quest for a faster median-nding algorithm, you meet and befriend
the Near-Middle Fairy. Given any set X, the Near-Middle Fairy can nd an element m X
that is near the middle of X in O(1) time. Specically, at least a third of the elements of X
are smaller than m, and at least a third of the elements of X are larger than m.
Describe and analyze an algorithm to nd the median of a set in O(n) time if you are
allowed to ask the Near-Middle Fairy for help. [Hint: You may need the PARTITION subroutine
from QUICKSORT.]
6. Suppose we have n points scattered inside a two-dimensional box. A kd-tree recursively
subdivides the points as follows. First we split the box into two smaller boxes with a vertical
line, then we split each of those boxes with horizontal lines, and so on, always alternating
between horizontal and vertical splits. Each time we split a box, the splitting line partitions
the rest of the interior points as evenly as possible by passing through a median point inside
the box (not on its boundary). If a box doesnt contain any points, we dont split it any more;
these nal empty boxes are called cells.
A kd-tree for 15 points. The dashed line crosses the four shaded cells.
(a) How many cells are there, as a function of n? Prove your answer is correct.
(b) In the worst case, exactly how many cells can a horizontal line cross, as a function of n?
Prove your answer is correct. Assume that n = 2
k
1 for some integer k.
(c) Suppose we have n points stored in a kd-tree. Describe and analyze an algorithm that
counts the number of points above a horizontal line (such as the dashed line in the
gure) as quickly as possible. [Hint: Use part (b).]
(d) Describe an analyze an efcient algorithm that counts, given a kd-tree storing n points,
the number of points that lie inside a rectangle R with horizontal and vertical sides.
[Hint: Use part (c).]
14
Algorithms Lecture 1: Recursion
7. For this problem, a subtree of a binary tree means any connected subgraph; a binary tree is
complete if every leaf has exactly the same depth. Describe and analyze a recursive algorithm
to compute the largest complete subtree of a given binary tree. Your algorithm should return
the root and the depth of this subtree.
The largest complete subtree of this binary tree has depth 2.
8. (a) Let A[1 .. m] and B[1 .. n] be two arbitrary arrays. A common subsequence of A and B is
both a subsequence of A and a subsequence of B. Give a simple recursive denition for
the function lcs(A, B), which gives the length of the longest common subsequence of A
and B.
(b) Let A[1 .. m] and B[1 .. n] be two arbitrary arrays. A common supersequence of A and B is
another sequence that contains both A and B as subsequences. Give a simple recursive
denition for the function scs(A, B), which gives the length of the shortest common
supersequence of A and B.
(c) Call a sequence X[1 .. n] oscillating if X[i] < X[i + 1] for all even i, and X[i] > X[i + 1]
for all odd i. Give a simple recursive denition for the function los(A), which gives the
length of the longest oscillating subsequence of an arbitrary array A of integers.
(d) Give a simple recursive denition for the function sos(A), which gives the length of the
shortest oscillating supersequence of an arbitrary array A of integers.
(e) Call a sequence X[1 .. n] accelerating if 2 X[i] < X[i 1] + X[i + 1] for all i. Give a
simple recursive denition for the function lxs(A), which gives the length of the longest
accelerating subsequence of an arbitrary array A of integers.
9. You are a contestant on the hit game show Beat Your Neighbors! You are presented with an
mn grid of boxes, each containing a unique number. It costs $100 to open a box. Your goal
is to nd a box whose number is larger than its neighbors in the grid (above, below, left, and
right). If you spend less money than any of your opponents, you win a week-long trip for two
to Las Vegas and a years supply of Rice-A-Roni
TM
, to which you are hopelessly addicted.
(a) Suppose m = 1. Describe an algorithm that nds a number that is bigger than any of its
neighbors. How many boxes does your algorithm open in the worst case?
(b) Suppose m = n. Describe an algorithm that nds a number that is bigger than any of its
neighbors. How many boxes does your algorithm open in the worst case?
(c) Prove that your solution to part (b) is optimal up to a constant factor.
c Copyright 2007 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License (http://creativecommons.org/licenses/by-nc-sa/2.5/)
15