GEOMETRIC APPLICATIONS OF BSTS - Cs.princeton.edu

1y ago
14 Views
2 Downloads
7.67 MB
43 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Victor Nelms
Transcription

AlgorithmsR OBERT S EDGEWICK K EVIN W AYNEG EOMETRIC A PPLICATIONSOFBST S‣ 1d range search‣ line segment intersectionAlgorithmsF O U R T H‣ k-d treesE D I T I O N‣ contextR OBERT S EDGEWICK K EVIN W AYNEhttps://algs4.cs.princeton.eduLast updated on 3/14/22 1:23 PM

OverviewThis lecture. Intersections among geometric objects.2d orthogonal range searchline segment intersectionApplications. CAD, games, movies, virtual reality, databases, GIS, . Efficient solutions. Binary search trees (and extensions).2

OverviewThis lecture. Only the tip of the iceberg.medical imagingVoronoi tessellationfluid flow3

G EOMETRIC A PPLICATIONS‣ 1d range search‣ line segment intersectionAlgorithmsR OBERT S EDGEWICK K EVIN W AYNEhttps://algs4.cs.princeton.edu‣ k-d treesOFBST S

1d range searchExtension of ordered symbol table.・Insert key–value pair.・Search for key k.・Delete key k (and associated value).・Range search: find all keys between k and k .・Range count: number of keys between k and k .1212Application. Database queries.insert BBinsert DB DGeometric interpretation.insert AA B D・・Find/count points in a given 1d interval.insert IA B D Iinsert HA B D H Iinsert FA B D F H Iinsert PA B D F H I Psearch G to KH Icount G to K2Keys are point on a line.5

Geometric applications of BSTs: quiz 1Suppose that the keys are stored in a sorted array of length n.What is the worst-case running time for range search as a function of both m and n ?number of matching keysA.Θ(log m)B.Θ(log n)C.Θ(log n m)D.Θ(m n)binary search scan for matches(à la Autocomplete)log n and m are incomparable(neither is a lower-order term)6

1d range search: elementary implementationsUnordered list. Slow insert; slow range search.Sorted array. Slow insert; fast range search.order of growth of running time for 1d range searchdata structureinsertrange countrange searchunordered listnnnsorted arraynlog nlog n mgoallog nlog nlog n mm number of keys that matchn number of keys7

1d range search: BST implementation1d range search. Find all keys between k1 and k2.・Recursively find all keys in left subtree (if any could fall in range).・Check key in current node.・Recursively find all keys in right subtree (if any could fall in range).range search [D Q]UEEXGHARLMCSHPGMLPassuming BST is balancedProposition. Takes Θ(log n m) time in the worst case.Pf. Nodes examined { search path to k1 } { search path to k2 } { matches }.Θ(log n)Θ(log n)Θ(m)8

1d range search: summary of performanceUnordered list. Slow insert; slow range search.Sorted array. Slow insert; fast range search.BST. Fast insert; fast range search/count.order of growth of running time for 1d range searchdata structureinsertrange countrange searchunordered listnnnsorted arraynlog nlog n mbalanced BSTlog nlog nlog n muse rank() method(see precept)m number of keys that matchn number of keys9

G EOMETRIC A PPLICATIONS‣ 1d range search‣ line segment intersectionAlgorithms‣ k-d trees‣ contextR OBERT S EDGEWICK K EVIN W AYNEhttps://algs4.cs.princeton.eduOFBST S

Orthogonal line segment intersectionGiven n horizontal and vertical line segments, find all intersections.Quadratic algorithm. Check all pairs of line segments for intersection.11

Microprocessors and geometryEarly 1970s. microprocessor design became a geometric problem.・Very Large Scale Integration (VLSI).・Computer-Aided Design (CAD).Design-rule checking.・Certain wires cannot intersect.・Certain spacing needed between different types of wires.・Debugging line segment (or rectangle) intersection.12

Orthogonal line segment intersection: sweep-line algorithmNon-degeneracy assumption. All x- and y-coordinates are distinct.91083745261013

