Practice Exam

Test Your Knowledge - Based on Quiz 5 (Reductions) & Quiz 2 (Divide & Conquer)

Instructions

Time Limit: 90 minutes (suggested)

Total Points: 200 points

Note: This is a CLOSED book, closed-notes exam. Work through each problem independently. Write your answers in the provided answer spaces. Click "Show Answer Key" at the bottom to reveal detailed solutions.

SECTION 1: Maximum Flow (50 points)

Consider the following flow network G. Edges are labeled with flow/capacity notation.

s a b c d t sa: 5/10 sb: 3/8 ac: 5/7 ab: 0/4 bc: 3/6 bd: 0/5 cd: 2/4 ct: 6/10 dt: 2/7
Question 1.1 [24 points]

Create the residual graph GR based on the current flow state shown above. Fill in the table below with the edge weights (flow values only, not capacity). The second row is for reverse edges.

sasbabacbcbdcdctdt
         
saRsbRabRacRbcRbdRcdRctRdtR
         
Question 1.2 [10 points]

Find an augmenting path that the Edmonds-Karp algorithm would provide (uses BFS - shortest path first). Write the path in the form "ABCD" where each letter is a vertex.

Question 1.3 [10 points]

Find an augmenting path that the Ford-Fulkerson algorithm could find but Edmonds-Karp would NOT find (any valid path, not necessarily shortest).

Question 1.4 [6 points]

What is the maximum flow of this graph? Show the augmenting paths you used.

SECTION 2: Reductions & NP-Completeness (50 points)

Word Limit: Questions 2.1-2.5 must be answered in 20 words or fewer. Question 2.6 can be up to 30 words.

Question 2.1 [8 points]

What does it mean if a problem X is in the complexity class P?

Question 2.2 [8 points]

What does it mean if a problem X is in the complexity class NP?

Question 2.3 [8 points]

Why is P ⊆ NP (why is P a subset of NP)?

Question 2.4 [9 points]

Explain, in plain English, what NP-complete means. This should be understandable to someone with no computing or mathematical background.

Question 2.5 [10 points]

What steps must you complete to show that a problem is NP-complete? (Maximum 30 words)

Question 2.6 [7 points]

For a problem reduction to be useful when analyzing complexity, what must be true about the time complexity of the reduction step?

SECTION 3: Divide and Conquer (60 points)
Question 3.1 [30 points]

Palindrome Finding Problem: Design a divide-and-conquer algorithm to find the longest palindrome in an array A[] of n characters in Θ(n log n) time. Answer the following:

a) Base case: [6 points]

b) Divide step: [6 points]

c) Conquer (recursive) step: [6 points]

d) Combine step: [6 points]

e) Recurrence relation: [6 points]

Question 3.2 [18 points]

Median-of-Medians: Consider a 3×3 blocks version of median-of-medians (three blocks of three elements each) on a list of 9 elements. Using a brief English explanation and/or a diagram, show that the median-of-medians is greater than at least 33% of the values.

Question 3.3 [12 points]

Master Theorem: For the recurrence T(n) = 4T(n/2) + n², determine:

  • Which case of the Master Theorem applies
  • Justification (values of a, b, f(n), ε, etc.)
  • The asymptotic growth
SECTION 4: Asymptotic Analysis & Proof by Induction (40 points)
Question 4.1 [25 points]

Proof by Induction: Using the guess-and-check method, prove that binary search is O(n) using induction.

a) Base case(s): What are your base case(s)? Show that they work. [8 points]

b) Inductive hypothesis: [6 points]

c) Inductive step: Prove your inductive step. [11 points]

Question 4.2 [15 points]

Master Theorem Applications: For each recurrence relation, state which case of the Master Theorem applies and give the asymptotic bound.

a) T(n) = 2T(n/2) + n log n [5 points]

b) T(n) = 3T(n/3) + n² [5 points]

c) T(n) = 8T(n/2) + n² [5 points]

DETAILED ANSWER KEY

All answers are provided with complete explanations to ensure no ambiguity.

