Bruteforce ExhaustiveSearch
Bruteforce ExhaustiveSearch
Bruteforce ExhaustiveSearch
1
First, unlike some of the other strategies, brute force is applicable
to a very wide variety of problems.
2
Selection Sort
We start selection sort by scanning the entire given list to find its
smallest element and exchange it with the first element, putting the
smallest element in its final position in the sorted list.
Then we scan the list, starting with the second element, to find the
smallest among the last n−1 elements and exchange it with the second
element, putting the second smallest element in its final position.
3
As an example, the action of the algorithm on the list 89, 45, 68, 90, 29,
34, 17 is illustrated in Figure 3.1.
4
The number of times it is executed depends only on the array size
and is given by the following sum:
Or,
5
Bubble Sort
Another 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 list. The next pass bubbles up the
second largest element, and so on, until after n − 1 passes the list is
sorted. Pass i (0 ≤ i ≤ n − 2) of bubble sort can be represented by the
following diagram:
6
The action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is
illustrated as an example in Figure below.
7
The number of key comparisons for the bubble-sort version given
above is the same for all arrays of size n; it is obtained by a sum that
is almost identical to the sum for selection sort:
8
Ex.
What is the time efficiency of the brute-force algorithm for
computing an as a function of n? As a function of the number of bits
in the binary representation of n?
Ans.
M(n) = n ≈ 2b where M(n) is the number of multiplications made by
the brute-force algorithm in computing an and b is the number of
bits in the n’s binary representation. Hence, the efficiency is linear
as a function of n and exponential as a function of b.
9
Sequential Search and Brute-Force String
Matching
Sequential Search
To repeat, the algorithm simply compares successive elements of a
given list with a given search key until either a match is
encountered (successful search) or the list is exhausted without
finding a match (unsuccessful search).
10
Another straightforward improvement can be incorporated in
sequential search if a given list is known to be sorted: searching in
such a list can be stopped as soon as an element greater than or
equal to the search key is encountered.
11
Brute-Force String Matching
Given 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. For the pattern 00001, the algorithm will make four successful
and one unsuccessful comparison on each of its trials and then shift
the pattern one position to the right:
0 0 0 0 0 0 ... ... ... 0 0 0 0 0
00001
00001
etc.
00001
The total number of character comparisons will be C = 5· 996 =
4980.
15
b. For the pattern 10000, the algorithm will make one unsuccessful
comparison
on each of its trials and then shift the pattern one position to the
right:
0 0 0 0 0 0 ... ... ... 0 0 0 0 0
10000
10000
etc.
10000
The total number of character comparisons will be C = 1· 996 =
996.
16
c. For the pattern 01010, the algorithm will make one successful and
one unsuccessful comparison on each of its trials and then shift the
pattern
one position to the right:
0 0 0 0 0 0... ... ... 0 0 0 0 0
01010
01010
etc.
01010
The total number of character comparisons will be,
C = 2· 996 = 1, 992.
17
Closest-Pair and Convex-Hull
Problems by Brute Force
18
Closest-Pair Problem
The closest-pair problem calls for finding the two closest points in
a set of n points.
d p , p x x y y
2 2
i j i j i j
20
Computing square roots in the loop can be avoided! (Can you think
how?) The trick is to realize that we can simply ignore the square-
root function and compare the values: (xi−xj)2+(yi− yj)2 themselves.
21
Then the basic operation of the algorithm will be squaring a number.
The number of times it will be executed can be computed as follows:
22
Convex-Hull Problem
A convex polygon is a simple polygon (not self-intersecting) in which
no line segment between two points on the boundary ever goes outside
the polygon.
Take a rubber band and stretch it to include all the nails, then let it
snap into place. The convex hull is the area bounded by the snapped
rubber band
Applications: Animation
and image processing
A set of points Q = {p0, p1,... ... ...,p12} with its convex hull CH(Q).
25
Graham’s scan
Graham’s scan solves the convex-hull problem by maintaining a stack
S of candidate points. It pushes each point of the input set Q onto the
stack one time, and it eventually pops from the stack each point that is
not a vertex of CH(Q).
26
From analytical geometry: if q1(x1, y1), q2(x2, y2), and q3(x3, y3) are
three arbitrary points in the Cartesian plane, then the area of the
triangle Δq1q2q3 is equal to one-half of the magnitude of the
determinant
P1
P0
P3
P2
P1
P0
P1
P0
29
P4
P3
P1
P0
Left turn, P3 will remain in the stack
P5
P4
P3
P1
P0
Right turn, P4 will be popped out 30
P5
P3
P1
P0
31
P6
P5
P3
P1
P0
P7
Left turn, P5 will remain in the stack
P6
P5
P3
P1
P0 32
Left turn, P6 will remain in the stack
P8
P7
P6
P5
P3
P1
P0
33
P9
P8
P7
P6
P5
P3
P1
P0
34
P9
P7
P6
P5
P3
P1
P0
35
P9
P6
P5
P3
P1
P0
36
P10
P9
P6
P5
P3
P1
P0
37
P10
P6
P5
P3
P1
P0
38
P10
P5
P3
P1
P0
39
P10
P3
P1
P0
40
P11
P10
P3
P1
P0
41
P12
P11
P10
P3
P1
P0
42
P12
P10
P3
P1
P0
43
P12
P10
P3
P1
P0
44
Exhaustive Search
Exhaustive search is simply a brute-force approach to combinatorial
problems. It suggests generating each and every element of the
problem domain, selecting those of them that satisfy all the
constraints, and then finding a desired element.
45
Traveling Salesman Problem
In layman’s terms, the problem asks to find the shortest tour
through a given set of n cities that visits each city exactly once
before returning to the city where it started.
46
It is easy to see that a Hamiltonian circuit can also be defined as a sequence of n + 1
adjacent vertices vi0, vi1, . . . , vin−1, vi0, where the first vertex of the sequence is the same
as the last one and all the other n − 1 vertices are distinct.
Thus, we can get all the tours by generating all the permutations of
n−1intermediatecities,compute the tour lengths, and find the shortest among them.
Figure below presents a small instance of the problem and its solution by this method.
47
An inspection of Figure reveals three pairs of tours that differ only by
their direction. Here results are 18, 11 and 23 and we got each of the
result twice as shown in fig. below. For example path length of a →
b → c → d → a and a → d → c → b → a are both 18 since path is
same but only direction is different.
48
Ex.
Let x1 <x2 < ... < xn be real numbers representing coordinates of n
villages located along a straight road. A post office needs to be
built in one of these villages.
a. Design an efficient algorithm to find the post-office location
minimizing the average distance between the villages and the post
office.
b. Design an efficient algorithm to find the post-office location
minimizing the maximum distance from a village to the post
office.
49
Hence, we could cut the number of vertex permutations by half. We
could, for example, choose any two intermediate vertices, say, b and c,
and then consider only permutations in which b precedes c.
50
Knapsack Problem
Given n items of known weights w1, w2, . . . , wn and values v1, v2, . . . ,
vn and a knapsack of capacity W, find the most valuable subset of the
items that fit into the knapsack.
The cost that would accrue if the ith person is assigned to the jth job is
a known quantity C[i, j ] for each pair i, j = 1, 2, . . . , n. The problem is
to find an assignment with the minimum total cost.
53
First few iterations of solving a small instance of the assignment
problem by exhaustive search
54
Since the number of permutations to be considered for the general
case of the assignment problem is n!, exhaustive search is
impractical for all but very small instances of the problem.
55