Chapter 1: Introduction

Chapter 1: Introduction · Study Questions
The notion of an algorithmAnalysis of algorithmsComplexity examplesEuclid's algorithm
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · The Notion of an Algorithm

Question 1: An Ambiguous Step

In a tea-making algorithm for a computer, which property does the step “Wait for the tea to steep.” violate?

B: Every step must have one meaning; for a computer the step should read “wait 10 minutes”. Waiting is feasible (no effectiveness problem); the problem is that the duration is left open.

Multiple Choice · The Notion of an Algorithm

Question 2: The Loop That Never Stops

Which property does the following method, as an algorithm, fail to satisfy for every input?

int countSteps(int n) {
  int counter = 0;
  while (n != 0) {
    n = n - 2;
    counter++;
  }
  return counter;
}

C: If n is odd it stays odd and never reaches 0, so the loop does not stop. An algorithm must stop after finitely many steps for every input. The steps are definite and feasible, and where it stops it also produces an output.

Multiple Choice · The Notion of an Algorithm

Question 3: Random?

Which of the following algorithms can follow different steps on the same input?

B: The pivot is chosen at random on every run; the same array gets different partitions, but the output is still the same sorted array. The others are deterministic: the same input always gives the same steps and result.

Output Question · Analysis of Algorithms

Question 4: When the Input Quadruples

An algorithm takes 2 ms for n = 1000 items. About how long does it take on the same machine for n = 4000 items? Compute separately if the running time is (a) Θ(n), (b) Θ(n²), (c) Θ(n³), (d) Θ(log n).

Show answer
Classratio f(4000)/f(1000)estimated time
Θ(n)48 ms
Θ(n²)4² = 1632 ms
Θ(n³)4³ = 64128 ms
Θ(log n)log₂ 4000 / log₂ 1000 = 11.97 / 9.972.4 ms

The constant c comes from the machine and cancels in the ratio: T(4000)/T(1000) = f(4000)/f(1000). For a logarithmic algorithm, quadrupling the input adds only 2 steps.

Time at n = 4000 (ms)8n32n²128n³2.4log n

Multiple Choice · Analysis of Algorithms

Question 5: The Dominant Term

For T(n) = 5n² + 100n log₂ n + 7, which of the following is true?

A: At n = 100, 5n² = 50,000 and 100n log₂ n = 66,439. But the class is set by the dominant term for large n: at n = 1000, 5,000,000 vs. 996,578. Coefficients do not change the class; beyond n ≈ 143 the n² term always leads.

Classic Question · Analysis of Algorithms

Question 6: The Largest n in 1 Second

On a computer doing 10⁹ basic operations per second, what is the largest n for which an algorithm with f(n) operations finishes within 1 second? (a) f(n) = n log₂ n, (b) f(n) = n³, (c) f(n) = 2ⁿ, (d) f(n) = n!

Show answer
f(n)conditionlargest n
n log₂ nn log₂ n ≤ 10⁹≈ 3.96 · 10⁷
n³n³ ≤ 10⁹1000
2ⁿ2²⁹ ≈ 5.4 · 10⁸, 2³⁰ ≈ 1.07 · 10⁹29
n!12! ≈ 4.8 · 10⁸, 13! ≈ 6.2 · 10⁹12

If the computer became 1000 times faster, n for n log₂ n would grow about 1000-fold; for 2ⁿ only by ≈ 10 (2¹⁰ ≈ 1000).

Output Question · Complexity Examples

Question 7: Primality Test

For the calls isPrime(121) and isPrime(101), how many divisions (n % i) does the for loop make, and what are the results?

boolean isPrime(int n) {
  if (n <= 1)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;
  int limit = (int) Math.sqrt(n) + 1;
  for (int i = 3; i < limit; i += 2)
    if (n % i == 0)
      return false;
  return true;
}
Show answer
  • 121: √121 = 11 → limit = 12 → i = 3, 5, 7, 9, 11. 121 % 11 = 0 → false, 5 divisions (121 = 11 · 11).
  • 101: (int) √101 = 10 → limit = 11 → i = 3, 5, 7, 9; none divides → true, 4 divisions.

Even numbers are ruled out before the loop; only odd i are tried: at most ≈ √n / 2 divisions → O(√n). + 1 is essential: without it, i = 11 would not be tried for 121 and 121 would wrongly be found prime.

Output Question · Complexity Examples

Question 8: The Largest Element

What does findMax(new int[]{3, 9, 2, 9, 5}) return? How many times is the comparison x > max made, and how many times is max updated? What if the array were [2, 3, 5, 9]?

Integer findMax(int[] arr) {
  if (arr.length == 0)
    return null;
  int max = arr[0];
  for (int x : arr)
    if (x > max)
      max = x;
  return max;
}
Show answer
  • [3, 9, 2, 9, 5]: returns 9; 5 comparisons (for-each also visits the first element), 1 update (3 → 9). The second 9 does not update, because 9 > 9 is false.
  • [2, 3, 5, 9]: returns 9; 4 comparisons, 3 updates (an increasing array causes the most updates).

The number of comparisons is n for every input: the time is Θ(n); the number of updates, however, ranges from 0 to n − 1 depending on the input. Extra memory is O(1).

Output Question · Complexity Examples

Question 9: Number of Multiplications in Matrix Product

The method multiply multiplies A (2 × 3) by B (3 × 4). What is the size of the result, and how often does the innermost line sum += A[i][k] * B[k][j] run? For n × n matrices, how many multiplications for n = 100 and n = 200?

