Design and Analysis of Algorithms
106502Module 1: Introduction
Q1a. Which of the following best describes space complexity? (i) Time taken by the algorithm (ii) Memory required to run an algorithm (iii) Number of recursive calls (iv) Number of loops used20242m
Module 1: Introduction
View this question on its own page →Which of the following best describes space complexity?
(i) Time taken by the algorithm
(ii) Memory required to run an algorithm
(iii) Number of recursive calls
(iv) Number of loops usedWorked SolutionAnswer: (ii) Memory required to run an algorithm
Space complexity = total memory (input + auxiliary + program). Time is execution steps.
Q1a. Which property of an algorithm ensures that every step is clear and has exactly one meaning? (i) Finiteness (ii) Effectiveness (iii) Definiteness (iv) Generality20252m
Module 1: Introduction
View this question on its own page →Which property of an algorithm ensures that every step is clear and has exactly one meaning?
(i) Finiteness
(ii) Effectiveness
(iii) Definiteness
(iv) GeneralityWorked SolutionAnswer: (iii) Definiteness
- Finiteness: terminates after finite steps
- Definiteness: each step unambiguous
- Effectiveness: each step basic feasible
- Generality: works for all inputs of type
Q1a. Which of the following notations is used to represent the worst-case time complexity of an algorithm? (i) O-notation (ii) \Omega-notation (iii) \Theta-notation (iv) \delta-notation20232m
Module 1: Introduction
View this question on its own page →Which of the following notations is used to represent the worst-case time complexity of an algorithm?
(i) O-notation
(ii) -notation
(iii) -notation
(iv) -notationWorked SolutionAnswer: (i) O-notation
- O = upper bound (worst-case)
- Ω = lower bound (best-case)
- Θ = tight bound (average-case when upper=lower)
Worst-case expressed with Big-O.
Q1b. An algorithm is made up of two independent time complexities f(n) and g(n). Then the complexities of the algorithm is in the order of (i) f(n) \times g(n) (ii) \max \{ f(n), g(n) \} (iii) \min \{ f(n), g(n) \} (iv) f(n) + g(n)20222m
Module 1: Introduction
View this question on its own page →An algorithm is made up of two independent time complexities and . Then the complexities of the algorithm is in the order of
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (ii) max{ f(n), g(n)} (which equals (iv) f+g asymptotically)
If algorithm has two independent parts sequentially, T = f+g = Θ(max). Sum asymptotically dominated by max. Both (ii) and (iv) are correct; exam expects (ii) max as simplified.
Q1b. If an algorithm has time complexity T(n) = 5n^2 + 3n + 7, its asymptotic upper bound is: (i) O(n) (ii) O(n^2) (iii) O(n \log n) (iv) O(n^3)20242m
Module 1: Introduction
View this question on its own page →If an algorithm has time complexity , its asymptotic upper bound is:
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (ii) O(n²)
5n² dominates 3n+7; tight upper bound is O(n²). O(n³) also upper but not minimal.
Q1b. In Asymptotic notation, Big- \Omega (Omega) is used to represent (i) The upper bound (Worst-case) (ii) The lower bound (Best-case) (iii) The tight bound (Average-case) (iv) None of the above20252m
Module 1: Introduction
View this question on its own page →In Asymptotic notation, Big- (Omega) is used to represent
(i) The upper bound (Worst-case)
(ii) The lower bound (Best-case)
(iii) The tight bound (Average-case)
(iv) None of the aboveWorked SolutionCorrect option: (ii) The lower bound (as the intended distinction in this question).
Big-Ω (Omega) notation describes an asymptotic lower bound on a function's growth. Formally, (f(n) \in \Omega(g(n))) if there are constants (c>0) and (n_0) such that (f(n) \ge c,g(n)) for every (n \ge n_0).
Important clarification: Big-Ω does not inherently mean best-case complexity. Upper bound, lower bound and tight bound are properties of asymptotic notation; best-case, worst-case and average-case describe which case of an algorithm's input behaviour is being analysed. A lower bound can be applied to any of those functions.
- Big-O: asymptotic upper bound.
- Big-Ω: asymptotic lower bound.
- Big-Θ: asymptotically tight bound.
Therefore, choose option (ii) for the lower-bound part, but do not memorize “Ω = best case” as a general rule.
Q1c. The recurrence T(n) = T(n/2) + 1 has a time complexity of: (i) O(n) (ii) O(\log n) (iii) O(n \log n) (iv) O(n^2)20242m
Module 1: Introduction
View this question on its own page →The recurrence has a time complexity of:
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (ii) O(log n)
Master theorem: a=1,b=2,f=1,n^{log_b a}=1 → case 2 with f=Θ(n^{0}) → Θ(log n).
Q1d. Which sorting algorithm is considered stable and has a time complexity of O(n^2)? (i) Quick sort (ii) Merge sort (iii) Insertion sort (iv) Selection sort20232m
Module 1: Introduction
View this question on its own page →Which sorting algorithm is considered stable and has a time complexity of ?
(i) Quick sort
(ii) Merge sort
(iii) Insertion sort
(iv) Selection sortWorked SolutionAnswer: (iii) Insertion sort
Insertion sort stable O(n²) worst, adaptive O(n) nearly sorted. Merge stable O(n log n), Quick unstable, Selection unstable. Question expects insertion.
Q1e. What is the complexity of T(n) = 2T(n/4) + n^2 \log n? (i) \Theta(n^2 \log(\log n)) (ii) \Theta(n^3 \log n) (iii) \Theta(n^2 \log n) (iv) \Theta(n \log n)20232m
Module 1: Introduction
View this question on its own page →What is the complexity of ?
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (iii) Θ(n² log n)
Master: a=2,b=4, n^{log_b a}=n^{0.5}. f=n² log n dominates → case 3 → Θ(f)=Θ(n² log n). Regularity holds.
Q1f. Which one of the following functions is asymptotically smallest? (i) 2^n (ii) n^{\log n} (iii) n^{\sqrt{n}} (iv) (100)^{(\log n)^{1/3}} + (\log \log n)^{2/3}20222m
Module 1: Introduction
View this question on its own page →Which one of the following functions is asymptotically smallest?
(i)
(ii)
(iii)
(iv)Worked SolutionAnswer: (iv) is asymptotically smallest
Explanation:
Take logs or compare growth orders:
- (i) → exponential
- (ii)
- (iii)
- (iv) plus which is doubly logarithmic.
For large : .
Hence ordering from smallest to largest: (iv) (ii) (iii) (i).
Formally (sub-polynomial, quasi-polylog), (super-polynomial but sub-exponential), , .
Correct choice: (iv)
Note: Options (ii) and (iii) are often swapped in ranking; but both dominate (iv) asymptotically.
- Install PocketlyBudget App•POCKETLY
Pocketly on Android
Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.
Q1f. In algorithm analysis, what does "space complexity" refer to? (i) The number of input elements (ii) The amount of physical memory used (iii) The number of recursive calls (iv) The number of lines of code in the algorithm20232m
Module 1: Introduction
View this question on its own page →In algorithm analysis, what does "space complexity" refer to?
(i) The number of input elements
(ii) The amount of physical memory used
(iii) The number of recursive calls
(iv) The number of lines of code in the algorithmWorked SolutionAnswer: (ii) The amount of physical memory used
Explanation:
Space complexity is the total amount of memory (in terms of asymptotic function of input size ) required by an algorithm to run to completion, including:
- Input space / auxiliary space for variables, data structures, recursion stack, dynamically allocated memory.
- Often reported as auxiliary space (extra space excluding input itself).
(i) number of input elements is itself, not space complexity.
(iii) number of recursive calls contributes to stack space but is only one component.
(iv) lines of code is program size / software metric, unrelated to asymptotic memory usage.
Hence space complexity = memory consumed as function of , e.g., iterative linear search , merge sort , recursive DFS stack.
Correct choice: (ii)
Q1h. The average number of comparisons performed by merge sort algorithm in merging two sorted lists of 2 elements is (i) 8/5 (ii) 11/7 (iii) 11/6 (iv) 8/320222m
Module 1: Introduction
View this question on its own page →The average number of comparisons performed by merge sort algorithm in merging two sorted lists of 2 elements is
(i) 8/5
(ii) 11/7
(iii) 11/6
(iv) 8/3Worked SolutionAnswer: (iv)
Explanation:
Merge of two sorted lists , with all 4! / (2!2!) = 6 interleavings equally likely (distinct elements, random order). Standard merge:
i=j=0, comps=0 while i<2 and j<2: comps++; if A[i]<B[j] i++ else j++Best case = 2 comps (one list entirely smaller: or )
Worst case = 3 comps (interleaved).Enumerate:
- : , → 2
- : , , → 3
- : , , → 3
- : 2 (symmetric to 1)
- : 3
- : 3
Average = .
Worst-case merging , best , average .
Correct choice: (iv)
Q2a. Explain the concept of asymptotic notation (Big O, Big Omega, and Big Theta) in algorithm analysis.20237m
Module 1: Introduction
View this question on its own page →Explain the concept of asymptotic notation (Big O, Big Omega, and Big Theta) in algorithm analysis.
Worked SolutionAsymptotic Notations — , ,
Asymptotic notation describes growth of for large , ignoring constants and lower-order terms.
1. Big O — Upper bound ()
Example: (choose , ). Means algorithm at most that fast-growing.2. Big Omega — Lower bound ()
Example: . Means algorithm at least that slow.3. Big Theta — Tight bound ()
Equivalently and .
Example: .Small o / small omega (strict)
- : eventually (non-tight upper).
- : strict lower.
Properties & Use
- Transitivity, reflexivity, symmetry ( symmetric).
- Transpose: .
- Used to classify algorithms: Linear search worst, best, average; Merge sort ; Binary search .
- Hierarchy: .
Graphical intuition
Plot bounding above (), below (), or sandwiching () beyond .
Q2a. Solve the following recurrence by successive substitution method: f(1) = 1 \quad \text{if } n=1 f(n) = 3f(n/2) + 6 \quad \text{if } n > 120227m
Module 1: Introduction
View this question on its own page →Solve the following recurrence by successive substitution method:
Worked SolutionSolution – Recurrence , by Successive Substitution
Assume ().
Unfold:
Since , and .
With :
for power of 2.Asymptotics:
Check by Master Theorem: , , dominates case 1 → consistent.
For general (not power of 2), embed between powers of 2 → same .
Q2a. Answer the following: (i) Differentiate between best-case, worst-case, and average-case complexity with suitable examples. (ii) Use examples like linear search to demonstrate the differences. (iii) Discuss why worst-case analysis is often preferred in practice.20247m
Module 1: Introduction
View this question on its own page →Answer the following:
(i) Differentiate between best-case, worst-case, and average-case complexity with suitable examples.
(ii) Use examples like linear search to demonstrate the differences.
(iii) Discuss why worst-case analysis is often preferred in practice.Worked Solution(i) Best / Worst / Average Case Complexity
- Best case : minimum time over all inputs of size . Often overly optimistic.
- Worst case : maximum time — guarantee that algorithm never exceeds this.
- Average case : expected time over distribution (usually uniform) of inputs — needs probabilistic analysis.
(ii) Example: Linear Search for key in array
for i=1..n: if A[i]==x return i return -1- Best: → 1 comparison → → / .
- Worst: $x
otin Ax=A[n]n\Theta(n)$. - Average (uniform, always present): expected position → comparisons; if absent with probability , average = .
Similarly Binary Search: best , worst/avg ; Bubble Sort: best (with early exit), worst/avg .
(iii) Why Worst-Case Preferred
- Guarantee: bounds response time for real-time / safety-critical systems.
- Simplicity: does not require input distribution knowledge (average case needs distribution).
- Dominant for many algorithms: average = worst order-wise (e.g., linear search, quicksort worst but average — here worst still informs; randomized analysis gives expected worst over random choices).
- Adversarial inputs: worst-case protects against malicious data.
- Standard in complexity theory: , defined via worst-case polytime.
Note: Average-case is important when worst-case is rare (e.g., QuickSort, Hashing expected ), but worst-case remains the default metric published.
Q2b. Define time complexity and space complexity. Write an algorithm for adding n natural numbers and find the space required by that algorithm.20257m
Module 1: Introduction
View this question on its own page →Define time complexity and space complexity. Write an algorithm for adding n natural numbers and find the space required by that algorithm.
Worked SolutionTime and Space Complexity + Algorithm to Add Naturals
Definitions
- Time Complexity: asymptotic number of basic operations (steps) as function of input size , .
- Space Complexity: asymptotic total memory (input + auxiliary + recursion stack) ; often reported as auxiliary space (extra beyond input).
Algorithm — Sum of First Natural Numbers
Iterative time, aux space:
Algorithm SumNatural(n): sum ← 0 for i ← 1 to n: sum ← sum + i return sumTime: iterations × → ( additions). Space: variables , , → 3 words → auxiliary ( if counting input array? Here input is just → ).
Formula time (mathematical):
Algorithm SumFormula(n): return n*(n+1)//2Time arithmetic operations, space . Uses closed form .
Recursive (for illustration):
RecSum(n): if n==1 return 1 else return n + RecSum(n-1)Time , stack space due to frames — worse.
Analysis Summary
Variant Time Auxiliary Space Total Space Iterative loop Formula Recursive stack Best practical: formula . If required to demonstrate space complexity concept, loop shows auxiliary despite time.
Q2b. Consider the following recurrence relation: T(n) = 2T(n/2) + n Use the substitution method to find an asymptotic upper bound for the function T(n).20237m
Module 1: Introduction
View this question on its own page →Consider the following recurrence relation:
Use the substitution method to find an asymptotic upper bound for the function .Worked Solution— Substitution Method, Upper Bound
Guess: for some , (with ).
Inductive Hypothesis: Assume for : .
Inductive Step:
With base 2, :
Choose → → .Base case: Need fails for log base2. Use or define , choose , pick large enough: → works. More formally prove for , or use with and show variant.
Hence .
Tight bound: Similarly show → → .
By Master Theorem case 2 (, , , ) → .Application: This is the Merge Sort recurrence.
Q2b. Suppose the number of basic operations in an algorithm is defined as: T(n) = T(n-1) + n, with T(1) = 1. Solve this recurrence using the substitution method and determine the time complexity.20247m
Module 1: Introduction
View this question on its own page →Suppose the number of basic operations in an algorithm is defined as: , with .
Solve this recurrence using the substitution method and determine the time complexity.Worked Solution, — Solve by Iteration / Substitution
Iterative Unfolding:
Substitution Proof: Guess .
- Base : ✓
- Step: assume → ✓
Asymptotics:
Application: Sum of first naturals; time of naive incremental algorithms (e.g., insertion sort worst-case without divide).
Q3a. Discuss the concepts of asymptotic notations and its properties.20257m
Module 1: Introduction
View this question on its own page →Discuss the concepts of asymptotic notations and its properties.
Worked SolutionAsymptotic Notations & Properties
Definitions (as in Q36)
- upper: .
- lower: .
- tight: → .
- strict upper: eventually ().
- strict lower ().
Properties
- Transitivity: (similar for ).
- Reflexivity: .
- Symmetry: ; transpose symmetry .
- Additivity/Max rule: If then . Hence polynomial .
- Scaling: for constant ; lower-order terms absorbed.
- Ordering: .
- Polynomial gap: if ; any poly is .
- Master/recurrence use: notations hide constants allowing comparison Unabhängig von Hardware.
Example Hierarchy
- Linear search worst , binary , merge , matrix mult naive , Strassen , TSP brute .
Pitfalls
- is not tight — use when both upper and lower known.
- Not all functions comparable ( incomparable).
- Amortized vs worst-case: still worst over sequence.
Q3a. What is the Master's Theorem? State the general form and the three cases of the theorem. Use it to solve the following recurrence relations: (i) T(n) = 2T(n/2) + n (ii) T(n) = 3T(n/2) + n^2 (iii) T(n) = 2T(n/2) + n/\log n Explain which case applies in each situation.20247m
Module 1: Introduction
View this question on its own page →What is the Master's Theorem? State the general form and the three cases of the theorem. Use it to solve the following recurrence relations:
(i)
(ii)
(iii)
Explain which case applies in each situation.Worked SolutionMaster's Theorem — Statement & Applications
Theorem
For , , :
Let .- Case 1: → (leaf heavy).
- Case 2: → ; with → (balanced).
- Case 3: + regularity → (root heavy).
(i)
- → , → Case 2 (k=0) → (Merge Sort).
(ii)
- , , with .
- Regularity: with holds → Case 3 →
Alternatively leaf vs root: root dominates.
(iii)
- → , .
- This is not polynomially smaller () nor ; it is — gap between cases 1 and 2.
- Master Theorem does NOT apply (fails polynomial gap condition). By recursion tree / Akra-Bazzi:
Proof sketch: . - If forced into extended Case 2 (), some extensions give .
Summary: (i) Case2, (ii) Case3, (iii) Gap case — , master not applicable in strict form.
- Install PocketlyBudget App•POCKETLY
Pocketly on Android
Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.
Q3a. Apply the Master Theorem to determine the time complexity of the following recurrence relations. (i) T(n) = 4T(n/2) + n^3 (ii) T(n) = T(n/2) + 2^n20235m
Module 1: Introduction
View this question on its own page →Apply the Master Theorem to determine the time complexity of the following recurrence relations.
(i)
(ii)Worked SolutionMaster Theorem Applications
General Form: , , , asymptotically positive.
Let be the watershed.- Case 1: for → .
- Case 2: → ; special : .
- Case 3: and regularity for → .
(i)
- , → .
- with .
- Regularity: with ✓
- Case 3 →
(ii)
- , → .
- is exponential, not polynomial; for any , dominated by .
- Although , regularity holds: for large () as .
- This fits Case 3 (generalized interpretation) where dominates polynomially. Hence
Note: Strict Master theorem in many textbooks requires polynomial; for exponential , the theorem still yields because and regularity holds; formally the answer is exponential dominated, not . Some state "Master theorem not applicable (f not polynomial)" but via extension the bound is ; if insisting on polynomial condition, declare not applicable, recurrence solves to by recursion tree: .
Q4a. Apply the Linear Search algorithm to find the position of the target element 7 in the following array: A=[3, 5, 2, 8, 7, 1, 4]. Also, analyze the time and space complexity.20237m
Module 1: Introduction
View this question on its own page →Apply the Linear Search algorithm to find the position of the target element 7 in the following array: . Also, analyze the time and space complexity.
Worked SolutionLinear Search for 7 in
Algorithm
LinearSearch(A, n, key): for i=0 to n-1: if A[i]==key: return i // 0-based index, +1 for 1-based position return -1 // not foundTrace for key=7
i (0-based) A[i] Compare A[i]==7? Action 0 3 3==7? No continue 1 5 5==7? No continue 2 2 2==7? No continue 3 8 8==7? No continue 4 7 7==7? Yes return 4 (0-based) / 5 (1-based) Search stops early; elements after index4 not examined. Result: Found at index 4 (0-based) → position 5 (1-based). Comparisons = 5 best/worst?
Complexity Analysis ()
- Time:
- Best (key at first position)
- Worst (key absent or at last) → here worst would be 7 comparisons
- Average → expected if key present uniformly
- In this instance 5 comparisons = worst-like.
- Space: auxiliary (loop variable + key), total if counting input array; in-place.
- No sorting needed; works on unsorted arrays; optimal for unsorted (any algorithm must examine in worst case).
- Time:
Q8b. State master's theorem and find the time complexity for the following recurrence: T(n) = 2T(n^{1/2}) + \log n20227m
Module 1: Introduction
View this question on its own page →State master's theorem and find the time complexity for the following recurrence:
Worked SolutionMaster's Theorem &
Master's Theorem Statement (standard)
For , , , :
- Case1
- Case2
- Case3 + regularity → Θ(f)
(Requires form, not .)
Can Master Apply to ?
Not directly — argument is , not with constant . Standard master not applicable. Use substitution / change of variable.
Solution via Domain Transformation
Let (assuming is power of 2 for asymptotics), define .
Then:
Now → This is master form with , , .- , → Case2 → .
Convert back: →
Result:
Alternatively .Verification via Recursion Tree
Depth: each level halves → levels = levels; work per level → total .
Q9. Write short notes on any two of the following: (i) Asymptotic Notations (ii) Clique decision problem (iii) Max-flow-min-cut theorem (iv) Union-Find Algorithm202214m
Module 1: Introduction
View this question on its own page →Write short notes on any two of the following:
(i) Asymptotic Notations
(ii) Clique decision problem
(iii) Max-flow-min-cut theorem
(iv) Union-Find AlgorithmWorked SolutionShort Notes — Any Two (Covering All Four for Choice)
(i) Asymptotic Notations (see Q36/Q88)
upper, lower, tight, / strict. Hierarchy . Used to compare algorithms independent of hardware; max rule for polynomials etc.
(ii) Clique Decision Problem
Given undirected and integer , does contain a clique (complete subgraph) of size ≥ ?
- NP-complete: In NP (certificate = vertices, check all edges in ) + NP-hard via reduction from 3-SAT or Vertex Cover (complement: has clique ⇔ complement has vertex cover ). Brute exponential; no known polytime.
- Applications: clustering, social networks.
(iii) Max-Flow Min-Cut Theorem (Ford-Fulkerson)
For network with source , sink , capacities , value of max flow = capacity of min cut (set , partition, capacity sum of edges ). Proof via Ford-Fulkerson: flow maximal iff no augmenting path in residual graph → reachable set from defines min cut with equal capacity. Consequences: integrality (if capacities integer, max flow integer), duality for bipartite matching, etc. Used to prove correctness of Ford-Fulkerson (Edmonds-Karp) etc.
(iv) Union-Find (Disjoint Set Union, DSU) Algorithm
Maintains partition of elements into disjoint sets with operations:
- MakeSet(x): , rank/size 0
- Find(x): with path compression →
- Union(x,y): by rank/size →
Sequence of ops on elements → almost constant ( inverse Ackermann <5 for practical ).
Uses: Kruskal MST cycle check, connected components, offline LCA. Pseudo:
Find(x): if parent[x]!=x: parent[x]=Find(parent[x]); return parent[x] Union(a,b): ra=Find(a), rb=Find(b); if ra==rb return; if rank[ra]<rank[rb] swap; parent[rb]=ra; if rank equal rank[ra]++Q9b. Write a short note on: Recursion tree method20257m
Module 1: Introduction
View this question on its own page →Write a short note on:
Recursion tree method
Worked SolutionRecursion Tree Method
Idea
Visualize recurrence as tree where node = subproblem cost, level = total cost of that recursion depth, sum over levels gives total. Complements Master Theorem and substitution; handles forms master cannot (e.g., , uneven splits).
General Form
- Root cost
- children cost each → level1
- nodes cost → level2
- Depth leaves, leaves count cost each → leaf level .
- Total .
Example
Level0 , Level1 , Level2 → geometric ratio → sum dominated by root → (Case3).
Example
Tree: root , level1 , level2 → each level , depth → total .
Steps
- Draw tree, label cost per node/level.
- Determine depth (until or constant).
- Sum levels; identify geometric vs constant series.
- Verify via substitution (induction).
Advantages
- Intuitive for guessing bound for substitution proof.
- Handles non-uniform: → recursion tree shows still worst but more complex.
Use: Guess then prove via substitution method for rigor.
Q9d. Write a short note on: Asymptotic Notations20257m
Module 1: Introduction
View this question on its own page →Write a short note on:
Asymptotic Notations
Worked SolutionAsymptotic Notations — Short Note
Purpose
Describe algorithm growth for large , abstracting constants/hardware, classifying efficiency (as in Q36/Q88).
Definitions
- : — at most (upper bound).
- : — at least (lower bound).
- : — tight bound ().
- : eventually — strict upper ().
- : strict lower ().
Examples
- .
- , .
Hierarchy
— helps choose algorithm (e.g., merge vs insertion ).
Properties
Transitivity, reflexivity, symmetry for , transpose , sum rule .
Use: Analyze worst/best/average cases; e.g., linear search worst , binary , quicksort avg .