0% found this document useful (0 votes)
12 views43 pages

Chapter 2

Uploaded by

elbana795
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views43 pages

Chapter 2

Uploaded by

elbana795
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

1

Chapter 2

Divide and Conquer


2

Objectives
 Describe the divide-and-conquer approach to
solving problems
 Apply the divide-and-conquer approach to solve a
problem
 Determine complexity analysis of divide and
conquer algorithms
3
2.1 Binary Search
 We present a recursive . The steps are
If x equals the middle item, quit. Otherwise:
1. Divide the array into two subarrays about half .
a) If x is smaller than the middle item, choose the left subarray.
b) If x is larger than the middle item, choose the right subarray.
2. Conquer (solve) the subarray : whether x is in that subarray.
Unless the subarray is sufficiently small, use recursion to do this.
3. Obtain the solution to the array from the solution to the subarray.
4

Binary search
Example:

10 12 13 14 18 20 25 27 30 35 40 45 47
Algorithm 2.1 5

Binary Search (Recursive)


 Problem: ... Inputs: ... Outputs:, the location ...
void location (index low, index high)
{
index mid;
if (low > high )
return 0;
else {
mid= ( low + high )/2  ;
if ( x == S [ mid ] ) return mid
else if ( x < S [ mid ] ) return location (low, mid - 1);
else
return location ( mid + 1, high )
}
}
6

 n, S, and x are not parameters to function location.


Because
o they remain unchanged in each recursive call,
o a new copy of any variable passed to the routine is
made in each recursive call.
oIf a variable’s value does not change, the copy is
unnecessary.
n =13 elements, search x=18
7
8

 Tail-recursion : no operations are done after the recursive call


 When a routine calls another routine, it is necessary to save the
first routine’s pending results by pushing them onto the stack of
activation records and so on.
 When control is returned to a calling routine, its activation record
is popped from the stack .
 The number of activation records pushed onto the stack is
determined by the depth reached in the recursive calls.
 For Binary Search, the stack reaches a depth that in the worst
case is about lg n + 1.
9
10

Worst Case Complexity Analysis


 x is larger than all array items.
 n is a power of 2 and x > S[n]
 W(n) = the number of comparisons in the recursive call
 W(n) = W(n/2) + 1 for n>1 and n power of 2
 W(1) = 1

 Example B1 in Appendix B:
 W(n) = lg n + 1
 n not a power of 2 (exercise)
 W(n) = lg n  +1  θ(lg n)
11

 Algorithm B.1 - Factorial


 Problem: Determine n! = n * (n − 1) ! if n ≥ 1.
0! = 1
 tn : the number of multiplications done for a given value of n
tn= tn-1+1
 initial condition : no multiplications when n = 0  t0 =0
12

t1 =1
t2 =t1 +1 =2
t3 =3

tn =n

Proof by induction
13

2.2 Mergesort
 Sort an array S of size n ( let n be a power of 2)
 Divide S into 2 sub-arrays of size n/2
 Conquer (solve) recursively sort each sub-array until
array is sufficiently small (size 1)
 Combine merge the solutions to the sub-arrays into a
single sorted array
14
Figure 2.2
15
Algorithm 2.2 - Mergesort
Problem: Sort n keys in nondecreasing sequence.
Inputs: +ve int n, array of keys S indexed from 1 to n.
Outputs: the sorted array S in nondec. order.
void mergesort (int n, keytype S [ ])
{
if (n > 1) {
const int h = n /2  ; m = n – h;
keytype U [1 . . h] , V [1 . . m] ,
copy S [ 1 ] through S [ h ] to U [1 ] through U [ h ] ;
copy S [ h + 1 ] through S [ n ] to V [1 ] through V [ m ] ;
mergesort ( h, U);
mergesort ( m, V);
merge ( h, m, U, V, S);
}
}
16

Algorithm 2.3 - Merge


 Merges the two arrays U and V :
 Input size
 h the number of items in U
 m the number of items in V
 Outputs: the array S containing the keys in
nondecreasing order.
17
18
19

Worse-Case Time Complexity (Merge)


 The number of comparisons depends on both h and m.
 Basic operation: the comparison of U [ i ] with V [ j ].
 Input size: h and m,
 The worst case : when one of the indices— say, i- has
reached its exit point h + 1 whereas the other index j has
reached m, 1 less than its exit point.
 at which time the loop is exited because i equals h + 1.
Therefore,
W(h, m)= h+m-1
20

Worst-Case Time Complexity Analysis- Mergesort


 W(n) = time to sort U + time to sort V + time to merge
 W(n) = W(h) + W(m) + h+m-1
 First analysis assumes n is a power of 2
 h = n/2 = n/2
 m = n – h = n – n/2 = n/2
 h + m = n/2 + n/2 = n
 W(n) = W(n/2) + W(n/2) + n – 1
= 2W(n/2) + n-1 for n > 1 and n a power of 2
 W(1) = 0

W(n) =2W(n/2) + n-1 for n > 1 and n a power of 2


W(1) = 0

 See Example B19 in Appendix B


21

Example B19 in Appendix B


