characters called the
text
and a string of m characters (m ≤ n)
called the pattern,
find a substring of the
text that matches the pattern.

A brute-force algorithm for the string-matching problem is quite
obvious:
•
Align the pattern against the first
m characters of the text
•
start matching the
corresponding pairs of characters from left
to right
•
Until either all the
m pairs
of the characters match (then the
algorithm can stop)
•
Or a mismatching pair is encountered.

Exercise 3.2
Finding the two closest points in a set of
n
points

Closest-Pair Problem
Find the two closest points in a set of
n
points
Points can represent 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

Closest-Pair Problem
Consider the two-dimensional case of the
closest-pair problem
points in question are specified in a standard
fashion by their
(x, y) Cartesian coordinates
the distance between two points
p
i
(x
i
, y
i
) and
p
j
(x
j
, y
j
) is the standard Euclidean distance

Closest-Pair Problem
The brute-force approach: compute the distance
between each pair of distinct points and find a
pair with the smallest distance.
Of course, we do not want to compute the
distance between the same pair of points twice.
To avoid doing so, we consider only the pairs of
points
(p
i
, p
j
) for which i < j.

Pseudocode below computes the distance
between the two closest points:

Fake Coins
A stack of fake coins There are n stacks of n
identical-looking coins.
All of the coins in one of these stacks are counterfeit,
while all the coins in the other stacks are genuine.

#### You've reached the end of your free preview.

Want to read all 32 pages?

- Fall '19