• date post

04-Jun-2018
• Category

## Documents

• view

222

0

Embed Size (px)

### 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 | sonia@cs.uct.ac.za)

• 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

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

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 ncitie