Orthogonal line segment intersection: sweep-line algorithmSweep vertical line from left to right. [ x-coordinates define events ]・Horizontal segment (left endpoint): insert y-coordinate into BST.・Horizontal segment (right endpoint): remove y-coordinate from BST.・Vertical segment: range search for interval of y-endpoints.910837452610y-coordinates(of horizontal lines that intersect sweep line)14

Orthogonal line segment intersection: sweep-line analysisProposition. The sweep-line algorithm takes Θ(n log n m) time in the worst caseto find all m intersections among n horizontal and vertical line segments.n log n and m are incomparable(neither is a lower-order term)Pf.・Sort x-coordinates.・Insert y-coordinates into BST.・Delete y-coordinates from BST.・Range searches in BST.[ n log n ][ n log n ][ n log n ][ n log n m ](log n m1) (log n m2) (log n m3) .Bottom line. Sweep line reduces 2d orthogonal line segment intersectionto 1d range search.15

Sweep-line algorithm: contextThe sweep-line algorithm is a powerful technique in computational geometry.Geometric intersection.・General line segment intersection.・Axis-aligned rectangle intersection.・.More problems.・Andrew’s algorithm for convex hull.・Fortune’s algorithm Voronoi diagram.・Scanline algorithm for rendering computer graphics.・.16

G EOMETRIC A PPLICATIONS‣ 1d range search‣ line segment intersectionAlgorithms‣ k-d trees‣ contextR OBERT S EDGEWICK K EVIN W AYNEhttps://algs4.cs.princeton.eduOFBST S

Two-dimensional orthogonal range searchExtension of ordered symbol-table to 2d keys.・Insert a 2d key.・Search for a 2d key.・2d orthogonal range search: find all keys that lie in a 2d range.・2d orthogonal range count: number of keys that lie in a 2d range.Applications. Networking, circuit design, databases, .Geometric interpretation.・Keys are point in the plane.・Find/count points in a given h-v rectangle.rectangle is axis-aligned18

Space-partitioning treesUse a tree to represent a recursive subdivision of 2d space.Grid. Divide space uniformly into squares.Quadtree. Recursively divide space into four quadrants.2d tree. Recursively divide space into two halfplanes.BSP tree. Recursively divide space into two regions.GridQuadtree2d treeBSP tree19

Space-partitioning trees: applicationsApplications.・Ray tracing.・Flight simulators.・N-body simulation.・Collision detection.・Astronomical databases.・Nearest neighbor search.・Adaptive mesh generation.・2d orthogonal range search.・Accelerate rendering in Doom.・Hidden surface removal and shadow casting.GridQuadtree2d treeBSP tree20

2d tree constructionRecursively partition plane into two halfplanes.HACFBICDFHGAEBEJIGJD21

Geometric applications of BSTs: quiz 2Where to insert point K in the 2d tree below?A.Left child of G.B.Left child of J.C.Right child of J.D.Right child of I.HAFICCAEBBDGKEFHGJIJD22

2d tree implementationData structure. BST, but alternate using x- and y-coordinates as key.・Even levels: compare x-coordinates.・Odd levels: compare y-coordinates.qpqpointsleft of pppointsright of ppointsabove qpointsbelow qodd levelseven levelsHAFICCAEBBDGKEFHGJIJD23

2d tree demo: range searchGoal. Find all points in a query rectangle.・Check if query rectangle contains point in node.・Recursively search left/bottom and right/top subtrees.・Optimization: prune subtree if it can’t contain a point in rectangle.HAqueryrectangleCFBICDFHGAEBEJIGJD24

2d tree demo: range searchGoal. Find all points in a query rectangle.・Check if query rectangle contains point in node.・Recursively search left/bottom and right/top subtrees.・Optimization: prune subtree if it can’t contain a point in rectangle.HAqueryrectangleCFBICDFHGAEBJEIGJDdone25

Geometric applications of BSTs: quiz 4Suppose we explore the right/top subtree before the left/bottom subtreein range search. What effect would it have on typical inputs?A.Returns wrong answer.B.Explores more nodes.C.Both A and B.D.Neither A nor B.26

Range search in a 2d tree analysisTypical case. Θ(log n m).Worst case (assuming tree is balanced). Θ( n m).HAqueryrectangleCFBICDFHGAEBEJIGJD27

