3

I have a problem showing with induction that the given function

foo :: [Int] -> Int
foo    []  = 0
foo (x:xs) = max (max x (foo xs)) (max (-x) (foo xs))

which returns the max absolute value of a given list of Int's , has a runtime of O(2^n).

I got so far by now:

t(0) = 1 and t(n>1)= 2 * t(n-1) + 4 where t shows the sum of calls of foo and max for a list of n elements.

Base Case:            n = 0 => t(0) = 1 <= c * 2^0, for c >= 1
Induction Hypothesis: t(n) <= c * 2^n
Induction Step:       n -> n+1
                   => t(n+1) <= c * 2^{n+1}
                  <=> 2 * t(n) + 4 <= c * 2^{n+1} | Induction Hypothesis
                  <=> 2 * c * 2^n + 4 <= c * 2^{n+1}
                  <=> c * 2^{n+1} + 4 <= c * 2^{n+1}

which is obviously wrong and I don't know how to fix that issue.

Thanks in advance!

nicksheen
  • 550
  • 6
  • 23

2 Answers2

4

Let's try to prove a tighter bound, like

t(n) <= c*2^n - k        (*)

for some constants c and k.

Assuming (*) holds by induction hypothesis, we get

t(n+1) 
= { recursive definition }
2*t(n) + 4
<= { induction hypothesis }
2*(c*2^n - k) + 4
<= { math }
c*2^(n+1) - 2k + 4
<= { ???? }
c*2^(n+1) - k

Now, we only need to choose k so that we can actually justify the last step, but that's easy.

Note that we also need to check the base case t(0), and choose c.

I'll leave the rest to you.

chi
  • 111,837
  • 3
  • 133
  • 218
1

Let's prove a bit more general claim. If algorithm complexity is such that:

 t(0) = c
 t(n) = a*t(n-1) + b 

assuming a>1 the algorithm's complexity is O(a^n).

Let's choose k1 = c, d = b/(a-1) (this choice of d will become clear at the end), and k2 = a + d. Let's assume that a > c (otherwise it would have been k1 = min(a,c), d= b/(max(a,c)-1), and k2 = max(a,c) + d but I'm too lazy to write all those max and min). We want to prove that

k1*a^n <= t(n) <= k2*a^n

but here is a twist, let's prove a bit stricter upper bound:

k1*a^n <= t(n) <= k2*a^n - d

Base case:

c <= c <= (a + d) - d

is obviously true

Induction step:

We know that

k1*a^n <= t(n) <= k2*a^n - d

is true and want to prove that

k1*a^(n+1) <= t(n+1) <= k2*a^(n+1) - d

The left side is easy:

t(n+1) = a*t(n) + b >= a*t(n) >= a*(k1*a^n) = k1*a^(n+1)

The right side is a bit more complicated

t(n+1) = a*t(n) + b <= a*(k2*a^n - d) + b = a*k2*a^n - a*b/(a-1) + b = k2*a^(n+1) - b/(a-1) =  k2*a^(n+1) - d

The last step is true because

a*b/(a-1) - b = b*(a/(a-1) - 1) = b*(a - (a-1))/(a-1) = b/(a-1)

in other words

a*d - b = d
SergGr
  • 23,570
  • 2
  • 30
  • 51