Introduction To Brute Force
Introduction To Brute Force
Brute-Force Algorithms
Introduction to Algorithms
Brute Force
• Powering a Number
• Selection Sort
• Exhaustive Search
• 0/1 Knapsack Problem
• Assignment Problem
2
Brute-Force Algorithm Design
• Straightforward, usually based on problem
definition.
• Rarely the most efficient but can be applied
to wide range of problems.
• For some elementary problems, almost as
good as most efficient. May not be worth cost.
• May work just as well on smaller data sets.
• Used to measure other algorithms against.
3
Calculating Powers of a Number
Problem: Compute a n, where n N.
Naive algorithm: (n).
an= n * n * n * …* n
a times
5
Selection Sort -
Example
Selection sort on the list: 89, 45, 68, 90, 29, 34, 17.
Each line corresponds to an iteration of the algorithm.
The values in bold are the smallest item for that iterations.
Elements to the left of the vertical bar are in their final
positions.
| 89 45 68 90 29 34 17
17 |45 68 90 29 34 89
17 29 | 68 90 45 34 89
17 29 34 | 90 45 68 89
17 29 34 45 | 90 68 89
17 29 34 45 68 | 90 89
17 29 34 45 68 89 | 90
CS 421 - Analysis of Algorithms 6
Selection Sort -
Analysis
Selection sort is implemented using nested for
loops:
Outer loop: iterates from 0 to n – 2 – don’t
have to visit element because already in final
sorted position
Inner loop: finds smallest value remaining the
list. Even though gets smaller each iteration, still
on order of n.
Therefore:
T(n) = (n2)
CS 421 - Analysis of Algorithms 7
Exhaustive Search
Brute-force approach to combinatorial problems
(i.e. permutations, combinations, subsets of a
given set).
1. Generate all elements of problem domain (all
possible solutions).
2. Select those that satisfy the constraints of the
problem.
3. Chose one or more that are most desirable (i.e.
optimize the objective function).
8
0/1 Knapsack Problem
Given n items of known weights w1, w2, …, wn and
values v1, v2, …, vn , and a knapsack of capacity W,
find most valuable subset of items that will fit.
9
0/1 Knapsack Problem
Analysis
The most costly operation is generating all of the
subsets of n items. Since there are 2n subsets,
10
Assignment Problem
There are n jobs that need to be completed, and n
people that need to be assigned to a job, one
person per job. The cost for the i th person to
perform job j is known C[i, j]. Find the assignment
with the lowest cost.
12
Conclusion
The Brute Force approach:
• Straightforward approach to solving
problem, usually based directly on
problem description.
• Wide applicability and simple.
• But in general, has subpar performance.
13