Design and Analysis of Algorithms - Chapter 3 1
Brute Force
Brute Force
A straightforward approach usually based on problem
A straightforward approach usually based on problem
statement and definitions
statement and definitions
Examples:
Examples:
1.
1. Computing
Computing a
an
n
(
(a
a > 0,
> 0, n
n a nonnegative integer)
a nonnegative integer)
2.
2. Computing
Computing n
n!
!
3.
3. Multiply two
Multiply two n
n by
by n
n matrices
matrices
4.
4. Selection sort
Selection sort
5.
5. Sequential search
Sequential search
Design and Analysis of Algorithms - Chapter 3 2
String matching
String matching
 pattern
pattern: a string of
: a string of m
m characters to search for
characters to search for
 text
text: a (long) string of
: a (long) string of n
n characters to search in
characters to search in
 Brute force algorithm:
Brute force algorithm:
1.
1. Align pattern at beginning of text
Align pattern at beginning of text
2.
2. moving from left to right, compare each character of pattern to the
moving from left to right, compare each character of pattern to the
corresponding character in text until
corresponding character in text until
– all characters are found to match (successful search); or
all characters are found to match (successful search); or
– a mismatch is detected
a mismatch is detected
3.
3. while pattern is not found and the text is not yet exhausted, realign
while pattern is not found and the text is not yet exhausted, realign
pattern one position to the right and repeat step 2.
pattern one position to the right and repeat step 2.
Design and Analysis of Algorithms - Chapter 3 3
Brute force string matching – Examples:
Brute force string matching – Examples:
1.
1. Pattern:
Pattern: 001011
001011
Text:
Text: 10010101101001100101111010
10010101101001100101111010
2.
2. Pattern:
Pattern: happy
happy
Text:
Text: It is never too late to have a happy childhood.
It is never too late to have a happy childhood.
Number of comparisons:
Number of comparisons:
Efficiency:
Efficiency:
Design and Analysis of Algorithms - Chapter 3 4
Brute force polynomial evaluation
Brute force polynomial evaluation
 Problem: Find the value of polynomial
Problem: Find the value of polynomial
p
p(
(x
x) =
) = a
an
nx
xn
n
+
+ a
an
n-1
-1x
xn
n-1
-1
+… +
+… + a
a1
1x
x1
1
+
+ a
a0
0
at a point
at a point x
x =
= x
x0
0
 Algorithm:
Algorithm:
 Efficiency:
Efficiency:
p
p := 0.0
:= 0.0
for
for i
i :=
:= n
n down to 0 do
down to 0 do
power := 1
power := 1
for
for j
j := 1 to
:= 1 to i
i do
do
power := power
power := power
*
* x
x
p
p :=
:= p
p +
+ a
a[
[i
i] * power
] * power
return
return p
p
Design and Analysis of Algorithms - Chapter 3 5
Polynomial evaluation: improvement
Polynomial evaluation: improvement
 We can do better by evaluating from right to left:
We can do better by evaluating from right to left:
 Algorithm:
Algorithm:
 Efficiency:
Efficiency:
p
p :=
:= a
a[0]
[0]
power := 1
power := 1
for
for i
i := 1 to
:= 1 to n
n do
do
power := power *
power := power * x
x
p
p :=
:= p
p +
+ a
a[
[i
i] * power
] * power
return
return p
p
Design and Analysis of Algorithms - Chapter 3 6
More brute force algorithm examples:
More brute force algorithm examples:
 Closest pair
Closest pair
• Problem
Problem: find closest among
: find closest among n
n points in
points in k
k-dimensional space
-dimensional space
• Algorithm
Algorithm: Compute distance between each pair of points
: Compute distance between each pair of points
• Efficiency
Efficiency:
:
 Convex hull
Convex hull
• Problem
Problem: find smallest convex polygon enclosing
: find smallest convex polygon enclosing n
n points on the
points on the
plane
plane
• Algorithm
Algorithm: For each pair of points
: For each pair of points p
p1
1 and
and p
p2
2 determine whether all
determine whether all
other points lie to the same side of the straight line through
other points lie to the same side of the straight line through p
p1
1 and
and
p
p2
2
• Efficiency
Efficiency:
:
Design and Analysis of Algorithms - Chapter 3 7
Brute force strengths and weaknesses
Brute force strengths and weaknesses
 Strengths:
Strengths:
• wide applicability
wide applicability
• simplicity
simplicity
• yields reasonable algorithms for some important problems
yields reasonable algorithms for some important problems
– searching
searching
– string matching
string matching
– matrix multiplication
matrix multiplication
• yields standard algorithms for simple computational tasks
yields standard algorithms for simple computational tasks
– sum/product of
sum/product of n
n numbers
numbers
– finding max/min in a list
finding max/min in a list
 Weaknesses:
