You are on page 1of 27

‫جــام ـع ـ ــة الـنـهـضـة‬

‫كلية علـ ـ ـوم الحاسب‬


‫برنامج علـ ــوم الحاسب‬

‫‪Analysis and Design Algorithms‬‬


‫‪CS 203‬‬
‫‪Dr.Amr Hassanain.‬‬
Divide-and-Conquer

Divide instance of problem


The most-well known Solve smaller instances
into two or more smaller
algorithm design strategy: recursively
instances

Distinction between divide


and conquer and decrease
Obtain solution to original
and conquer is somewhat
(larger) instance by
vague. Dec/con typically
combining these solutions
does not solve both
subproblems.
Divide-and-Conquer Technique (cont.)

a problem of size n

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to
the original problem

3
Divide-and-Conquer
Examples
• Sorting: mergesort and quicksort
• Binary tree traversals
• Multiplication of large integers
• Matrix multiplication: Strassen’s
algorithm
• Closest-pair and convex-hull
algorithms.
• Binary search: decrease-by-half (or
degenerate divide&conq.)
• T(n) = aT(n/b) + f (n) where f(n)  (nd),
d0

• Master Theorem: If a < bd, T(n)  (nd)


General •
log n)
If a = bd, T(n)  (nd

Divide-and- •
a)
If a > bd, T(n)  (nlog b

Conquer • Note: The same results hold with O instead


of .

Recurrence • Examples: T(n) = 4T(n/2) + n  T(n)  ?


• T(n) = 4T(n/2) + n2  T(n)  ?
• T(n) = 4T(n/2) + n3  T(n)  ?
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 71 5 4

Merge sort 8 3 2 9 7 1 5 4

Example 3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9
Merge sort

Split array A[0..n-1] in two about Sort arrays B and C recursively Merge sorted arrays B and C into
equal halves and make copies of array A as follows:
each half in arrays B and C
Repeat the following until no elements
remain in one of the arrays:
•compare the first elements in the
remaining unprocessed portions of the
arrays
•copy the smaller of the two into A, while
incrementing the index indicating the
unprocessed portion of that array
Once all elements in one of the arrays are
processed, copy the remaining
unprocessed elements from the other
array into A.
Pseudocode
of Merg esort
• Thinking about recursive
programs: Assume recursive
calls work
• and ask does the
whole program then work?
Pseudocode
of Merge
Analysis of Mergesort
• Number of key comparisons – recurrence:

• C(n) = …

• For merge step:


• C’(n) = …
Number of key comparisons – recurrence:

• C(n) = 2C(n/2) + C’(n) = 2C(n/2) + n-1

For merge step – worst case:


Analysis of • C’(n) = n-1
Mergesort
How to solve?

Number of calls? Number of active calls?


T(n) = aT(n/b) + f (n) where f(n)  (nd), d  0
General Master Theorem: If a < bd, T(n)  (nd)
Divide-and- If a = bd, T(n)  (nd log n)
Conquer
If a > bd, T(n)  (nlog b a )
Recurrence
Using log – Take log b of both sides:
• Case 1: logb a < d, T(n)  (nd)
• Case 2: logb a = d, T(n)  (nd logb n)
• Case 3: logb a > d, T(n)  (nlog b a )
All cases have same efficiency: Θ(n log n)

Number of comparisons in the worst case is


close to theoretical minimum for comparison-
Analysis of based sorting:
Mergesort • - Theoretical min: log2 n! ≈ n log2 n - 1.44n

Space requirement:

• Book’s algorithm?
Analysis of Number of comparisons in
the worst case is close to

Merge sort All cases have same


efficiency: Θ(n log n)
theoretical minimum for
comparison-based sorting:
•- Theoretical min: log2 n! ≈
n log2 n - 1.44n

Space requirement:
•Book’s algorithm: n + n
Sum(1+1/2+1/4+…) = n + n Θ(2)=
Θ(3n)
•Can be done Θ(2n) : Don’t copy Can be implemented
in sort; just specify bounds, copy without recursion
into extra array on merge, then (bottom-up)
copy back
•Any other space required?
•Can be implemented in place.
How …
8 3 2 9 7 1 5 4

Merge sort
Example 8 3 2 9 7 1 5 4

8 3 2 9 71 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9
Quicksort

A[i]p A[i]p

Select a pivot (partitioning element) – Rearrange the list so that all the Exchange the pivot with the last element Sort the two subarrays recursively
here, the first element elements in the first s positions are in the first (i.e., ) subarray — the pivot is
smaller than or equal to the pivot and all now in its final position
the elements in the remaining n-s
positions are larger than or equal to the
pivot (see next slide for an algorithm)
Quicksort Algorithm

QS (A, l, r) if l < r then


s = partition (A, l, r)
QS (A, , )
QS (A, , )
Quicksort Algorithm

QS (A, l, r) if l < r then Ask later: What if we skip final use simpler bounds for first
swap and recursive call?
s = partition (A, l, r)
QS (A, l , s-1)
QS (A, s+1, r )
Quicksort
Example
5 3 1 9 8 2 4 7
Quicksort
Recursion Tree
0 1 2 3 4 5 6 7
5 3 1 9 8 2 4 7

0 .. 7 / s=4
0 .. 3 / s=x
0 .. s-1 / s=?
s+1 .. 3
5 .. 7 / s=y
5 .. y-1 / s=?
y+1 .. 7 / s=?
Analysis of Quicksort

Recurrences:
• Best case:
• Worst case:
• Average case:

Performance:
• Best case:
• Worst case:
• Average case:
Binary Tree Algorithms
Binary tree is a divide-and-conquer ready structure!

Ex. 1: Classic traversals (preorder, inorder, postorder)


Algorithm Inorder(T)
a a
b c b c
d e • • d e
••••

Efficiency: Θ(n)
A. Levitin “Introduction to the Design & Analysis of
Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. 23
Upper Saddle River, NJ. All Rights Reserved.
Binary Tree Algorithms
Binary tree is a divide-and-conquer ready structure!

Ex. 1: Classic traversals (preorder, inorder, postorder)


Algorithm Inorder(T)
if T   a a
Inorder(Tleft) b c b c
print(root of T) d e • • d e
Inorder(Tright) ••••

Efficiency: Θ(n)
24
Binary Tree Algorithms (cont.)

Ex. 2: Computing the height of a binary tree

TL TR

h(T) = ??

Efficiency: Θ(n)

25
Ex. 2: Computing the height of a binary tree

Binary Tree TL TR

Algorithms
(cont.) h(T) = max{h(TL), h(TR)} + 1 if T   and h() = -1

Efficiency: Θ(n)

26

You might also like