Algorithms - Chapter 3 Bruteforce
Transcript of Algorithms - Chapter 3 Bruteforce
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
1/31
Theory of Algorithms:
Brute Force
Michelle Kuttel and Sonia
Berman
(mkuttel | [email protected])
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
2/31
Objectives
To introduce the brute-force mind set
To show a variety of brute-force solutions:
Brute-Force String Matching
Polynomial Evaluation
Closest-Pair
Convex-Hull
Exhaustive SearchTo discuss the strengths and weaknesses of abrute-force strategy
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
3/31
Brute Force
A straightforward approach usuallydirectly based on problem statementand definitions
Motto: Just do it!Crude but often effective
Examples already encountered:
Computing an (a > 0, na nonnegative
integer) by multiplying atogether ntimes
Computation of n! using recursion
Multiplying two nby n matrices
Selection sort
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
4/31
Brute-Force Sorting
What is the most straight-forward
approach to sorting?
selection sort?
(n-1)n/2 O(n2) comparisons
however, number of key swaps is n-1 O(n)
Bubble sort?
(n-1)n/2 - O(n2) comparisons number of key swaps depends on input
Worst case?
4
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
5/31
Brute Force String
MatchingProblem:
Find a substring in some text that matches a pattern
Pattern: a string of mcharacters to search for
Text: a (long) string of ncharacters to search in
1. Align pattern at beginning of text
2. Moving left to right, compare each character of patternto the corresponding character in text UNTIL
All characters are found to match (successful search);
or A mismatch is detected
3. WHILE pattern is not found and the text is not yetexhausted, realign pattern one position to the right andrepeat step 2.
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
6/31
Brute-Force String
Matching
Example:Pattern: AKA
Text: ABRAKADABRA
Trace: AKA AKA
AKA
AKA
Number of Comparisons:In the worst case, mcomparisons beforeshifting, for each of n-m+1tries
Efficiency: !(nm)
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
7/31
Brute Force Polynomial
EvaluationProblem:
Find the value of polynomialp(x) = anx
n+ an-1xn-1 + +a1x
1 + a0
at a pointx=x0
Algorithm:
Efficiency? Improvements?
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
8/31
Brute Force Polynomial
EvaluationProblem:
Find the value of polynomialp(x) = anx
n+ an-1xn-1 + +a1x
1 + a0
at a pointx=x0
Algorithm:
Efficiency? Improvements?
p"0.0
fori"n down to0 do
power"1
for j"1 toi do power"power * x
p"p + a[i] * power
returnp
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
9/31
Brute Force Closest Pair
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
10/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
11/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
12/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
13/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
14/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
15/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
16/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
17/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
Efficiency: !(n2)
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
18/31
Brute Force Closest Pair
Problem:Find the two points that are closest together in a set of n2-D points P1= (x1, y1), , Pn= (xn, yn)
Using Cartesian coordinates and Euclidean distance
Algorithm:
Efficiency: !(n2)
dmin""
fori"1 ton-1 do forj"i+1 ton do d"sqrt((xi - xj)
2+ (yi- yj)2)
ifd < dmin dmin"d; index1"i; index2"j
returnindex1, index2
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
19/31
The Convex Hull Problem
Problem:
Find the convex hull enclosing n2-D points
Convex Hull: If S is a set of points then the Convex Hull of Sis the smallest convex set containing S
Convex Set: A set of points in the plane is convex if for anytwo points P and Q, the line segment joining P and Qbelongs to the set
ConvexNon-
Convex
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
20/31
The Convex Hull Problem
Problem:
Find the convex hull enclosing n2-D points
Convex Hull: If S is a set of points then the Convex Hull of Sis the smallest convex set containing S
Convex Set: A set of points in the plane is convex if for anytwo points P and Q, the line segment joining P and Qbelongs to the set
ConvexNon-
Convex
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
21/31
The Convex Hull Problem
Problem:
Find the convex hull enclosing n2-D points
Convex Hull: If S is a set of points then the Convex Hull of Sis the smallest convex set containing S
Convex Set: A set of points in the plane is convex if for anytwo points P and Q, the line segment joining P and Qbelongs to the set
ConvexNon-
Convex
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
22/31
The Convex Hull Problem
Problem:
Find the convex hull enclosing n2-D points
Convex Hull: If S is a set of points then the Convex Hull of Sis the smallest convex set containing S
Convex Set: A set of points in the plane is convex if for anytwo points P and Q, the line segment joining P and Qbelongs to the set
ConvexNon-
Convex
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
23/31
The Convex Hull Problem
Problem:
Find the convex hull enclosing n2-D points
Convex Hull: If S is a set of points then the Convex Hull of Sis the smallest convex set containing S
Convex Set: A set of points in the plane is convex if for anytwo points P and Q, the line segment joining P and Qbelongs to the set
ConvexNon-
Convex
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
24/31
Brute Force Convex Hull
Algorithm:For each pair of points p1and p2
Determine whether all other points lie to the
same side of the straight line through p1and p2Efficiency:
for n(n-1)/2point pairs, check sidedness of (n-2)others
O(n3
)
P1
P2
P3
P4
P5
P6
P7
P8
P9
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
25/31
Pros and Cons of Brute
Force! Strengths:
Wide applicability
Simplicity
Yields reasonable algorithms for some important problems
and standard algorithms for simple computational tasks
A good yardstick for better algorithms
Sometimes doing better is not worth the bother
" Weaknesses:
Rarely produces efficient algorithms
Some brute force algorithms are infeasibly slow
Not as creative as some other design techniques
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
26/31
Exhaustive Search
Definition:A brute force solution to the search for an element witha special property
Usually among combinatorial objects such a
permutations or subsetssuggests generating each and every element of theproblems domain
Method:1. Construct a way of listing all potential solutions to the
problem in a systematic manner All solutions are eventually listed
No solution is repeated
2. Evaluate solutions one by one (disqualifying infeasibleones) keeping track of the best one found so far
3. When search ends, announce the winner
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
27/31
Travelling Salesman
Problem
Problem:Given ncities with known distances between each pair
Find the shortest tour that passes through all the citiesexactly once before returning to the starting city
Alternatively:Find shortest Hamiltonian Circuit in a weighted connectedgraph
Example: a b
c d
8
2
7
53
4
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
28/31
Travelling Salesman by
Exhaustive Search Tour Cost .
a#b#c#d#a 2+3+7+5 = 17
a#b#d#c#a 2+4+7+8 = 21
a#c#b#d#a 8+3+4+5 = 20
a#c#d#b#a 8+7+4+2 = 21
a#d#b#c#a 5+4+3+8 = 20
a#d#c#b#a 5+7+3+2 = 17
Improvements:Start and end at one particular city
Remove tours that differ only in direction
Efficiency:
(n-1)!/2 = O(n!)
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
29/31
Knapsack Problem
Problem: Given nitemsweights: w1, w2 wn
values: v1, v2 vn
a knapsack of capacity WFind the most valuable subset of the items that fitinto the knapsack
Example (W = 16): Item Weight Value
1 2kg R2002 5kg R300
3 10kg R500
4 5kg R100
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
30/31
Knapsack by Exhaustive
Search
Efficiency:!
(2n
)
Subset TotalWgt
TotalValue
{1} 2 kg R200
{2} 5 kg R300
{3} 10 kg R500
{4} 5 kg R100
{1,2} 7 kg R500
{1,3} 12 kg R700
{1,4} 7 kg R300
{2,3} 15 kg R800
Subset TotalWgt
TotalValue
{2,4} 10 kg R400
{3,4} 15 kg R600
{1,2,3} 17 kg n/a
{1,2,4} 12 kg R600
{1,3,4} 17 kg n/a
{2,3,4} 20 kg n/a
{1,2,3,4} 22 kg n/a
-
8/14/2019 Algorithms - Chapter 3 Bruteforce
31/31
Final Comments on
Exhaustive Search
Exhaustive searchalgorithms run in a realisticamount of time only onvery small instances
In many cases there aremuch better alternatives!
In some cases exhaustive
search (or variation) is theonly known solution
and parallel solutions canspeed it up!