SECTION 1: Maximum Flow (50 points)

Question 1.1 [24 points]: Residual Graph Edges

Forward Edges (remaining capacity):

sasbabacbcbdcdctdt
554235245

Reverse Edges (current flow that can be pushed back):

saRsbRabRacRbcRbdRcdRctRdtR
530530262

Explanation:

  • Forward edge weight = capacity - current_flow
  • Example: sa has flow 5/10, so forward edge = 10 - 5 = 5
  • Reverse edge weight = current_flow
  • Example: sa has flow 5, so reverse edge saR = 5
  • Edges with 0 current flow (ab, bd) have 0 reverse capacity
Question 1.2 [10 points]: Edmonds-Karp Augmenting Path

ANSWER: s → a → c → t

Explanation:

  • Edmonds-Karp uses BFS, which finds the shortest path (fewest edges) first
  • This path has length 3 (3 edges)
  • All edges have available capacity in the residual graph:
    • s → a: capacity 5 available
    • a → c: capacity 2 available
    • c → t: capacity 4 available
  • Bottleneck = min(5, 2, 4) = 2
  • Any other path of length 3 would also be acceptable for EK
Question 1.3 [10 points]: FF Path (NOT EK)

ANSWER: s → b → c → d → t

Explanation:

  • Ford-Fulkerson can find any valid augmenting path, not just shortest
  • This path has length 4 (4 edges)
  • Edmonds-Karp would NOT find this because it's longer than the shortest path (length 3)
  • All edges have available capacity:
    • s → b: capacity 5 available
    • b → c: capacity 3 available
    • c → d: capacity 2 available
    • d → t: capacity 5 available
  • Bottleneck = min(5, 3, 2, 5) = 2
  • Other acceptable answers: s → a → b → c → t (length 4), s → a → b → c → d → t (length 5)
Question 1.4 [6 points]: Maximum Flow

ANSWER: Maximum flow = 13

Augmenting paths used:

  1. s → a → c → t with bottleneck = 2
  2. s → b → c → d → t with bottleneck = 3
  3. s → a → b → d → t with bottleneck = 4
  4. s → b → d → t with bottleneck = 1
  5. s → a → c → d → t with bottleneck = 2
  6. s → b → c → t with bottleneck = 1

Verification:

  • Flow out of s = 5 (to a) + 8 (to b) = 13
  • Flow into t = 10 (from c) + 3 (from d) = 13
  • Minimum cut = {sa, sb} with capacity 10 + 8 = 18, or {ct, dt} with capacity 10 + 7 = 17
  • Actual minimum cut = 13 (verified by max-flow min-cut theorem)

SECTION 2: Reductions & NP-Completeness (50 points)

Question 2.1 [8 points]: Complexity Class P

ANSWER: Problem X can be solved in polynomial time.

Alternative acceptable answers:

  • "X can be solved by a deterministic Turing machine in polynomial time"
  • "There exists an algorithm that solves X in O(nk) time for some constant k"
  • "X is efficiently solvable"
Question 2.2 [8 points]: Complexity Class NP

ANSWER: Problem X can be verified in polynomial time.

Alternative acceptable answers:

  • "X can be solved by a non-deterministic Turing machine in polynomial time"
  • "A solution to X can be checked in polynomial time"
  • "X can be solved in polynomial time via non-determinism"

Note: NP stands for "Nondeterministic Polynomial time," NOT "Non-Polynomial"

Question 2.3 [8 points]: Why P ⊆ NP

ANSWER: If a problem can be solved in polynomial time, it can also be verified in polynomial time.

Alternative acceptable answers:

  • "Any deterministic polynomial algorithm can also verify in polynomial time"
  • "Solving is at least as hard as verifying"
  • "If you can solve it fast, you can check it fast"

Detailed explanation: If we have an algorithm that solves problem X in polynomial time, we can use that same algorithm to verify a proposed solution (just solve the problem ourselves and compare the result). Since solving takes polynomial time, verification also takes polynomial time, so X ∈ NP.

