concepts of algorithms csc-244 unit 15 & 16 divide-and-conquer algorithms ( binary search and...

47
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University K.S.A.

Upload: horace-palmer

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

Divide-and-conquer technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n CSC 244 Concepts of Algorithms3

TRANSCRIPT

Page 1: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Concepts of AlgorithmsCSC-244

Unit 15 & 16Divide-and-conquer Algorithms

( Binary Search and Merge Sort )

Shahid Iqbal LoneComputer College

Qassim University K.S.A.

Page 2: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

2

Divide and Conquer

CSC 244 Concepts of Algorithms

The most well known algorithm design strategy:1. Divide a large problem into two or more smaller

problems

2. Solve smaller problems recursively

3. Obtain solution to original (larger) problem by combining/merging these solutions of small problems.

Page 3: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Divide-and-conquer technique

subproblem 2 of size n/2

subproblem 1 of size n/2

a solution to subproblem 1

a solution tothe original problem

a solution to subproblem 2

a problem of size n

CSC 244 Concepts of Algorithms 3

Page 4: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Divide and Conquer Examples

• Searching: binary search• Sorting: mergesort • Sorting: quicksort• Tree traversals• Matrix multiplication-Strassen’s algorithm

CSC 244 Concepts of Algorithms 4

Page 5: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Binary SearchRecall your mind, we have studied something about Binary

Search in Unit-1 & Unit-2. Binary search is an efficient algorithm for searching in a

sorted array. Search requires the following steps:

1. Inspect the middle item of an array of size N.2. Inspect the middle of an array of size N/2.3. Inspect the middle item of an array of size N/4 and

so on until Lower Bound becomes > Upper bound.– This implies k = log2N– k is the number of partitions.

CSC 244 Concepts of Algorithms 5

Page 6: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Binary Search• Requires that the array be sorted. • Rather than start at either end, binary

searches split the array in half and works only with the half that may contain the value.

• This action of dividing continues until the desired value is found or the remaining values are either smaller or larger than the search value.

CSC 244 Concepts of Algorithms 6

Page 7: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Pseudo code for Binary Search BinarySearch(A, START, END, key)

{ If (START <= END) { MID = (START + END) / 2 [ compute mid point] if (key equal to A[MID] return MID [ found it, Successful Search.] else if ( key < A[MID] ) [ Recursive Call itself for the lower part of the array ] return BinarySearch( START, MID-1, key) else[Recursive Call itself for the upper part of the array ] return BinarySearch(MID+1, END, key) } return -1 [ failed to find key, Un-Successful Search. ]}

CSC 244 Concepts of Algorithms 7

Page 8: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Binary Search• Suppose that the data set is n = 2k – 1 sorted items, where k

is the number of partitions.• Each time examine middle item. If larger, look left, if

smaller look right.• Second ‘chunk’ to consider is n/2 in size, third is n/4, fourth

is n/8, etc.• Worst case, examine k chunks. n = 2k – 1 so, k = log2(n ).

(Decision binary Search: one comparison on each level)• Best case, O(1). (Found at n/2).• Worst Case: O(log2 (n))

CSC 244 Concepts of Algorithms 8

Page 9: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

5 7 9 13 32 33 42 54 56 880 1 2 3 4 5 6 7 8 9Indices

Contents

Target/Skey is 33The array a looks like this:

Binary Search Example-1 (Successful Search)

start + End

mid = (0 + 9) / 2 (which is 4)33 > A[mid] (that is, 33 > A[4]) Start = Mid + 1So, if 33 is in the array, then 33 is one of:

33 42 54 56 88 5 6 7 8 9

Eliminated half of the remaining elements from consideration because array elements are sorted.

CSC 244 Concepts of Algorithms 9

Page 10: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

5 7 9 13 32 33 42 54 56 880 1 2 3 4 5 6 7 8 9Indices

Contents

Target/Skey is 33The array a looks like this:

Binary Search Example

mid = (5 + 6) / 2 (which is 5)33 == A[mid]So we found 33 at index 5:

33 5

mid = (5 + 9) / 2 (which is 7)33 < A[mid] (that is, 33 < A[7]) End= Mid - 1So, if 33 is in the array, then 33 is one of:

33 42 5 6

Eliminate half of the remaining elements

CSC 244 Concepts of Algorithms 10

Page 11: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

5 7 9 13 32 33 42 54 56 880 1 2 3 4 5 6 7 8 9Indices

Contents

Target/Skey is 8The array a looks like this:

Binary Search Example-2(Un-Successful Search)

start + End

mid = (0 + 9) / 2 (which is 4)8 < A[mid] (that is, 8 < A[4]) End = Mid - 1So, if 8 is in the array, then 8 is one of:

Eliminated half of the remaining elements from consideration because array elements are sorted.

CSC 244 Concepts of Algorithms 11

5 7 9 130 1 2 3Indices

Contents

Page 12: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

5 7 9 13 32 33 42 54 56 880 1 2 3 4 5 6 7 8 9Indices

Contents

Target/Skey is 8The array a looks like this:

Binary Search Example

mid = (2 + 3) / 2 (which is 2)8 < A[mid] (that is, 8 < A[2]) change End = Mid - 1So, now Start = 2 and End = 1, ( that is, Start > End ) which indicates that 8 is not in the array ( Un-successful Search )

mid = (0 + 3) / 2 (which is 1)8 > A[mid] (that is, 8 > A[1]) change Start = Mid + 1So, if 8 is in the array, then 8 is one of:

9 13 2 3 Eliminate half

of the remaining elements

CSC 244 Concepts of Algorithms 12

Page 13: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

CSC 244 Concepts of Algorithms 13

Page 14: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

14CSC 244 Concepts of Algorithms

Page 15: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

C-Language Code for Recursive Binary Search

#include <iostream.h>#include <process.h> int size; // Globle Variable int *A; // integer pointer to store the Base // Address of an integer arrayvoid get_Size( ) { cout<<" Put the size of array: ? ";

cin>>size; A = new int[size]; // Creation of a

// Dynamic Array}

void get_numbers( ){ cout<<"\n\n Put "<<size<<“ Numbers : \n";

for( int i = 0 ; i < size ; i++) { cout<<" Put "<< i+1<<" value: "; cin>> A[i];}

}

15CSC 244 Concepts of Algorithms

void BubbleSort( ){

int i, pass, hold, sw=1;for (pass=1; pass<= size-1; pass++) { sw=0; for (i=0; i< size-pass; i++) { if(A[i] > A[i+1])

{ hold =A[i]; A[i]=A[i+1]; A[i+1]=hold; sw=1;}

} if(sw==0) break;}

}

void print_sorted_numbers(){ cout<<"\n\n\n Sorted numbers are:\n\n"; for ( int i=0; i< size; i++) { cout<<A[i]<<"\t"; }}

Page 16: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

C-Language Code for Recursive Binary Searchint main(){ int SKEY, LOC; get_Size(); get_numbers(); BubbleSort(); print_sorted_numbers( ); while(1) { cout<<"\n\n Put Search Key or 0 to stop: ";

cin>>SKEY;if (SKEY == 0 ) break;LOC = BinarySearch ( 0, size-1, SKEY );if(LOC == -1) cout<<" \n "<<SKEY<<" not found"<<endl;elsecout<<SKEY<< “ found at Index/location: "<<LOC<<endl;

} return 0;} // end of main() function

16CSC 244 Concepts of Algorithms

int BinarySearch(int START, int END, int key) { if (START <= END) { int MID = (START + END) / 2; // compute // mid point. if (key == A[MID]) return MID; // found it, Successful Search. else if (key < A[MID] ) // Call itself for // the lower part of the array return BinarySearch( START, MID-1, key); else // Call itself for the upper part of the array return BinarySearch(MID+1, END, key); } return -1; // Un-Sucessfull Search.}

Page 17: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort

17CSC 244 Concepts of Algorithms

Page 18: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Divide and Conquer1. Base case/criteria: the problem is small

enough, solve directly

2. Divide the problem into two or more similar and smaller subproblems

3. Recursively solve the subproblems

4. Combine solutions to the subproblems

18CSC 244 Concepts of Algorithms

Page 19: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Divide and Conquer - Sort

Problem: • Input: A[n] – unsorted array of n ≥1 integers. • Output: A[n] – sorted in non-decreasing order

19CSC 244 Concepts of Algorithms

Page 20: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Divide and Conquer - Sort• Base case

single element (n=1), return

• Divide A into two subarrays: FirstPart, SecondPart Two Subproblems:

• sort the FirstPart • sort the SecondPart

• Recursively• sort FirstPart• sort SecondPart

• Combine sorted FirstPart and sorted second part

20CSC 244 Concepts of Algorithms

Page 21: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging

Suppose A is sorted list with r elements and B is a sorted list with s elements. The operation that combine the elements of A and B into a single sorted list C with n= r+s elements is called merging. One simple way to merge is to place the elements of B after the elements of A and then use some sorting algorithm on the entire lsit. This method does not take advantage of the fact that A and B are individually sorted. A much more efficient algorithm is merge sort algorithm.

Suppose one is given two sorted decks of cards. The decks are merged as:

21CSC 244 Concepts of Algorithms

Page 22: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

3 10 23 54 1 5 25 75X: Y:

Result:

22CSC 244 Concepts of Algorithms

Page 23: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

3 10 23 54 5 25 75

1

X: Y:

Result:

23CSC 244 Concepts of Algorithms

Page 24: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

10 23 54 5 25 75

1 3

X: Y:

Result:

24CSC 244 Concepts of Algorithms

Page 25: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

10 23 54 25 75

1 3 5

X: Y:

Result:

25CSC 244 Concepts of Algorithms

Page 26: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

23 54 25 75

1 3 5 10

X: Y:

Result:

26CSC 244 Concepts of Algorithms

Page 27: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

54 25 75

1 3 5 10 23

X: Y:

Result:

27CSC 244 Concepts of Algorithms

Page 28: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

54 75

1 3 5 10 23 25

X: Y:

Result:

28CSC 244 Concepts of Algorithms

Page 29: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

75

1 3 5 10 23 25 54

X: Y:

Result:

29CSC 244 Concepts of Algorithms

Page 30: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merging (cont.)

1 3 5 10 23 25 54 75

X: Y:

Result:

30CSC 244 Concepts of Algorithms

Page 31: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

99 6 86 15 58 35 86 4 0

31CSC 244 Concepts of Algorithms

Page 32: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

32CSC 244 Concepts of Algorithms

Page 33: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

33CSC 244 Concepts of Algorithms

Page 34: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

99 6 86 15 58 35 86 4 0

34CSC 244 Concepts of Algorithms

Page 35: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

99 6 86 15 58 35 86 4 0

4 0

35CSC 244 Concepts of Algorithms

Page 36: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

99 6 86 15 58 35 86 0 4

4 0

36CSC 244 Concepts of Algorithms

Page 37: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

15 866 99 35 58 0 4 86

99 6 86 15 58 35 86 0 4

37CSC 244 Concepts of Algorithms

Page 38: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

6 15 86 99 0 4 35 58 86

15 866 99 35 58 0 4 86

38CSC 244 Concepts of Algorithms

Page 39: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

0 4 6 15 35 58 86 86 99

6 15 86 99 0 4 35 58 86

39CSC 244 Concepts of Algorithms

Page 40: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort Example

0 4 6 15 35 58 86 86 99

40CSC 244 Concepts of Algorithms

Page 41: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Another Example MergeSort

Original 24 13 26 1 12 27 38 15Divide in 2 24 13 26 1 12 27 38 15Divide in 4 24 13 26 1 12 27 38 15Divide in 8 24 13 26 1 12 27 38 15Merge 2 13 24 1 26 12 27 15 38Merge 4 1 13 24 26 12 15 27 38Merge 8 1 12 13 15 24 26 27 38

41CSC 244 Concepts of Algorithms

Page 42: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort: Algorithm

Algorithm: MERGESORT (A, N)1- If N=1, Return.

2- Set N1=N/2, N2=N-N1. 3- Repeat for i=0,1,2 . . . . . (N1-1)

Set L [i]=A [i].4- Repeat for j=0,1,2 . . . . . (N2-1)

Set R [j]=A [N1+j].5- Call MERGESORT (L, N1).

6 - Call MERGESORT (R, N2). 7- Call MERGE (A, L, N1, R,N2). 8- Return.

42CSC 244 Concepts of Algorithms

Page 43: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge : Algorithm

Algorithm: MERGE (A, L, N1, R, N2)1- Set i=0, j:=0.

2- Repeat for k=0,1,2 . . . . . (N1+N2-1)If i<N1, then:

If j=N2 or L [i] ≤ R [j], then:Set A [k] =L [i].Set i=i+1;

Else:If j < N2, then:

Set A [k]=R [j].Set j=j+1.

3- Return.

43CSC 244 Concepts of Algorithms

Page 44: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Merge Sort: Algorithm

Merge-Sort (A, n) if n=1 return else

n1 = n/2 and n2 = n – n1

create array L[n1], R[n2]for i = 0 to n1-1 do L[i] ← A[i]for j = 0 to n2-1 do R[j] ← A[n1+j]

Merge-Sort(L, n1) Merge-Sort(R, n2) Merge(A, L, n1, R, n2 )

Space: n

Recursive Call

Time: n

44CSC 244 Concepts of Algorithms

Page 45: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

merge(A,L,n1,R,n2)i = j = 0for k = 0 to n1+n2-1

if i < n1 if j = n2 or L[i] ≤ R[j]

A[k] = L[i]i = i + 1

else if j < n2

A[k] = R[j]j = j + 1

Number of iterations: (n1+n2)

Total time: c(n1+n2) for some c

Merge Sort: Algorithm

CSC 244 Concepts of Algorithms 45

Page 46: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

Home WorkUsing the algorithm described in above

slides, write a c-language code for MERGE-SORT

46CSC 244 Concepts of Algorithms

Page 47: Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University

END

47CSC 244 Concepts of Algorithms