Show answer
  • n = A.length = 2, m = B[0].length = 4, p = B.length = 3. The result is 2 × 4.
  • Three nested loops: n · m · p = 2 · 4 · 3 = 24 multiplications.
  • For square matrices it is n³: n = 100 → 10⁶, n = 200 → 8 · 10⁶. When n doubles, the time grows 8 times as large: Θ(n³).

Multiple Choice · Complexity Examples

Question 10: How Many Permutations?

How many lines does the call permute(new int[]{1, 2, 3, 4, 5}, 0) print to the screen?

B: 5 choices for the first position, 4 for the second, …: 5! = 120 permutations, one line each. 32 = 2⁵ is the number of subsets, 720 = 6!. Each line prints n elements, so the total work is Θ(n · n!).

Step-by-Step Trace · Complexity Examples

Question 11: Call Tree of fib(4)

When naive recursive fib(4) is called, in what order do the calls open, how many calls are made, and what is the result?

f4

fib(4) is called: 4 ≤ 1 is false. On line 3, fib(3) is called first; fib(4) waits on the stack for the addition.

f4f3

fib(3) is not a base case either → fib(2) first.

f4f3f2

fib(2) → call to fib(1). Stack depth is 4 (including main).

f4f3f2f1=1

fib(1): n ≤ 1 → base case, 1 is returned.

f4f3f2=1f1=1f0=0

The second call of fib(2) is fib(0) = 0. fib(2) = 1 + 0 = 1 is returned.

f4f3=2f2=1f1=1f1=1f0=0

fib(3) makes its second call: fib(1) = 1. fib(3) = 1 + 1 = 2 is returned.

f4f3=2f2f2=1f1=1f1=1f0=0

fib(4) calls fib(2): the same subproblem is solved a second time from scratch.

f4f3=2f2=1f2=1f1=1f1=1f0=0f1=1f0=0

fib(1) = 1 and fib(0) = 0 → this fib(2) also returns 1.

f4=3f3=2f2=1f2=1f1=1f1=1f0=0f1=1f0=0

fib(4) = fib(3) + fib(2) = 2 + 1 = 3. 9 calls in total; fib(2) was computed twice, fib(1) three times.

long fib(int n) {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}

Output Question · Complexity Examples

Question 12: Number of Calls

If fib(6) and fibMemo(6) are called separately (the memo array starts as zeros), how many times is each called in total (including the first call), and what does each return?

long fib(int n) {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}
long fibMemo(int n) {
  if (n <= 1) return n;
  if (memo[n] == 0)
    memo[n] = fibMemo(n - 1)
              + fibMemo(n - 2);
  return memo[n];
}
Show answer

Both return 8.

  • fib(6): 25 calls. C(n) = 1 + C(n − 1) + C(n − 2), C(0) = C(1) = 1: 1, 1, 3, 5, 9, 15, 25. The number of calls grows like Fibonacci → O(2ⁿ).
  • fibMemo(6): 11 calls = 2n − 1. Each F(k) is computed once; the second call (n − 2) returns straight from the memo → O(n).

Multiple Choice · Complexity Examples

Question 13: Order of Growth

Which is the ordering from slowest to fastest growth?

D: 1 < log n < √n < n < n log n < n² < n³ < 2ⁿ < n!. At n = 10⁶, log₂ n ≈ 20 and √n = 1000. n! grows fastest: already 3,628,800 at n = 10, while 2¹⁰ is 1024.

Step-by-Step Trace · Euclid's Algorithm

Question 14: gcd(1001, 385)

In the call gcd(1001, 385), which calls are made, how many division (mod) operations are needed, and what is the result?

call stack (top = running)gcd(1001, 385)1001 = 2·385 + 231

gcd(1001, 385): b ≠ 0. 1001 = 2 · 385 + 231 → gcd(385, 231) is called.

call stack (top = running)gcd(385, 231)385 = 1·231 + 154gcd(1001, 385)1001 = 2·385 + 231

385 = 1 · 231 + 154 → gcd(231, 154).

call stack (top = running)gcd(231, 154)231 = 1·154 + 77gcd(385, 231)385 = 1·231 + 154gcd(1001, 385)1001 = 2·385 + 231

231 = 1 · 154 + 77 → gcd(154, 77).

call stack (top = running)gcd(154, 77)154 = 2·77 + 0gcd(231, 154)231 = 1·154 + 77gcd(385, 231)385 = 1·231 + 154gcd(1001, 385)1001 = 2·385 + 231

154 = 2 · 77 + 0 → gcd(77, 0).

call stack (top = running)gcd(77, 0)b = 0gcd(154, 77)154 = 2·77 + 0gcd(231, 154)231 = 1·154 + 77gcd(385, 231)385 = 1·231 + 154gcd(1001, 385)1001 = 2·385 + 231

b == 0 → return 77. 77 is the last nonzero remainder.

call stack (top = running)gcd(77, 0)→ 77gcd(154, 77)→ 77gcd(231, 154)→ 77gcd(385, 231)→ 77gcd(1001, 385)→ 77

77 returns unchanged through all the frames (tail recursion). 4 divisions were enough: 1001 = 7 · 11 · 13, 385 = 5 · 7 · 11 → GCD = 77.

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

Multiple Choice · Euclid's Algorithm

Question 15: Consecutive Fibonacci Numbers

How many mod (%) operations does the call gcd(89, 55) perform before reaching the result?

A: The sequence 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 0 forms in turn (each number is the remainder of the two before it); from (89, 55) to (2, 1), each of the 9 calls does one mod operation, and gcd(1, 0) is the base case. For consecutive Fibonacci numbers every quotient but the last is 1, so the numbers shrink as slowly as possible: the worst case (Lamé). gcd(55, 34) takes 8 mod operations.

The End

Chapter 1: Introduction: Study Questions
Lecture slides: Chapter 1: Introduction · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.