Question 2.4 [9 points]: NP-Complete Explanation (Plain English)

ANSWER: NP-complete problems are the hardest problems to solve, but easy to check if someone gives you an answer.

Alternative acceptable answers:

  • "Problems that are extremely difficult to solve but solutions can be verified quickly"
  • "The hardest problems where checking an answer is fast but finding it is slow"
  • "Problems with no known fast solution, but you can verify a solution quickly"

Real-world analogy: Like a jigsaw puzzle - very hard to solve, but easy to check if it's completed correctly by just looking at it.

Question 2.5 [10 points]: Proving NP-Completeness

ANSWER: (1) Show the problem is in NP (verifiable in polynomial time), AND (2) Reduce a known NP-complete problem to it in polynomial time.

Breakdown of the two steps:

  1. Show X is in NP: Demonstrate that given a candidate solution, you can verify it's correct in polynomial time
  2. Show X is NP-hard: Take a known NP-complete problem Y and show how to transform any instance of Y into an instance of X in polynomial time (reduction Y ≤p X)

Common mistakes to avoid:

  • ❌ Reducing X to a known NP-complete problem (direction is backwards)
  • ❌ Only showing X is in NP (missing the NP-hard part)
  • ❌ Using exponential-time reduction (must be polynomial)
Question 2.6 [7 points]: Reduction Time Complexity

ANSWER: d) Polynomial time

Explanation: For a reduction to be useful in complexity analysis, the reduction itself must take polynomial time (or faster). This is critical because:

  • If we reduce problem A to problem B in polynomial time, and B is solvable in polynomial time, then A is also solvable in polynomial time
  • The reduction should not be the bottleneck - it should be faster than or comparable to solving the target problem
  • For NP-completeness proofs, we specifically use polynomial-time reductions

Why other answers are wrong:

  • a, b, c: Too restrictive - polynomial time is sufficient
  • e, f: If reduction is exponential, it defeats the purpose of using it to analyze complexity

SECTION 3: Divide and Conquer (60 points)

Question 3.1 [30 points]: Palindrome Finding Algorithm

a) Base case [6 points]:

ANSWER: If the array has 1 or 2 elements, check directly. A single character is a palindrome of length 1. Two characters are a palindrome if they match, otherwise the longest palindrome is length 1.

b) Divide step [6 points]:

ANSWER: Divide the array A[1...n] into two halves: left = A[1...n/2] and right = A[n/2+1...n]

c) Conquer step [6 points]:

ANSWER: Recursively find the longest palindrome in the left half and in the right half. Let these be Lleft and Lright.

d) Combine step [6 points]:

ANSWER: Find the longest palindrome that crosses the midpoint by expanding from the center outward (similar to merge in mergesort). Compare Lleft, Lright, and Lcross, and return the longest one. The crossing palindrome check takes O(n) time.

e) Recurrence relation [6 points]:

ANSWER: T(n) = 2T(n/2) + O(n)

This solves to Θ(n log n) by Master Theorem Case 2 (a=2, b=2, logba = 1, f(n) = n = n1)

Question 3.2 [18 points]: Median-of-Medians 3×3 Proof

ANSWER:

Arrange 9 elements into 3 groups of 3:

Group 1: [a, b, c]  → median = b
Group 2: [d, e, f]  → median = e
Group 3: [g, h, i]  → median = h

Medians: [b, e, h] → median-of-medians = e
                    

After sorting each group:

a ≤ b ≤ c
d ≤ e ≤ f
g ≤ h ≤ i
                    

And sorting the medians: b ≤ e ≤ h

Elements definitely ≤ e: {a, b, d, e} = 4 elements

  • From Group 1: a, b (both ≤ b ≤ e)
  • From Group 2: d, e (both ≤ e)

Elements definitely ≥ e: {e, f, h, i} = 4 elements

  • From Group 2: e, f (both ≥ e)
  • From Group 3: h, i (both ≥ h ≥ e)

