Guides / Algorithmic Thinking: tracing, loop invariants and Theta
Algorithmic Thinking: tracing, loop invariants and Theta
What TU/e asks in Algorithmic Thinking, with worked pseudocode examples for tracing, loop invariants and order of growth.
Algorithmic Thinking is 40% of the CSE ranking. TU/e prepares candidates with the video lecture The fundamentals of algorithms. Pseudocode is shown in the question, arrays are indexed from 1, and toy algorithms need not make practical sense.
Question types named by TU/e: the output for a given input, what problem an algorithm solves, whether it is correct, which loop invariant is useful, best and worst case running time, and which of two algorithms is asymptotically faster. TU/e explicitly does not ask you to write correctness proofs, improve an algorithm or design your own.
Tracing
Keep a small table with one column per variable and one row per iteration. Do not skip the last iteration: most tracing errors are off by one at the end.
Loop invariants
A useful invariant is true before the loop (initialisation), stays true after each iteration (maintenance), and together with the stop condition gives you the result (termination). Take linear search for x in :
i := 1
while i <= n and A[i] != x do
i := i + 1A useful invariant is: x does not occur in . Before the loop and the range is empty, so it holds. If the loop continues, , so after it still holds. When the loop stops, either and x is not in the array, or .
Counting and Theta
Count how often the innermost statement runs as a function of n. Keep the leading term, drop constants, and ignore small n.
s := 0
for i := 1 to n do
for j := i to n do
s := s + 1The inner loop runs times, so s ends at . That is . A loop that halves its range each time runs about times, so it is .
The usual order is . Best case and worst case are different inputs of the same size: for linear search the best case is x at position 1, the worst case is x absent.
Programme pages: CSE
TU Eindhoven Selection Prep is an independent product of Anannt Education. It is not affiliated with, endorsed by, or produced by Eindhoven University of Technology (TU/e). Confirm dates and rules on tue.nl.
