Presentation is loading. Please wait.

Presentation is loading. Please wait.

What does that mean? To get the taste we will just look only at some sample problems... [Adapted from S.Suri]

Similar presentations


Presentation on theme: "What does that mean? To get the taste we will just look only at some sample problems... [Adapted from S.Suri]"— Presentation transcript:

1 What does that mean? To get the taste we will just look only at some sample problems... [Adapted from S.Suri]

2 Some basic algorithms Given directed line segments p0p1 and p0p2,
determine whether p0p1 is clockwise from p0p2 with respect to point p0? Given two line segments p0p1 and p1p2, if we traverse p0p1 and then p1p2, do we make a left turn at point p1? Do line segments p0p1 and p2p3 intersect?

3 Cross Products y p2 = (x2,y2) p1  p2 = x1y2 – x2y1 p1 = (x1,y1)
(0,0) x We assume that cross product is scalar given by this formula... [Adapted from K.Howard]

4 Cross Products y p2 = (x2,y2) p1 + p2 p1  p2 = x1y2 – x2y1
(0,0) x [Adapted from K.Howard]

5 + – Cross Products y The sign (+ or –) of cross
product depends in which half plane (relative to p1) lies p2 p1 (0,0) x

6 Some basic algorithms Given directed line segments p0p1 and p0p2,
determine whether p0p1 is clockwise from p0p2 with respect to point p0? Compute  = p1  p2 if  > 0, then p0p1 is clockwise from p0p2 if  < 0, then p0p1 is counterclockwise from p0p2

7 Some basic algorithms Given two line segments p0p1 and p1p2, if we
traverse p0p1 and then p1p2, do we make a left turn at point p1? Compute  = (p1 – p0)  (p2 – p0) if  > 0, then we make a right turn at p1 if  < 0, then we make a left turn at p1 p1 p2 p0

8 Some basic algorithms Do line segments p0p1 and p2p3 intersect?
Bounding box - the smallest rectangle that contains the figure and whose segments are parallel to x-axis and y-axis. Bounding box of papb - rectangle with lower left point (x’a,y’a) and upper right point (x’b,y’b), where x’a = min(xa,xb), y’a = min(ya,yb), x’b = max(xa,xb), y’b = max(ya,yb)

9 Some basic algorithms Two rectangles (p’0,p’1) and (p’2,p’3) intersect
if and only if (x’1  x’3) & (x’3  x’0) & (y’1  y’3) & (y’3  y’0) Quick rejection test - p0p1 and p2p3 does not intersect, if (p’0,p’1) and (p’2,p’3) does not intersect

10 Some basic algorithms p0p1 and p2p3 intersect if and only if they
pass a quick rejection test, and segments p0p2 and p0p3 have opposite orientation with respect to p0p1, and segments p2p0 and p2p1 have opposite direction with respect to p2p3

11 Segment Intersection - Problem
Given n line segments s1, s2, ,sn, determine whether there exists two of them sa and sb, such that sa intersects with sb. [Adapted from A.Vigneron]

12 Segment Intersection - Naïve approach
[Adapted from A.Vigneron]

13 Segment Intersection - Assumptions
[Adapted from A.Vigneron]

14 Segment Intersection - Idea
[Adapted from A.Vigneron]

15 Segment Intersection - Idea
f b a a b a c b d a c b d c b e d c b e d b

16 Segment Intersection - Idea
[Adapted from A.Vigneron]

17 Segment Intersection - Example
[Adapted from A.Vigneron]

18 Segment Intersection - Case 1
[Adapted from A.Vigneron]

19 Segment Intersection - Case 2
[Adapted from A.Vigneron]

20 Segment Intersection - Data structure
[Adapted from A.Vigneron]

21 Segment Intersection - Algorithm
procedure SegmentIntersection(set S) T  0 sort the endpoints of the segments S from left to right, for each point p in the sorted list of endpoints do if p is the left endpoint of segment s then Insert(T,s) if Above(T,s) exists and intersects s, or Below(T,s) exists and intersect s then return true if p is the right endpoint of segment s then if both Above(T,s) and Below(T,s) exist and Above(T,s) intersect Below(T,s) then return true Delete(T,s) return false

