1

I need to compute the time and space complexity in Big O notation for this algorithm I constructed for binary multiplication.

mul(a, b) =
  • 0 if a = 0 or b = 0
  • (b + p) if lowest(a) != 0
  • p if lowest(a) = 0

where

  • p = join(mul(higher(a), b), 0)

  • lowest(a) returns the lowest order bit of a binary numeral a

e.g. lowest(1001101) = 1

  • higher(a) returns the binary numeral excluding the lowest order bit of binary numeral a

e.g. higher(1001101) = 100110

  • join(a, b) appends bit b at the end of binary numeral a

e.g. join(1101, 1) = 11011

2 Answers2

1

It's a recursive algorithm. Each time you call mul (through the use of p) you decrease the number of bits of 'a' by one. So the time complexity is the number of bit in 'a'. And since the number of bits equals the log of the value of 'a', so the recursion depth is Log(a).

In each step of the recursion you might perform an add operation of Max(Log(b),Log(a)) bits.

So the time complexity is O(Log(a)*Max(Log(b), Log(a)))

If a and b are roughly the same size you will get O(Log(a)^2)

YanivR
  • 11
  • 1
0

It’s quite simple if you look at the right condition. How many recursive calls do you make until a = 0? Look at the cases a = 0, a = 1, 2 <= a < 4, 4 <= a < 8, 8 <= a < 16, 16 <= a < 32 and so on.

gnasher729
  • 32,238
  • 36
  • 56