Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Analysis, Design of Algorithms

CS3610

Dr. Islam Hegazy


Associate Professor

F2023 Analysis, Design of Algorithms 1


Objectives

By the end of this lecture, you will:

➢ Use a divide-and-conquer algorithm to solve some examples.

F2023 Analysis, Design of Algorithms 2


Agenda

01 Divide and conquer 02 Brute force

03 Binary search

F2023 Analysis, Design of Algorithms 3


Divide and conquer

➢ Write a pseudocode for a divide-and-conquer algorithm for the


exponentiation problem of computing an where a > 0 and n is a positive
integer.
1. Divide and conquer

➢ Set up and solve a recurrence relation for the number of multiplications


made by this algorithm.
➢ How does this algorithm compare with the brute-force algorithm for
this problem?

F2023 Analysis, Design of Algorithms 4


Divide and conquer

➢ The following divide-and-conquer algorithm for computing an is based on


𝑛 𝑛
the formula an = 𝑎 2 𝑎 2

➢ Computes an by a divide-and-conquer algorithm


1. Divide and conquer

➢ Input: A positive number a and a positive integer n


➢ Output: The value of an
Algorithm DivConqPower(a, n)
if n = 1
return a
else
𝑛 𝑛
return DivConqPower(a, ) ∗ DivConqPower(a, )
2 2

F2023 Analysis, Design of Algorithms 5


Divide and conquer

➢ The recurrence formula is


𝑛 𝑛
M(n) = M( ) +M( ) + 1 for n > 1, M(1) = 0
2 2
1. Divide and conquer

➢ Solving it by backward substitutions for n = 2k


M(2k) = 2M(2k−1) + 1
= 2[2M(2k−2) + 1] + 1 = 22M(2k−2) + 2 + 1
= 22[2M(2k−3) + 1] + 2 + 1 = 23M(2k−3) + 22 + 2 + 1
= ...
= 2iM(2k−i) + 2i−1 +2i−2 + ... + 1
= ...
= 2kM(2k−k) + 2k−1 +2k−2 + ...+ 1 = 2k − 1 = n − 1 → O(n)
F2023 Analysis, Design of Algorithms 6
Divide and conquer

➢ The recurrence formula is


𝑛 𝑛
M(n) = M( ) +M( ) + 1 for n > 1, M(1) = 0
2 2
1. Divide and conquer

➢ Solving it by Master theorem


𝑛
M(n) = 2M( ) + 1
2

a = 2, b = 2, d = 0
𝑙𝑜𝑔𝑏 𝑎
a> bd, use case 3: O(n )
𝑙𝑜𝑔2 2 = 1 → O(n)

F2023 Analysis, Design of Algorithms 7


Divide and conquer

➢ Brute force for solving the same problem


Algorithm BruteForcePower(a, n)
pow = a
1. Divide and conquer

for i = 2 to n do
pow =* pow
return pow

➢ It takes O(n) time

F2023 Analysis, Design of Algorithms 8


Agenda

01 Divide and conquer 02 Brute force

03 Binary search

F2023 Analysis, Design of Algorithms 9


Brute force

➢ Design an algorithm to rearrange elements of a given array of n real


numbers so that all its negative elements precede all its positive
elements. Your algorithm should be both time- and space-efficient.
2. Brute force

9 4 -10 -3 2 -6

.
.
F2023 Analysis, Design
. of Algorithms 10
Brute force

➢ Puts negative elements before positive (and zeros, if any) in an array


➢ Input: Array A[0. n − 1] of real numbers
➢ Output: Array A[0. n − 1] in which all its negative elements precede
2. Brute force

nonnegative
Algorithm NegBeforePos(A[0..n − 1])
i ← 0; j ← n − 1
while i < j do
if A[i] < 0 //shrink the unknown section from the left
i ← i +1
else //shrink the unknown section from the right
swap(A[i],A[j]) .
.
F2023
j←j−1 Analysis, Design
. of Algorithms 11
Agenda

01 Divide and conquer 02 Brute force

03 Binary search

F2023 Analysis, Design of Algorithms 12


Binary search

➢ Estimate how many times faster an average successful search will be in a


sorted array of 100,000 elements if it is done by binary search versus
sequential search.
3. Binary search

𝐶𝑠𝑒𝑞 (𝑛) 𝑛/2



𝐶𝑏𝑖𝑛 (𝑛) 𝑙𝑜𝑔2 𝑛

For n = 100000 = 105


105/2 1 105 104
= ∗ = ≃ 3000
𝑙𝑜𝑔2 105 2∗5 𝑙𝑜𝑔2 10 𝑙𝑜𝑔2 10

F2023 Analysis, Design of Algorithms 13


Binary search

➢ Sequential search can be used with about the same efficiency whether a list
is implemented as an array or as a linked list. Is it also true for binary search?
(Of course, we assume that a list is sorted for binary search.)
3. Binary search

➢ Unlike an array, where any element can be accessed in constant time,


reaching the middle element in a linked list is a Θ(n) operation. Hence,
though implementable in principle, binary search would be a horribly
inefficient algorithm for searching in a (sorted) linked list.

F2023 Analysis, Design of Algorithms 14


F2023 Analysis, Design of Algorithms 15

You might also like