22 Segment Intersection - Complexity
(n log n) for sorting Around (n) Insert, Delete, Above and Below operations, each requires O(log n) time procedure SegmentIntersection(set S) T  0 sort the endpoints of the segments S from left to right, for each point p in the sorted list of endpoints do if p is the left endpoint of segment s then Insert(T,s) if Above(T,s) exists and intersect s, or Below(T,s) exists and intersect s then return true if p is the right endpoint of segment s then if both Above(T,s) and Below(T,s) exist and Above(T,s) intersect Below(T,s) then return true Delete(T,s) return false T(n)=(n log n)

23 Segment Intersection - Correctness
[Adapted from A.Vigneron]

24 Segment Intersection - Correctness
[Adapted from A.Vigneron]

25 Convex Hull - Problem Given n points on plane p1, p2, ,pn, find the smallest convex polygon that contains all points p1, p2, ,pn. [Adapted from G.Miller]

26 Convexity - a closer look
Given n points on plane p1, p2, ,pn, find the smallest convex polygon that contains all points p1, p2, ,pn. A set SRn is convex, if for all x,yS and t[0,1] we also have x+(1t)yS. A convex hull of XRn is a minimal convex set C such that XC. Convex hull always exists (since intersection of any two convex sets is also convex). In our case we have to find a smallest convex set C containing given n points.

27 Convexity - a closer look
Given n points on plane p1, p2, ,pn, find the smallest convex polygon that contains all points p1, p2, ,pn. In our case we have to find a smallest convex set C containing given n points. It can be shown that all border points of C lies on a line segment between a pair of points pi and pj. Thus we have to find a polygon containing all pi's and with vertices from pi's.

28 Convex Hull - Example Given n line points on plane p1, p2, ,pn, find the smallest convex polygon that contains all points p1, p2, ,pn. p10 p6 p9 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

29 Convex Hull - Example Given n line points on plane p1, p2, ,pn, find the smallest convex polygon that contains all points p1, p2, ,pn. p10 p6 p9 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

30 Graham Scan - Algorithm
procedure GrahamScan(set Q) let p0 be the point with the minimum y-coordinate let <p1, , pm> be the remaining points in Q, sorted by the angle in counterclockwise order around p0 Top(S)  0 Push(p0, S); Push(p1, S); Push(p2, S) for i  3 to m do while the angle formed by points NextToTop(S), Top(S) and pi makes a non-left turn do Pop(S) Push(pi, S) return S

31 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

32 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

33 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

34 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

35 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

36 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

37 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

38 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

39 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

40 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

41 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

42 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

43 Graham Scan - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

44 Graham Scan - Algorithm
procedure GrahamScan(set Q) let p0 be the point with the minimum y-coordinate let <p1, , pm> be the remaining points in Q, sorted by the angle in counterclockwise order around p0 Top(S)  0 Push(p0, S); Push(p1, S); Push(p2, S) for i  3 to m do while the angle formed by points NextToTop(S), Top(S) and pi makes a non-left turn do Pop(S) Push(pi, S) return S

45 Graham Scan - Complexity
Sorting takes (n log n) time for loop executes (n) times each while loop might take up (n) time however, no more than (n) for all while loops together procedure GrahamScan(set Q) let p0 be the point with the minimum y-coordinate let <p1, , pm> be the remaining points in Q, sorted by the angle in counterclockwise order around p0 Top(S)  0 Push(p0, S); Push(p1, S); Push(p2, S) for i  3 to m do while the angle formed by points NextToTop(S), Top(S) and pi makes a non-left turn do Pop(S) Push(pi, S) return S T(n) = (n log n) + const (n) = (n log n)

46 Graham Scan - Correctness
We have to show: 1) that all discarded points are within the constructed convex set 2) the constructed set is convex [Adapted from W.Goddard]

47 Graham Scan - Correctness
1) are all discarded points within the constructed convex set? - for subsequent non-discarded i and k the triangle (0,i,k) lies within constructed convex hull (convex hull is union of such triangles) - the last discarded point (preceding k) lies within a triangle within conctructed convex hull - by induction we can shown the same for all discarded points between j and i [Adapted from W.Goddard]

48 Graham Scan - Correctness
2) is the constructed set convex? - every triangle is convex (lets say it is kind of obvious :) - in each step we place a convex triagle on an edge of convex polygon in such a way, that both new angles of resulting polygon doesn't exceed 180 degrees - if resulting polygon is not convex, there is a point in initial polygon and apoint in triangle, such that line segment between these points doesn't belong to resulting polygon - this contradicts that new angles are at most 180 degrees [Adapted from W.Goddard]

49 Jarvis March - Algorithm
[Adapted from G.Elber]

50 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

51 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

52 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

53 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

54 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

