Chapter 3 Brute Force
Chapter 3 Brute Force
Brute Force
Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-2
Brute-Force Strengths and Weaknesses
Strengths
• wide applicability
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-3
Brute Force algorithms
Bubble sort
Selection sort
Sequential or Linear Search
Brute-Force String Matching
Closest Pair problems
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-4
Selection Sort algorithm
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-6
Brute-Force Sorting Algorithm
Selection Sort Scan the array to find its smallest element and
swap it with the first element. Then, starting with the second
element, scan the elements to the right of it to find the
smallest among them and swap it with the second elements.
Generally, on pass i (0 i n-2), find the smallest element in
A[i..n-1] and swap it with A[i]:
Example: 7 3 2 5
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-7
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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-8
Bubble Sort algorithm
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-9
Analysis of Bubble sort
No. of Comparisons
No. of swaps
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-10
Bubble sort Vs Selection sort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-11
Improve efficiency of Bubble sort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-14
Analysis of sequential search
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-15
Enhanced Sequential search
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-16
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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-17
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.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-18
Pseudocode and Efficiency
Brute-force algorithm
p 0.0
for i n downto 0 do
power_of_x 1
for j 1 to i do //compute xi
power_of_x power _of_x x
p p + a[i] power_of_x
return p
0in i = Θ(n^2) multiplications
Efficiency:
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-20
Polynomial Evaluation: Improvement
We can do better by evaluating from right to left:
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-21
Closest-Pair Problem
The closest-pair problem calls for finding the two closest points in a set of n
points.
Points in question can represent such physical objects as airplanes or post
offices as well as database records, statistical samples, DNA sequences, and
so on.
An air-traffic controller might be interested in two closest planes as the
most probable collision candidates.
A regional postal service manager might need a solution to the closest pair
problem to find candidate post-office locations to be closed.
One of the important applications of the closest-pair problem is cluster
analysis in statistics.
A bottom-up algorithm begins with each element as a separate
cluster and merges them into successively larger clusters by combining the
closest pair of clusters.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-22
Closest-Pair Problem
standard Euclidean distance
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-23
Analysis
basic operation of the algorithm will be squaring a number
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-24
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
Efficiency: Θ((n-1)!)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-27
Example 2: Knapsack Problem
Given n items:
• weights: w1 w2 … wn
• values: v1 v2 … v n
• a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-31
Final Comments on Exhaustive Search
Exhaustive-search algorithms run in a realistic amount of
time only on very small instances
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 3 3-32