Corrections
This commit is contained in:
parent
e8563ccc65
commit
9b59a99640
64
luku30.tex
64
luku30.tex
|
@ -7,20 +7,20 @@ Many geometric problems can be solved using
|
||||||
The idea in such algorithms is to represent
|
The idea in such algorithms is to represent
|
||||||
the problem as a set of events that correspond
|
the problem as a set of events that correspond
|
||||||
to points in the plane.
|
to points in the plane.
|
||||||
The events are processed in a sorted order
|
The events are processed in increasing order
|
||||||
according to their x or y coordinate.
|
according to their x or y coordinate.
|
||||||
|
|
||||||
As an example, let us consider a problem
|
As an example, let us consider a problem
|
||||||
where there is a company that has $n$ employees,
|
where there is a company that has $n$ employees,
|
||||||
and we know for each employee arrival and
|
and we know for each employee their arrival and
|
||||||
leaving times on a certain day.
|
leaving times on a certain day.
|
||||||
Our task is to calculate the maximum number of
|
Our task is to calculate the maximum number of
|
||||||
employees that were in the office at the same time.
|
employees that were in the office at the same time.
|
||||||
|
|
||||||
The problem can be solved by modelling the situation
|
The problem can be solved by modelling the situation
|
||||||
so that each employee is assigned two events that
|
so that each employee is assigned two events that
|
||||||
corresponds to the arrival and leaving times.
|
corresponds to their arrival and leaving times.
|
||||||
After sorting the events, we can go trough them
|
After sorting the events, we can go through them
|
||||||
and keep track of the number of people in the office.
|
and keep track of the number of people in the office.
|
||||||
For example, the table
|
For example, the table
|
||||||
\begin{center}
|
\begin{center}
|
||||||
|
@ -33,7 +33,7 @@ Kaaleppi & 14 & 16 \\
|
||||||
Liisa & 5 & 13 \\
|
Liisa & 5 & 13 \\
|
||||||
\end{tabular}
|
\end{tabular}
|
||||||
\end{center}
|
\end{center}
|
||||||
generates the following events:
|
corresponds to the following events:
|
||||||
\begin{center}
|
\begin{center}
|
||||||
\begin{tikzpicture}[scale=0.6]
|
\begin{tikzpicture}[scale=0.6]
|
||||||
\draw (0,0) rectangle (17,-6.5);
|
\draw (0,0) rectangle (17,-6.5);
|
||||||
|
@ -62,8 +62,8 @@ and maintain a counter.
|
||||||
Always when a person arrives, we increase
|
Always when a person arrives, we increase
|
||||||
the value of the counter by one,
|
the value of the counter by one,
|
||||||
and when a person leaves,
|
and when a person leaves,
|
||||||
we decrease the value by one.
|
we decrease the value of the counter by one.
|
||||||
The answer for the problem is the maximum
|
The answer to the problem is the maximum
|
||||||
value of the counter during the algorithm.
|
value of the counter during the algorithm.
|
||||||
|
|
||||||
In the example, the events are processed as follows:
|
In the example, the events are processed as follows:
|
||||||
|
@ -119,12 +119,12 @@ In the example, the events are processed as follows:
|
||||||
\end{tikzpicture}
|
\end{tikzpicture}
|
||||||
\end{center}
|
\end{center}
|
||||||
The symbols $+$ and $-$ indicate whether the
|
The symbols $+$ and $-$ indicate whether the
|
||||||
value of the counter increases of decreases,
|
value of the counter increases or decreases,
|
||||||
and the value of the counter is shown below.
|
and the value of the counter is shown below.
|
||||||
The maximum value of the counter is 3
|
The maximum value of the counter is 3
|
||||||
between Uolevi's arrival and Maija's leaving.
|
between Uolevi's arrival time and Maija's leaving time.
|
||||||
|
|
||||||
The running time of the solution is $O(n \log n)$,
|
The running time of the algorithm is $O(n \log n)$,
|
||||||
because sorting the events takes $O(n \log n)$ time
|
because sorting the events takes $O(n \log n)$ time
|
||||||
and the rest of the algorithm takes $O(n)$ time.
|
and the rest of the algorithm takes $O(n)$ time.
|
||||||
|
|
||||||
|
@ -133,8 +133,8 @@ and the rest of the algorithm takes $O(n)$ time.
|
||||||
\index{intersection point}
|
\index{intersection point}
|
||||||
|
|
||||||
Given a set of $n$ line segments, each of them being either
|
Given a set of $n$ line segments, each of them being either
|
||||||
horizontal or vertical, the problem is to efficiently
|
horizontal or vertical, consider the problem of
|
||||||
calculate the total number of intersection points.
|
counting the total number of intersection points.
|
||||||
For example, when the line segments are
|
For example, when the line segments are
|
||||||
\begin{center}
|
\begin{center}
|
||||||
\begin{tikzpicture}[scale=0.5]
|
\begin{tikzpicture}[scale=0.5]
|
||||||
|
@ -206,12 +206,12 @@ Intersection points are calculated at event 3.
|
||||||
When there is a vertical segment between points
|
When there is a vertical segment between points
|
||||||
$y_1$ and $y_2$, we count the number of active
|
$y_1$ and $y_2$, we count the number of active
|
||||||
horizontal segments whose y coordinate is between
|
horizontal segments whose y coordinate is between
|
||||||
$y_1$ and $y_2$, and at this number to the total
|
$y_1$ and $y_2$, and add this number to the total
|
||||||
number of intersection points.
|
number of intersection points.
|
||||||
|
|
||||||
An appropriate data structure for storing
|
An appropriate data structure for
|
||||||
y coordinates of horizontal segments is either
|
y coordinates of horizontal segments is either
|
||||||
a binary-indexed tree or a segment tree,
|
a binary indexed tree or a segment tree,
|
||||||
possibly with index compression.
|
possibly with index compression.
|
||||||
Using such structures, processing each event
|
Using such structures, processing each event
|
||||||
takes $O(\log n)$ time, so the total running
|
takes $O(\log n)$ time, so the total running
|
||||||
|
@ -268,11 +268,12 @@ we should find the following points:
|
||||||
\end{center}
|
\end{center}
|
||||||
\end{samepage}
|
\end{samepage}
|
||||||
|
|
||||||
This problem can be also solved in $O(n \log n)$ time
|
This is another example of a problem
|
||||||
|
that can be also solved in $O(n \log n)$ time
|
||||||
using a sweep line algorithm.
|
using a sweep line algorithm.
|
||||||
We go through the points from left to right
|
We go through the points from left to right
|
||||||
and maintain a value $d$: the minimum distance
|
and maintain a value $d$: the minimum distance
|
||||||
between two points so far.
|
between two points seen so far.
|
||||||
At each point, we find the nearest point to the left.
|
At each point, we find the nearest point to the left.
|
||||||
If the distance is less than $d$, it is the
|
If the distance is less than $d$, it is the
|
||||||
new minimum distance and we update
|
new minimum distance and we update
|
||||||
|
@ -292,7 +293,7 @@ For example, in the following picture the
|
||||||
region marked with dashed lines contains
|
region marked with dashed lines contains
|
||||||
the points that can be within a distance of $d$
|
the points that can be within a distance of $d$
|
||||||
from the active point:
|
from the active point:
|
||||||
\\
|
|
||||||
\begin{center}
|
\begin{center}
|
||||||
\begin{tikzpicture}[scale=0.7]
|
\begin{tikzpicture}[scale=0.7]
|
||||||
\draw (0,0)--(12,0)--(12,4)--(0,4)--(0,0);
|
\draw (0,0)--(12,0)--(12,4)--(0,4)--(0,0);
|
||||||
|
@ -325,27 +326,27 @@ from the active point:
|
||||||
\end{center}
|
\end{center}
|
||||||
|
|
||||||
The efficiency of the algorithm is based on the fact
|
The efficiency of the algorithm is based on the fact
|
||||||
that the region limited by $d$ always contains
|
that such a region always contains
|
||||||
only $O(1)$ points.
|
only $O(1)$ points.
|
||||||
We can go through those points in $O(\log n)$ time
|
We can go through those points in $O(\log n)$ time
|
||||||
by maintaining a set of points whose x coordinate
|
by maintaining a set of points whose x coordinate
|
||||||
is between $[x-d,x]$ and that are sorted according
|
is between $[x-d,x]$, in increasing order according
|
||||||
to the y coordinate.
|
to their y coordinates.
|
||||||
|
|
||||||
The time complexity of the algorithm is $O(n \log n)$,
|
The time complexity of the algorithm is $O(n \log n)$,
|
||||||
because it goes through $n$ points and
|
because we go through $n$ points and
|
||||||
finds for each point the nearest point to the left
|
find for each point the nearest point to the left
|
||||||
in $O(\log n)$ time.
|
in $O(\log n)$ time.
|
||||||
|
|
||||||
\section{Convex hull}
|
\section{Convex hull}
|
||||||
|
|
||||||
The \key{convex hull} is the smallest convex polygon
|
A \key{convex hull} is the smallest convex polygon
|
||||||
that contains all points in a given set.
|
that contains all points of a given set.
|
||||||
Convexity means that a line segment between
|
Convexity means that a line segment between
|
||||||
any two vertices of the polygon is completely
|
any two vertices of the polygon is completely
|
||||||
inside the polygon.
|
inside the polygon.
|
||||||
A good intuitive definition is that we surround
|
An intuitive definition for a convex hull
|
||||||
the points using a tight rope.
|
is that it surrounds the given points using a tight rope.
|
||||||
|
|
||||||
\begin{samepage}
|
\begin{samepage}
|
||||||
For example, for the points
|
For example, for the points
|
||||||
|
@ -392,9 +393,10 @@ the convex hull is as follows:
|
||||||
|
|
||||||
\index{Andrew's algorithm}
|
\index{Andrew's algorithm}
|
||||||
|
|
||||||
A good way to construct the convex hull
|
\key{Andrew's algorithm} is an easy algorithm
|
||||||
is \key{Andrew's algorithm}
|
that can be used to
|
||||||
that works in $O(n \log n)$ time.
|
construct the convex hull for a set of points
|
||||||
|
in $O(n \log n)$ time.
|
||||||
The algorithm constructs the convex hull
|
The algorithm constructs the convex hull
|
||||||
in two steps:
|
in two steps:
|
||||||
first the upper hull and then the lower hull.
|
first the upper hull and then the lower hull.
|
||||||
|
@ -410,7 +412,7 @@ whether the tree last point in the hull turn left.
|
||||||
If this holds, we remove the middle point from the hull.
|
If this holds, we remove the middle point from the hull.
|
||||||
After this we keep checking again the three last points
|
After this we keep checking again the three last points
|
||||||
and removing points, until the three last points
|
and removing points, until the three last points
|
||||||
don't turn left.
|
do not turn left.
|
||||||
|
|
||||||
The following pictures show how
|
The following pictures show how
|
||||||
Andrew's algorithm works:
|
Andrew's algorithm works:
|
||||||
|
|
Loading…
Reference in New Issue