{"id":25916,"date":"2017-10-25T21:26:52","date_gmt":"2017-10-25T15:56:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25916"},"modified":"2017-10-25T21:26:52","modified_gmt":"2017-10-25T15:56:52","slug":"convex-hull-set-2-graham-scan","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/convex-hull-set-2-graham-scan\/","title":{"rendered":"Given n line segments, find if any two segments intersect"},"content":{"rendered":"<p>We have discussed the problem to detect if<a href=\"http:\/\/www.geeksforgeeks.org\/check-if-two-given-line-segments-intersect\/\" target=\"_blank\" rel=\"noopener noreferrer\"> two given line segments intersect or not<\/a>. In this post, we extend the problem. <span id=\"more-120766\"><\/span>Here we are given n line segments and we need to find out if any two line segments intersect or not.<\/p>\n<p><strong>Naive Algorithm<\/strong> A naive solution to solve this problem is to check every pair of lines and check if the pair intersects or not. <a href=\"http:\/\/www.geeksforgeeks.org\/check-if-two-given-line-segments-intersect\/\" target=\"_blank\" rel=\"noopener noreferrer\">We can check two line segments in O(1) time<\/a>. Therefore, this approach takes O(n<sup>2<\/sup>).<\/p>\n<p><strong>Sweep Line Algorithm:<\/strong> We can solve this problem in <strong>O(nLogn)<\/strong> time using Sweep Line Algorithm. The algorithm first sorts the end points along the x axis from left to right, then it passes a vertical line through all points from left to right and checks for intersections. Following are detailed steps.<\/p>\n<p><strong>1) <\/strong>Let there be n given lines. There must be 2n end points to represent the n lines. Sort all points according to x coordinates. While sorting maintain a flag to indicate whether this point is left point of its line or right point.<\/p>\n<p><strong>2)<\/strong> Start from the leftmost point. Do following for every point<br \/>\n\u2026..<strong>a)<\/strong> If the current point is a left point of its line segment, check for intersection of its line segment with the segments just above and below it. And add its line to <em>active<\/em> line segments (line segments for which left end point is seen, but right end point is not seen yet). \u00a0Note that we consider only those neighbors which are still active.<br \/>\n\u2026.<strong>b) <\/strong>If the current point is a right point, remove its line segment from active list and check whether its two active neighbors (points just above and below) intersect with each other.<\/p>\n<p>The step 2 is like passing a vertical line from all points starting from the leftmost point to the rightmost point. That is why this algorithm is called Sweep Line Algorithm.<\/p>\n<p><strong>What data structures should be used for efficient implementation?<\/strong><br \/>\nIn step 2, we need to store all active line segments. We need to do following operations efficiently:<br \/>\na) Insert a new line segment<br \/>\nb) Delete a line segment<br \/>\nc) Find predecessor and successor according to y coordinate values<br \/>\nThe obvious choice for above operations is Self-Balancing Binary Search Tree like AVL Tree, Red Black Tree. With a Self-Balancing BST, we can do all of the above operations in O(Logn) time.<br \/>\nAlso, in step 1, instead of sorting, we can use min heap data structure. Building a min heap takes O(n) time and every extract min operation takes O(Logn) time (See <a href=\"http:\/\/courses.csail.mit.edu\/6.006\/spring11\/lectures\/lec24.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">this<\/a>).<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>PseudoCode:<\/strong><br \/>\nThe following pseudocode doesn\u2019t use heap. It simply sort the array.<\/p>\n<pre><strong>sweepLineIntersection(Points[0..2n-1]):<\/strong>\r\n<strong>1.<\/strong> Sort Points[] from left to right (according to x coordinate)\r\n\r\n<strong>2.<\/strong> Create an empty Self-Balancing BST T. It will contain all active line \r\n   Segments ordered by y coordinate.\r\n\r\n\/\/ Process all 2n points \r\n<strong>3.<\/strong> for i = 0 to 2n-1\r\n\r\n    \/\/ If this point is left end of its line  \r\n    if (Points[i].isLeft) \r\n       T.insert(Points[i].line())  \/\/ Insert into the tree\r\n\r\n       \/\/ Check if this points intersects with its predecessor and successor\r\n       if ( doIntersect(Points[i].line(), T.pred(Points[i].line()) )\r\n         return true\r\n       if ( doIntersect(Points[i].line(), T.succ(Points[i].line()) )\r\n         return true\r\n\r\n    else  \/\/ If it's a right end of its line\r\n       \/\/ Check if its predecessor and successor intersect with each other\r\n       if ( doIntersect(T.pred(Points[i].line(), T.succ(Points[i].line()))\r\n         return true\r\n       T.delete(Points[i].line())  \/\/ Delete from tree\r\n\r\n<strong>4. <\/strong>return False<\/pre>\n<p><strong>Example:<\/strong><br \/>\nLet us consider the following example taken from <a href=\"http:\/\/www.eecs.wsu.edu\/~cook\/aa\/lectures\/l25\/node10.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. \u00a0There are 5 line segments 1, 2, 3, 4 and 5. \u00a0The dotted green lines show sweep lines.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25930\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sweepline.png\" alt=\"Given n line segments, find if any two segments intersect\" width=\"513\" height=\"355\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sweepline.png 513w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/sweepline-300x208.png 300w\" sizes=\"(max-width: 513px) 100vw, 513px\" \/><\/p>\n<p>Following are steps followed by the algorithm. \u00a0All points from left to right are processed one by one. We maintain a self-balancing binary search tree.<\/p>\n<p><em>Left end point of line segment\u00a01 is processed<\/em>: 1 is inserted into the Tree. The tree contains 1. \u00a0\u00a0No intersection.<\/p>\n<p><em>Left end point of line segment\u00a0\u00a02 is processed<\/em>: \u00a0Intersection of 1 and 2 is checked. 2 is inserted into the Tree. No intersection.\u00a0The tree contains 1, 2.<\/p>\n<p><em>Left end point of line segment 3\u00a0is processed: <\/em>\u00a0Intersection of \u00a03 with 1 is checked. \u00a0No intersection.\u00a03 is inserted into the Tree. The tree contains \u00a02, 1, 3.<\/p>\n<p><em>Right end point of line segment\u00a01 is processed:<\/em> 1 is deleted from the Tree. \u00a0Intersection of 2 and 3 is checked. \u00a0Intersection of 2 and 3 is reported. \u00a0The tree contains \u00a02, 3. Note that the above pseudocode returns at this point. We can continue from here to report all intersection points.<\/p>\n<p><em>Left end point of line segment\u00a0\u00a04 is processed<\/em>: \u00a0\u00a0Intersections of \u00a0line 4 with lines 2 and 3 are checked. \u00a0No intersection. 4 is inserted into the Tree. The tree contains \u00a02, 4, 3.<\/p>\n<p><em>Left end point of line segment\u00a05 is processed<\/em>: \u00a0\u00a0Intersection of \u00a05 with 3 is checked. \u00a0No intersection. 4 is inserted into the Tree. The tree contains \u00a0\u00a02,\u00a04, 3, 5.<\/p>\n<p><em>Right end point of line segment\u00a05 is processed:\u00a0<\/em>5 is deleted from the Tree. \u00a0 The tree contains \u00a02, 4, 3.<\/p>\n<p><em>Right end point of line segment\u00a04 is processed:<\/em>\u00a04 is deleted from the Tree. \u00a0 The tree contains \u00a02, 4, 3. \u00a0\u00a0Intersection of 2 with\u00a03 is checked. \u00a0Intersection of 2 with 3 is reported. \u00a0The tree contains \u00a02, 3. Note that the intersection of 2 and 3 is reported again. We can add some logic to check for duplicates.<\/p>\n<p><em>Right end point of line segment\u00a0\u00a02 and 3 are processed:\u00a0 <\/em>\u00a0Both are deleted from tree and tree becomes empty.<\/p>\n<p><strong>Time Complexity:<\/strong> The first step is sorting which takes O(nLogn) time. The second step process 2n points and for processing every point, it takes O(Logn) time. Therefore, overall time complexity is O(nLogn)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>\u00a0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given n line segments, find if any two segments intersect &#8211; Geometric Algorithm &#8211; We have discussed the problem to detect if two given line segments.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73905,83575],"tags":[76178,76176,76175,76177,76181,76180,76179,76182],"class_list":["post-25916","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-geometric-algorithms","category-line-segments","tag-check-if-two-lines-intersect-java","tag-intersection-of-two-line-segments-algorithm","tag-line-segment-intersection-c","tag-line-segment-intersection-problem","tag-line-sweep-algorithm-tutorial","tag-sweep-line-algorithm-c","tag-sweep-line-algorithm-for-segment-intersection","tag-sweep-line-algorithm-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25916"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25916\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}