Professional Documents
Culture Documents
Chapter 6
Science is as far removed from brute force as this
sword from a crowbar.
—Edward Lytton (1803–1873), Leila, Book II, Chapter I
• And often, the brute-force strategy is indeed the one that is easiest to
apply.
2. Computing n!
Selection sort
Bubble sort
Insertion sort
Searching,
Closest pair problem,
Element uniqueness,
Frequency distribution,
Selection,
…
a[0] < a[2] < ... < a[n – 2] < a[n – 1], Ascending order
a[0] ≤ a[2] ≤ ... ≤ a[n – 2] ≤ a[n – 1], Non-Decreasing order
a[0] > a[2] > ... > a[n – 2] > a[n – 1], Decreasing order
a[0] ≥ a[2] ≥ ... ≥ a[n – 2] ≥ a[n – 1], Non Ascending order
• Numbers: the sort is called numerical and the order is based on the
value of the numbers.
• Letters: the sort is called alphabetical and the order is based on the
alphabetical order.
• Letters + Numbers + other sings, the sort is called lexicographic,
where each character is given a numeric value (eg, according to
ASCII code)
• Algorithms that rely on comparison sort and have almost linear and
quadratic complexity
Corresponding table
Key Table
• It does not require extra memory, except, possibly for a few memory
units
2, 4, 5, , 7, 8, 6, 9
SelectionSort (n, a) {
1. for i ← 0 to n – 2 do
2. { min ← i; // min, the index of the element with the smallest value in step i
3. for j ← i + 1 to n – 1 do
4. if (a[j] ≤ a[min])
5. then min ← j; // update the index
6. swap (a[i], a[min]); // exchange the value
7. }
8. return
}
Table elements
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
Initial data 89 45 68 90 29 34 17
After 1st step (i = 0) 17 45 68 90 29 34 89
After 2nd step (i = 1) 17 29 68 90 45 34 89
After 3rd step (i = 2) 17 29 34 90 45 68 89
After 4th step (i = 3) 17 29 34 45 90 68 89
After 5th step (i = 4) 17 29 34 45 68 90 89
After 6th step (i = 5) 17 29 34 45 68 89 90
• Its execution time depends very little on the degree of sorting of the initial
data
• The algorithm does not require additional memory (except for some
variables)
• Simple algorithm
• Applies to all data types
• The “worst”
2, 5, 4, , 6, 7, 8, 9
?
• 𝑎0 , ⋯ , 𝑎𝑗 𝑎𝑗+1 , ⋯ , 𝑎𝑛−𝑖−1 ∥ 𝑎𝑛−𝑖 ≤ ⋯ ≤ 𝑎𝑛−1
BubbleSort (n, a) {
1. for i ← 0 to n – 2 do
2. for j ← 0 to n – 2 – i do
3. if (a[j] > a[j + 1])
4. then swap(a[j], a[j + 1]); // exchanges values
5. return
}
?
89 45 ? 68 90 29 34 17
45 89 68 90 29 34 17
? ?
45 68 89 90 29 34 17
?
45 68 89 29 90 34 17
?
45 68 89 29 34 90 17
45 68 89 29 34 17 90
? ? ?
45 68 89 29 34 17 90
?
45 68 29 89 34 ? 17 90
45 68 29 34 89 17 90
45 68 29 34 17 89 90
etj
• Number of comparisons:
1, 4, 5, , 2, 8, 6, 9
smaller or equal than the A[i] value greater than the A[i] value
InsertionSort(n, a){
for i ← 1 to n – 1 do {
v ← a[i]; // give the next value
j ← i – 1;
while (j ≥ 0 and a[j] > v) do { // Find the first smaller element
a[j + 1] ← a[j];
j ← j – 1;
}
a[j + 1] ← v
}
return
}
Table elements
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
Initial data 89 45 68 90 29 34 17
After 1st step 45 89 68 90 29 34 17
After 2nd step 45 68 89 90 29 34 17
After 3rd step 45 68 89 90 29 34 17
After 4th step 29 45 68 89 90 34 17
After 5th step 29 34 45 68 89 90 17
After 6th step 17 29 34 45 68 89 90
• Number of comparisons:
• The Data :
• The Result :
We denote with :
The algorithm seek to find the i index, which is the leftmost index of the character of
the first match pattern in the text, such that ti = p1, …, ti + j-1 = pj, …, ti + m - 1 = pm :
StringMatchingBF (n, t, m, p) {
for i ← 1 to n – m + 1 do {
j ← 1;
while (j ≤ m ) and t[i + j – 1] = p[j]) do
j ← j + 1;
if j > m
then return i;
}
return –1
}
Position
Text
Pattern
shifts
• Worst case when the pattern is not found or is found in the last
search by n - m + 1 attempt for effectiveness in Θ(nm) class.
• In the average case the effectiveness has been linear, that is Θ(n + m)
• We will only consider the plan case, as stated in the problem in 2-D space .
• The problem can be generalized to higher spaces.
• Find the distance between the two nearest points in a set of points
in the plan.
• Points are specified by their Cartesian coordinates (x, y).
• The distance between two points Pi (xi, yi) and Pj (xj, yj) is the
Euclidean distance calculated according to the formula:
• The Data :
The amount of points n, and the abscissas x(1:n) and the ordinates
y(1:n)
• Result:
d, the distance of the nearest pair of points
closestPair(n, x , y) {
1. dmin ← + ∞ ; // The greater value in computer
2. for i ← 1 to n - 1 do
3. for j ← i + 1 to n do
4. d ←min(d, sqr((x[i] – x[j])2 + (y[i] – y[j])2));
5. return d
• Through the exhaustive search are explore all the solution and
between them the optimal is selected.
• Problem Statement
Given n cities with known distances between each pair, find the shortest
tour (path ) that passes through all the cities exactly once before returning
to the starting city
a→d→b→c→a 5+4+3+8 = 20
a→d→c→b→a 5+7+3+2 = 17
Efficiency: O(n!)
Brute Force design technique 53
Time Efficiency
• Methods like backtracking and branch-and-bound allow some but not all
cases of these problems to be solved in less time than exponential.
• Also, for their solution, algorithms that provide approximate solutions can
be used as alternatives.
• This means that each person is assigned one and only one task and each
task is performed by one and only one person.
• Meanwhile, it is known the cost C[i, j] when person i is assigned task j for
each couple
(i, j) = 1, 2,…, n.
• The problem lies in finding such a distribution of persons and tasks so that
the total cost is minimal.
9 2 7 8 • Efficiency? O(n!)
6 4 3 7
5 8 1 8
7 6 9 4
• This is a good news: the fact that the problem area is growing
exponentially (or faster) does not necessarily mean that there
will be no effective algorithms to solve it.