2d tree demo: nearest neighborGoal. Find closest point to query point.HACFBIquerypointCDFHGAEBEJIGJD28

2d tree demo: nearest neighbor・Check distance from point in node to query point.・Recursively search left/bottom and right/top subtrees.・Optimization 1: prune subtree if it can’t contain a closer point.・Optimization 2: explore subtree toward the query point first.HACFBIquerypointCDFHGAEBEJIGJDnearest neighbor E29

Geometric applications of BSTs: quiz 6Suppose we always explore the left/bottom subtree before the right/top subtree innearest-neighbor search. What effect will it have on typical inputs?A.Returns wrong answer.B.Explores more nodes.C.Both A and B.D.Neither A nor B.30

Geometric applications of BSTs: quiz 7Which of the following is a worst-case input for nearest-neighbor search?A.C.B.D.31

Nearest neighbor search in a 2d tree analysisTypical case. Θ(log n).Worst case (even if tree is balanced). Θ(n).HACFBICDFHGAEBEJIGJDnearest neighbor E32

2d tree: implementationQ. How to implement a 2d-tree?each node corresponds to a rectangleA. Explicit node data type for binary tree.containing all points in its subtreeAesegissemn4tnprivate class KdTreeST Value {private Node root;Cprivate class Node{ABprivate Point2D p;private Value val;private Node left, right;private Node parent;Dprivate RectHV rect;A.}.}Cvery useful for 2D range searchBand nearest neighbor searchD33

G EOMETRIC A PPLICATIONS‣ 1d range search‣ line segment intersectionAlgorithms‣ k-d trees‣ contextR OBERT S EDGEWICK K EVIN W AYNEhttps://algs4.cs.princeton.eduOFBST S

K-d treeK-d tree. Recursively partition k-dimensional space into 2 halfspaces.Implementation. BST, but cycle through dimensions à la 2d trees.plevelpointswhose ithcoordinateis less than p’s i (mod k)pointswhose ithcoordinateis greater than p’sEfficient, simple data structure for processing k-dimensional data.・Widely used.・Adapts well to high-dimensional and clustered data.・Discovered by an undergrad in an algorithms class!35

Flocking birdsQ. Which “natural algorithm” do starlings, migrating geese, starlings,cranes, bait balls of fish, and flashing fireflies use to flock?https://www.youtube.com/watch?v XH-groCeKbE36

Flocking boids [Craig Reynolds, 1986]Boids. Three simple rules lead to complex emergent flocking behavior:・Flock centering (cohesion): move toward the center of mass of k nearest boids.・Direction matching (alignment): update velocity toward average velocity of k nearest boids.・Collision avoidance (separation): point away from k nearest boids.37

N-body simulationGoal. Simulate the motion of n particles, mutually affected by gravity.Brute force. For each pair of particles, compute force:G m1 m2F r2Running time. Time per step is Θ(n 2).https://www.youtube.com/watch?v ua7YlN4eL w38

Appel’s algorithm for n-body simulationKey idea. Suppose that a particle is in a galaxy far, far away from a cluster of particles.・Treat the cluster of particles as a single aggregate particle.・Compute force between particle and center of mass of aggregate.39

Appel’s algorithm for n-body simulation・Build 3d-tree with n particles as nodes.・Store center-of-mass of subtree in each node.・To compute total force acting on a particle, traverse tree, but stopas soon as distance from particle to subdivision is sufficiently large.SIAM J. ScI. STAT. COMPUT.Vol. 6, No. 1, January 19851985 Society for Industrial and Applied MathematicsO08AN EFFICIENT PROGRAM FOR MANY-BODY SIMULATION*ANDREW W. APPELAbstract. The simulation of N particles interacting in a gravitational force field is useful in astrophysics,but such simulations become costly for large N. Representing the universe as a tree structure with theparticles at the leaves and internal nodes labeled with the centers of mass of their descendants allows severalsimultaneous attacks on the computation time required by the problem. These approaches range fromalgorithmic changes (replacing an O(N ’) algorithm with an algorithm whose time-complexity is believedto be O(N log N)) to data structure modifications, code-tuning, and hardware modifications. The changesreduced the running time of a large problem (N 10,000) by a factor of four hundred. This paper describesboth the particular program and the methodology underlying such speedups.Impact.1. Introduction. Isaac Newton calculated the behavior of two particles interactingthrough the force of gravity, but he was unable to solve the equations for three particles.In this he was not alone [7, p. 634], and systems of three or more particles can besolved only numerically. Iterative methods are usually used, computing at each discretetime interval the force on each particle, and then computing the new velocities andRunningfor each perpositionstimeparticle.step is O(n log n) enables newA naive implementation of an iterative many-body simulator is computationallyvery expensive for large numbers of particles, where "expensive" means days of Cray-1time or a year of VAX time. This paper describes the development of an efficientresearch.40

