4
$\begingroup$

I write this code in python:

def sub(ma):
 n = len(ma); m = len(ma[0])
 if n != m : return
 n2 = int(ceil(n/2))
 a = []; b = []; c = []; d = [] 
 for i in range(n2):
 a.append(ma[i][0:n2])
 b.append(ma[i][n2:n])
 c.append(ma[n2+i][0:n2])
 d.append(ma[n2+i][n2:n])
 return [a,b,c,d] 
def sum(ma):
 if len(ma) == 1 : return ma[0][0]
 div = sub(ma) 
 return sum(div[0])+sum(div[1])+sum(div[2])+sum(div[3]) 

Do you know what is a possibly recurrence equation $T(n)$ to the 'sum' method? I suppose that is like that $$T(n) = 4T(n/2) + f(n)$$ what it is $f(n)$ ? Thanks,

asked Sep 17, 2012 at 6:29
$\endgroup$

1 Answer 1

4
$\begingroup$

The subroutine sub quarters the matrix into four parts. This takes time $f(n),ドル and so the running-time of sum (which sums all entries in its input square matrix in a curious divide-and-conquer fashion) satisfies the recurrence $$ T(n) = \begin{cases} O(1) & n=1 \\ 2T(\lfloor n/2 \rfloor) + 2T(\lceil n/2 \rceil) + f(n) + O(1) & n>1. \end{cases} $$ The subroutine sub is quite simple, and I'm sure you can figure out its running time yourself (though you'll have to be careful about the semantics of list appending in python).

answered Sep 17, 2012 at 7:22
$\endgroup$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.