Analysis of Algorithms : An Active Learning Approach

(Ron) #1

24 ANALYSIS BASICS



  1. For each of the following pairs of functions f(n) and g(n), either
    f(n)=O(g(n)) or g(n)=O(f(n)), but not both. Determine which is the case.
    a.
    b.
    c.
    d.
    e.
    f.
    g.
    h.


1.5 Divide and Conquer Algorithms


As the introduction indicated, divide and conquer algorithms can provide a
small and powerful means to solve a problem; this section is not about how to
write such an algorithm but rather how to analyze one. When we count com-
parisons that occur in loops, we only need to determine how many compari-
sons there are inside the loop and how many times the loop is executed. This
is made more complex when a value of the outer loop influences the number
of passes of an inner loop.
When we look at divide and conquer algorithms, it is not clear how many
times a task will be done because it depends on the recursive calls and perhaps
on some preparatory and concluding work. It is usually not obvious how
many times the function will be called recursively. As an example of this, con-
sider the following generic divide and conquer algorithm:
DivideAndConquer( data, N, solution )
data a set of input values
N the number of values in the set
solution the solution to this problem


if (N ≤ SizeLimit) then
DirectSolution( data, N, solution )
else
DivideInput( data, N, smallerSets, smallerSizes, numberSmaller )


f()n ==()n^2 – n ⁄ 2 ,gn() 6 n
f()n ==n+ 2 n, gn() n^2
f()n ==nn n+ log ,gn() nn
f()n ==n^2 ++ 3 n 4 ,gn() n^3
f()n ==nnlog ,gn() nn⁄ 2
f()n ==nn+log, gn() n
f()n == 2 ()logn^2 , gn() logn+ 1
f()n == 4 nnnlog + , gn() ()n^2 – n ⁄ 2
Free download pdf