SlideShare uma empresa Scribd logo
1 de 8
Baixar para ler offline
Computer Science 385
                                     Analysis of Algorithms
                                     Siena College
                                     Spring 2011

                     Topic Notes: Divide and Conquer
Divide–and-Conquer is a very common and very powerful algorithm design technique. The gen-
eral idea:

   1. Divide the complete instance of problem into two (sometimes more) subproblems that are
      smaller instances of the original.
   2. Solve the subproblems (recursively).
   3. Combine the subproblem solutions into a solution to the complete (original) instance.

While the most common case is that the problem of size n is divided into 2 subproblems of size
n
2
  . But in general, we can divide the problem into b subproblems of size n , where a of those
                                                                           b
subproblems need to be solved.
This leads to a general recurrence for divide-and-conquer problems:

                      T (n) = aT (n/b) + f (n), where f (n) ∈ Θ(nd ), d ≥ 0.

When we encounter a recurrence of this form, we can use the master theorem to determine the
efficiency class of T :

                                          Θ(nd )     if a < bd
                                         
                                         
                                T (n) =  Θ(nd log n) if a = bd
                                        
                                          Θ(nlogb d ) if a > bd

Application of this theorem will often allow us to do a quick analysis of many divide-and-conquer
algorithms without having to solve the recurrence in detail.


Mergesort
Each sorting procedure we have considered so far is an “in-place” sort. They require only Θ(1)
extra space for temporary storage.
Next, we consider a divide-and-conquer procedure that uses Θ(n) extra space in the form of a
second array.
It’s based on the idea that if you’re given two sorted arrays, you can merge them into a third in Θ(n)
time. Each comparison will lead to one more item being placed into its final location, limiting the
number of comparisons to n − 1.
CS 385                               Analysis of Algorithms                            Spring 2011



In the general case, however, this doesn’t do anything for our efforts to sort the original array. We
have completely unsorted data, not two sorted arrays to merge.
But we can create two arrays to merge if we split the array in half, sort each half independently,
and then merge them together (hence the need for the extra Θ(n) space).
If we keep doing this recursively, we can reduce the “sort half of the array” problem to the trivial
cases.
This approach, the merge sort, was invented by John von Neumann in 1945.
How many splits will it take? Θ(log n)
Then we will have Θ(log n) merge steps, each of which involves sub-arrays totaling in size to n, so
each merge (which will be k independent merges into n -element arrays) step has Θ(n) operations.
                                                    k

This suggests an overall complexity of Θ(n log n).
Let’s look at pseudocode for this:

mergesort(A[0..n-1])
  if n>1
    copy first half of array A into a temp array B
    copy second half of array A into a temp array C
    mergesort(B)
    mergesort(C)
    merge(B and C into A)

where the merge operation is:

merge(B[0..p-1], C[0..q.1], A[0..(p+q-1)])
  while B and C have more elements
    choose smaller of items at the start of B or C
    remove the item from B or C
    add it to the end of A
  copy remaining items of B or C into A

Let’s do a bit more formal analysis of mergesort. To keep things simple, we will assume that
n = 2k .
Our basic operation will be the number of comparisons that need to be made.
The recurrence for the number of comparisons for a mergesort of a problem of size n = 2k is

                      C(n) = 2C(n/2) + Cmerge (n) for n > 1, C(1) = 0.

The best, worst, and average cases depend on how long we are in the main while loop in the merge
operation before one of the arrays is empty (as the remaining elements are then taken from the

                                                 2
CS 385                               Analysis of Algorithms                            Spring 2011



other array with no comparions needed). Let’s consider the worst case, where the merge will take
n−1 comparisons (one array becomes empty only when the other has a single element remaining).
This leads to the recurrence:

                  Cworst (n) = 2Cworst (n/2) + n − 1 for n > 1, Cworst (1) = 0.

The master theorem, gives is that Cworst (n) ∈ Θ(n log n).