55 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

56 Jarvis March - Example p10 p9 p6 p5 p12 p7 p3 p4 p11 p1 p8 p2 p0

57 Jarvis March - Complexity
T(n) = (n h), where h is the number of vertices on convex hull Can we do faster than O(n log n)? [Adapted from R.Dawes]

58 Convex hull in plane - the lower bound
Sort: X = (x1, x2, … , xn ) (n given real numbers on the x-axis) Step 1: P  {p1, p2 , … , pn}, where pj = (xj , xj2), j=1..n Step 2: Compute CH(P) y pj y = x2 parabola (convex) CH ( p1, … , pn ) xj x Step 3: Sorted(X) can be obtained from CH(P) in O(n) added time. (n log n) = O(n) + T(n) + O(n)  T(n) = (n log n). [Adapted from A.Mirzaian]

59 What if we have more than 2 dimensions?
Can any of these algorithms be extended for more than 2 dimensions? What complexity estimates we should expect for n dimensional case? Graham scan? Jarvis march? this might work, complexity likely will be (n h), where h is the number of “faces” on convex hull.

60 Convex hull - few other methods
Divide & Conquer Sort the points from left to right Let A be the leftmost n/2 points Let B be the rightmost n/2 points Compute convex hulls H(A) and H(B) Compute H(AB) by merging H(A) and H(B) Merging is tricky, but can be done in linear time [Adapted from K.Lim Low]

61 Convex hull - few other methods
A B lower tangent upper tangent Need to find the upper and lower tangents They can be found in linear time [Adapted from K.Lim Low]

62 Convex hull - few other methods
A B lower tangent upper tangent Need to find the upper and lower tangents They can be found in linear time [Adapted from K.Lim Low]

63 Convex hull - few other methods
Divide & Conquer in 3D before Merging still can be done in linear time! We have O(n log n) in 3D after [Adapted from K.Lim Low]

64 Convex gull - few other methods
Idea: incrementally add a point to a convex polyhedra P Two cases: The new point is inside P  do nothing The new point is outside of P  fix P! [Adapted from F.Joskowitz]

65 Convex gull - few other methods
How fast is this? Naive implementation: O(n2) O(n log n) randomized algorithm Can be made deterministic with the same running time. [Adapted from F.Joskowitz]

66 Complexity in 3 and more dimensions?
How many “faces” complex hull might have? 2D up to n 3D up to ? 3D up to n kD up to ? Unfortunately, convex hull in d dimensions can have (nd/2) facets (proved by Klee, 1980) No O(n lg n) algorithm possible for d > 3

67 Complexity in 3 and more dimensions?
Gift wrapping method requires O(nd/2+1) time. There are algorithms that works in O( n log n + nd/2 ) time.

68 Voronoi diagrams The problem:
For a given set of n points to split kD space into regions of points closest to the given ones

69 Voronoi diagrams The “dual graphs” for Voronoi diagrams

70 Delaunay traingulation (tessalonation)
An edge e is called Delaunay iff there exist an empty circle passing through e. Delaunay triangulation - a set of Delaunay edges One can shown that Delaunay triangulation is a dual graph to a Voronoi diagram.

71 Delaunay triangulations and convex hulls
Compute the 3D lower convex hull z=x2+y2 Project the 3D facets Back to the plane. z=x2+y2 z=x2+y2 Project the 2D point set onto the 3D paraboloid The 2D triangulation is Delaunay!

72 Closest pair of points - The problem
Given n points on plane p1, p2, ,pn, find the pair of points pa and pb, such that distance between pa and pb is as small as possible. [Adapted from R.Tamassia]

73 Closest pair of points - Brute force method
[Adapted from R.Tamassia]

74 Closest pair of points - Divide and conquer
[Adapted from R.Tamassia]

75 Closest pair of points - Divide and conquer
[Adapted from R.Tamassia]

76 Closest pair of points - Divide and conquer
[Adapted from R.Tamassia]

77 Closest pair of points - Divide and conquer
[Adapted from R.Tamassia]

78 Closest pair of points - Divide and conquer
[Adapted from R.Tamassia]

79 Closest pair of points - Complexity
[Adapted from R.Tamassia]

80 Closest pair of points - Complexity
[Adapted from R.Tamassia]

81 Closest pair of points - Complexity
[Adapted from R.Tamassia]


Download ppt "What does that mean? To get the taste we will just look only at some sample problems... [Adapted from S.Suri]"

Similar presentations


Ads by Google