Back to the 2025 paper

Module 2: Fundamental Algorithmic Strategies

20257m

Explain the Brute Force method of algorithm design with suitable examples.

Worked SolutionAI Assisted

Brute Force Method of Algorithm Design

Definition

Brute force (exhaustive search) tries all possible candidates and picks those satisfying constraints. Simplest paradigm, directly from problem statement, no clever pruning.

Characteristics

  • Generates entire solution space, tests each candidate.
  • Always correct (finds optimum if enumerates all), but often exponential O(n!)O(n!), O(2n)O(2^n), O(nn)O(n^n).
  • Serves as baseline to compare smarter algorithms and for small nn verification.

Steps

  1. Enumerate all candidates (e.g., permutations, subsets, partitions).
  2. Test feasibility (constraints) and evaluate objective.
  3. Keep best feasible.

Examples

1. Sorting by brute force: Generate all n!n! permutations, test if sorted → Θ(n⋅n!)Θ(n·n!) vs Θ(nlog⁡n)Θ(n\log n) optimal.

2. TSP brute force: Enumerate (n−1)!/2(n-1)!/2 tours, compute length, pick min → O(n!)O(n!) vs DP Held-Karp O(n22n)O(n^2 2^n).

3. Selection / Search: Linear search O(n)O(n) brute vs binary O(log⁡n)O(\log n) (needs sortedness).

4. String Matching brute force: For text T[0..n−1]T[0..n-1], pattern P[0..m−1]P[0..m-1], slide window: for i=0..n−mi=0..n-m compare P[0..m−1]P[0..m-1] with T[i..i+m−1]T[i..i+m-1] → worst O((n−m+1)m)=O(nm)O((n-m+1)m)=O(nm) vs KMP O(n+m)O(n+m).

BruteForceMatch(T,P):
  for i=0..n-m:
    j=0; while j<m and P[j]==T[i+j]: j++
    if j==m: return i
  return -1

5. Closest Pair brute: Check all (n2)\binom{n}{2} pairs → O(n2)O(n^2) vs divide&conquer O(nlog⁡n)O(n\log n).

Advantages / Disadvantages

  • Pros: Simple, easy to implement/correct, covers all cases, good for small nn or to prove optimality via exhaustion.
  • Cons: Infeasible for large nn; does not exploit structure; usually improved by divide&conquer, DP, greedy, or heuristics.

Often first attempt; then optimize with clever design.

Similar questions

Student FinanceWeb Platform

Pocketly — Personal Finance for Students

Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.