Weaknesses:
• rarely yields efficient algorithms
rarely yields efficient algorithms
• some brute force algorithms unacceptably slow
some brute force algorithms unacceptably slow
• not as constructive/creative as some other design techniques
not as constructive/creative as some other design techniques
Design and Analysis of Algorithms - Chapter 3 8
Exhaustive search
Exhaustive search
 A brute force solution to a problem involving search for an
A brute force solution to a problem involving search for an
element with a special property, usually among
element with a special property, usually among
combinatorial objects such a permutations, combinations,
combinatorial objects such a permutations, combinations,
or subsets of a set.
or subsets of a set.
 Method:
Method:
• construct a way of listing all potential solutions to the problem in a
construct a way of listing all potential solutions to the problem in a
systematic manner
systematic manner
– all solutions are eventually listed
all solutions are eventually listed
– no solution is repeated
no solution is repeated
• Evaluate solutions one by one, perhaps disqualifying infeasible ones
Evaluate solutions one by one, perhaps disqualifying infeasible ones
and keeping track of the best one found so far
and keeping track of the best one found so far
• When search ends, announce the winner
When search ends, announce the winner
Design and Analysis of Algorithms - Chapter 3 9
Example 1: Traveling salesman problem
Example 1: Traveling salesman problem
 Given
Given n
n cities with known distances between each pair, find
cities with known distances between each pair, find
the shortest tour that passes through all the cities exactly
the shortest tour that passes through all the cities exactly
once before returning to the starting city.
once before returning to the starting city.
 Alternatively: Find shortest
Alternatively: Find shortest Hamiltonian circuit
Hamiltonian circuit in a
in a
weighted connected graph.
weighted connected graph.
 Example:
Example:
a b
c d
8
2
7
5 3
4
Design and Analysis of Algorithms - Chapter 3 10
Traveling salesman by exhaustive search
Traveling salesman by exhaustive search
 Tour Cost .
Tour Cost .
 a
a→b→c→d→a 2+3+7+5 = 17
→b→c→d→a 2+3+7+5 = 17
 a
a→b→d→c→a 2+4+7+8 = 21
→b→d→c→a 2+4+7+8 = 21
 a
a→c→b→d→a 8+3+4+5 = 20
→c→b→d→a 8+3+4+5 = 20
 a
a→c→d→b→a 8+7+4+2 = 21
→c→d→b→a 8+7+4+2 = 21
 a
a→d→b→c→a 5+4+3+8 = 20
→d→b→c→a 5+4+3+8 = 20
 a
a→d→c→b→a 5+7+3+2 = 17
→d→c→b→a 5+7+3+2 = 17
 Efficiency:
Efficiency:
Design and Analysis of Algorithms - Chapter 3 11
Example 2: Knapsack Problem
Example 2: Knapsack Problem
Given
Given n
n items:
items:
• weights:
weights: w
w1
1 w
w2
2 … w
… wn
n
• values:
values: v
v1
1 v
v2
2 … v
… vn
n
• a knapsack of capacity
a knapsack of capacity W
W
Find the most valuable subset of the items that fit into the knapsack
Find the most valuable subset of the items that fit into the knapsack
Example:
Example:
item weight value Knapsack capacity W=
item weight value Knapsack capacity W=16
16
1
1 2 $20
2 $20
2
2 5 $30
5 $30
3
3 10 $50
10 $50
4
4 5 $10
5 $10
Design and Analysis of Algorithms - Chapter 3 12
Knapsack by exhaustive search
Knapsack by exhaustive search
Subset Total weight Total value
Subset Total weight Total value
{1} 2 $20
{1} 2 $20
{2} 5 $30
{2} 5 $30
{3} 10 $50
{3} 10 $50
{4} 5 $10
{4} 5 $10
{1,2} 7 $50
{1,2} 7 $50
{1,3} 12 $70
{1,3} 12 $70
{1,4} 7 $30
{1,4} 7 $30
{2,3} 15 $80
{2,3} 15 $80
{2,4} 10 $40
{2,4} 10 $40
{3,4} 15 $60
{3,4} 15 $60
{1,2,3} 17 not feasible
{1,2,3} 17 not feasible
{1,2,4} 12 $60
{1,2,4} 12 $60
{1,3,4} 17 not feasible
{1,3,4} 17 not feasible
{2,3,4} 20 not feasible
{2,3,4} 20 not feasible
{1,2,3,4} 22 not feasible
{1,2,3,4} 22 not feasible
Efficiency:
Design and Analysis of Algorithms - Chapter 3 13
Final comments:
Final comments:
 Exhaustive search algorithms run in a realistic amount of
Exhaustive search algorithms run in a realistic amount of
time
time only on very small instances
only on very small instances
 In many cases there are much better alternatives!
