Professional Documents
Culture Documents
GUN SRIJUNTONGSIRI
POINT-LINE DISTANCE
Given a point P and a line L in 2D, the distance between them is the distance between P and the point P on L that is closest to P. How to compute this distance?
Note: a line with slope = m is perpendicular to a line with slope = -1/m.
FORMAL PROBLEM
Assume the floor plan of a gallery is a closed simple polygon.
Edges of the polygon do not intersect among themselves. No holes inside!
Lets find the upper bound for the number of cameras needed for a gallery of size n.
Will find the worst-case bound. Finding the minimum number of cameras for the specific polygon is NP-hard.
THE IDEA
Decompose the polygon into pieces that are easier to guard---triangles. 1. By drawing diagonals between pairs of vertices.
The diagonals must stay inside the polygon. They must not intersect each other.
2. Keep drawing diagonals until you cannot add any more. 3. Now you have a triangulation of the polygon. Then place one camera in each triangle. (Why does this work?)
Theorem: Every simple polygon admits a triangulation, and any triangulation of a simple polygon with n vertices consists of exactly n-2 triangles. Proof: Exercise.
10
11
12
A camera on some vertex may be even better. Can guard many triangles sharing the vertex.
13
BETTER APPROACH
Find a subset of vertices of the polygon so that any triangle in a triangulation has at least one selected vertex. Place cameras at the chosen vertices. Question: How to choose this subset?
14
15
16
17
18
19
TO FIND 3-COLORING
Use Depth-First-Search on G(T) starting from any vertex v. Color the three vertex of the triangle corresponding to v with the three colors. When reach the next node w of G(T), two of the vertices of the triangle corresponding to w are already colored. Color the remaining vertex with the third color.
Can do so because G(T) is a tree so the other nodes adjacent to w are not colored yet.
20
So, a simple polygon can be guarded with floor(n/3) cameras. Can we do better?
21
For any n, there are simple polygons that require floor(n/3) cameras. Ex: n vertex polygon that needs floor(n/3) cameras:
22
ARE WE DONE?
Not yet. Still need an algorithm for finding triangulation.
23
24
Take linear time to find a diagonal. The diagonal may split the polygon into a triangle and a polygon with n-1 vertices.
E.g. if uw is a chosen diagonal.
25
26
27
1D RANGE SEARCHING
Given a set of real numbers, find all points in the set that are in [x, x]. There is a solution using an array.
But cannot generalize to higher dimensions.
28
29
Assume: nodes on the left subtree of v have values < that of v. Nodes on the right subtree of v have values >= that of v.
30
1D RANGE SEARCH
Range is [x, x]. Search with x in the tree. Let m be the leaf where the search ends. Search with x in the tree. Let m be the leaf where the search ends. All leaves between m and m are in the range. Question: Are m and m in the range?
Maybe. Have to check.
31
32
Then, starting from v_split, follow the search path of x. At each node where the path goes left, report all leaves in the right subtree.
Dont report anything when the path goes right.
33
Similarly, follow the search path of x starting from v_split. At each node where the path goes right, report all leaves in the left subtree.
Dont report anything when the path goes left.
34
EXERCISE
Implement 1D Range Searching
35
TIME COMPLEXITY
Let n be the number of points. Building a balanced binary search tree can be done in O(n log n) time. Worst-case for 1D range search (assuming you already have the tree)?
(n) if all points are in the range.
But just checking every point directly is also (n)! So whats the point?
36
Let k be the number of outputs (the number of points in [x, x]). Lets analyze the running time as a function of both n and k. Note: the time to report all leaves in a subtree is linear in the number of reported points. Therefore, the time spent to report all points in the range (excluding possibly m and m) is O(k). Time to search for x and x = O(log n). So total is O(k + log n).
37
2D RANGE SEARCHING
Given a set of points (px, py), find all points inside a given rectangle [x, x] x [y, y]. A point is inside this rectangle if x <= px <= x and y <= py <= y.
Assume for now: no two points have the same x value. No two points have the same y value.
38
TO GENERALIZE TO 2D
Can think of 1D binary search tree as follows: The set of 1D points is split into two subsets of roughly equal size:
One subset contains the points smaller than or equal to the splitting value. The other contains the points larger than the splitting value.
The splitting value is stored at the root. The two subsets are stored recursively in the two subtrees.
39
In 2D, each point has two values: its x value and its y yalue. First, split on x-coordinate, Next on y-coordinate, Then on x-coordinate, and so on.
40
At the root, we split the set of points with a vertical line l. All points to the left of l and on l will be in the left subtree. All points to the right of l will be in the right subtree. Store l at the root.
41
42
EXERCISE
Implement a program to construct a 2D tree from a given set of 2D points.
Split at the median. (In general, you can split at other places). The median of n numbers should be defined as the floor(n/2)-th smallest number. E.g. the median of 2 numbers is the smaller one. Otherwise, the algorithm may not terminate. Median can be found in O(n), where n is the number of points (dont have to do this unless you know how to do).
43
2D QUERY
An internal node v of a 2D tree defines a rectangular region. Call it region(v). Note: Any points stored in a subtree rooted at a node v lies in region(v).
44
THE ALGORITHM
45
46
EXERCISE
Implement 2D Range Search.
47
48
49
A composite number of two reals a and b is denoted by (a|b). (a|b) < (a|b) iff a < a or (a = a and b < b) Replace each point p := (px, py) by a new point p = ( (px|py), (py|px) ). Also replace the desired range R := [x, x] x [y, y] by R := [(x|-), (x|)] x [(y|-), (y|)] Will we get correct result? Lemma: p is in R iff p is in R. Proof: Exercise.
50
Proof: p in R iff x <= px <= x and y <= py <= y, which is iff (x|-) <= (px|py) <= (x|) and (y|-) <= (py|px) <= (y|).
51
52
53
This algorithm is O(sqrt(n)) in worst case, O(log n) if points are randomly distributed.
55
EXERCISE
Implement nearest neighbor search using 2D-tree.
56
POINT LOCATION
57
POINT LOCATION
Given a map and a location, in what country is this location? Problem: Given a planar subdivision (formed by multiple polygons) S and a point q, report the face of S that contains q. If q lies on an edge or a vertex, report that, too.
58
PLANAR SUBDIVISION
59
61
62
REMARKS
IncidentEdge of a vertex is an arbitrary half-edge that has v as its origin (if more than one, choose any). InnerComponents(f) is when holes are allowed. Can ignore them for now.
63
POINT LOCATION
Draw vertical lines through all vertices of the subdivision. This partitions the plane into vertical slabs. Store the x-coordinates of the vertices in sorted order in an array.
So can determine in O(log n) time which slab contains a point q.
64
PROPERTIES OF A SLAB
No vertices of S inside a slab (only at boundaries). So all edges of S in a slab do not cross each other! So can order the edges from top to bottom. Region in the slab between two consecutive edges belong to a unique face of S. The lowest and highest region of the slab are the unbounded face of S. So store edges in sorted order in an array. Label each edge with the face of S immediately above and below it.
65
66
EXERCISE
Implement this algorithm for Point Location. You can store the faces immediately above and below a given edge. Or you can use doublyconnected edge list.
67
VORONOI DIAGRAM
68
69
70
71