W(n)= (log2n)n – (n -1)  θ(n lg n)
22

 n not a power of 2
 W(n)=W(n/2) + W(n/2) + n-1
 From Theorem B4: (W(n) eventually nondecreasing,
n lg n is smooth )
 W(n)  Θ(n lg n)
23

EXERCISE
Solve
T(n) = 2T(n/2) + n, T(1)=1
24

T(n) = 2T(n/2) + n = 2[ 2T(n/22) + n/2] + n


= 22T(n/22) + 2n = 22 [2T(n/23) + n/22] +2n
=23T(n/23) + 3n

=2kT(n/2k) + kn
If n=2k then we have
T(n) = nT(1) + (log2n)n = n+ n log2n
= Θ (n log2n)
25

 An in-place sort is a sorting algorithm that does not use


any extra space beyond that needed to store the input.
 Algorithm 2.2 is not an in-place sort (it uses U and V )
 At the top level, the sum of the numbers of items in these
two arrays is n.
 However, it is possible to reduce the amount of extra
space to only one array containing n items. This is
accomplished by doing much of the manipulation on
the input array S.
26
Algorithm 2.4 – Mergesort2
Problem: Sort n keys in nondecreasing sequence.
Inputs: +ve int n, array of keys S indexed from 1 to n.
Outputs: the sorted array S in nondec. order.
27

Algorithm 2.5 - Merge 2


 Problem: Merge the two sorted subarrays of S created in
Mergesort 2.
 Inputs: indices low, mid, and high, and the subarray of S
indexed from low to high. The keys in array slots from low
to mid are already sorted in nondecreasing order, as are
the keys in array slots from mid + 1 to high.
 Outputs: the subarray of S indexed from low to high
containing the keys in nondecreasing order.
28
29

2.4 Quicksort
 Array recursively divided into two partitions and
recursively sorted
 Division based on a pivot
 pivot divides the array into two sub-arrays
 All items < pivot placed in sub-array before pivot
 All items >= pivot placed in sub-array after pivot
30

Example 2.3
 Suppose the array contains these numbers in
sequence:
Pivot item

15 22 13 27 12 10 20 25
Pivot item

13 12 10 15 22 27 20 25
Sort the subarrays:
10 12 13 15 20 22 25 27
31

 Algorithm 2.6 – Quicksort


 Problem: Sort n keys in nondecreasing order.
 Inputs: positive integer n, array of keys S indexed from 1 to n.
 Outputs: the array S containing the keys in nondecreasing
void quicksort(index low, index high)
{
index pivotpoint;
if (high > low){
partition(low, high, pivotpoint);
quicksort(low, pivotpoint - 1);
quicksort(pivotpoint + 1,high);
}
}
32

15 22 13 27 12 10 20 25

swap S[2] and S[3]


j i void partition (index low, index
15 13 22 27 12 10 20 25 high, index& pivot)
{
index i,j; keytype pivotitem;
j=2 I swap S[3] and S[5] pivotitem = S[low];
j=low;
15 13 12 27 22 10 20 25 for (i=low+1; i<=high; i++)
if (S[i] < pivotitem){
j++;
j=3 i swap S[i] and S[j];
15 13 12 10 22 27 20 25 }
pivot= j;
swap S[low] and S[pivot];
j=4, pivot=j }

Exchange S[low] and S[pivot]


10 13 12 15 22 27 20 25
 Algorithm 2.7 - Partition
33
 Problem: Partition the array S for Quicksort.
 Inputs: two indices, low and high, and the subarray of S [low ,high].
 Outputs: pivotpoint, the pivot point for the subarray S [low ,high].
void partition (index low, index high, index& pivot)
{
index i,j; keytype pivotitem;
pivotitem = S[low];
j=low;
for (i=low+1; i<=high; i++)
if (S[i] < pivotitem){
j++;
swap S[i] and S[j];
}
pivot= j;
swap S[low] and S[pivot];
}
34
Figure 2.3
35

Basic operation
Comparison of S[i] with pivot item
Input size n
36

Every-Case Complexity Analysis of Partition

Input size: n = high − low + 1, the number of


items in the subarray.
Because every item except the first is
compared,
T(n) = n - 1
37

Worst-Case Complexity Analysis


of Quicksort
 Array is repeatedly sorted into an empty sub-array
which is less than the pivot and a sub-array of n-1
containing items greater than pivot
 If there are k keys in the current sub-array, k-1 key
comparisons are executed
38

Worst-Case Complexity Analysis of Quicksort


 the worst case occurs if the array is already sorted
 T(n) is specified because analysis is for the every-
case complexity for the class of instances already
sorted in non-decreasing order
T(n) = time to sort left sub-array + time to sort right
sub-array + time to partition
T(n) = T(0) + T(n-1) + n – 1
T(n) = T(n – 1) + n – 1 for n > 0
T(0) = 0
From B16
T(n) = n(n-1)/2
39

Worst Case
Use induction to show it is the worst case
W(n)<= n(n-1)/2
40

Average-Case Time Complexity of Quicksort


 Value of pivotpoint is equally likely to be any of the
numbers from 1 to n
 Average obtained is the average sorting time when
every possible ordering is sorted the same number
of times
 Let p be the value of pivotpoint returned by partition
41


42
43

You might also like