Asymptotics and disjoint sets
Derive orders of growth for loops, simplify expressions, trace weighted quick union with path compression, and an exam-level question about the worst case of union–find.
- Lessons
- Asymptotic analysis · Disjoint sets
This discussion goes with Lessons 34 and 35.
Q1: What would Swift print?
What would Swift print?
var count = 0
let n = 16
for i in 1...n {
var j = i
while j <= n {
count += 1
j *= 2
}
}
print(count)Show answer and explanation
Answer31
For each i, the inner loop doubles j from i until it passes 16. For i = 1 that is 5 steps (1, 2, 4, 8, 16); for i = 2, 4 steps; for i = 3 and 4, 3 steps each; for i = 5 to 8, 2 each; and for i = 9 to 16, 1 each: 5 + 4 + 6 + 8 + 8 = 31. Half the values of i take a single step, so the total is about 2N: Θ(N), even though the inner loop can run log N times.
Q2: Orders of growth
Give the worst-case order of growth of each function, in terms of N.
func a(_ n: Int) {
for i in 0..<n {
for j in i..<min(i + 10, n) { work(j) }
}
}
func b(_ n: Int) {
var i = n
while i > 0 {
for _ in 0..<n { work(i) }
i /= 2
}
}
func c(_ values: [Int]) -> Bool {
for (i, x) in values.enumerated() {
if values[(i + 1)...].contains(x) { return true }
}
return false
}
Show solution
a: the inner loop runs at most 10 times, a constant, so Θ(N). A nested loop is not automatically quadratic: look at what the inner bound depends on.b: the outer loop runs about log₂ N times and the inner loop N times each, so Θ(N log N). Unlike Lesson 36’s halving loop, the inner loop here does not shrink withi.c:containson the rest of the array is linear, so in the worst case, when there are no duplicates, the total is (N − 1) + (N − 2) + … = Θ(N²): it isdup1with the inner loop hidden inside a library call.
Q3: Simplify
Give the tightest Θ for each, and for the first two, constants k₁ and k₂ that prove it.
- 5N³ + N² log N
- (N² + 1)(N + 3) / N
- log₂(8N) + log₂ log₂ N
- 2^(N + 10)
- 2^(2N)
Show solution
- Θ(N³). For N ≥ 1, log N ≤ N, so 5N³ ≤ R ≤ 6N³: k₁ = 5, k₂ = 6.
- Θ(N²). It is N² + 3N + 1 + 3/N. For N ≥ 1: N² ≤ R ≤ 8N².
- Θ(log N): log₂(8N) = 3 + log₂ N, and log log N grows slower still.
- Θ(2ᴺ): 2^(N + 10) = 1024 · 2ᴺ, a constant factor.
- Θ(4ᴺ): 2^(2N) = 4ᴺ, which is not a constant multiple of 2ᴺ. Constants in an exponent are not constant factors.
Q4: Tracing weighted quick union
Start with ten items, 0 to 9, each alone. Using weighted quick union where ties put the second item’s root under the first’s, and with path compression, perform these operations and give the parent array at the end:
connect(0, 1), connect(2, 3), connect(0, 2), connect(4, 5), connect(6, 7),
connect(4, 6), connect(0, 4), isConnected(3, 7)
Show solution
var parent = Array(0..<10)
var size = Array(repeating: 1, count: 10)
func find(_ p: Int) -> Int {
var root = p
while parent[root] != root { root = parent[root] }
var p = p
while parent[p] != root && p != root {
let next = parent[p]
parent[p] = root
p = next
}
return root
}
func connect(_ p: Int, _ q: Int) {
let (a, b) = (find(p), find(q))
guard a != b else { return }
if size[a] < size[b] { parent[a] = b; size[b] += size[a] }
else { parent[b] = a; size[a] += size[b] }
}
connect(0, 1); connect(2, 3); connect(0, 2)
connect(4, 5); connect(6, 7); connect(4, 6)
connect(0, 4)
let connected = find(3) == find(7)
assert(connected)
assert(parent == [0, 0, 0, 0, 0, 4, 0, 0, 8, 9])After the first three connections, 0 is the root of 3, with 3 under 2.
The same happens to 7 under 4. connect(0, 4) joins two trees of size
4, a tie, so 4 goes under 0. Then isConnected(3, 7) runs find(3) and
find(7), which compress their paths: 3 and 7 now point directly at 0, and so
does 6, which was on 7’s path.
Q5: Exam-level: the worst case
- Give a sequence of
connectcalls on N items that makes plain quick union’sfindtake Θ(N) time. - What is the worst-case height of a weighted quick union tree with 8 items? Give a sequence that reaches it.
- With weighted quick union but no path compression, what is the order of
growth of M
isConnectedcalls on N items, in the worst case?
Show solution
connect(0, 1),connect(1, 2),connect(2, 3), …: with “make the first root a child of the second”, each call hangs the current chain’s root under a new item, and the chain grows into a path of length N − 1.- 3, which is log₂ 8. Connect pairs, then pairs of pairs, then the two
halves:
connect(0, 1),connect(2, 3),connect(4, 5),connect(6, 7),connect(0, 2),connect(4, 6),connect(0, 4). Every merge is between equal sizes, so every merge makes some items one level deeper. - Θ(M log N): each
isConnecteddoes twofinds, each up to log₂ N steps, and an adversary can build a worst-case tree first and then query its deepest item M times.