python - What is the complexity of this recursive algorithm? -
does following algorithm have complexity of o(nlogn)?
the thing confuses me algorithm divides twice, not once regular o(nlogn) algorithm, , each time o(n) work.
def equivalent(a, b): if isequal(a, b): return true half = int(len(a) / 2) if 2*half != len(a): return false if (equivalent(a[:half], b[:half]) , equivalent(a[half:], b[half:])): return true if (equivalent(a[:half], b[half:]) , equivalent(a[half:], b[:half])): return true return false
each of 4 recursive calls equivalent
reduces amount of input data factor of 2. thus, assuming a
, b
have same length, , isequal
has linear time complexity, can construct recurrence relation overall complexity:
where c
constant. can solve relation repeatedly substituting , spotting pattern:
what upper limit of summation, m
? stopping condition occurs when len(a)
odd. may anywhere between n
, 1
, depending on prime decomposition of n
. in worse case scenario, n
power of 2, function recurses until len(a) = 1
, i.e.
Comments
Post a Comment