21
Brute Force

Brute Force - Faculty of Science and Technology

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Brute Force - Faculty of Science and Technology

Brute Force

Page 2: Brute Force - Faculty of Science and Technology

2

Brute Force - DefinitionBrute Force is a straightforward approach to solve a problem. Usually directly based on the problem’s statement and definitions of the concepts involved.The “force” implied by the strategy’s not that of one’s intellect.“Just do it” would be another way to describe the prescription of the brute-force approach.

Page 3: Brute Force - Faculty of Science and Technology

3

Brute ForceConsider the exponentiation problem: compute an for given number a and a nonnegative integer n.Brute-force method would simply computing an by multiplying 1 by a n times.

Unlike other strategies, brute force is applicable to a very wide variety of problems. It is used for many elementary but important algorithmic tasks

Computing sum of n numbersFinding the largest element in a listEtc

Page 4: Brute Force - Faculty of Science and Technology

4

Brute ForceIn some problems (Sorting, Searching, Matrix multiplication, String matching), the brute force approach yields reasonable algorithms of at least some practical value.The expense of designing a more efficient algorithm may be unjustifiable while a brute force algorithm can solve those instances with acceptable speed.Consider a calculation of 1+2+3…100

A brute force strategy would be adding 1 to 2 to 3 …to 100A non brute force strategy would be using n(n+1)/2 where n = 100

Page 5: Brute Force - Faculty of Science and Technology

5

Brute Force - SortingConsider the application of the brute force approach to the problem of sorting: given a list of n orderable items.What would be the most straightforward method for solving sorting problem?Selection sort and Bubble sort would be the answer.

Page 6: Brute Force - Faculty of Science and Technology

6

Brute Force - Selection SortSelection Sort

Selection sort Find its smallest element by scanning the listExchange it with the first element making the smallest element in its final position in the sorted list.Scan the list, starting with the second element to find the smallest among the last n-1.The second element will be put in its final position.

Generally, on the ith pass through the list, the algorithm searches for the smallest item among the last n-ielements and swaps it with Ai

Page 7: Brute Force - Faculty of Science and Technology

7

Brute Force - Selection Sort

The basic operation is A[j]<A[min].The number of times it is executed T(n) depends only on the array’s size and is given by the following sum:

Algorithm SelectionSort (A[0…n-1])for i ← 0 to n-2 do

min ← ifor j ← i+1 to n-1 do

if A[j]<A[min] min ← j

swap A[i] and A[min]