Quicksort
Another very popular divide and conquer sorting algorithm is the quicksort. This was developed
by C. A. R. Hoare in 1962.
Unlike merge sort, quicksort is an in-place sort.
While merge sort divided the array in half at each step, sorted each half, and then merged (where
all work is in the merge), quicksort works in the opposite order.
That is, quicksort splits the array (which takes lots of work) into parts consisting of the “smaller”
elements and of the “larger” elements, sorts each part, and then puts them back together (trivially).
It proceeds by picking a pivot element, moving all elements to the correct side of the pivot, re-
sulting in the pivot being in its final location, and two subproblems remaining that can be solved
recursively.
A common and simple choice for the pivot is the leftmost element. We put it into its correct
position and put all elements on their correct side of the pivot.
Psuedocode for a quicksort:

quicksort(A[l..r]) // we would start with l=0, r=n-1
  if l < r
    s = partition(A[l..r]) // s is pivot’s location
    quicksort(A[l..s-1])
    quicksort(A[s+1..r])

partition(A[l..r])
  p = A[l] // leftmost is pivot
  i = l; j = r+1
  do
    do i++ until A[i] >= p
    do j-- until A[j] <= p
    swap(A[i],A[j])
  until i>=j
  swap(A[i],A[j]) // undo last
  swap(A[l],A[j]) // swap in pivot
  return j

                                                    3
CS 385                                 Analysis of Algorithms                          Spring 2011



Note: we always make a recursive call on a smaller array (but it’s easy to make a coding mistake
where it doesn’t, and then the sort never terminates).
The complexity of quicksort is harder to evaluate than merge sort because the pivot will not always
wind up in the middle of the array (in the worst case, the pivot is the largest or smallest element).
Again, the basic operation will be the comparisons that take place in the partition.
The partition method is clearly Θ(n) because every comparison results in left or right
moving toward the other and quit when they cross.
In the best case, the pivot element is always in the middle.
This would lead to a number of comparisons according to the recurrence:

                      Cbest (n) = 2Cbest (n/2) + n for n > 1, Cbest (1) = 0.

By solving the recurrence or applying the Master Theorem, we find that Cbest (n) ∈ Θ(n log n),
exactly like merge sort.
In the worst case the pivot is at one of the ends and quicksort behaves like a selection sort. This
occurs with already-sorted input or reverse-sorted input. To analyze this case, think of the first
pass through the full n-element array. The first element, A[0], is chosen as the pivot. The left-
to-right inner loop will terminate after one comparison (since A[0] is the smallest element). The
right-to-left inner loop will perform comparisons with A[n-1], A[n-2], ... all the way down to
A[0] since we need to “move” the pivot item to its own position. That’s n + 1 comparisons for the
partition step. In the process, the problem size is decreased by 1, so there will be n comparisons in
the next step, n−1 in the third, and so on. We stop after processing the two-element case (requiring
3 comparisons), so the total comparisons is given by:

                                                         (n + 1)(n + 2)
                Cworst (n) = (n + 1) + n + · · · + 3 =                  − 3 ∈ Θ(n2 )
                                                               2

A careful analysis can show that quicksort is Θ(n log n) in the average case (under reasonable
assumptions on distribution of elements of array). We can proceed by assuming that the partition
                                                      1
can occur at any position with the same probability ( n ). This leads to a more complex recurrence:

            1 n−1
 Cavg (n) =       [(n + 1) + Cavg (s) + Cavg (n − 1 − s)] for n > 1, Cavg (0) = 0, Cavg (1) = 0.
            n s=0

We will not solve this in detail, but it works out to:

                          Cavg (n) ≈ 2n ln n ≈ 1.38n log2 n ∈ Θ(n log n).

Clearly, the efficiency of quicksort depends on the selection of a good pivot. Improving our chances
to select a good pivot will ensure quicker progress. One way to do this is to consider three can-
didates (often the leftmost, rightmost, and middle elements) and take the median of these three as
the pivot value.

                                                   4
CS 385                                  Analysis of Algorithms                              Spring 2011



Other improvements include switching to a simpler (Θ(n2 )) sort once the subproblems get below
a certain threshold size, and implementing quicksort iteratively rather than recursively.
Quicksort is often the method of choice for general purpose sorting with large data sizes.


Binary Search
We next briefly recall the idea of a binary search – the efficient procedure for searching for an item
in a sorted array.
Here is a nonrecursive algorithm that implements binary search:

bin_search(A[0..n-1], K)
  l=0; r=n-1
  while (l <= r)
    m = floor((l+r)/2)
    if (K == A[m]) return m
    else if (K < A[m]) r=m-1
    else l=m+1
  return -1

