A
Algorithms
Lab Session
Quiz reviews, summations, LaTeX, DFS,
convex hull and string matching
Austine Iheji
B
D
C
E
F
Quiz 1: summations and cost counting
A doubling or halving index does not always mean log n. Check what the inner loop
depends on.
Case A: inner loop runs to i
Case B: inner loop runs to n
for (i = 1; i <= n; i *= 2)
for (j = 1; j <= i; j++)
work();
for (i = 1; i <= n; i *= 2)
for (j = 1; j <= n; j++)
work();
Inner loop runs i times, with i = 1, 2, 4, ..., n.
Total = 1 + 2 + 4 + ... + n
= 2n - 1
= O(n)
Inner loop runs n times, outer loop runs log n +
1 times.
Total = n + n + ... + n
= n(log n + 1)
= O(n log n)
Check to run: write the inner loop count for each value of the outer index, then add them up. If the count
changes with i, sum it. If it stays n, multiply.
2
Quiz 1: the same idea, side by side
• One loop with a doubling index: i = 1, 2, 4, ..., n gives log n iterations.
• Outer doubles, inner runs to i: a geometric sum dominated by its last term, so
O(n).
• Outer doubles, inner runs to n: log n copies of n, so O(n log n).
• Outer runs to n, inner doubles up to n: n copies of log n, so O(n log n).
3
LaTeX: introduction
• A typesetting system: you write plain text with commands and the compiler
produces a PDF.
• Strong for math, structured reports, and consistent formatting.
\documentclass{article}
\begin{document}
Hello, world. Inline math: $T(n) = 2T(n/2) + n$.
\end{document}
Link: In class introduction Single column - Online LaTeX Editor Overleaf
Link: In class Introduction Second column - Online LaTeX Editor Overleaf
LaTeX cheat sheet: latexsheet.pdf
4
DFS: the two orderings
Undirected graph, neighbors visited alphabetically, start at A.
Discovery order (first reached, pushed)
A, B, D, E, F, C
A
B
D
C
E
F
Finish order (popped, all neighbors explored)
D, C, F, E, B, A
Times (discover/finish): A 1/12, B 2/11, D 3/4, E 5/10, F 6/9, C 7/8
•
D is a dead end: discovered at 3, finished at 4.
•
From E we reach F, and from F we reach C, so C finishes
before F, and F before E.
•
Cost: every vertex and edge is handled once, O(V + E).
5
Convex hull
• The smallest convex polygon containing all points. Picture a rubber band
stretched around nails on a board.
In the Brute-Force Convex-Hull algorithm, every pair of points is tested to see if all other n - 2 points lie on one side of the line. What is the time complexity?
A. O(n)
B. O(n^2)
C. O(n^3)
D. O(2^n)
Answer: C
Reasoning: Testing each of the O(n^2) point pairs against the remaining n - 2 points costs O(n) per pair, giving O(n^3) overall.
Suppose there are 𝑛 points.
To consider every possible edge, we choose 2 points out of 𝑛:
𝑛
𝑛 𝑛−1
=
2
2
So there are approximately:
For each pair, the brute-force algorithm asks:
"Are all the other points on the same side of the line formed by these two points?"
So after choosing a pair, you potentially check all 𝑛 points.
Therefore:
𝑛 𝑛−1
×ณ
𝑛
2
Citation: Levitin, p. 139: "What is the time efficiency of this algorithm? It is in O(n^3)."
6
String matching
Find pattern P (length m) in text T (length n).
• Naive: try every shift and compare character by character. Worst case O(nm).
Suppose Brute-Force String Matching searches for pattern '001011' (m = 6) in a text of 1000 zeros. How many total
character comparisons are made?
A. 1000
B. 2985
C. 5970
D. 6000
Answer: B
Reasoning: At each of the 995 valid start positions in the all-zero text, the pattern's first two zeros match before the
third character ('1') fails, giving 3 comparisons per position: 995 × 3 = 2985. m(n−m+1) is the worst case but this is
not a worst case scenario.
7