Brute Force: The Design and Analysis of Algorithms
Brute Force: The Design and Analysis of Algorithms
Brute Force: The Design and Analysis of Algorithms
Basic Idea
Brute Force Search and Sort
Brute Force String Matching
Closest Pair and Convex Hull Problems
Exhaustive Search
Conclusion
2
Basic Idea
A straightforward approach to solve a
problem based on the problem’s
statement and definitions of the concepts
involved.
Example
Computing an based on the definition of exponentiation:
an = a* a* a* …. * a
3
Brute Force Search and Sort
Sequential Search O(n)
Selection Sort O(n2)
Bubble Sort O(n2)
4
Brute Force String Matching
Pattern: program
Comparisons:
Text:
(n*m) in the worst
possible case
Write a program.
(n+m) (n) in the
program
average case.
program
…
program
5
Closest Pair Problem
Find the two closest points in a set of
n points in k-dimensional space.
Algorithm ClosestPairPoints (P)
dmin ← ∞
for i ← 1 to n-1 do
for j ← i + 1 to n do
d ← sqrt ((xi – xj) 2 + (yi – yj)2)
if d < dmin
(n2) dmin ← d
6
Convex Hull Problem
Convex set: For any two points P and Q in the
set, the entire line segment with the end points
at P and Q belongs to the set
Convex hull of a set S of points is the smallest
convex set containing S
Algorithm:
for each of n(n-1)/2 pairs of distinct points
for each of the other n – 2 points
find the sign of ax + by – c
8
Exhaustive Search
State-space search
Given an initial state,
a goal state, and
a set of operations,
find a sequence of operations that transforms the
initial state to the goal state.
9
Exhaustive Search
Combinatorial problems
10
Conclusion - Strengths
11
Conclusion - Weaknesses
Brute Force approach rarely yields
efficient algorithms
Some brute force algorithms are
unacceptably slow
Brute Force approach is neither as
constructive nor creative as some other
design techniques
12