Design of Algorithms by Induction: Algorithm Design and Analysis 2015 - Week 3
Design of Algorithms by Induction: Algorithm Design and Analysis 2015 - Week 3
Induction
Part 1
Algorithm Design and Analysis
2015 - Week 3
http://bigfoot.cs.upt.ro/~ioana/algo/
• But:
– Given a problem, how do we design a
solution that is both correct and efficient ?
• Is there a general method for this ?
Induction proofs vs
Design of algorithms by induction (1)
• Induction used to prove that a statement P(n)
holds for all integers n:
– Base case: Prove P(0)
– Assumption: assume that P(n-1) is true
– Induction step: prove that P(n-1) implies P(n) for all
n>0
K=3 Ann
Bob
Finn
Chris
Ellie Dan
Successful party – Direct approach
• Direct approach for solving: remove persons
who have less than k friends
• But: which is the right order of removing ?
1. Remove all persons with less than k friends, then
deal with the persons that are left without enough
friends ?
2. Remove first one person, then continue with
affected persons ?
• T(n)=1+2+3+ +n=n*(n+1)/2
• T(n) is O(n2)
p = Celebrity_Sol3(S-elim)
if p != 0 and knows[elim,p] and not knows[p,elim]
return p
else
return 0 // no celebrity
end if
end if
end function Celebrity_Sol3
Celebrity – Solution 3 Analysis
Function Celebrity_Sol3(S:Set of persons) return person
if card(S) = 1 return S(1)
pick i, j any persons in S
if knows[i, j] then // i no celebrity
elim=i
else // if not knows[i, j] then j no celebrity
elim=j
p = Celebrity_Sol3(S-elim)
if p != 0 and knows[elim,p] and not knows[p,elim]
return p
else
return 0 // no celebrity
end if
end if
end function Celebrity_Sol3 T(n)=T(n-1)+c
Algorithm is O(n)
Celebrity - Conclusions
• The size of the problem should be reduced (from
n to n-1) in a clever way
• This example shows that it sometimes pays off
to expend some effort ( in this case – one
question) to perform the reduction more
effectively
The Skyline Problem
• Problem: Given the exact locations and heights
of several rectangular buildings, having the
bottoms on a fixed line, draw the skyline of these
buildings, eliminating hidden lines.
The Skyline Problem
• A building Bi is represented as a triplet (Li, Hi, Ri)
• A skyline of a set of n buildings is a list of x coordinates and the
heights connecting them
• Input (1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22),
(23,13,29), (24,4,28)
• Output (1, 11), (3, 13), (9, 0), (12, 7), (16, 3), (19, 18), (22, 3), (23,
13), (29, 0)
Skyline – Solution 1
• Base case: If number of buildings n=1, the
skyline is the building itself
• Inductive step: We assume that we know to
solve the skyline for n-1 buildings, and then we
add the n’th building to the skyline
Adding One Building to the Skyline
• We scan the skyline, looking at one horizontal
line after the other, and adjusting when the
height of the building is higher than the skyline
height
Worst case:
Bn O(n)
Skyline – Solution 1 Analysis
T(n)
Algorithm Skyline_Sol1(n:integer) returns Skyline
if n = 1 then return Building[1]
else
sky1 = Skyline_Sol1(n-1); T(n-1)
sky2 = MergeBuilding(Building[n], sky1);
return sky2;
O(n)
end algorithm
T(n)=T(n-1)+O(n)
O(n2)
Skyline – Solution 2
• Base case: If number of buildings n=1, the
skyline is the building itself
• Inductive step: We assume that we know to
solve the skylines for n/2 buildings, and then we
merge the two skylines
Merging two skylines
• We scan the two skylines together, from left to
right, match x coordinates and adjust height
where needed
n1 n2
Worst case:
O(n1+n2)
Skyline – Solution 2 Analysis
T(n)
Algorithm Skyline_Sol2(left, right:integer) returns
Skyline
if left=right then return Building[left]
else
middle=left+(left+right)/2 T(n/2)
sky1 = Skyline_Sol2(left, middle);
sky2 = Skyline_Sol2(middle+1, right);T(n/2)
sky3 = MergeSkylines(sky1, sky2);
return sky3;
O(n)
end algorithm
T(n)=2T(n/2)+O(n)
O(n log n)
Skyline - Conclusion
• When the effort of combining the subproblems
cannot be reduced, it is more efficient to split
into several subproblems of the same type which
are of equal sizes
– T(n)=T(n-1)+O(n) … O(n2)
– T(n)=2T(n/2)+O(n) … O(n log n)
– T(n)=T(n-1)+O(n) … O(n2)
– T(n)=T(n-1)+O(1) … O(n)
– T(n)=2T(n/2)+O(1) … O(n)
– T(n)=a*T(n/b) + f (n) ;
Conclusions (1)
• What is Design by induction ?
• An algorithm design method that uses the idea
behind induction to solve problems
– Instead of thinking about our algorithm as a sequence of steps
to be executed, think of proving a theorem that the algorithm
exists
• We need to prove that this “theorem” holds for a base case, and that
if it holds for “n-1” this implies that it holds for “n”
Conclusions (2)
• Why/when should we use Design by induction ?
– It is a well defined method for approaching a large variety of
problems (“where do I start ?”)
• Just take the statement of the problem and consider it is a theorem
to be proven by induction
– This method is always safe: designing by induction gets us to a
correct algorithm, because we designed it proving its
correctness
– Encourages abstract thinking vs coding => you can handle the
reasoning in case of complex algorithms
• A[1..n]; A[n/4+1 .. 3n/4]
• L, R; L=(R+3L+1)/4, R=(L+3R+1)/4
– We can make it also efficient (see next slide)
Conclusions (3)
• The inductive step is always based on a reduction
from problem size n to problems of size <n. How to
efficiently make the reduction to smaller problems:
– Sometimes one has to spend some effort to find the suitable
element to remove. (see Celebrity Problem)
– If the amount of work needed to combine the subproblems is not
trivial, reduce by dividing in subproblems of equal size – divide
and conquer (see Skyline Problem)