0% found this document useful (0 votes)
16 views17 pages

Divide and Conquer

The document discusses the Divide and Conquer algorithm design strategy, which involves dividing a problem into smaller instances, solving them recursively, and combining their solutions. It provides examples such as mergesort, quicksort, and binary search, illustrating how these algorithms utilize the divide and conquer approach. Additionally, it includes pseudocode and explanations for mergesort and quicksort algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views17 pages

Divide and Conquer

The document discusses the Divide and Conquer algorithm design strategy, which involves dividing a problem into smaller instances, solving them recursively, and combining their solutions. It provides examples such as mergesort, quicksort, and binary search, illustrating how these algorithms utilize the divide and conquer approach. Additionally, it includes pseudocode and explanations for mergesort and quicksort algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

CSE408

Divide And Conquer And


Order Statistics

Lecture #

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Divide And Conquer

• An Algorithm is only called divide


and conquer if it contains two
or more recursive calls
• The Divide And Conquer Strategy used
to
Make a more efficient search algorithm
can also be applied to sorting

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Divide And Conquer(Cont..)

The most-well known algorithm design strategy:


1. Divide instance of problem into two
or more smaller instances

2. Solve smaller instances recursively

3. Obtain solution to original (larger) instance


by combining these solutions

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Divide-and-Conquer Technique (cont.)

A problem size n

Subproblem 1 Subproblem 2
Of size n/2 Of size n/2

A Solution to a subproblem 1 A Solution to a subproblem 2

a solution to the original


Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 nd
ed., Ch. 1
Divide-and-Conquer Examples

 Sorting: mergesort and quicksort

 Binary tree traversals

 Binary search (?)

 Multiplication of large integers

 Matrix multiplication: Strassen’s algorithm

 Closest-pair and convex-hull algorithms

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Mergesort
 Split array A[0..n-1] into about equal
halves and make copies of each half
in arrays B and C
 Sort arrays B and C recursively
 Merge sorted arrays B and C into
array A are as follows:-
• Repeat the following until no elements
remain in one of the arrays:

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Cont…

– compare the first elements in the


remaining unprocessed portions of the arrays
– copy the smaller of the two into A,
while incrementing the index indicating the
unprocessed portion of that array
 Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements
from the other array into A.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Pseudocode of Mergesort

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Pseudocode of Merge

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Mergesort Example

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 71 5 4
The non-
recursive
8 3 2 9 7 1 5 4
version of
Mergesort starts
from merging
3 8 2 9 1 7 4 5
single elements
into sorted
2 3 8 9 1 4 5 7
pairs.

1 2 3 4 5 7 8 9

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Quicksort

 Select a pivot (partitioning element) – here, the first


element
 Rearrange the list so that all the elements
in the first s positions are smaller
than or equal to the pivot
and all the elements in the remaining
n-s positions are larger than or equal to the pivot

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Cont…

 Exchange the pivot with the last element in the first (i.e., )
subarray
— the pivot is now in its final position
 Sort the two subarrays recursively

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Partitioning Algorithm

Time complexity: Θ(r-l)


comparisons

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Quicksort Example

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Binary Search

Very efficient algorithm for searching in sorted array:


K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search); otherwise, continue
searching by the same method in A[0..m-1] if K < A[m]
and in A[m+1..n-1] if K > A[m]
l  0; r  n-1
while l  r do
m  (l+r)/2
if K = A[m] return m
else if K < A[m] r  m-1
else l  m+1
return -1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Cont..

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
! ! !
a nk You
Th

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1

You might also like