6

I am reading the book "Introduction to Algorithms" and I am confused by this part:

We also assume a limit on the size of each word of data . For example when working with >inputs of size n , we typically assume that integers are represented by c lg n bits for some >constant c>=1. We require c>=1 so that each word can hold the value of n , enabling us to >index the individual input elements, and we restrict c to be a constant so that the word >size doesn't grow arbitrarily .

What is the purpose of this constant c?

user3731608
  • 69
  • 1
  • 2

3 Answers3

9

The reason for the constant c is for the case where you're working with small inputs on a huge word-size machine. For example, if you are working on a 64-bit machine but only processing inputs of size, say, 212, the value of lg n will be 12 but the machine is a 64-bit machine. It wouldn't be correct to assume that the word size is exactly 12 bits here. The added factor of c allows us to assume that the machine word size is at least lg n, but possibly a lot larger, without messing up the analysis.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Then why even care for the `lg n`? Wouldn't it be more correct to assume `w` with `w = wordsize` instead of `c lg n`? – Absurd-Mind Jun 11 '14 at 20:10
  • 1
    @Absurd-Mind We need the word size to be at least large enough to hold a number representing the size of the input (otherwise, you can't even store array indices in a single machine word). If the input has size n, then you need lg n bits to write out the number n, hence the requirement that it's at least lg n. Does that make sense? – templatetypedef Jun 11 '14 at 20:48
  • I don't get it... what would c be in this case? And why would c be necessary? Why would it be larger if it's only processing inputs of size 2^12? – user3731608 Jun 12 '14 at 00:42
  • @user3731608 c can be any constant; it doesn't matter what. The numbers I used above are just examples. The main point is that the size of a machine word is large enough to hold the problem size. Any extra slack above that doesn't really matter. – templatetypedef Jun 12 '14 at 01:19
  • Sorry but I still don't get it...what is the point in c? You said that you may be working on small inputs on a huge word-size machine but why does this necessitate the use of such a constant? What does the constant do? – user3731608 Jun 12 '14 at 11:12
  • @user3731608 We want the math to be able to match the real world as much as possible. We could say that the word size is exactly lg n, but if we did that the theory wouldn't match reality. At the same time, the gap between the word size and lg n depends on the actual computer and the value of n. We can then use c to account for the gap. There is no one set value of c. It's deliberately left as "something >= 1" so that the theory accounts for the gap, but doesn't make any assumptions on exactly how big that gap is. – templatetypedef Jun 12 '14 at 15:47
  • I get it slightly better but I don't understand what c does here. It is there to multiply lg n by something, but how would that 'account for the gap' – user3731608 Jun 12 '14 at 20:12
0

consider following,

long long int num = 4096;

the binary value for above number is 2^12 hence requires at least 12 bits for the storage. Now consider that the machine you are using runs on a 32 bit architecture. now coming to your question, == lg n == is used here to calculate the number of bits require to store the input size. here the no bits required is 12 as shown

lg n = lg 2^12 = 12 bits

but now we know that the total space is 32 bits hence if we want we can we can take more of the space but can we take less than 12 bits? definitely no, we anyhow need at least 12 bits and that is why we need a constant c such that

c >= 1

this is to ensure that we don't get less than 12 bit for an instance let us consider if

c = 0.5

then in this case total bits assigned will be

c lg n = 0.5 * lg 2^12 = 6 bits

and this is less than the required hence c should be greater than 1 even if c = 2 which is greater than 1 creates no problem and assigns a total of 24 bits which is more than sufficient which is 12 bit but less than 32 bits which is the maximum word size hence c is used to get at least the minimum size to store the word and in fact in real situation its always minimum.

aditya rai
  • 67
  • 6
0

suppose that the size is 8. we all know that log base 2 of 8 is 3, since 2^3 is 8. if we transform 8 into binary form, it will become 1 0 0 0 and we need 4 bits and not 3 bits to represent 8. so what do we do is we add c as a constant. and c is bigger than 1 because if c is less than 1 then it will be less than 3 and we don't want to do that. So we can add a constant that is bigger than 1. and by adding constant, that 3 bits can be upgraded to 4 bits. so that's why it's c log n.

Angel
  • 1
  • 1