To find an item, we look for it in the middle. If we do not find it, we look only to the left (if the
target is smaller than the middle element) or to the right (if the target is larger than the middle
element).
We will not look in detail at the recurrence or its solution, just note that this is a very fast algorithm:
requiring Θ(log n) comparisons in the worst case.


Binary Trees
No discussion of divide and conquer can proceed without a discussion of binary trees and algo-
rithms on them.
We discussed the idea of tree data structures and introduced lots of tree-related terminology at the
start of the semester. For now, we will just consider a few examples.
We can make analysis convenient by defining a binary tree as either the empty set, or a tree node
plus two other binary trees: a left subtree and a right subtree.
To find the height of a tree:

height(T)
  if (T is empty) return -1
  else return max(height(T.left),height(T.right)) + 1

To analyze this, we first note that the size of the problem is the number of nodes in our tree T ,
denoted n(T ).

                                                    5
CS 385                                 Analysis of Algorithms                                 Spring 2011



The basic operation is either the comparison needed to find the max or the addition of 1 once we
find the max. A recurrence for either:

            A(n(T )) = A(n(T.lef t)) + A(n(T.right)) + 1 for n(T ) > 0, A(0) = 0

Note, however, that there are in fact more checks to see if the tree is empty than there are additions
or comparisons. That check happens at the base case only.
The number of additions/max comparisons: A(n) = n, while the number of checks for an empty
tree: C(n) = 2n + 1.
The other key divide and conquer operations are the recursively defined tree traversals, which we
discussed earlier:

   1. preorder: visit the root, then visit the left subtree, then visit the right subtree.

   2. in-order visit the left subtree, then visit the root, then visit the right subtree.

   3. postorder: visit the left subtree, then visit the right subtree, then visit the root.

Pseudocode is staightforward, for example:

inorder(T)
  if (T is not empty)
    inorder(T.left)
    visit(T.root)
    inorder(T.right)

Analysis is similar to that of height for this and for preorder and postorder. Each node is visited
once.


Strassen’s Matrix Multiplication
Our text describes a divide and conquer multiplication of large numbers, but since you have seen
that in Discrete, we will move along to consider Strassen’s matrix-matrix multiplication.
This algorithm improves upon the standard matrix-matrix multiplication by observing that the
product of two 2 × 2 matrices can be performed using 7 multiplications instead of the usual 8.
This in itself does not seem that significant, especially when we consider that the standard algo-
rithm requires 4 additions, while Strassen’s algorithm requires 18 additions/subtractions.
The real benefit comes when we apply this idea to get a divide-and-conquer algorithm for multi-
plying matrices. To multiply 2 n × n matrices, we break it into 4 n × n matrices and use Strassen’s
                                                                  2   2
algorithm to multiply them.


                                                    6
CS 385                                Analysis of Algorithms                             Spring 2011



Our recurrence for the number of multiplications with this approach:

                            M (n) = 7M (n/2) for n > 1, M (1) = 1.

From which the Master Theorem (or a backward substitution) will yield:

                                    M (n) ∈ Θ(nlog2 7 ) ≈ n2.807 .

Which is definitely a slower rate of growth than Θ(n3 ).
An analysis of the (larger) number of additions/subtractions results in the same efficiency class:
A(n) ∈ Θ(nlog2 7 ).
Many other approaches have been invented with even smaller rates of growth than Strassen’s algo-
rithm, but most have constant factors that make them impractical for real usage.


Computational Geometry
We return to two familiar problems from computational geometry to explore divide-and-conquer
solutions that are more efficient than the brute force approaches considered previously.

Closest Pairs
Our problem is to find among a set of points in the plane the two points that are closest together.
We begin by assuming that the points in the set are ordered by increasing x coordinate values. If
this is not the case, the points can certainly be sorted in O(n log n) time as a preprocessing step.
                                                                                   n
