0
$\begingroup$

I'm going through a presentation on the topic of analysis of algorithms and I encountered the following inequalities as part of the steps being explained:

  1. the loop stops: $n/2^k ≤ 0$
  2. that is: $n < 2^k$
  3. thus, when the loop stops: $k > ⌈lg n⌉$

(Note: For the sake of being explicit, $lg n$ is shorthand for $log_2 n$. Also, $n/2^k$ can be assumed to be integer division.)

Now, it's been a while for me, but from what I've been checking, it looks like the change from ≤ into < on step 2 might be a typo(?), except for the fact that it carries over to subsequent steps.

If it's not a typo, the only thing I can think of is that it's related to the 0 on the right when multiplying by $2^k$ on both sides of step 1, but I couldn't immediately find an identity or property that justified changing the signs from ≤ to < or ending with a non-zero value on the right-hand side.

Can someone explain what I'm missing between these steps?


The pseudo-code for the algorithm in question, quoted from the slide, is below:

p = 1;
e = a;
N = n;
while (N > 0) {
    if (n % 2 != 0)
        p *= e;
    e *= e;
    N /= 2;
}

It's analyzed to be in the order of O(lg n). A few notes for clarity, though not necessarily relevant to the original question:

  1. n is the input size of the problem instance, and it's a non-negative integer (e.g. 50)
  2. statements like p *= e are shorthand for $p \leftarrow p * e$
  3. a is not defined anywhere, but can be assumed to be an arbitrary, though unknown, integer constant
  • 0
    This must be a typo. If the first condition is $n/2^k \le 1$, everything falls into place.2017-01-15
  • 0
    Well, if $n$ is negative, then it's true, since $n < 0 < 2^k$. But if $n$ is positive, then the first inequality $\frac{n}{2^k} \leq 0$ is false.2017-01-15
  • 1
    You need to provide more context. The conclusion follows iff $n/2^k$ is assumed to mean *integer* division or, equivalently, if you change it to $\lfloor n/2^k \rfloor$. In that case $\lfloor n/2^k \rfloor \le 0 \iff \lfloor n/2^k \rfloor \lt 1 \iff n \lt 2^k$.2017-01-15
  • 0
    @dxiv I'm quoting the presentation slides, so I don't think it'd be correct to speculate by adding things that are not present. Additional slide info is a statement prior to the steps, saying "Termination condition: N ≤ 0"2017-01-15
  • 0
    @MathematicsStudent1122 The variable `n` is a positive integer, since this is on the topic of algorithm analysis for problem instances of size `n`.2017-01-15
  • 0
    @ray Is $N$ defined elsewhere to be an integer maybe? In many programming languages `int c = a / b;` does in fact mean *integer* division.2017-01-15
  • 0
    @HansEngler The slide is talking about a loop's termination condition; it runs `while (N > 0)` evaluates to true.2017-01-15
  • 0
    @ray It is customary to assume that loop counters are integers (in fact, the loop would never end otherwise). In that context the pseudo-code `N = N / 2;` is equivalent to the math assignment $N \leftarrow \lfloor N / 2 \rfloor$ and my first comment applies.2017-01-15
  • 0
    @dxiv It's correct to assume the operation is *integer* division in $n/2^k$. What sort of additional context are you looking for?2017-01-15
  • 0
    That assumption wasn't stated when I posted my first comment. Now that it *is* stated, the second part of that comment answers the question: $\lfloor n/2^k \rfloor \le 0 \iff \lfloor n/2^k \rfloor \lt 1 \iff n \lt 2^k$.2017-01-15
  • 0
    @dxiv It looks like it was simply a matter of convenience, not really some kind of property or identity I was missing somewhere, right? If you turn that into an answer with a short explanation, I'll take it. Thanks.2017-01-15
  • 0
    @ray Right. Also, something between confusion and untold assumptions in the pseudo-code.2017-01-15

1 Answers 1

1

The following collects bits and pieces gathered in the comments to provide a formal answer.

The pseudo-code for the algorithm in question, quoted from the slide, is below:

It is customary to assume that loop counters are integers (in fact, the loop would never end otherwise). In that context the pseudo-code N = N / 2; denotes integer division with truncation towards $0$, and is equivalent to the math assignment $N \leftarrow \lfloor N/2 \rfloor$.

Under this interpretation the RHS in $\lfloor n/2^k \rfloor \le 0$ is an integer, so the condition $\lfloor n/2^k \rfloor \le 0$ is equivalent to $\lfloor n/2^k \rfloor \lt 1$. By definition of $\lfloor \cdot \rfloor$ the latter is equivalent to $n/2^k \lt 1 \iff n \lt 2^k$.