Introduction to Algorithms

Slides:



Advertisements
Similar presentations
Mathematical Preliminaries
Advertisements

2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Introduction to Algorithms
Advanced Piloting Cruise Plot.
October 17, 2005 Copyright© Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms 6.046J/18.401J LECTURE9 Randomly built binary.
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J/SMA5503
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 17 L9.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 9 Prof. Charles E. Leiserson.
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Improved Shortest Path Algorithms for Nearly Acyclic Directed Graphs L. Tian and T. Takaoka University of Canterbury New Zealand 2007.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Ken C. K. Lee, Baihua Zheng, Huajing Li, Wang-Chien Lee VLDB 07 Approaching the Skyline in Z Order 1.
CS16: Introduction to Data Structures & Algorithms
Randomized Algorithms Randomized Algorithms CS648 1.
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Data Structures Using C++
ABC Technology Project
Quadratic Inequalities
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Two Segments Intersect?
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Introduction to Algorithms Red-Black Trees
Differential Forms for Target Tracking and Aggregate Queries in Distributed Networks Rik Sarkar Jie Gao Stony Brook University 1.
Constant, Linear and Non-Linear Constant, Linear and Non-Linear
Squares and Square Root WALK. Solve each problem REVIEW:
CSE 4101/5101 Prof. Andy Mirzaian Computational Geometry.
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Geometry Introduction
25 seconds left…...
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 12 Binary Search Trees
CSE Lecture 17 – Balanced trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Other Dynamic Programming Problems
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Bart Jansen 1.  Problem definition  Instance: Connected graph G, positive integer k  Question: Is there a spanning tree for G with at least k leaves?
Polygon Triangulation
Interval Graph Test.
Computational Geometry
INTERVAL TREE & SEGMENTATION TREE
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006.
I/O-Algorithms Lars Arge Fall 2014 September 25, 2014.
CS16: Introduction to Data Structures & Algorithms
I/O-Algorithms Lars Arge University of Aarhus March 1, 2005.
AALG, lecture 11, © Simonas Šaltenis, Range Searching in 2D Main goals of the lecture: to understand and to be able to analyze the kd-trees and.
Orthogonal Range Searching I Range Trees. Range Searching S = set of geometric objects Q = query object Report/Count objects in S that intersect Q Query.
UNC Chapel Hill M. C. Lin Point Location Reading: Chapter 6 of the Textbook Driving Applications –Knowing Where You Are in GIS Related Applications –Triangulation.
Mehdi Mohammadi March Western Michigan University Department of Computer Science CS Advanced Data Structure.
2IL50 Data Structures Fall 2015 Lecture 9: Range Searching.
Segment Trees Basic data structure in computational geometry. Computational geometry.  Computations with geometric objects.  Points in 1-, 2-, 3-, d-space.
CMPS 3130/6130 Computational Geometry Spring 2015
CMPS 3130/6130 Computational Geometry Spring 2017
KD Tree A binary search tree where every node is a
Orthogonal Range Searching and Kd-Trees
Computational Geometry Capter:1-2.1
Reporting (1-D) Given a set of points S on the line, preprocess them to build structure that allows efficient queries of the from: Given an interval I=[x1,x2]
CMPS 3130/6130 Computational Geometry Spring 2017
Presentation transcript:

Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 12 Prof. Erik Demaine

Computational geometry Algorithms for solving “geometric problems” in 2D and higher. Fundamental objects: Basic structures: © 2001 by Erik D. Demaine Introduction to Algorithms

Computational geometry Algorithms for solving “geometric problems” in 2D and higher. Fundamental objects: © 2001 by Erik D. Demaine Introduction to Algorithms

Orthogonal range searching Input: n points in d dimensions • E.g., representing a database of n records each with d numeric fields Query: Axis-aligned box (in 2D, a rectangle) • Report on the points inside the box: • Are there any points? • How many are there? • List the points. © 2001 by Erik D. Demaine Introduction to Algorithms

Orthogonal range searching Input: n points in d dimensions Query: Axis-aligned box (in 2D, a rectangle) • Report on the points inside the box Goal: Preprocess points into a data structure to support fast queries • Primary goal: Static data structure • In 1D, we will also obtain a dynamic data structure supporting insert and delete © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms 1D range searching In 1D, the query is an interval: First solution using ideas we know: • Interval trees • Represent each point x by the interval [x, x]. • Obtain a dynamic structure that can list k answers in a query in O(k lg n) time. © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms 1D range searching In 1D, the query is an interval: Second solution using ideas we know: • Sort the points and store them in an array • Solve query by binary search on endpoints. • Obtain a static structure that can list k answers in a query in O(k + lg n) time. Goal: Obtain a dynamic structure that can list © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms 1D range searching In 1D, the query is an interval: New solution that extends to higher dimensions: • Balanced binary search tree • New organization principle: Store points in the leaves of the tree. • Internal nodes store copies of the leaves to satisfy binary search property: • Node x stores in key[x] the maximum key of any leaf in the left subtree of x. © 2001 by Erik D. Demaine Introduction to Algorithms