2)1()1()(

)1(]1)1()1[(1)(

2

0

2

0

2

0

2

0

1

1

nninnT

ininnT

n

i

n

i

n

i

n

i

n

ij

−=−−=

−−=++−−==

∑ ∑∑∑−

=

=

=

=

+=

Page 8: Brute Force - Faculty of Science and Technology

8

Brute Force - Selection Sort| 89 , 45 , 68 , 90 , 29 , 34 , 17

17 | 45 , 68 , 90 , 29 , 34 , 8917 , 29 | 68 , 90 , 45 , 34 , 89 17 , 29 , 34 | 90 , 45 , 68 , 89 17 , 29 , 34 , 45 | 90 , 68 , 8917 , 29 , 34 , 45 , 68 | 90 , 8917 , 29 , 34 , 45 , 68 , 89 | 90

Selection sort is Θ(n2) on all inputs. Note, however, that this number of key swap is only Θ(n).

Page 9: Brute Force - Faculty of Science and Technology

9

Brute Force - Bubble SortAnother brute force application to the sorting problem is to

compare adjacent elements of the list. and exchange them if they are out of order.

By doing it repeatedly, we end up “Bubbling up” the largest element to the last position on the listThe next pass bubbles up the second largest element, and so on until, after n-1 passes, the list is sorted.

Page 10: Brute Force - Faculty of Science and Technology

10

Brute Force - Bubble Sort

The number of times it is executed T(n) depends only on the array’s size and is given by the following sum:

Algorithm BubbleSort (A[0…n-1])for i ← 0 to n-2 do

for j ← 0 to n-2-i doif A[j]<A[j+1] swap A[j] and A[j+1]

)(2

)1()1()(

)1(]10)2[(1)(

22

0

2

0

2

0

2

0

2

0

nnninnT

ininnT

n

i

n

i

n

i

n

i

in

j

Θ∈−

=−−=

−−=+−−−==

∑ ∑∑ ∑−

=

=

=

=

−−

=

Page 11: Brute Force - Faculty of Science and Technology

11

Brute Force - Bubble SortThe number of key swaps, depends on the input. The worst case of number of key swaps is the same as number of key comparisons:

)(2

)1()()( 2nnnnTnSwapworst Θ∈−

==

It is often the case an application of the brute force strategy, that the first version algorithm can often be improved with a modest amount of effort. In the current bubble sort,

Can we improve anything ?What is the time efficiency compare to the previous version ?

Page 12: Brute Force - Faculty of Science and Technology

12

Brute Force - Bubble Sort89 45 68 90 29 34 1745 89 68 90 29 34 1745 68 89 90 29 34 1745 68 89 29 90 34 1745 68 89 29 34 90 1745 68 89 29 34 17 |90

45 68 89 29 34 17 |9045 68 29 89 34 17 |9045 68 29 34 89 17 |9045 68 29 34 17 |89 90

Etc..

Page 13: Brute Force - Faculty of Science and Technology

13

Brute Force – Sequential Search

The algorithm simply compares successive elements of a given list with a given search key until either a match is encountered (successful) or the program is stopped without finding a match (unsuccessful).

Algorithm SequentialSearch(A[0…n-1],K) //From chapter 2i ← 0 while i< n and A[i]≠ K do

i ← i + 1if i<n return ielse return -1

Page 14: Brute Force - Faculty of Science and Technology

14

Brute Force – Sequential SearchAlgorithm SequentialSearch2(A[0…n],K)

A[n] ← Ki ← 0while A[i]≠ K do

i ← i + 1if i<n return ielse return -1

The SequentialSearch2() append the search key to the end of the list, therefore, the search for the key will have to be successful.What is time efficiency compare to the previous version ?If we assume that the list is sorted, can we improve the algorithm ?

Page 15: Brute Force - Faculty of Science and Technology

15

Brute Force – String MatchingGiven a string of n characters called the text and a string of m characters (m<=n) called the pattern, find a substring of the text that matches the pattern.A brute force algorithm for string matching:

Align the pattern against the first m characters of the text.Start matching the corresponding pair of characters from left to right until either all the m pairs are match.Or if the missing pair is found, the pattern is shifted one position to the right and character comparisons are resumed, starting again from the 1st character.

Page 16: Brute Force - Faculty of Science and Technology

16

Brute Force – String Matching

Note: The last position in the text can still be a beginning of a matching substring is n-m.However beyond n-m position, there are not enough characters to match the entire pattern; the algorithm need not make any comparison.

Algorithm BruteForceStringMatch (T[0…n-1],P[0…m-1])for i ← 0 to n-m

j ← 0while j<m and P[j] = T[i+j] do

j ← j + 1if i=m return i

return -1

Page 17: Brute Force - Faculty of Science and Technology

17

Brute Force – String MatchingWorst case: The algorithm may have to make all m comparisons before shifting the pattern, and this can happen for each of the n-m+1 tries. Therefore the algorithm is Θ(nm).Give the specific example situation!

However, in a typical word search, most shifts would happen after very few comparison. Therefore the average case should be considerably better than the worst case. It has show to be linear Θ(n+m) = Θ(n).

N O T B O D Y _ N O T I C E D _ H I M TextN O T Pattern

Page 18: Brute Force - Faculty of Science and Technology

18

Brute Force – Convex HullConvex Definition: A set of points (finite or infinite) on the plane is called convex if for any two points P and Q in the set, the entire line segment with the end points at P and Q belongs to set Convex Hull Definition: The convex hull of a set S of points is the smallest convex set containing S (“smallest” means that the convex hull of S must be a subset of any convex set containing S)

Convex Concave

Page 19: Brute Force - Faculty of Science and Technology

19

Brute Force – Convex HullConvex Hull problem is the problem of constructing the convex hull for the given set S of n points.Therefore, we need to find the points that will serve as the “extreme points”. Extreme points of a convex set is a point of this set that is not a middle point of any line segment with end points in the set.

Convex Hull: Elastic band analogy http://en.wikipedia.org/wiki/Convex_Hull

Page 20: Brute Force - Faculty of Science and Technology

20

Brute Force – Convex HullBrute Force manner: A line segment connecting two points Pi and Pi of a set of n points is a part of its convex hull’s boundary, if and only if all the other points of the set lie on the same side of the straight line.Since there are O(n2) edges, and each check requires going through the O(n) points, this algorithm is O(n3).

Page 21: Brute Force - Faculty of Science and Technology

21

Brute Force – Exhaustive SearchA brute force approach to combinatorial problems. It suggests generating each and every element of the problem’s domain, selecting those of them that satisfy the problem’s constraints, and then finding a desired element.Example: Traveling salesman problem

a

b

c

d

2

3

1

785