1

This algorithm computes the number of ways one can make change for n cents assuming one can use six types of coins. Singles, half dollars, quarters, dimes, nickels and pennies. In the following algorithm, what is this type of algorithm called ? Is it dynamic or greedy ? Also, how do I start about finding the complexity of such an algorithm ?

B(n)
count ← 0
s ← 0
while s ≤ n/100
   do srem ← n − 100*s
   s ← s + 1
   h ← 0
   while h ≤ srem/50
       do hrem ← srem − 50*h
       h ← h + 1
       q ← 0
       while q ≤ hrem/25
          do qrem ← hrem − 25*q
          q ← q + 1
          d ← 0
          while d ≤ qrem/10
             do drem ← qrem − 10*d
             d ← d + 1
             ni ← 0
             while ni ≤ drem/5
                 do ni ← ni + 1
                 count ← count + 1
return count

I have so far calculated: Time complexity T(n) = 1 + a(1+b(1+c(1+d(1+e*1)))) a, b, c, d, e are the while loops.

In the worst case, s is n/100, so substituting that on a, and srem <- n - 100*n/100 = 0,

T(n) = n/100(1 + 0) = Big-O(n).

But when s = 0,

T(n) = 1 + 0(1+ ....)
     = Big-O(1) 

How do I find the time complexity ? Which is the way to go? Thank you in advance.

0 Answers0