We then divide the points into two subsets, S1 and S2 , each of which contains     2
                                                                                       points (which is
easy since the points are sorted by x coordinate).
We then recursively find the closest pair of points in each subset S1 and S2 . If the distance between
the closest pair in S1 = d1 and the distance between the closest pair in S2 = d2 . We then know
that d = min{d1 , d2 } is an upper bound on the minimum distance between any pair, but we still
need to make sure we check for shorter distances where one point is in S1 and the other is in S2 .
The only points which might be closer together than distance d are those within a strip of width d
from the dividing line between the subsets. For each point within that strip and within one subset,
we potentially need to consider all points from within the strip within the other subset. That still
sounds like a lot of work. The key observation is that for each point on one side, we have to
consider points whose y coordinates are within d. This will mean at most 6 points from the other
side, since if there are more points than that within d in the y coordinate on the other side, at least
one pair from among that point would be closer than distance d to each other.
So how do we find those points to check? They can be found quickly if we also keep the points
sorted in order by y coordinate. Still, this seems difficult but it can be done efficiently (see the
text’s description for details).

                                                  7
CS 385                                Analysis of Algorithms                             Spring 2011



We end up with a recurrence:


                                      T (n) = 2T (n/2) + O(n)

which given an overall time of T (n) ∈ O(n log n).

Quickhull
The other computational geometry problem discussed in the text is called quickhull – an algorithm
for finding the convex hull of a set of points. We will not discuss it in class, but it is worth reading.




                                                   8

Mais conteúdo relacionado

Mais procurados

Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Firefly exact MCMC for Big Data
Firefly exact MCMC for Big DataFirefly exact MCMC for Big Data
Firefly exact MCMC for Big DataGianvito Siciliano
 
Application of Numerical Methods (Finite Difference) in Heat Transfer
Application of Numerical Methods (Finite Difference) in Heat TransferApplication of Numerical Methods (Finite Difference) in Heat Transfer
Application of Numerical Methods (Finite Difference) in Heat TransferShivshambhu Kumar
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problemsSumita Das
 
Planetary Science Assignment Help
Planetary Science Assignment HelpPlanetary Science Assignment Help
Planetary Science Assignment HelpEdu Assignment Help
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...
Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...
Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...Hector Zenil
 
Fractal dimension versus Computational Complexity
Fractal dimension versus Computational ComplexityFractal dimension versus Computational Complexity
Fractal dimension versus Computational ComplexityHector Zenil
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm CaseyHaaland
 

Mais procurados (20)

Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Firefly exact MCMC for Big Data
Firefly exact MCMC for Big DataFirefly exact MCMC for Big Data
Firefly exact MCMC for Big Data
 
Statistical Physics Assignment Help
Statistical Physics Assignment Help Statistical Physics Assignment Help
Statistical Physics Assignment Help
 
Diffusion Homework Help
Diffusion Homework HelpDiffusion Homework Help
Diffusion Homework Help
 
Digital Signal Processing Assignment Help
Digital Signal Processing Assignment HelpDigital Signal Processing Assignment Help
Digital Signal Processing Assignment Help
 
Application of Numerical Methods (Finite Difference) in Heat Transfer
Application of Numerical Methods (Finite Difference) in Heat TransferApplication of Numerical Methods (Finite Difference) in Heat Transfer
Application of Numerical Methods (Finite Difference) in Heat Transfer
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
 
Planetary Science Assignment Help
Planetary Science Assignment HelpPlanetary Science Assignment Help
Planetary Science Assignment Help
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...
Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...
Fractal Dimension of Space-time Diagrams and the Runtime Complexity of Small ...
 
Fractal dimension versus Computational Complexity
Fractal dimension versus Computational ComplexityFractal dimension versus Computational Complexity
Fractal dimension versus Computational Complexity
 
Signals Processing Homework Help
Signals Processing Homework HelpSignals Processing Homework Help
Signals Processing Homework Help
 
Stochastic Processes Assignment Help
Stochastic Processes Assignment HelpStochastic Processes Assignment Help
Stochastic Processes Assignment Help
 
BSE and TDDFT at work
BSE and TDDFT at workBSE and TDDFT at work
BSE and TDDFT at work
 
ICLR 2018
ICLR 2018ICLR 2018
ICLR 2018
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
 
Physics Assignment Help
Physics Assignment HelpPhysics Assignment Help
Physics Assignment Help
 
Statistical Physics Assignment Help
Statistical Physics Assignment HelpStatistical Physics Assignment Help
Statistical Physics Assignment Help
 

Destaque

