8

The source of this question comes from an undergraduate course I am taking, which covers an introduction to the analysis of algorithms. This is not for homework, but rather a question asked in CLRS.

You have a slow machine running at $x$ MIPS, and a fast machine running at $y$ MIPS. You also have two algorithms of the same class, but different running time complexities: one "slow" algorithm runs at $T(n) = c_1n^2$ whereas a "fast" algorithm runs at $T(n) = c_2n \log n$.

You execute the slow algorithm on the fast machine, and the fast algorithm on the slow machine. What is the largest value of n such that the fast machine running the slow algorithm beats the slow machine running the fast algorithm?

My solution so far:

Find the set of all $n$ such that $$\frac{c_2n\log n}{x} > \frac{c_1n^2}{y}$$ where $n$ is a natural number.

This is my work thus far:

$$\{n : \frac{c_2 n \log_2 n}{x} > \frac{c_1 n^2}{y}, n \in \mathbb{N}\} = \{n : n < \frac{c_2 y}{c_1 x} \log_2 n, n \in \mathbb{N}\}$$

The only solution that comes to mind now is to plug-n-chug all values of $n$ until I find the first n where

$$n < \frac{c_2y}{c_1x}\log(n)$$

no longer holds.

Raphael
  • 73,212
  • 30
  • 182
  • 400
DoggoDougal
  • 181
  • 1

1 Answers1

2

Consider your last inequality:

$\qquad \displaystyle n < \frac{c_2y}{c_1x}\log(n)$

Now, $C\log n$ with $C = \frac{c_2y}{c_1x}$ is a concave function. Therefore, there are at most two intersections, and only one of them is interesting for you; that is, solve

$\qquad \displaystyle n = C\log(n)$

and find out which algorithm is faster in which interval by simply calculating the respective function values at some point in the respective interval.

It is true that solving this equality explicitly is hard/impossible. For some fixed $C$, computer algebra gives you an expression in a well-known function; interesting (real) intersection turns out to be at

$\qquad \displaystyle n = e^{-W_{-1}(-\frac{\ln 2}{C})}$

if $C \geq e \ln 2$, with $W_k$ the analytic continuation of the product log function. This function does not have a nice closed form, but it can be evaluated numerically for given $C$; for instance, you get $\approx 116,74$ for $C=17$.

Raphael
  • 73,212
  • 30
  • 182
  • 400