Conclusion: e is greater than or equal to at least 4 out of 9 elements = 44.4% > 33% ✓

Note: We're unsure about c and g, but we don't need them to prove the 33% guarantee.

Question 3.3 [12 points]: Master Theorem Analysis

Recurrence: T(n) = 4T(n/2) + n²

Identify parameters:

  • a = 4 (number of recursive calls)
  • b = 2 (division factor)
  • f(n) = n² (work outside recursion)
  • logba = log24 = 2
  • So nlogba = n²

Compare f(n) with nlogba:

  • f(n) = n²
  • nlogba = n²
  • They are equal: f(n) = Θ(n²)

Which case applies: Case 2

Since f(n) = Θ(nlogba logk n) where k = 0

Solution: T(n) = Θ(n² log n)

Intuition: The work at each level is n², and there are log n levels, so total work is n² × log n.

SECTION 4: Asymptotic Analysis & Proof by Induction (40 points)

Question 4.1 [25 points]: Binary Search O(n) Proof

a) Base case(s) [8 points]:

ANSWER: Base case: n = 1

  • For an array of size 1, binary search makes 1 comparison and terminates
  • We claim T(1) ≤ c·1 for some constant c
  • Choose c = 1, then T(1) = 1 ≤ 1·1 = 1 ✓

Alternative base case: n = 0

  • Empty array requires 0 operations
  • T(0) = 0 ≤ c·0 for any c ✓

b) Inductive hypothesis [6 points]:

ANSWER: Assume that for all k < n, T(k) ≤ c·k for some constant c ≥ 1

c) Inductive step [11 points]:

ANSWER: Need to prove: T(n) ≤ c·n

Binary search recurrence: T(n) = T(n/2) + O(1)

Let d be the constant for the O(1) work, so T(n) = T(n/2) + d

By inductive hypothesis: T(n/2) ≤ c·(n/2)

Therefore:

T(n) = T(n/2) + d
     ≤ c·(n/2) + d         (by inductive hypothesis)
     = cn/2 + d
     ≤ cn                  (if we choose c ≥ 2d)
                    

Conclusion: By choosing c ≥ max(1, 2d), we have T(n) ≤ cn for all n ≥ 0, proving binary search is O(n) ✓

Note: This is a weak bound - binary search is actually O(log n), but O(n) is technically correct since O(log n) ⊆ O(n).

Question 4.2 [15 points]: Master Theorem Applications

a) T(n) = 2T(n/2) + n log n [5 points]

  • a = 2, b = 2, f(n) = n log n
  • logba = log22 = 1, so nlogba = n
  • Compare f(n) = n log n with n1 = n
  • f(n) = n log n = Θ(n1 log1 n)
  • Case 2 applies (with k=1)
  • Answer: T(n) = Θ(n log² n)

b) T(n) = 3T(n/3) + n² [5 points]

  • a = 3, b = 3, f(n) = n²
  • logba = log33 = 1, so nlogba = n
  • Compare f(n) = n² with n1 = n
  • f(n) = n² = Ω(n1+ε) where ε = 1
  • Check regularity: 3·f(n/3) = 3·(n/3)² = n²/3 ≤ (1/3)·n² for large n ✓
  • Case 3 applies
  • Answer: T(n) = Θ(n²)

c) T(n) = 8T(n/2) + n² [5 points]

  • a = 8, b = 2, f(n) = n²
  • logba = log28 = 3, so nlogba = n³
  • Compare f(n) = n² with n³
  • f(n) = n² = O(n3-ε) where ε = 1
  • Case 1 applies
  • Answer: T(n) = Θ(n³)

Grading Rubric Summary

  • Section 1 (Max Flow): 50 points - Focus on correct residual graph calculation and understanding difference between FF and EK
  • Section 2 (Reductions/NP): 50 points - Must stay within word limits; precision in definitions is critical
  • Section 3 (D&C): 60 points - Algorithm design and Master Theorem application
  • Section 4 (Asymptotic): 40 points - Proof rigor and correct application of Master Theorem cases

Total: 200 points