Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms. Presentation Topic Divide & Conquer Paradigm to design/develop algorithms.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms. Presentation Topic Divide & Conquer Paradigm to design/develop algorithms."— Presentation transcript:

1 Design and Analysis of Algorithms

2 Presentation Topic Divide & Conquer Paradigm to design/develop algorithms

3 Content of Presentation Topic Search one problem, which can be solved by using divide, & conquer approach. Explain the problem with the help of an example. Design an algorithm-using divide & conquer approach to solve the problems. Analyse an algorithm to find its time & space complexity.

4 About Presented to Sir Tahir Rasheed Presented by Muhammad Ali SP(17)-BS(CS)-031 Section (A)

5 Problem Statement and Consideration Statement Given an array array[0.. n-1] of distinct integers, the task is to find a local minima in it. We say that an element array[x] is a local minimum if it is less than or equal to both its neighbors. Consideration I. For corner elements, we need to consider only one neighbor for comparison. II. There can be more than one local minima in an array, we need to find any one of them.

6 Related Example Input: array[] = {9, 8, 7, 6, 3, 4 }; Output: Index of local minima is 4 The output prints index of 3 because it is smaller than both of its neighbors.

7 Simple Solution The simple solution is to traverse the array linear and find the local minima. The complexity of the solution is O(n).

8 Divide & Conquer Binary search Array[] = {9, 8, 7, 6, 3, 4 }; Now I want to show the analyze on above given example in following steps

9 Algorithm int localMinUtil(int arr[], int low, int high, int n) { int mid = low + (high - low)/2; if ((mid == 0 || arr[mid-1] > arr[mid]) && (mid == n-1 || arr[mid+1] > arr[mid])) return mid; else if (mid > 0 && arr[mid-1] < arr[mid]) return localMinUtil(arr, low, (mid -1), n); else return localMinUtil(arr, (mid + 1), high, n); }

10 Step 1 Find the middle element Low=0, high=5 Array[] = {9, 8, 7, 6, 3, 4 };Array[] = {9, 8, 7, 6, 3, 4 }; Mid= (low + high)/2 {9, 8, 7} {6, 3, 4 } Mid index = 2 Array[2] = 7

11 Step 2 if ((mid == 0 || array[mid-1] > array[mid]) && (mid == n-1 || array[mid+1] > array[mid])) return mid; Mid=2, array[mid]=7, array[mid-1]=8, array[mid+1]=6 Note: This statement is not run in algorithm.

12 Step 3 else if (mid > 0 && array[mid-1] < array[mid]) return localMinUtil (array, low, (mid -1), n); Mid=2, array[mid]=7, array[mid-1]=8, array[mid]=7 Note: This statement is not run in algorithm.

13 Step 4 else return localMinUtil(array, (mid + 1), high, n); Note: This statement is run in algorithm.

14 Step 5 Find the middle element Low=3 high=5 Array[] = {9, 8, 7, 6, 3, 4 };Array[] = {9, 8, 7, 6, 3, 4 }; Mid= (low + high)/2 {9, 8, 7} {6, 3, 4 } Mid index = 4 {6, 3} { 4 } Array[4] = 3

15 Solution Statement if ((mid == 0 || array[mid-1] > array[mid]) && (mid == n-1 || array[mid+1] > array[mid])) return mid; Mid=4, array[mid]=3, array[mid-1]=6, array[mid+1]=5, Note: This statement is run in algorithm and consider as solution.

16 Time Complexity of the Given Algorithm int localMinUtil(int arr[], int low, int high, int n) T(N) { int mid =( low +high) /2; 1 if ((mid == 0 || arr[mid-1] > arr[mid]) && (mid == n-1 || arr[mid+1] > arr[mid])) 1 return mid; else if (mid > 0 && arr[mid-1] < arr[mid]) return localMinUtil(arr, low, (mid -1), n); n/2 else return localMinUtil(arr, (mid + 1), high, n); n/2 }

17 Time Complexity of the Given above Algorithm T(n)= T(n/2)+c (Equation 1) T(n/2)= T(n/4)+c (Equation 2) Substitute eq 2 into 1 T(N)= T(N/4)+2c (Equation 3) T(N/4)=T(N/8)+c (Equation 4) Substitute eq 4 into 3 T(N)= T(N/8)+3c (Equation 5)

18 Time Complexity of the Given above Algorithm Cont..

19

20

21


Download ppt "Design and Analysis of Algorithms. Presentation Topic Divide & Conquer Paradigm to design/develop algorithms."

Similar presentations


Ads by Google