Body movement and mental health
Body movement and mental healthBody movement and mental health
Body movement and mental healthKelly Bawden
 
Research Paper
Research PaperResearch Paper
Research Paperhenry515
 
Picture Worklog
Picture WorklogPicture Worklog
Picture Workloghenry515
 
Final Presentation
Final PresentationFinal Presentation
Final Presentationhenry515
 
Adhiveshan 2010
Adhiveshan   2010Adhiveshan   2010
Adhiveshan 2010Atul Ahire
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
전문성을 가지고 일해라!
전문성을 가지고 일해라!전문성을 가지고 일해라!
전문성을 가지고 일해라!zeldababo
 
Agricultura de eeuu
Agricultura de eeuuAgricultura de eeuu
Agricultura de eeuusupersergio
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unitchidabdu
 
قصص الانبياء لابن كثير
قصص الانبياء لابن كثيرقصص الانبياء لابن كثير
قصص الانبياء لابن كثيرHamza Mabrouk
 
Dunya internet Agın’da Kisisel Markalasma
Dunya internet Agın’da  Kisisel MarkalasmaDunya internet Agın’da  Kisisel Markalasma
Dunya internet Agın’da Kisisel MarkalasmaZafer Teber
 

Destaque (18)

Body movement and mental health
Body movement and mental healthBody movement and mental health
Body movement and mental health
 
My dream lesson
My dream lessonMy dream lesson
My dream lesson
 
B O D ( Y)
B  O  D ( Y)B  O  D ( Y)
B O D ( Y)
 
Group@me1
Group@me1Group@me1
Group@me1
 
Speech
SpeechSpeech
Speech
 
Research Paper
Research PaperResearch Paper
Research Paper
 
Picture Worklog
Picture WorklogPicture Worklog
Picture Worklog
 
Final Presentation
Final PresentationFinal Presentation
Final Presentation
 
Adhiveshan 2010
Adhiveshan   2010Adhiveshan   2010
Adhiveshan 2010
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
전문성을 가지고 일해라!
전문성을 가지고 일해라!전문성을 가지고 일해라!
전문성을 가지고 일해라!
 
Agricultura de eeuu
Agricultura de eeuuAgricultura de eeuu
Agricultura de eeuu
 
Wwe ppt
Wwe pptWwe ppt
Wwe ppt
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
Retro Pazarlama
Retro PazarlamaRetro Pazarlama
Retro Pazarlama
 
قصص الانبياء لابن كثير
قصص الانبياء لابن كثيرقصص الانبياء لابن كثير
قصص الانبياء لابن كثير
 
Opp dahsyat melianature
Opp dahsyat melianatureOpp dahsyat melianature
Opp dahsyat melianature
 
Dunya internet Agın’da Kisisel Markalasma
Dunya internet Agın’da  Kisisel MarkalasmaDunya internet Agın’da  Kisisel Markalasma
Dunya internet Agın’da Kisisel Markalasma
 

Semelhante a Sienna 4 divideandconquer

Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
lecture 7
lecture 7lecture 7
lecture 7sajinsc
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquerchidabdu
 
lecture 8
lecture 8lecture 8
lecture 8sajinsc
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)Eric Zhang
 

Semelhante a Sienna 4 divideandconquer (20)

Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Ada notes
Ada notesAda notes
Ada notes
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
lecture 7
lecture 7lecture 7
lecture 7
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
 
lecture 8
lecture 8lecture 8
lecture 8
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
lecture 10
lecture 10lecture 10
lecture 10
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
220exercises2
220exercises2220exercises2
220exercises2
 

Mais de chidabdu

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffmanchidabdu
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphschidabdu
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsortschidabdu
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysischidabdu
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitationschidabdu
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organizationchidabdu
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8chidabdu
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5chidabdu
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2chidabdu
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm reviewchidabdu
 

Mais de chidabdu (20)

Sienna 12 huffman
Sienna 12 huffmanSienna 12 huffman
Sienna 12 huffman
 