Geometric applications of BSTsproblemexamplesolution1d range searchbinary search tree2d orthogonal linesweep line(reduces to 1d range search)segment intersection2d range searchk-d range search2d treek-d tree41

Copyright 2022 Robert Sedgewick and Kevin Wayne42

Copyright 2022 Robert Sedgewick and Kevin Wayne43

・2d orthogonal range search: find all keys that lie in a 2d range. ・2d orthogonal range count: number of keys that lie in a 2d range. Applications. Networking, circuit design, databases, . Geometric interpretation. ・Keys are point in the plane. ・Find/count points in a given h-v rectangle. Two-dimensional orthogonal range search 18

Related Documents:

The formula for the sum of a geometric series can also be written as Sn a 1 1 nr 1 r. A geometric series is the indicated sum of the terms of a geometric sequence. The lists below show some examples of geometric sequences and their corresponding series. Geometric Sequence Geometric Series 3, 9, 27, 81, 243 3 9 27 81 243 16, 4, 1, 1 4, 1 1 6 16 .

The first term in a geometric sequence is 54, and the 5th term is 2 3. Find an explicit form for the geometric sequence. 19. If 2, , , 54 forms a geometric sequence, find the values of and . 20. Find the explicit form B( J) of a geometric sequence if B(3) B(1) 48 and Ù(3) Ù(1) 9. Lesson 4: Geometric Sequences Unit 7: Sequences S.41

The first term in a geometric sequence is 54, and the 5th term is 2 3. Find an explicit form for the geometric sequence. 19. If 2, , , 54 forms a geometric sequence, find the values of and . 20. Find the explicit form B( J) of a geometric sequence if B(3) B(1) 48 and Ù(3) Ù(1) 9. Lesson 7: Geometric Sequences

been proven to be stable and effective and could significantly improve the geometric accuracy of optical satellite imagery. 2. Geometric Calibration Model and the Method of Calculation 2.1. Rigorous Geometric Imaging Model Establishment of a rigorous geometric imaging model is the first step of on-orbit geometric calibration

Introduction to Visualization and Computer Graphics, Tino Weinkauf, KTH Stockholm, Fall 2015 Geometric Modeling: Introduction Geometric Modeling is the computer-aided design and manipulation of geometric objects. (CAD) It is the basis for: computation of geometric properties rendering of geometric objects

Arithmetic & Geometric Sequences Worksheet Level 2: Goals: Identify Arithmetic and Geometric Sequences Find the next term in an arithmetic sequence Find the next term in a geometric sequence Practice #1 Does this pattern represent an arithmetic or geometric sequence? Explain. Find how many dots would be in the next figure?

Geometric Sequence In a geometric sequence, the ratio between consecutive terms is the same. This ratio is called the common ratio. Each term is found by multiplying the previous term by the common ratio. 1, 5, 25, 125, . . . Terms of a geometric sequence 5 5 5 Common ratio Exercises 11–16 EXAMPLE 1 Extending a Geometric Sequence

WHAT IS MY SECOND GRADE STUDENT LEARNING IN MODULE 1? Wit & Wisdom is our English curriculum. It builds knowledge of key topics in history, science, and literature through the study of excellent texts. By reading and responding to stories and nonfiction texts, we will build knowledge of the following topics: Module 1: A Season of Change Module 2: American West Module 3: Civil Rights Heroes .