In many cases there are much better alternatives!
• Euler circuits
Euler circuits
• shortest paths
shortest paths
• minimum spanning tree
minimum spanning tree
• assignment problem
assignment problem
 In some cases exhaustive search (or variation) is the only
In some cases exhaustive search (or variation) is the only
known solution
known solution

brute force ppt lsdgldslgkdsglsdghsdghsdgsg

  • 1.
    Design and Analysisof Algorithms - Chapter 3 1 Brute Force Brute Force A straightforward approach usually based on problem A straightforward approach usually based on problem statement and definitions statement and definitions Examples: Examples: 1. 1. Computing Computing a an n ( (a a > 0, > 0, n n a nonnegative integer) a nonnegative integer) 2. 2. Computing Computing n n! ! 3. 3. Multiply two Multiply two n n by by n n matrices matrices 4. 4. Selection sort Selection sort 5. 5. Sequential search Sequential search
  • 2.
    Design and Analysisof Algorithms - Chapter 3 2 String matching String matching  pattern pattern: a string of : a string of m m characters to search for characters to search for  text text: a (long) string of : a (long) string of n n characters to search in characters to search in  Brute force algorithm: Brute force algorithm: 1. 1. Align pattern at beginning of text Align pattern at beginning of text 2. 2. moving from left to right, compare each character of pattern to the moving from left to right, compare each character of pattern to the corresponding character in text until corresponding character in text until – all characters are found to match (successful search); or all characters are found to match (successful search); or – a mismatch is detected a mismatch is detected 3. 3. while pattern is not found and the text is not yet exhausted, realign while pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat step 2. pattern one position to the right and repeat step 2.
  • 3.
    Design and Analysisof Algorithms - Chapter 3 3 Brute force string matching – Examples: Brute force string matching – Examples: 1. 1. Pattern: Pattern: 001011 001011 Text: Text: 10010101101001100101111010 10010101101001100101111010 2. 2. Pattern: Pattern: happy happy Text: Text: It is never too late to have a happy childhood. It is never too late to have a happy childhood. Number of comparisons: Number of comparisons: Efficiency: Efficiency:
  • 4.
    Design and Analysisof Algorithms - Chapter 3 4 Brute force polynomial evaluation Brute force polynomial evaluation  Problem: Find the value of polynomial Problem: Find the value of polynomial p p( (x x) = ) = a an nx xn n + + a an n-1 -1x xn n-1 -1 +… + +… + a a1 1x x1 1 + + a a0 0 at a point at a point x x = = x x0 0  Algorithm: Algorithm:  Efficiency: Efficiency: p p := 0.0 := 0.0 for for i i := := n n down to 0 do down to 0 do power := 1 power := 1 for for j j := 1 to := 1 to i i do do power := power power := power * * x x p p := := p p + + a a[ [i i] * power ] * power return return p p
  • 5.
    Design and Analysisof Algorithms - Chapter 3 5 Polynomial evaluation: improvement Polynomial evaluation: improvement  We can do better by evaluating from right to left: We can do better by evaluating from right to left:  Algorithm: Algorithm:  Efficiency: Efficiency: p p := := a a[0] [0] power := 1 power := 1 for for i i := 1 to := 1 to n n do do power := power * power := power * x x p p := := p p + + a a[ [i i] * power ] * power return return p p
  • 6.
    Design and Analysisof Algorithms - Chapter 3 6 More brute force algorithm examples: More brute force algorithm examples:  Closest pair Closest pair • Problem Problem: find closest among : find closest among n n points in points in k k-dimensional space -dimensional space • Algorithm Algorithm: Compute distance between each pair of points : Compute distance between each pair of points • Efficiency Efficiency: :  Convex hull Convex hull • Problem Problem: find smallest convex polygon enclosing : find smallest convex polygon enclosing n n points on the points on the plane plane • Algorithm Algorithm: For each pair of points : For each pair of points p p1 1 and and p p2 2 determine whether all determine whether all other points lie to the same side of the straight line through other points lie to the same side of the straight line through p p1 1 and and p p2 2 • Efficiency Efficiency: :
  • 7.
    Design and Analysisof Algorithms - Chapter 3 7 Brute force strengths and weaknesses Brute force strengths and weaknesses  Strengths: Strengths: • wide applicability wide applicability • simplicity simplicity • yields reasonable algorithms for some important problems yields reasonable algorithms for some important problems – searching searching – string matching string matching – matrix multiplication matrix multiplication • yields standard algorithms for simple computational tasks yields standard algorithms for simple computational tasks – sum/product of sum/product of n n numbers numbers – finding max/min in a list finding max/min in a list  Weaknesses: Weaknesses: • rarely yields efficient algorithms rarely yields efficient algorithms • some brute force algorithms unacceptably slow some brute force algorithms unacceptably slow • not as constructive/creative as some other design techniques not as constructive/creative as some other design techniques
  • 8.
    Design and Analysisof Algorithms - Chapter 3 8 Exhaustive search Exhaustive search  A brute force solution to a problem involving search for an A brute force solution to a problem involving search for an element with a special property, usually among element with a special property, usually among combinatorial objects such a permutations, combinations, combinatorial objects such a permutations, combinations, or subsets of a set. or subsets of a set.  Method: Method: • construct a way of listing all potential solutions to the problem in a construct a way of listing all potential solutions to the problem in a systematic manner systematic manner – all solutions are eventually listed all solutions are eventually listed – no solution is repeated no solution is repeated • Evaluate solutions one by one, perhaps disqualifying infeasible ones Evaluate solutions one by one, perhaps disqualifying infeasible ones and keeping track of the best one found so far and keeping track of the best one found so far • When search ends, announce the winner When search ends, announce the winner
  • 9.
    Design and Analysisof Algorithms - Chapter 3 9 Example 1: Traveling salesman problem Example 1: Traveling salesman problem  Given Given n n cities with known distances between each pair, find cities with known distances between each pair, find the shortest tour that passes through all the cities exactly the shortest tour that passes through all the cities exactly once before returning to the starting city. once before returning to the starting city.  Alternatively: Find shortest Alternatively: Find shortest Hamiltonian circuit Hamiltonian circuit in a in a weighted connected graph. weighted connected graph.  Example: Example: a b c d 8 2 7 5 3 4
  • 10.
    Design and Analysisof Algorithms - Chapter 3 10 Traveling salesman by exhaustive search Traveling salesman by exhaustive search  Tour Cost . Tour Cost .  a a→b→c→d→a 2+3+7+5 = 17 →b→c→d→a 2+3+7+5 = 17  a a→b→d→c→a 2+4+7+8 = 21 →b→d→c→a 2+4+7+8 = 21  a a→c→b→d→a 8+3+4+5 = 20 →c→b→d→a 8+3+4+5 = 20  a a→c→d→b→a 8+7+4+2 = 21 →c→d→b→a 8+7+4+2 = 21  a a→d→b→c→a 5+4+3+8 = 20 →d→b→c→a 5+4+3+8 = 20  a a→d→c→b→a 5+7+3+2 = 17 →d→c→b→a 5+7+3+2 = 17  Efficiency: Efficiency:
  • 11.
    Design and Analysisof Algorithms - Chapter 3 11 Example 2: Knapsack Problem Example 2: Knapsack Problem Given Given n n items: items: • weights: weights: w w1 1 w w2 2 … w … wn n • values: values: v v1 1 v v2 2 … v … vn n • a knapsack of capacity a knapsack of capacity W W Find the most valuable subset of the items that fit into the knapsack Find the most valuable subset of the items that fit into the knapsack Example: Example: item weight value Knapsack capacity W= item weight value Knapsack capacity W=16 16 1 1 2 $20 2 $20 2 2 5 $30 5 $30 3 3 10 $50 10 $50 4 4 5 $10 5 $10
  • 12.
    Design and Analysisof Algorithms - Chapter 3 12 Knapsack by exhaustive search Knapsack by exhaustive search Subset Total weight Total value Subset Total weight Total value {1} 2 $20 {1} 2 $20 {2} 5 $30 {2} 5 $30 {3} 10 $50 {3} 10 $50 {4} 5 $10 {4} 5 $10 {1,2} 7 $50 {1,2} 7 $50 {1,3} 12 $70 {1,3} 12 $70 {1,4} 7 $30 {1,4} 7 $30 {2,3} 15 $80 {2,3} 15 $80 {2,4} 10 $40 {2,4} 10 $40 {3,4} 15 $60 {3,4} 15 $60 {1,2,3} 17 not feasible {1,2,3} 17 not feasible {1,2,4} 12 $60 {1,2,4} 12 $60 {1,3,4} 17 not feasible {1,3,4} 17 not feasible {2,3,4} 20 not feasible {2,3,4} 20 not feasible {1,2,3,4} 22 not feasible {1,2,3,4} 22 not feasible Efficiency:
  • 13.
    Design and Analysisof Algorithms - Chapter 3 13 Final comments: Final comments:  Exhaustive search algorithms run in a realistic amount of Exhaustive search algorithms run in a realistic amount of time time only on very small instances only on very small instances  In many cases there are much better alternatives! In many cases there are much better alternatives! • Euler circuits Euler circuits • shortest paths shortest paths • minimum spanning tree minimum spanning tree • assignment problem assignment problem  In some cases exhaustive search (or variation) is the only In some cases exhaustive search (or variation) is the only known solution known solution