Professional Documents
Culture Documents
Week 5
Week 5
Week 5
ALGORITHMS
Course Code : AR212
Credit Hours :3
Prerequisite : AR211
Instructor Information
[Pre-req. AR211
Textbook: Introduction to Algorithms, Thomas Cormen, Charls Leiserson & Ronald Rivest. 3rd Edition,
MIT Press, 2009.
By:
Dr. Aryaf A. Al-adwan
Autonomous Systems Dept.
Faculty of Artificial Intelligence
Design and Analysis of Algorithms Course
Divide and ConquerStrategy
• Divide-and-conquer, breaks a problem into sub-problems that are
similar to the original problem, recursively solves the sub problems,
and finally combines the solutions to the sub problems to solve the
original problem.
• You should think of a divide-and-conquer algorithm as having three
parts:
1)Divide the problem into a number of subproblems that are smaller
instances of the same problem.
2)Conquer the subproblems by solving them recursively. If they are
small enough, solve the subproblems as base cases.
3) Combine the solutions to the subproblems into the solution for the
original problem.
Divide and ConquerGeneral Method
Divide and ConquerTime
6/28
Common Divide and ConquerAlgorithms
• Binary Search
• Tower of Hanoi
• Merge Sort
• Quick Sort
• Strassen's Matrix Multiplication Algorithm
• Finding maximum and minimum
• Multiplication of Large Integers
Binary SearchAlgorithm
• Given a sorted array arr[] of n elements, write a
function to search a given element x in arr[].
• A simple approach is to do linear search. The time
complexity of the linear search algorithm is O(n).
Another approach to perform the same task is using
Binary Search.
BinarySearch
• It is a search algorithm that finds the position of a key within a sorted
array.
• Binary search compares the key to the middle element of the array. If
they are not equal, the half in which the key cannot lie is eliminated
and the search continues on the remaining half.
• Again taking the middle element to compare to the key and
repeating this until the key is found.
• If the search ends with the remaining half being empty, then the key
is not in the array.
Binary Search AlgorithmImplementation
1) Iterative Binary Search
2) Recursive Binary Search
0 1 2 3 4 5 6 7 8
2 6 13 21 36 47 63 81 97
• Binarysearch(A,3,3,21) ➔ mid=3 • }
• int mid = (low + high) / 2;
• Return A[mid] = 21 ➔ found if (x == A[mid]) {
• return mid;
• }
• If not found, then low > high • else if (x < A[mid]) {
20
21
22
23
2 k=n
Log 2 k = log n ➔ K = log n
Recursive Binary SearchAnalysis
int binarySearch(int[] A, int low, int high, int x) ➔ T(n)
{
if (low > high) { ➔ 1 ➔ since it will be executed one time when notfound
return -1;
}
int mid = (low + high) / 2; ➔ 1 ➔ since it will be executed one time in every recursivecall
if (x == A[mid]) { ➔ 1 ➔ since it will be executed one time in every recursivecall
return mid;
}
else if (x < A[mid]) { ➔ 1 ➔ since it will be executed one time in every recursivecall
return binarySearch(A, low, mid - 1, x);
}
else { ➔ T(n/2) ➔ At each recursive call we execute the binary search on n/2
return binarySearch(A, mid + 1, high, x);
}
}
TOH(n - 1, A, C, B);
cout << "Move disk from “
<< A<< " to”<< C<< endl;
TOH(n - 1, B, A, C);
}
void main()
{
int n = 3; // Number of disks
TOH(n, 1,2,3); // 1, 2 and 3 are names of rods
}
RecursionTree A=1
B =2
C= 3
Moves ➔
Output
T(n)=2T(n-1)+1
Solving the recurrence usingIteration
• T(n)=2T(n-1)+1 ……………… Eq 1
• Solution
• T(n-1) = 2T(n-2)+1 …………………Eq 2
• Substitute Eq2 in Eq1
• T(n)= 2[2[T(n-2)+1]+1 ➔ T(n) = 2 2 T(n-2)+2+1 ……………… Eq3
• T(n-2)= 2T(n-3)+1 ………………..Eq4
• Substitute Eq4 in Eq3
• T(n)= 2 2 [2T(n-3)+1]+2+1 ➔ T(n)=2 3 T(n-3) +4+2+1
• T(n) = 2k T(n-k)+2 (k-1) + 2 (k-2) + ……………+ 2 2 + 2 1 +1
Cont.
• Base condition T(0) == 1
• n–k=0
• n = k;
• put, k = n
• T(n) = 2n T(0) + 2(n-1) + 2(n-2) + ............ +22 +21 +1
• T(n) = 2n + 2(n-1) + 2(n-2) + ............ +22 +21 + 1 ➔
• It is Geometric Progression (GP) series, and sum is 2(n+1) - 1
• T(n) = O(2n ) ➔ Bad
Number ofmovesinrecursiveTowerofHanoi
MergeSort
• The problem here is to sort a list of n elements.
• Merge Sort is a Divide and Conquer algorithm.
• Merge sort works as follows:
• Divide the unsorted list into n sublists, each containing one
element (a list of one element is considered sorted).
• Repeatedly merge sublists to produce new sorted sublists
until there is only one sublist remaining. This will be the
sorted list.
Merge SortandDivide &Conquer
• To sort an array A with n elements using the merge sort :
1) Divide:
- Divide the array A with n elements into 2 subarrays with n/2 elements.
2) Conquer:
- Sort the subarrays recursively using merge sort.
- when the size of the subarray is 1, then stop.
3) Combine:
- Merge the 2 sorted subarrays.