Lec05-Brute Force PDF
Lec05-Brute Force PDF
Winter 2019
Andrew P. Black
just do it!
!2
Why study them?
✦ Simple to implement
✦ Widely applicable
!3
Sequential Search
!4
Sequential Search
!4
Sequential Search
!4
Sequential Search
!5
Timing Sequential Search
testSequentialSearch
| A B N M res t1 t2 t3|
N ← 100000.
M ← 5000000. " bigger than the array to be searched, and any value in it"
A ← self randomArrayOfSize: N.
t1 ← Time millisecondsToRun: [1000 timesRepeat: [res ← A searchFor: M ]].
self deny: res.
B ← A copyWith: M.
t2 ← Time millisecondsToRun: [1000 timesRepeat: [res ← B searchUsingSentinel: M ]].
self deny: res.
t3 ← Time millisecondsToRun: [1000 timesRepeat: [res ← A searchUsingAt: M ]].
self deny: res.
Transcript show: 'Sequential search, size: '; show: N; cr;
show: ' sequential, for each: '; show: t1; show: 'µs'; cr;
show: ' with sentinel: '; show: t2; show: 'µs'; cr;
show: ' without sentinel, at: '; show: t3; show: 'µs'; cr; cr.
!6
Timing Results
Sequential search, size: 100000
sequential, for each: 1430µs
with sentinel: 850µs
without sentinel, at: 1287µs
!7
Timing Results
Sequential search, size: 100000
sequential, for each: 1430µs
with sentinel: 850µs
without sentinel, at: 1287µs
!7
Timing Results
Sequential search, size: 100000
sequential, for each: 1430µs
with sentinel: 850µs
without sentinel, at: 1287µs
!7
Selection Sort
!8
!9
integer, how would you circumvent the problem of a very large magnitude
of an ?
Ex 3.1, Problem 4
3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
4. a. Design a brute-force algorithm for computing the value of a polynomial
Ex 3.1, Problem 4
3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
4. a. Design a brute-force algorithm for computing the value of a polynomial
Assume
b. thatalgorithm
If the exponentiation is not built-in.
you designed is in Θ(n2 ), design a linear algorithm
for this problem.
Ex 3.1, Problem 4
3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
4. a. Design a brute-force algorithm for computing the value of a polynomial
Assume
b. thatalgorithm
If the exponentiation is not built-in.
you designed is in Θ(n2 ), design a linear algorithm
for this problem.
Write it down clearly, so I can project it with the document camera.
c. Is it possible to design an algorithm with a better than linear efficiency
for this problem?
5. Sort the list E, X, A, M, P, L, E in alphabetical order by selection sort.
6. Is selection sort stable? (The definition of a stable sorting algorithm was
given in Section 1.3.)
7. Is it possible to implement selection sort for linked lists with the same
!10
2
Θ(n ) efficiency as the array version?
integer, how would you circumvent the problem of a very large magnitude
of an ?
Ex 3.1, Problem 4
3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
4. a. Design a brute-force algorithm for computing the value of a polynomial
Ex 3.1, Problem 4
3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
4. a. Design a brute-force algorithm for computing the value of a polynomial
Solution to Problem 4
!n 2
3.
3. For each 4of(computes
Problem the algorithms inyes
1 i ): Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
Problem 5 (computes the range of an array’s values): yes
4. a. Design a brute-force algorithm for computing the value of a polynomial
Problem 6 (checks whether an matrix isn−1
symmetric): yes
p(x) = an x + an−1 x + ... + a1 x + a0
4. at
a. aHere is apoint
given pseudocode
x0 and of the most its
determine straightforward version: class.
worst-case efficiency
Algorithm
b. If the algorithm you designed is in Θ(n2 ),[0design
BruteForcePolynomialEvaluation(P ..n], x) a linear algorithm
//The
for thisalgorithm
problem. computes the value of polynomial P at a given point x
//by the “highest-to-lowest term” brute-force algorithm
//Input:
c. Array P
Is it possible to[0 ..n] of the
design coefficientswith
an algorithm of a apolynomial of linear
better than degreeefficiency
n,
// this problem?
for stored from the lowest to the highest and a number x
//Output: The value of the polynomial at the point x
p←0
5. Sort .0 list E, X, A, M, P, L, E in alphabetical order by selection sort.
the
for i ← n downto 0do
6. Is selection
power ← sort
1 stable? (The definition of a stable sorting algorithm was
givenforin jSection
← 1to 1.3.)
i do
power ← power ∗ x
7. Is itppossible
← p + P to
[i] implement
∗ power selection sort for linked lists with the same
2
Θ(n ) efficiency as the array version?
return p !11
Problem 5 (computes the range of an array’s values): yes
Solution to Problem 4
4. a. Here is a pseudocode of the most straightforward version:
•
for i ← n downto 0do
power ← 1 size of input is degree of polynomial, n
• number of multiplications depends
for j ← 1to i do
power ← power ∗ x
p ← p + P [i] ∗ power only on n
•
return p
number of multiplications, M(n) ∈ ?
We will measure the input’s size by the polynomial’s degree n. The ba-
sic operation of this algorithm is a multiplication of two numbers; the
Θ(n) C. Θ(n lg n)
number of multiplications M (n) depends on the polynomial’s degree only.
A.
Θ(n2) D. Θ(n3)
5
B.
!12
integer, how would you circumvent the problem of a very large magnitude
of an ?
Ex 3.1, Problem 4
3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell
whether or not the algorithm is based on the brute-force approach.
4. a. Design a brute-force algorithm for computing the value of a polynomial
!14
(while the number of additions is n), i.e., we have a linear algorithm.
True or False?
! Itis possible to design an algorithm
with better-than-linear efficiency to
calculate the value of a polynomial.
A. True
B. False
!15
Ex 3.1, Problem 9
! Is selection sort stable?
" The definition of a stable sort was given in
Levitin §1.3
A. Yes, it is stable
B. No, it is not stable
!16
Ex 3.1, Problem 10
! Is it possible to implement selection
sort for a linked-list with the same
Θ(n2) efficiency as for an array?
A. Yes, it is possible
B. No, it is not possible
!17
BubbleSort
!18
BubbleSort
• Is BubbleSort stable?
!18
BubbleSort
• Is BubbleSort stable?
!18
BubbleSort
• Is BubbleSort stable?
!18
BubbleSort
• Is BubbleSort stable?
!19
Applications:
Find all occurrences of a particular word in a
given text
" Searching for text in an editor
" …
…
!20
Notation
Let A be a set of characters (the alphabet)
!21
Simple Complexities:
Assume that string is represented by an
array of consecutive characters
What’s the worst case running time for
brute-force testing to determine:
# whether s = t
!22
Simple Complexities:
Assume that string is represented by an
array of consecutive characters
What’s the worst case running time for
brute-force testing to determine:
# whether s = t A. O(1)
B. O(|s|)
C. O(|min(s, t)|)
D. O(|s|2)
E. None of the above
!22
Simple Complexities:
Assume that string s is represented by an
array of consecutive characters
Worst case running time for computing
s[i] ?
!23
Simple Complexities:
Assume that string s is represented by an
array of consecutive characters
Worst case running time for computing
s[i] ?
A. Θ(1)
B. Θ(|s|)
C. Θ(|min(|s|, i)|)
D. Θ(i)
E. None of the above
!23
Simple Complexities:
Assume that strings are represented by
arrays of consecutive characters
Worst case running time for
computing st ?
!24
Simple Complexities:
Assume that strings are represented by
arrays of consecutive characters
Worst case running time for
computing st ?
A. Θ(1)
B. Θ(|s|)
C. Θ(|min(s, t)|)
D. Θ(|min(s, t)|2)
E. None of the above
!24
Simple Complexities:
Assume that string is represented by an
array of consecutive characters
Worst case running times for
computing s[i..j]
!25
Simple Complexities:
Assume that string is represented by an
array of consecutive characters
Worst case running times for
computing s[i..j]
A. Θ(1)
B. Θ(|s[i..j]|)
C. Θ(j-i)
D. Θ((j-i)2)
E. None of the above
!25
String Matching
Find all occurrences of a pattern string
p in a text string t
For example:
a b r a c a d a b r a c a l a m a z o o
r a c r a c
!26
String Matching, formally
Given a text string, t, and a pattern
string, p, of length m = |p|, find the
set of all shifts s such that
p = t[s+1..s+m]
a b r a c a d a b r a c a l a m a z o o
s=2 r a c
a b r a c a d a b r a c a l a m a z o o
s=9 r a c
!27
Brute-force Matching Algorithm
a b r a c a d a b r a c a l a m a z o o
r a c
!28
Brute-force Matching Algorithm
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
!28
Brute-force Matching Algorithm
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
!28
Brute-force Matching Algorithm
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
…
!28
Brute-force Matching Algorithm
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
a b r a c a d a b r a c a l a m a z o o
r a c
…
a b r a c a d a b r a c a l a m a z o o
r a c
!28
Brute-force Matching Algorithm
t= a b r a c a d a b r a c a l a ma z o o
p= r a c
a b r a c a d a b r a c a l a m a z o o
r a c
!29
Brute-force Matching Algorithm
t= a b r a c a d a b r a c a l a ma z o o
p= r a c
a b r a c a d a b r a c a l a m a z o o
r a c
!29
Brute-force Matching Algorithm
t= a b r a c a d a b r a c a l a ma z o o
p= r a c
a b r a c a d a b r a c a l a m a z o o
r a c A. Θ(1)
B. Θ(|t|)
What's the asymptotic C. Θ(|p|)
complexity of brute- D. Θ(|p|(|t|-|p|+1))
force matching?: E. None of the above
!29
Brute-force Matching Algorithm
match(t, p)
m ← |p|
n ← |t|
results ← {}
for s ← 0..n-m do
if p == t[s+1 .. s+m] then
results ← results ∪ {s}
return results
!30
Brute-force Matching Algorithm
match(t, p)
m ← |p|
n ← |t|
results ← {}
for s ← 0..n-m do
if p == t[s+1 .. s+m] then
results ← results ∪ {s}
return results
Asymptotic Complexity:
Θ(m(n-m+1))
!30
Brute-force Matching Algorithm
match(t, p)
m ← |p| Cost of this test
is O(m)
n ← |t|
results ← {}
for s ← 0..n-m do
if p == t[s+1 .. s+m] then
results ← results ∪ {s}
return results
Asymptotic Complexity:
Θ(m(n-m+1))
!30
Can we do better?
Perhaps surprisingly: yes!
Key insight: when a match fails, we
learned something
" Better algorithms in Chapter 7
!31