3

Not sure if this is the right place to ask this. In Cormen page 1056 I read that if the running time of an algorithm is O(k) and "k" is represented in unary i.e. a string of k 1s then running time of the algorithm is 0(n) where "n" is the input-size in bits and if "k" is represented as binary then as n=lg k+1 ,the running time of the algorithm becomes o(2^n).

So my doubt is then why "unary" representation won't be preferred in this case as it gives polynomial time in contrast to exponential in other case.

code4fun
  • 2,661
  • 9
  • 25
  • 39

1 Answers1

6

Unary time gives polynomial time relative to the input size, but the actual running time is the same, regardless of the representation used.

The thing is that complexity is calculated as a function of the input. When using unary representation, you increase the size of the input, while not changing the run time.
Since to represent k in unary base you need n bits, O(k) is O(n) - since it is linear in the size of the input. However, for the same solution, it will be O(k) = O(2^logk) = O(2^n), if you use binary representation since you need logk bits to represent k.

What you are describing is closely related to pseudo-polynomial time algorithms, such as knapsack solution with dynamic porogramming which is O(W*n), it is pseudo-polynomial because it is actually exponential in the number of bits used to represent W.

amit
  • 175,853
  • 27
  • 231
  • 333