Sienna 11 graphs
Sienna 11 graphsSienna 11 graphs
Sienna 11 graphs
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm review
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Último (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Sienna 4 divideandconquer

  • 1. Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide–and-Conquer is a very common and very powerful algorithm design technique. The gen- eral idea: 1. Divide the complete instance of problem into two (sometimes more) subproblems that are smaller instances of the original. 2. Solve the subproblems (recursively). 3. Combine the subproblem solutions into a solution to the complete (original) instance. While the most common case is that the problem of size n is divided into 2 subproblems of size n 2 . But in general, we can divide the problem into b subproblems of size n , where a of those b subproblems need to be solved. This leads to a general recurrence for divide-and-conquer problems: T (n) = aT (n/b) + f (n), where f (n) ∈ Θ(nd ), d ≥ 0. When we encounter a recurrence of this form, we can use the master theorem to determine the efficiency class of T :  Θ(nd ) if a < bd   T (n) =  Θ(nd log n) if a = bd  Θ(nlogb d ) if a > bd Application of this theorem will often allow us to do a quick analysis of many divide-and-conquer algorithms without having to solve the recurrence in detail. Mergesort Each sorting procedure we have considered so far is an “in-place” sort. They require only Θ(1) extra space for temporary storage. Next, we consider a divide-and-conquer procedure that uses Θ(n) extra space in the form of a second array. It’s based on the idea that if you’re given two sorted arrays, you can merge them into a third in Θ(n) time. Each comparison will lead to one more item being placed into its final location, limiting the number of comparisons to n − 1.
  • 2. CS 385 Analysis of Algorithms Spring 2011 In the general case, however, this doesn’t do anything for our efforts to sort the original array. We have completely unsorted data, not two sorted arrays to merge. But we can create two arrays to merge if we split the array in half, sort each half independently, and then merge them together (hence the need for the extra Θ(n) space). If we keep doing this recursively, we can reduce the “sort half of the array” problem to the trivial cases. This approach, the merge sort, was invented by John von Neumann in 1945. How many splits will it take? Θ(log n) Then we will have Θ(log n) merge steps, each of which involves sub-arrays totaling in size to n, so each merge (which will be k independent merges into n -element arrays) step has Θ(n) operations. k This suggests an overall complexity of Θ(n log n). Let’s look at pseudocode for this: mergesort(A[0..n-1]) if n>1 copy first half of array A into a temp array B copy second half of array A into a temp array C mergesort(B) mergesort(C) merge(B and C into A) where the merge operation is: merge(B[0..p-1], C[0..q.1], A[0..(p+q-1)]) while B and C have more elements choose smaller of items at the start of B or C remove the item from B or C add it to the end of A copy remaining items of B or C into A Let’s do a bit more formal analysis of mergesort. To keep things simple, we will assume that n = 2k . Our basic operation will be the number of comparisons that need to be made. The recurrence for the number of comparisons for a mergesort of a problem of size n = 2k is C(n) = 2C(n/2) + Cmerge (n) for n > 1, C(1) = 0. The best, worst, and average cases depend on how long we are in the main while loop in the merge operation before one of the arrays is empty (as the remaining elements are then taken from the 2
  • 3. CS 385 Analysis of Algorithms Spring 2011 other array with no comparions needed). Let’s consider the worst case, where the merge will take n−1 comparisons (one array becomes empty only when the other has a single element remaining). This leads to the recurrence: Cworst (n) = 2Cworst (n/2) + n − 1 for n > 1, Cworst (1) = 0. The master theorem, gives is that Cworst (n) ∈ Θ(n log n). Quicksort Another very popular divide and conquer sorting algorithm is the quicksort. This was developed by C. A. R. Hoare in 1962. Unlike merge sort, quicksort is an in-place sort. While merge sort divided the array in half at each step, sorted each half, and then merged (where all work is in the merge), quicksort works in the opposite order. That is, quicksort splits the array (which takes lots of work) into parts consisting of the “smaller” elements and of the “larger” elements, sorts each part, and then puts them back together (trivially). It proceeds by picking a pivot element, moving all elements to the correct side of the pivot, re- sulting in the pivot being in its final location, and two subproblems remaining that can be solved recursively. A common and simple choice for the pivot is the leftmost element. We put it into its correct position and put all elements on their correct side of the pivot. Psuedocode for a quicksort: quicksort(A[l..r]) // we would start with l=0, r=n-1 if l < r s = partition(A[l..r]) // s is pivot’s location quicksort(A[l..s-1]) quicksort(A[s+1..r]) partition(A[l..r]) p = A[l] // leftmost is pivot i = l; j = r+1 do do i++ until A[i] >= p do j-- until A[j] <= p swap(A[i],A[j]) until i>=j swap(A[i],A[j]) // undo last swap(A[l],A[j]) // swap in pivot return j 3
  • 4. CS 385 Analysis of Algorithms Spring 2011 Note: we always make a recursive call on a smaller array (but it’s easy to make a coding mistake where it doesn’t, and then the sort never terminates). The complexity of quicksort is harder to evaluate than merge sort because the pivot will not always wind up in the middle of the array (in the worst case, the pivot is the largest or smallest element). Again, the basic operation will be the comparisons that take place in the partition. The partition method is clearly Θ(n) because every comparison results in left or right moving toward the other and quit when they cross. In the best case, the pivot element is always in the middle. This would lead to a number of comparisons according to the recurrence: Cbest (n) = 2Cbest (n/2) + n for n > 1, Cbest (1) = 0. By solving the recurrence or applying the Master Theorem, we find that Cbest (n) ∈ Θ(n log n), exactly like merge sort. In the worst case the pivot is at one of the ends and quicksort behaves like a selection sort. This occurs with already-sorted input or reverse-sorted input. To analyze this case, think of the first pass through the full n-element array. The first element, A[0], is chosen as the pivot. The left- to-right inner loop will terminate after one comparison (since A[0] is the smallest element). The right-to-left inner loop will perform comparisons with A[n-1], A[n-2], ... all the way down to A[0] since we need to “move” the pivot item to its own position. That’s n + 1 comparisons for the partition step. In the process, the problem size is decreased by 1, so there will be n comparisons in the next step, n−1 in the third, and so on. We stop after processing the two-element case (requiring 3 comparisons), so the total comparisons is given by: (n + 1)(n + 2) Cworst (n) = (n + 1) + n + · · · + 3 = − 3 ∈ Θ(n2 ) 2 A careful analysis can show that quicksort is Θ(n log n) in the average case (under reasonable assumptions on distribution of elements of array). We can proceed by assuming that the partition 1 can occur at any position with the same probability ( n ). This leads to a more complex recurrence: 1 n−1 Cavg (n) = [(n + 1) + Cavg (s) + Cavg (n − 1 − s)] for n > 1, Cavg (0) = 0, Cavg (1) = 0. n s=0 We will not solve this in detail, but it works out to: Cavg (n) ≈ 2n ln n ≈ 1.38n log2 n ∈ Θ(n log n). Clearly, the efficiency of quicksort depends on the selection of a good pivot. Improving our chances to select a good pivot will ensure quicker progress. One way to do this is to consider three can- didates (often the leftmost, rightmost, and middle elements) and take the median of these three as the pivot value. 4
  • 5. CS 385 Analysis of Algorithms Spring 2011 Other improvements include switching to a simpler (Θ(n2 )) sort once the subproblems get below a certain threshold size, and implementing quicksort iteratively rather than recursively. Quicksort is often the method of choice for general purpose sorting with large data sizes. Binary Search We next briefly recall the idea of a binary search – the efficient procedure for searching for an item in a sorted array. Here is a nonrecursive algorithm that implements binary search: bin_search(A[0..n-1], K) l=0; r=n-1 while (l <= r) m = floor((l+r)/2) if (K == A[m]) return m else if (K < A[m]) r=m-1 else l=m+1 return -1 To find an item, we look for it in the middle. If we do not find it, we look only to the left (if the target is smaller than the middle element) or to the right (if the target is larger than the middle element). We will not look in detail at the recurrence or its solution, just note that this is a very fast algorithm: requiring Θ(log n) comparisons in the worst case. Binary Trees No discussion of divide and conquer can proceed without a discussion of binary trees and algo- rithms on them. We discussed the idea of tree data structures and introduced lots of tree-related terminology at the start of the semester. For now, we will just consider a few examples. We can make analysis convenient by defining a binary tree as either the empty set, or a tree node plus two other binary trees: a left subtree and a right subtree. To find the height of a tree: height(T) if (T is empty) return -1 else return max(height(T.left),height(T.right)) + 1 To analyze this, we first note that the size of the problem is the number of nodes in our tree T , denoted n(T ). 5
  • 6. CS 385 Analysis of Algorithms Spring 2011 The basic operation is either the comparison needed to find the max or the addition of 1 once we find the max. A recurrence for either: A(n(T )) = A(n(T.lef t)) + A(n(T.right)) + 1 for n(T ) > 0, A(0) = 0 Note, however, that there are in fact more checks to see if the tree is empty than there are additions or comparisons. That check happens at the base case only. The number of additions/max comparisons: A(n) = n, while the number of checks for an empty tree: C(n) = 2n + 1. The other key divide and conquer operations are the recursively defined tree traversals, which we discussed earlier: 1. preorder: visit the root, then visit the left subtree, then visit the right subtree. 2. in-order visit the left subtree, then visit the root, then visit the right subtree. 3. postorder: visit the left subtree, then visit the right subtree, then visit the root. Pseudocode is staightforward, for example: inorder(T) if (T is not empty) inorder(T.left) visit(T.root) inorder(T.right) Analysis is similar to that of height for this and for preorder and postorder. Each node is visited once. Strassen’s Matrix Multiplication Our text describes a divide and conquer multiplication of large numbers, but since you have seen that in Discrete, we will move along to consider Strassen’s matrix-matrix multiplication. This algorithm improves upon the standard matrix-matrix multiplication by observing that the product of two 2 × 2 matrices can be performed using 7 multiplications instead of the usual 8. This in itself does not seem that significant, especially when we consider that the standard algo- rithm requires 4 additions, while Strassen’s algorithm requires 18 additions/subtractions. The real benefit comes when we apply this idea to get a divide-and-conquer algorithm for multi- plying matrices. To multiply 2 n × n matrices, we break it into 4 n × n matrices and use Strassen’s 2 2 algorithm to multiply them. 6
  • 7. CS 385 Analysis of Algorithms Spring 2011 Our recurrence for the number of multiplications with this approach: M (n) = 7M (n/2) for n > 1, M (1) = 1. From which the Master Theorem (or a backward substitution) will yield: M (n) ∈ Θ(nlog2 7 ) ≈ n2.807 . Which is definitely a slower rate of growth than Θ(n3 ). An analysis of the (larger) number of additions/subtractions results in the same efficiency class: A(n) ∈ Θ(nlog2 7 ). Many other approaches have been invented with even smaller rates of growth than Strassen’s algo- rithm, but most have constant factors that make them impractical for real usage. Computational Geometry We return to two familiar problems from computational geometry to explore divide-and-conquer solutions that are more efficient than the brute force approaches considered previously. Closest Pairs Our problem is to find among a set of points in the plane the two points that are closest together. We begin by assuming that the points in the set are ordered by increasing x coordinate values. If this is not the case, the points can certainly be sorted in O(n log n) time as a preprocessing step. n We then divide the points into two subsets, S1 and S2 , each of which contains 2 points (which is easy since the points are sorted by x coordinate). We then recursively find the closest pair of points in each subset S1 and S2 . If the distance between the closest pair in S1 = d1 and the distance between the closest pair in S2 = d2 . We then know that d = min{d1 , d2 } is an upper bound on the minimum distance between any pair, but we still need to make sure we check for shorter distances where one point is in S1 and the other is in S2 . The only points which might be closer together than distance d are those within a strip of width d from the dividing line between the subsets. For each point within that strip and within one subset, we potentially need to consider all points from within the strip within the other subset. That still sounds like a lot of work. The key observation is that for each point on one side, we have to consider points whose y coordinates are within d. This will mean at most 6 points from the other side, since if there are more points than that within d in the y coordinate on the other side, at least one pair from among that point would be closer than distance d to each other. So how do we find those points to check? They can be found quickly if we also keep the points sorted in order by y coordinate. Still, this seems difficult but it can be done efficiently (see the text’s description for details). 7
  • 8. CS 385 Analysis of Algorithms Spring 2011 We end up with a recurrence: T (n) = 2T (n/2) + O(n) which given an overall time of T (n) ∈ O(n log n). Quickhull The other computational geometry problem discussed in the text is called quickhull – an algorithm for finding the convex hull of a set of points. We will not discuss it in class, but it is worth reading. 8