Ch03 Chen
Ch03 Chen
Brute Force
2. Computing n!
Example: 89 45 68 90 29 34 17
17 | 45 68 90 29 34 89
29 | 68 90 45 34 89
34 | 90 45 68 89
45 | 90 68 89
68 | 90 89
89 | 90
90
Selection Sort
Algorithm
N-2
for i 0 to N-2 do ∑
min i; i=0 N-1
for j i+1 to N-1 do ∑ c
if (A[j] < A[min]) min j; j=i+1
}
swap A[i] and A[min] ;
}
Analysis:
N-2 N-1 N-2 2
T(N) = ∑ ∑ c = ∑ c (N-1-i) = c (N-1)N / 2 in O(N )
i=0 j=i+1 i=0
Bubble Sort
Bubble Sort
1. In each pass, we compare adjacent elements and swap them if they are out of
order until the end of the list. By doing so, the 1 st pass ends up “bubbling up”
the largest element to the last position on the list
2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1 passes, the list
is sorted.
Example:
Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 17
45 89 | 68 90 29 34 17 45 68 | 89 29 34 17
68 89 | 90 29 34 17 68 89 | 29 34 17
89 90 | 29 34 17 29 89 | 34 17
29 90 | 34 17 34 89 | 17
34 90 | 17 17 89
17 90
45 68 89 29 34 17 90 45 68 29 34 17 89
largest 2nd largest
Bubble Sort
Algorithm
N-2
for i 0 to N-2 do ∑
i=0 N-2-i
for j 0 to N-2-i do ∑ c
j=i+1
if (A[j+1] < A[j]) swap A[j] and A[j+1] ;
}
}
Analysis: N-2 N-2-i N-2
T(N) = ∑ ∑ c = ∑ c (N-1-i) = c (N-1)N / 2 in O(N2 )
i=0 j=0 i=0
Sec 3.2 Sequential Search and String Matching
Sequential search
Compare successive elements of a given list with a search key until
1. either a match is encountered
2. or the list is exhausted without a match.
0 1 N-1 N
Algorithm:
extra position
SequentialSearch(A[0..N], key){ to store the key
A[N] key;
i 0;
while (i<N and A[i] != k ) do { i = i + 1};
if i < n return i; Best case: O(1)
else return –1; Analysis: Worst case: O(N)
} Avg. case: O(N)
Brute-Force String Matching
pattern: a string of m characters to search for
text: a (longer) string of n characters to search in
problem: find a substring in the text that matches the pattern
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
all characters are found to match (successful search); or
a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Examples of Brute-Force String Matching
1. Pattern: 001011
Text: 10010101101001100101111010
2. Pattern: happy
Text: It is never too late to have a
happy childhood.
Brute-force String Matching
Pattern: p[0] p[1] … p[m-1] m characters (m <= n)
Text: t[0] t[1] … … … t [n-1] n characters
Brute-force algorithm:
for k = 0 to n-m do
j 0;
while (j < m and p[j] = t[k+j]) do j j+1;
if j = m return k; // matched
}
return –1; // ( no match) Complexity (assuming m << n)
Worst case: m (n – m + 1) in Θ (nm)
Avg. case: Θ (n + m) = Θ (n) (for random text)
Brute-Force Polynomial Evaluation
Problem: Find the value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0
Brute-force algorithm
p 0.0;
for i n downto 0 do {
power 1;
for j 1 to i do //compute xi
power power x;
p p + a[i] power;
}
return p;
Efficiency:
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:
Better brute-force algorithm
p a[0]
power 1
for i 1 to n do {
power power x
p p + a[i] power
}
return p
Efficiency:
Closest-Pair Problem
Find the two closest points in a set of n points
(in the two-dimensional Cartesian plane).
Brute-force algorithm
Compute the distance between every pair of distinct
points
and return the indexes of the points for which the distance
is the smallest.
Closest-Pair Brute-Force Algorithm (cont.)
Efficiency:
Strengths
wide applicability
simplicity
yields reasonable algorithms for some important problems
(e.g., matrix multiplication, sorting, searching, string
matching)
Weaknesses
rarely yields efficient algorithms
some brute-force algorithms are unacceptably slow
not as constructive as some other design techniques
Exhaustive Search
A brute force solution to a problem involving search for an element
with a special property, usually among combinatorial objects such as
permutations, combinations, or subsets of a set.
Method:
generate a list of all potential solutions to the problem in a
systematic manner (see algorithms in Sec. 5.4)
c 7 d
TSP 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
More tours?
Less tours?
Efficiency:
Example 2: Knapsack Problem
Given n items:
weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack
< 2, 1, 3, 4 > 2 + 6 + 1 + 4 = 13
< 2, 1, 4, 3 > 2 + 6 + 8 + 9 = 25
…
etc. j1 j2 j3 … jN
Total # of permutations = N !
p1 p2 p3 … pN
Final Comments on Exhaustive Search
Exhaustive-search algorithms run in a realistic amount of
time only on very small instances
In some cases, there are much better alternatives!
Euler circuits
shortest paths
minimum spanning tree
assignment problem