Vous êtes sur la page 1sur 24

Homework

page 102 questions 1, 4, and 10


page 106 questions 4 and 5
page 111 question 1
page 119 question 9

Brute Force
A straightforward approach usually based on
problem statement and definitions
Examples:
1. Computing an (a > 0, n a nonnegative
integer)
2. Computing n!
3. Multiply two n by n matrices
4. Selection sort
5. Sequential search

String matching
pattern: a string of m characters to search for
text: a (long) string of n characters to search in

Brute force algorithm:


1.

2.

3.

Align pattern at beginning of text


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
while pattern is not found and the text is not yet exhausted,
realign pattern one position to the right and repeat step 2.

String Matching
Pattern: GATTTCG (length = m = 7)
Text: GATTTCATCAGATTTCGATACAGAT
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
GATTTCG
Text length = n = 25
Best Case: ?
Worst Case: ?

Brute Force String Matching


Pattern length = m;
Text length = n;

Best Case:
GATTTCATCAGATTTCGATACAGAT
GATTTCA
O(m) The pattern is found right away, but
you still have to do m comparison to verify
that is was found.

Brute Force String Matching


Pattern length = m;
Text length = n;

Worst Case:
O(mn)
How is this possible?

AAAAAABAAAAAABAAAAAABAAAAAABA
AAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAB

Brute Force String Matching


Pattern length = m;
Text length = n;

Average Case:
Observation: If the number of symbols is
relatively large (26 for example) then
Most shifts will happen after very few
comparisons.
In fact, there are very few pattern-text
combinations that will cause (m) comparisons
for each shift.
Note: There are always (n) shifts if the pattern
is not found

Brute Force String Matching


Pattern length = m;
Text length = n;

Average Case:
It has been shown

(very convincing, not mathematically proven)


that for both random text and human
generated text
the average case is O(m+n) = O(n)
Since n > m.

Brute force polynomial


evaluation
Problem: Find the value of polynomial
p(x) = anxn + an-1xn-1 + + a1x1 + a0
at a point x = x0
Algorithm:
p := 0.0
for i := n down to 0 do
power := 1
for j := 1 to i do
power := power * x
p := p + a[i] * power
return p

Polynomial evaluation:
improvement
We can do better by evaluating from right
to left:
Algorithm:
p := a[0]
power := 1
for i := 1 to n do
power := power * x
p := p + a[i] * power
return p

More brute force algorithm


examples:
Closest pair

Problem: find closest among n points in kdimensional space


Algorithm: Compute distance between each
pair of points
Efficiency:

More brute force algorithm


examples:
Convex hull

Problem: find smallest convex polygon


enclosing n points on the plane
Algorithm: For each pair of points p1 and p2
determine whether all other points lie to the
same side of the straight line through p1 and
p2
Efficiency:

Brute force strengths


Strengths:

wide applicability
simplicity
yields reasonable algorithms for some
important problems
searching
string matching
matrix multiplication

yields standard algorithms for simple


computational tasks
sum/product of n numbers
finding max/min in a list

Brute force weaknesses


Weaknesses:

rarely yields efficient algorithms


some brute force algorithms unacceptably
slow
not as constructive/creative as some other
design techniques

Exhaustive search
A brute force solution to a problem
involving search for an element with a
special property, usually among
combinatorial objects such a permutations,
combinations, or subsets of a set.

Exhaustive search
Method:

construct a way of listing all potential solutions


to the problem in a systematic manner
all solutions are eventually listed
no solution is repeated

Evaluate solutions one by one, perhaps


disqualifying infeasible ones and keeping
track of the best one found so far
When search ends, announce the winner

Example 1: Traveling salesman


problem
Given n cities with known distances
between each pair, find the shortest tour
that passes through all the cities exactly
once before returning to the starting city.
Alternatively: Find shortest Hamiltonian
circuit in a weighted connected graph.

Example 1: Traveling salesman


problem
Example:
2

b
5

3
4

Traveling salesman by exhaustive


search
abcda
abdca
acbda
acdba
adbca
adcba

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

Example 2: Knapsack Problem


Given n items:

weights: w1 w2 wn
values: v1 v2 vn
a knapsack of capacity W

Find the most valuable subset of the items that fit


into the knapsack

Example 2: Knapsack Problem


Example:
item
weight
1
2
2
5
3
10
4
5

value Knapsack capacity W=16


$20
$30
$50
$10

Knapsack by exhaustive search


Subset
{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}

Total weight
2
5
10
5
7
12
7
15
10
15
17
12
17
20
22

Total value
$20
$30
$50
Efficiency:
$10
$50
$70
$30
$80
$40
$60
not feasible
$60
not feasible
not feasible
not feasible

Final comments:
Exhaustive search algorithms run in a
realistic amount of time only on very small
instances
In many cases there are much better
alternatives!

Euler circuits
shortest paths
minimum spanning tree
assignment problem

In some cases exhaustive search (or

Homework
page 102 questions 1, 4, and 10
page 106 questions 4 and 5
page 111 question 1
page 119 question 9

Vous aimerez peut-être aussi