๐ Brute Force Algorithm (Naive Method)
The Brute Force algorithm is the simplest method for solving a problem:
๐ It tries all possible solutions until it finds the correct one.
In other words:
A brute force algorithm systematically checks every possible candidate until a solution is found.
It does not use heuristics, patterns, or optimization โ it relies on pure exhaustive search.
๐ Key Characteristics
- Simple and straightforward
- Guaranteed to find a solution (if one exists)
- Inefficient for large inputs
- Often used as a baseline for comparison with optimized algorithms
๐ Brute Force in Different Problems
โ 1. Searching
Linear Search is a brute force method:
- Compare the target with every element in the list
- Time: O(n)
โ 2. String Matching
Naive String Matching:
- Try to match pattern at every position in text
- Time: O(n ร m)
โ 3. Optimization Problems
Examples:
- Traveling Salesman Problem (TSP): Try all permutations
- Time: O(n!)
- Subset sum: Try all subsets
- Time: O(2โฟ)
โ 4. Password Cracking (real-world example)
- Try every possible string until password matches
- Very slow for long passwords
- But guaranteed to find the result
๐ Why Study Brute Force?
Even though brute force is inefficient, it is important because:
- Easy to implement
- Used to validate optimized solutions
- Often used when input size is small
- Sometimes only feasible solution when no better algorithm exists
- Helps understand the problem before designing improvements
๐ Brute Force Algorithm โ General Structure
for each candidate solution:
if candidate satisfies problem conditions:
return candidate
๐ Time & Space Complexity
- Time complexity: Usually very high โ exponential or factorial
- O(n), O(nยฒ), O(2โฟ), O(n!), etc.
- Space complexity: Usually low, often O(1) or O(n)
๐ Advantages of Brute Force
- โ Simple to write and understand
- โ Guarantees to find solution (if it exists)
- โ No special knowledge required
- โ Works well for small-sized problems
- โ Useful for test-case generation and correctness checking
๐ Disadvantages of Brute Force
- โ Very slow for large inputs
- โ May perform unnecessary work
- โ Not practical for real-time or large-scale systems
- โ Often leads to exponential running time
๐ Brute Force Example in Python (Linear Search)
def brute_force_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
# Example usage
arr = [7, 3, 9, 1, 5]
print(brute_force_search(arr, 1)) # Output: 3
๐ Brute Force Example (String Matching) in Python
def brute_force_string_match(text, pattern):
n, m = len(text), len(pattern)
positions = []
for i in range(n - m + 1):
match = True
for j in range(m):
if text[i + j] != pattern[j]:
match = False
break
if match:
positions.append(i)
return positions
print(brute_force_string_match("ABAAABCD", "ABC")) # Output: [4]
๐ Summary (Exam Notes)
- Brute Force = try all possibilities
- Simple but inefficient
- Used in:
- Linear Search
- Naive String Matching
- TSP
- Subset Problems
- Time complexity often exponential
- Great for small input sizes or baseline comparison
- Easy to implement and guarantees correctness
