03_D&C Intro_all
03_D&C Intro_all
Algorithms
BinarySearch():
if then return
if return
if then BinarySearch()
else BinarySearch()
1 2 3 4 5 6 7 8 9
10 7
4 10 15 19 20 42 54 87 90 (A, 1, 10, 42)
4 7 10 15 19 20 42 54 87 90 𝑞= 𝐴 [ 5 ] =19
4 7 10 15 19 20 42 54 87 90 𝑞= 𝐴 [ 8 ] =54
4 7 10 15 19 20 42 54 87 90 (A, 6, 7, 42)
4 7 10 15 19 20 42 54 87 90 𝑞= 𝐴 [ 6 ] =20
4 7 10 15 19 20 42 54 87 90 (A, 7, 7, 42)
4 7 10 15 19 20 42 54 87 90 (A, 7, 7, 42)
FOUND
3
Analysis of Binary Search
if n > 1, with
Solve the recurrence by the expansion method:
= 2
= +2 Note: Binary search
may terminate faster
= +4 Genera
than , but the worst-
l
= .…. Case case running time is
= + 2i still
𝑖=log 2 𝑛
= ….
= +
=
¿ 𝟏+𝟐 𝐥𝐨𝐠𝟐 𝐧 4
Binary Search recurrence with the recursion tree method
For
level 𝑇 ( 𝑛 / 2) 2 level
level 𝑇 ( 𝑛 / 2𝑖 ) 2 level
level 2 level (
𝑇 ( 1) level
= [9, 13, 16, 18, 19, 23, 28, 31, 37, 42, 0, 1, 2, 5, 7, 8].
1) Design an -time algorithm to find the value of . ( is the
maximum element in the array)
Find-k(
if then return
if then return Find-k()
Else return Find-k()
6
Exercise 1b Rotated Sorted Array (cont)
2) Design an -time algorithm that for any given , find in
the rotated sorted array, or report that it does not exist.
Find-x()
← Find-k()
if then return BinarySearch()
Else return BinarySearch()
7
Exercise 2a Finding the last 0
You are given an array that contains a sequence of 0
followed by a sequence of 1 (e.g., 0001111111). contains
at least one 0 and one 1.
1 ) Design an -time algorithm that finds the position of the
last , i.e., and .
find-k()
←
if and , RETURN
if , find-k()
else find-k(
8
Exercise 2b Finding the last 0 (cont)
2) Suppose that is much smaller than . Design an -time
algorithm that finds the position of the last 0. (you can re-
use solution of part 1).
1
while
find-k()
The while loop will stop when it finds a 1. Since each time
we double the value of , the while loop performs iterations.
The first 1 occurs somewhere between the positions and .
To find it, we call find-k(), which has cost . Therefore, the
total cost is
9
More complex example: Towers of Hanoi
size
10
Analyzing a recursive algorithm with recurrence
Q: How many steps (movement of discs) are needed?
1: move disks from peg 1 to 2
2: move disk from peg 1 to
3
3: move disks from peg 2 to 3
Thus, the recurrence counting the number of steps is:
11
Solving the recurrence with the Expansion method
= 1
= 2
= + 2 +1
= + +2 +1
= .….
Genera
l = + + +2 +1
Case = .…. geometric series
𝑖=𝑛 −1 = + + +2 +1
12
Exercise 3 Geometric Series
Assume c is a positive constant. Prove that
Set
Then,
For , )
13
Solving the recurrence with the recursion tree method
For
𝑇 ( 𝑛 )1 level node
𝑇 ( 𝑛 −1 1
) 𝑇 ( 𝑛 −1 )1 level nodes
level nodes
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6 divide 𝑂 (1)
2 4 5 7 1 2 3 6 sort 2 𝑇 (𝑛/2)
1 2 2 3 4 5 6 7 merge 𝑂 (𝑛)
15
Merge
Merge. Combine two sorted lists into a sorted whole.
Merge():
create two new arrays and
,
append at the end of and
for to
if then
else
16
Merge: Example
17
Merge: Example
18
Splits each array
into left and right Mergesort: Example
Sorts Left
Sorts Right
Merges results 1 5
2 4
3 8
4 10
5 2
6 6
7 9
8 12 11 11
9 10 3 12
7
1 2
5 4 5
8 8 10
10 2 6
3 9
6 12
7 11 3 12
9 11 7
1
1 4
5 5
4 2
8 8 10
10 2 6 9 12 11
3 3
7 7
11
1 5 4 8 10 2 6 9 12 11 3
3 11 7
1 5 8 10 6 9 11 3
19
Analyzing merge sort
A few simplifications
Replace with
– since we are interested in a big-O upper bound of
Replace with , replace with
– since we are interested in a big-O upper bound of
– Can also think of this as rescaling running time
Assume is a power of , so that we can ignore
– since we are interested in a big-O upper bound of
– for any , let be the smallest power of such that ,
=> ,
as long as is a increasing polynomial function.
20
Solve the recurrence
Simplified merge sort recurrence.
==
=
=
¿ 𝑛 log 2 𝑛+ 𝑛
21
Solve the recurrence 2
log2 𝑛 − 1
=
𝑛
2
Simplified merge sort recurrence.
𝑇 (𝑛)𝑛 𝑛
𝑛 𝑛
𝑇 (𝑛/2) 2 𝑇 (𝑛/ 2) 2 2 ⋅ 𝑛/ 2 ¿𝑛
𝑛 𝑛 𝑛 𝑛
𝑇 (𝑛/ 4)4 𝑇 (𝑛/ 4)4 𝑇 (𝑛/ 4)4 𝑇 (𝑛/ 4)4 4 ⋅ 𝑛/ 4 ¿𝑛
log 2 𝑛
...
𝑇 (𝑛/ 2 𝑖 ) 2 𝑖 ⋅ 𝑛/ 2 𝑖 ¿ 𝑛
...
𝑛
𝑇 (2) 𝑇 (2) ) 𝑇 (2) 𝑇 (2) 𝑇 (2) 𝑇 (2) 𝑇 (2) 2
log2 𝑛 − 1
2
log 2 𝑛 −1 ¿𝑛
𝑇 (1)𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑇 (1) 𝑛𝑇 ( 1 ) ¿𝑛
A: Yes
Since the “merge” step always takes time no matter what the
input is, the algorithm’s running time is actually “the same”
(up to a constant multiplicative factor), independent of the
input.
Equivalently speaking, every input is a worst case input.
The whole analysis holds if we replace every with
23