0% found this document useful (0 votes)
8 views

Lecture 4 - Brute-Force Algorithms (Part 2) - Miscellaneous

Free notebooks

Uploaded by

Umama Zafran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 4 - Brute-Force Algorithms (Part 2) - Miscellaneous

Free notebooks

Uploaded by

Umama Zafran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CIS*3490

Lecture 4: Brute-force algorithms, part 2: strings, math, and


geometry

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

• Unfortunately, we have had a number of no-shows in my o ce ours on Friday


• Please remember that you must cancel a meeting if you no longer need to meet with me, so that your slot
can be used by another student.
• Meetings can be cancelled using the cancellation link in the initial booking con rmation.
• Please note that if you miss meetings without cancelling them, you may lose access to instructor o ce hours
for several weeks.

• 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

• problem: nd a substring in the text that matches the pattern


• pattern: a string of m characters to search for
• text: a (longer) string of n characters to search in
• 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
fi
Examples of 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

• We can do better by evaluating from right to left


Find the value of polynomial p(x) = anxn + an-1xn-1 +...+ a1x1 + a0
at a point x = x0

• 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

• 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

• We can use this for squared distances


• compute the displacement vector between two points
• compute the squared magnitude of this vector
• compare this squared magnitude to other squared magnitudes
Brute-Force Strengths and Weaknesses

• 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

You might also like