Lecture 4 - Brute-Force Algorithms (Part 2) - Miscellaneous
Lecture 4 - Brute-Force Algorithms (Part 2) - Miscellaneous
Based on the notes for “Introduction to the Design & Analysis of Algorithms, 3rd ed." by Levitin
Reminders: homework
• Starting with Homework 4, please submit your homework as a single le in PDF format. This will help us
speed up the grading process.
• The instructions for placing multiple images into a single PDF have been added to the Homework
4 dropbox.
• To give you extra time to comply with this requirement, Homework 4 has been extended by one day until
the end of Monday, February 12
• Please remember to answer homework questions fully. Some issues that we have seen include:
• Stating the complexity class without precision, e.g. "exponential". This is de nitely not su cient - you
have to provide both the appropriate notation (big-Oh, Theta, or Omega) and specify the appropriate
function - e.g. O(2n) or Θ(n).
• Questions that are answered without showing the intermediate work. All answers must always include all
intermediate work, unless the question states otherwise (as discussed in Lecture 0).
• If you are not sure about your answers, ask us - we are here to help.
Reminders: office hours
• All meetings must be booked using our UofG email. Bookings the do not comply with this policy will be
cancelled.
• Students only get one meeting slot with the instructor per o ce hour block - again, I need to maximize my
availability to the large class
• Multiple bookings for the same block will be cancelled.
• The Zoom link for the instructor meetings is available on the course website, right beside the instructor o ce
hours booking link.
Brute-Force String Matching
• Pattern: 001011
• Text: 10010101101001100101111010
• Pattern: happy
• Text: It is never too late to have a happy childhood.
Pseudocode and Efficiency
• E ciency:
• Worst case: P is not in T
• n-m+1 tries
• m comparisons per try
• n*m comparisons in total
• O(nm)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
ffi
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
• E ciency: O(n2)
ffi
Polynomial Evaluation: Improvement
• E ciency: O(n)
ffi
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
• Return the indexes of the points for which the distance is the smallest.
2 2 2 2
1 1 1 1
3 3 3 3
4 4 4 4
Closest-Pair Brute-Force Algorithm (cont.)
• E ciency: O(n2)
•
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
ffi
Closest-Pair - improvement
How to make it faster?
• The sqrt() is the most expensive operation - it computes an irrational umber and is usually
implemented using some sort of approximation
• To get rid of it, we use a property - sqrt() is strictly increasing, i.e. sqrt(x1) < sqrt(x2) i x1 < x2
• Instead of comparing distances, we will compare squared distances:
• d2 = (xi-xj)2 + (yi-yj)2
• if d1 < d2 then d12 < d22
• The Big-Oh complexity is the same, but we have a constant factor improvement
Squared distances elsewhere
• This "trick" is often using in programs related to geometry - e.g. gaming or graphics
• If we need to compare the distance d, we compare d2 instead - the relationship between
squared distances holds for distances
• This avoids calls to sqrt() when dealing with large 3D meshes or complex game schemes
with large number of interacting entities
Squared distances and magnitudes
• This also applies to vector magnitudes, which use the same Euclidean distance equation
• Gaming APIs such as Unity provide methods that return squared vector magnitudes, which
can be used as a small optimization
• Strengths
• wide applicability
• simplicity
• yields reasonable algorithms for some important problems
• e.g., matrix multiplication, sorting, searching, string matching
• Weaknesses
• rarely yields e cient algorithms
• some brute-force algorithms are unacceptably slow
• not as constructive as some other design techniques
ffi