Brute Force
Brute Force
Brute Force
Brute Force
A straightforward approach, usually based directly on the
problems statement and definitions of the concepts involved
Examples:
1.
Computing an (a > 0, n a nonnegative integer)
2.
3.
4.
Computing n!
Multiplying two matrices
Searching for a key of a given value in a list
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-1
Example: 7 3 2 5
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-2
Time efficiency:
(n^2)
Space efficiency:
(1), so in place
Stability:
yes
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-3
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
all characters are found to match (successful search); or
a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-4
Pattern:
001011
Text: 10010101101001100101111010
2.
Pattern: happy
Text: It is never too late to have a happy
childhood.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-5
Time efficiency:
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
3-6
Efficiency:
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-7
(n) multiplications
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-8
Closest-Pair Problem
Find the two closest points in a set of n points (in the twodimensional Cartesian plane).
Brute-force algorithm
Compute the distance between every pair of distinct points
and return the indexes of the points for which the distance
is the smallest.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-9
Efficiency:
Using divide-and-conquer!
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-10
Strengths
wide applicability
simplicity
yields reasonable algorithms for some important problems
(e.g., matrix multiplication, sorting, searching, string
matching)
Weaknesses
rarely yields efficient algorithms
some brute-force algorithms are unacceptably slow
not as constructive as some other design techniques
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-11
Exhaustive Search
A brute force solution to a problem involving search for an
element with a special property, usually among
combinatorial objects such as permutations, combinations, or
subsets of a set.
Method:
generate a list of all potential solutions to the problem in a
systematic manner (see algorithms in Sec. 5.4)
evaluate potential solutions one by one, disqualifying
infeasible ones and, for an optimization problem, keeping
track of the best one found so far
when search ends, announce the solution(s) found
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-12
b
5
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-13
Cost
2+3+7+5 = 17
2+4+7+8 = 21
8+3+4+5 = 20
8+7+4+2 = 21
5+4+3+8 = 20
5+7+3+2 = 17
((n-1)!)
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-14
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-15
Total value
{1}
{2}
{3}
{4}
{1,2}
{1,3}
{1,4}
{2,3}
{2,4}
{3,4}
{1,2,3}
{1,2,4}
{1,3,4}
{2,3,4}
{1,2,3,4}
$20
$30
$50
$10
$50
$70
$30
$80
$40
$60
not feasible
$60
not feasible
not feasible
not feasible
2
5
10
5
7
12
7
15
10
15
17
12
17
20
22
Efficiency: (2^n)
Each subset can be represented by a binary string (bit vector, Ch 5).
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-16
Person 0
Person 1
Person 2
Person 3
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-17
C= 5 8 1 8
7 6 9 4
Assignment (col.#s)
1, 2, 3, 4
1, 2, 4, 3
1, 3, 2, 4
1, 3, 4, 2
1, 4, 2, 3
1, 4, 3, 2
Total Cost
9+4+1+4=18
9+4+8+9=30
9+3+8+4=24
9+3+8+6=26
9+7+8+9=33
9+7+1+6=23
etc.
(For this particular instance, the optimal assignment can be found by
exploiting the specific features of the number given. It is: 2,1,3,4 )
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-18
A. Levitin Introduction to the Design & Analysis of Algorithms, 2nd ed., Ch. 3
3-19