Skip to main content
Computer Science 385
                                     Analysis of Algorithms
                                     Siena College
                                     Spring 2011


                  Topic Notes: Decrease and Conquer
Our next class of algorithms are the decrease-and-conquer group.
The idea here:

   1. Reduce the problem to a smaller instance

   2. Solve the smaller instance

   3. Modify the smaller instance solution to be a solution to the original

The main variations:

   • Decrease by a constant (often by 1)

   • Decrease by a constant factor (often by half)

   • Variable size decrease

For example, consider variations on how to compute the value an .
The brute-force approach would involve applying the definition and multiplying a by itself, n
times.
A decrease-by-one approach would reduce the problem to computing the result for the problem of
size n − 1 (computing an−1 ) then modifying that to be a solution to the original (by multiplying
that result by a).
                                                                                    n
A decrease-by-constant-factor (2, in this case) approach would involve computing a 2 and multi-
plying that result by itself to compute the answer. The only complication here is that we have to
treat odd exponents specially, leading to a rule:

                                    (a 2 )2
                                       n
                                   
                                                    if n is even
                               n               2
                              a =  (a   n−1
                                          2    ) · a if n > 1 and odd
                                    a                if n = 1
                                  
                                  


This approach will lead to O(log n) multiplications.


Insertion Sort
CS 385                                    Analysis of Algorithms                       Spring 2011



Our decrease-by-one approach to sorting is the insertion sort.
The insertion sort sorts an array of n elements by first sorting the first n−1 elements, then inserting
the last element into its correct position in the array.

insertion_sort(A[0..n-1]) {
  for (i=0 to n-1) {
    v = A[i]
    j = i-1
    while (j >= 0 and A[j] > v) {
      A[j+1] = A[j]
      j--
    }
    A[j+1] = v

This is an in-place sort and is stable.
Our basic operation for this algorithm is the comparison of keys in the while loop.
We do have differences in worst, average, and best case behavior. In the worst case, the while loop
always executes as many times as possible. This occurs when each element needs to go all the way
at the start of the sorted portion of the array – exactly when the starting array is in reverse sorted
order.
The worst case number of comparisons:

                                       n−1 i−1         n−1
                                                                  n(n − 1)
                        Cworst (n) =             1=          i=            ∈ Θ(n2 )
                                       i=1 j=0         i=1           2

In the best case, the inner loop needs to do just one comparison, determining that the element is
already in its correct position. This happens when the algorithm is presented with already-sorted
input. Here, the number of comparisons:

                                                 n−1
                                  Cbest (n) =          1 = n − 1 ∈ Θ(n)
                                                 i=1


This behavior is unusual – after all, how often do we attempt to sort an already-sorted array?
However, we come close in some very important cases. If we have nearly-sorted data, we have
nearly this same performance.
A careful analysis of the average case would result in:

                                                        n2
                                          Cavg (n) ≈       ∈ Θ(n2 )
                                                        4

Of the simple sorting algorithms (bubble, selection, insertion), insertion sort is considered the best
option in general.

                                                       2
CS 385                               Analysis of Algorithms                          Spring 2011




Graph Traversals
We return to graph structures for our next group of decrease-and-conquer algorithms. In particular,
we consider the problem of visiting all vertices in a graph.
The two main approaches are the depth-first search (DFS) and the breadth-first search (BFS).
See the text for details...


Topological Sort
Again, see the text for details...




                                                3