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

Advanced Algorithms

Analysis & Design


Lecture No : 4

By
Dr. Abdelghafar Refat Elshenawy
How Design Algorithms ?
How Design Algorithms ?
How Design Algorithms
Algorithm design techniques/strategies Important problem types
 sorting

 searching

 string processing

 graph problems

 combinatorial problems

 geometric problems
 numerical problems
Design New Algorithms Techniques

The Carrier Pigeon Techniques


for IoT
Design Algorithms
Divider & Conquer
Divider
&
Conquer
Divider & Conquer

7 × 6 × 5 × 4 ×3 ×2 × 1

6 × 5
Merge Sort

Or
Merge Sort

Merge Sort Visualization

https://opendsa-server.cs.vt.edu/embed/mergesortAV
Merge Sort

log n

n
Merge Sort
• Mergesort is a perfect example of a successful application of the divide-and conquer
technique.
• It sorts a given array A[0..n - 1] by dividing it into two halves A[0..(n/ 2)- 1] and A[(n/ 2)..n -
1], sorting each of them recursively, and then merging the two smaller sorted arrays into
a single sorted one.
• The merging of two sorted arrays can be done as follows. Two pointers (array
indices) are initialized to point to the first elements of the arrays being merged.

• The elements pointed to are compared, and the smaller of them is added to a new
array being constructed; after that, the index of the smaller element is incremented
to point to its immediate successor in the array it was copied from.

• This operation is repeated until one of the two given arrays is exhausted, and then the
remaining elements of the other array are copied to the end of the new array.
Merge Sort

Divider

Conquer
Merge Sort
Merge Sort
Max-Min Problem
Max-Min Problem
Divide and conquer approach for Max. Min problem works in three stages.

•If a1 is the only element in the array, a1 is the maximum and minimum.

•If the array contains only two elements a1 and a2, then the single comparison
between two elements can decide the minimum and maximum of them.

•If there are more than two elements, the algorithm divides the array from the
middle and creates two sub problems. Both sub problems are treated as an
independent problem and the same recursive process is applied to them. This
division continues until sub problem size becomes one or two.
Max-Min Problem
Max-Min Problem
Max-Min Problem
Max-Min Problem
Max-Min Problem
Max-Min Problem
Max-Min Problem
Tower of Hanoi Problem

https://www.mathsisfun.com/games/towerofhanoi.html
Search Problem

Linear Search Binary Search

https://www.cs.usfca.edu/~galles/visualization/Search.html
Search Problem
Linear Search
Search Problem
Binary Search
Search Problem
Binary Search
Recursion

Function Calls Itself


Analysis Recursion Algorithms

Loop if = > &


while < ==
|| ! Loop
Analysis Signal D0 while
print

Analysis Blood Analysis Code


Simplifying Recurrences

n/b n/b n/b n/b n/b n/b


Simplifying Recurrences

4
1 Tree Method

n
1 Tree Method ( Merge Sort )

log n

n
2 Iteration Method
2 Iteration Method

O(n)
2 Iteration Method

log n

O(log n)
2 Iteration Method

O(n log n)
2 Iteration Method

n
O(2 )
3 Master Theorem Method
3 Master Theorem Method

a, b F(n)
3 Master Theorem Method
3 Master Theorem Method
3 Master Theorem Method
3 Master Theorem Method
3 Master Theorem Method
3 Master Theorem Method
3 Master Theorem Exceptions
By
Dr. Abdelghafar Refat Elshenawy

You might also like