Example of a 1D range tree © 2001 by Erik D. Demaine Introduction to Algorithms

Example of a 1D range tree © 2001 by Erik D. Demaine Introduction to Algorithms

Example of a 1D range query © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms General 1D range query © 2001 by Erik D. Demaine Introduction to Algorithms

Pseudocode, part 1: Find the split node 1D-RANGE-QUERY(T, [x1, x2]) w ← root[T] while w is not a leaf and (x2 ≒ key[w] or key[w] < x1) do if x2 ≒ key[w] else w ← right[w] then w ← left[w] >w is now the split node [traverse left and right from w and report relevant subtrees] © 2001 by Erik D. Demaine Introduction to Algorithms

Pseudocode, part 2: Traverse left and right from split node 1D-RANGE-QUERY(T, [x1, x2]) [find the split node] >w is now the split node if w is a leaf then output the leaf w if x1 ≒ key[w] ≒ x2 else v ← left[w] >Left traversal while v is not a leaf do if x1 ≒ key[v] then output the subtree rooted at right[v] v ← left[v] else v ← right[v] output the leaf v if x1 ≒ key[v] ≒ x2 [symmetrically for right traversal] © 2001 by Erik D. Demaine Introduction to Algorithms

Analysis of 1D-RANGE-QUERY Query time: Answer to range query represented by O(lg n) subtrees found in O(lg n) time. Thus: • Can test for points in interval in O(lg n) time. • Can count points in interval in O(lg n) time if we augment the tree with subtree sizes. • Can report the first k points in interval in O(k + lg n) time. Space: O(n) Preprocessing time: O(n lg n) © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms 2D range trees Store a primary 1D range tree for all the points based on x-coordinate. Thus in O(lg n) time we can find O(lg n) subtrees representing the points with proper x-coordinate. How to restrict to points with proper y-coordinate? © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms 2D range trees Idea: In primary 1D range tree of x-coordinate, every node stores a secondary 1D range tree based on y-coordinate for all points in the subtree of the node. Recursively search within each. © 2001 by Erik D. Demaine Introduction to Algorithms

Analysis of 2D range trees Preprocessing time: O(n lg n) Space: The secondary trees at each level of the primary tree together store a copy of the points. Also, each point is present in each secondary tree along the path from the leaf to the root. Either way, we obtain that the space is O(n lg n). © 2001 by Erik D. Demaine Introduction to Algorithms

d-dimensional range trees Each node of the secondary y-structure stores a tertiary z-structure representing the points in the subtree rooted at the node, etc. © 2001 by Erik D. Demaine Introduction to Algorithms

Primitive operations: Crossproduct Crossproduct v1 × v2 = x1 y2 – y1 x2 = |v1| |v2| sin θ . Thus, sign(v1 × v2) = sign(sin θ) > 0 if θ convex, < 0 if θ reflex, = 0 if θ borderline. © 2001 by Erik D. Demaine Introduction to Algorithms

Primitive operations: Orientation test Given three points p1, p2, p3 are they • in clockwise (cw) order, • in counterclockwise (ccw) order, or • collinear? (p2 – p1) × (p3 – p1) > 0 if ccw < 0 if cw = 0 if collinear © 2001 by Erik D. Demaine Introduction to Algorithms

Primitive operations: Sidedness test Given three points p1, p2, p3 are they • in clockwise (cw) order, • in counterclockwise (ccw) order, or • collinear ? Let L be the oriented line from p1to p2. Equivalently, is the point p3 • right of L, • left of L, or • on L? © 2001 by Erik D. Demaine Introduction to Algorithms

Line-segment intersection © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms Sweep-line algorithm • Sweep a vertical line from left to right (conceptually replacing x-coordinate with time). • Maintain dynamic set S of segments that intersect the sweep line, ordered (tentatively) by y-coordinate of intersection. • Order changes when • Key event points are therefore segment endpoints. © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms Sweep-line algorithm Process event points in order by sorting segment endpoints by x-coordinate and looping through: • For a left endpoint of segment s: • Add segment s to dynamic set S. • Check for intersection between s and its neighbors in S. • For a right endpoint of segment s: • Remove segment s from dynamic set S. • Check for intersection between the neighbors of s in S. © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms Analysis Use red-black tree to store dynamic set S. Total running time: O(n lg n). © 2001 by Erik D. Demaine Introduction to Algorithms

Introduction to Algorithms Correctness Theorem: If there is an intersection, the algorithm finds it. Proof: Let X be the leftmost intersection point. Assume for simplicity that • only two segments s1, s2 pass through X, and • no two points have the same x-coordinate. At some point before we reach X, s1 and s2 become consecutive in the order of S. Either initially consecutive when s1 or s2 inserted, or became consecutive when another deleted. © 2001 by Erik D. Demaine Introduction to Algorithms