Geometric Applications of Binary Search Trees

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

31 Terms

1
New cards

Geometric Applications of Binary Search Trees

  • Intersection of geometric objects in 1 and 2 dimensional spaces

  • Applications: CAD, games, movies, virtual reality, databases, GIS,…

<ul><li><p><span>Intersection of geometric objects in 1 and 2 dimensional spaces</span></p></li><li><p><span>Applications: CAD, games, movies, virtual reality, databases, GIS,…</span></p></li></ul><p></p>
2
New cards

Intersection of Geometric Objects

  • Orthogonal Line Segment and Rectangle Intersection

  • 1-D & 2-D Range Search, 1-D Interval Search

3
New cards

Orthogonal Line Segment and Rectangle Intersection

Computer Assisted Design (CAD), and CAD for Very Large Scale Integration (VLSI)

  • For example, designing electronic chip can require:

  • Wires must not intersect

  • Wires must have some minimum distance between them

CAD design software helps with checking that

<p>Computer Assisted Design (CAD), and CAD for Very Large Scale Integration (VLSI)</p><ul><li><p><span>For example, designing electronic chip can require:</span></p></li><li><p><span>Wires must not intersect</span></p></li><li><p><span>Wires must have some minimum distance between them</span></p></li></ul><p>CAD design software helps with checking that</p>
4
New cards

1-D & 2-D Range Search, 1-D Interval Search

Applications:

  • Databases

  • Geographic information systems (GIS)

  • Games and physic simulations

    • E.g., via collision detection

<p>Applications:</p><ul><li><p><span>Databases</span></p></li><li><p><span>Geographic information systems (GIS)</span></p></li><li><p><span>Games and physic simulations</span></p><ul><li><p><span>E.g., via collision detection</span></p></li></ul></li></ul><p></p>
5
New cards

Naïve Solutions

knowt flashcard image
6
New cards

1-Dimension Range Search

Use a Dictionary ADT extension:

  • void insert(Key key, Value value)

  • Value Search(Key k)

  • void delete(Key k)

  • List rangeSearch(Key keyLeft, Key keyRight) 

  • int rangeCount(Key keyLeft, Key keyRight)

More generally (beyond just geometric search), rangeSearch and rangeCount can be used for database queries

7
New cards

1-Dimension Range Search Worst Case Time Complexity

  • rangeCount: 𝑶(𝐥og 𝒏) time

    • Operation rank() takes 𝑂(log 𝑛) time

    • rangeCount uses rank() twice (four times in code)

  • rangeSearch: 𝑶(𝑹 + 𝐥og 𝒏) time, where 𝑹 is the number of keys contained in range

    • Time is a factor of the search path to low key value, as well as to the search path to high key value

    • And finally factor of all keys in range

  • Compare to the naïve solution with 𝑂(𝑛) worst-case time complexity

8
New cards

Orthogonal Line Segment Intersection

  • Given 𝑁 line segments (either horizontal or vertical), find all intersections

  • Non-degenerate segments: all 𝑥 and 𝑦 coordinates are different

  • 2D problem seems hard, but we have just solved a 1-D problem: 1-D Range Search

    • Can we reduce our 2-D problem to the 1-D range search?

      • Sweep-Line Algorithm

    • Transform 2-D problem into a sequence of 1-D range search problem instances

<ul><li><p><span>Given </span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> line segments (either horizontal or vertical), find all intersections</span></p></li><li><p><span>Non-degenerate segments: all </span><span style="font-family: &quot;Cambria Math&quot;">𝑥</span><span> and </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span> coordinates are different</span></p></li><li><p><span>2D problem seems hard, but we have just solved a 1-D problem: 1-D Range Search</span></p><ul><li><p><span>Can we reduce our 2-D problem to the 1-D range search?</span></p><ul><li><p>Sweep-Line Algorithm</p></li></ul></li><li><p>Transform 2-D problem into a sequence of 1-D range search problem instances</p></li></ul></li></ul><p></p>
9
New cards

Sweep-Line Algorithm

  • Create a BST

  • Insert key 𝑦1

  • Range search (𝑎1, 𝑎2)

  • Insert key 𝑦2

  • Range search (𝑏1, 𝑏2)

  • Insert key 𝑦3

  • Insert key 𝑦4

  • Insert key 𝑦

  • rangeSearch(𝑐1, 𝑐2)

  • Remove key 𝑦1

Common technique for solving geometrical problems:

  • Convex hull,

  • Convex object intersection,

  • Voronoi diagrams …

The concept is also used for generating slices in additive 3D printing

<ul><li><p><span>Create a BST</span></p></li><li><p><span>Insert key </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span>1</span></p></li><li><p><span>Range search (</span><span style="font-family: &quot;Cambria Math&quot;">𝑎</span><span>1, </span><span style="font-family: &quot;Cambria Math&quot;">𝑎</span><span>2)</span></p></li><li><p><span>Insert key </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span>2</span></p></li><li><p><span>Range search (</span><span style="font-family: &quot;Cambria Math&quot;">𝑏</span><span>1, </span><span style="font-family: &quot;Cambria Math&quot;">𝑏</span><span>2)</span></p></li><li><p><span>Insert key </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span>3</span></p></li><li><p><span>Insert key </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span>4</span></p></li><li><p><span>Insert key </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span>5&nbsp;</span></p></li><li><p><span>rangeSearch(</span><span style="font-family: &quot;Cambria Math&quot;">𝑐</span><span>1, </span><span style="font-family: &quot;Cambria Math&quot;">𝑐</span><span>2)</span></p></li><li><p><span>Remove key </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span>1</span></p></li><li><p><span>…</span></p></li></ul><p><span>Common technique for solving geometrical problems:</span></p><ul><li><p><span>Convex hull,</span></p></li><li><p><span>Convex object intersection,</span></p></li><li><p><span>Voronoi diagrams …</span></p></li></ul><p><span>The concept is also used for generating slices in additive 3D printing</span></p>
10
New cards

Transforming 2-D problem into a sequence of 1-D range search problem instances

  • At most:

    • N rangeSearch,

    • N insertions,

    • N deletions

  • Thus it takes time 𝑂(𝑁 log 𝑁 + 𝑅), for R intersections

  • Worst-case time complexity of intersection: 𝑂(𝑁 log 𝑁 + 𝑅), when there are 𝑅 intersections

<ul><li><p><span>At most:</span></p><ul><li><p><span>N rangeSearch,</span></p></li><li><p><span>N insertions,</span></p></li><li><p><span>N deletions</span></p></li></ul></li><li><p><span>Thus it takes time </span><span style="font-family: &quot;Cambria Math&quot;">𝑂</span><span>(</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> log </span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> + </span><span style="font-family: &quot;Cambria Math&quot;">𝑅</span><span>), for R intersections</span></p></li><li><p><span>Worst-case time complexity of intersection: </span><span style="font-family: &quot;Cambria Math&quot;">𝑂</span><span>(</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> log </span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> + </span><span style="font-family: &quot;Cambria Math&quot;">𝑅</span><span>), when there are </span><span style="font-family: &quot;Cambria Math&quot;">𝑅</span><span> intersections</span></p></li></ul><p></p>
11
New cards

Interval Search Tree

Operations for interval search tree (generalizing search trees)

  • void insert(Key low, Key high, Value value)

  • Value search(Key low, Key high)

  • void delete(Key low, Key high)

  • LinkedList intersects(Key low, Key high)

In interval search tree,

left endpoint is the BST key

And nodes store max endpoint in subtree (rooted at the node)

Non-degenerate intervals: no two intervals have the same left endpoint

<p>Operations for interval search tree (generalizing search trees)</p><ul><li><p><span>void insert(Key low, Key high, Value value)</span></p></li><li><p><span>Value search(Key low, Key high)</span></p></li><li><p><span>void delete(Key low, Key high)</span></p></li><li><p><span>LinkedList intersects(Key low, Key high)</span></p></li></ul><p>In interval search tree,</p><p>left endpoint is the <span>BST key</span></p><p>And nodes store<span> max endpoint in subtree</span> (rooted at the node)</p><p>Non-degenerate intervals: no two intervals have the same left endpoint</p>
12
New cards

Insertion - Interval Search Tree

void insert(Key low, Key high, Value value):

  • Insert the new node using low as the BST key

  • Update max in each node on the search path

insert(26,40, *)

13
New cards

Intersection - Interval Search Tree

Value intersectionSingle(Key low, Key high):

  • If interval in node intersects (low,high)-interval, return that interval

  • Else if left child (and subtree) is null, recurse on right child,

  • Else if max endpoint of left child (i.e., in left subtree) < low, recurse on right

  • Else recurse on left subtree

intersectionSingle(20,21)

14
New cards

Intersection Analysis - Interval Search Tree

Proof sketch:

  • If recurse right, there are no intervals in left subtree that recurse

    • Max (in left subtree) < low 

    • For any interval (a,b) in left subtree, 𝑏 ≤ max < 𝑙ow

    • Thus for any interval (a,b) in left subtree, (a,b) does not intersect (low,high)

  • If recurse left, there is either an intersection in the left subtree or no intersection on either subtree

    • Recursing on left implies 𝑙ow ≤ 𝑚ax (for max in left subtree)

    • If no intersection on left subtree, then (c, max) does not intersect (low,high), i.e., ℎ𝑖gh < 𝑐

    • Since we have a BST ordered by intervals’ left endpoints, for any interval (a,b), 𝑐𝑎

    • Thus ℎ𝑖gh < 𝑐𝑎, and there are no intersections on the right subtree

Worst-case Time Complexity of intersectionSingle: 𝑂(log 𝑁)

Worst-case Time Complexity of intersection: 𝑂(𝑅 log 𝑁)

15
New cards

Orthogonal Rectangle Insertion

  • Find all intersections among a set of 𝑁 orthogonal rectangles

  • Non-degenerate: All 𝑥 and 𝑦 coordinates are distance

  • To avoid naïve 𝑂(𝑁^2) time solution, transform/reduce orthogonal rectangle insertion to 1D interval search problem instances

    • Use Sweep-line algorithm

  • Interval searches when there are 𝑅 intersections: 𝑂(𝑁 + 𝑅 log 𝑛) worst-case time complexity

  • Compared to orthogonal line segment intersection -> extra complexity due to the interval search tree implementation

<ul><li><p><span>Find all intersections among a set of </span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> orthogonal rectangles</span></p></li><li><p><span>Non-degenerate: All </span><span style="font-family: &quot;Cambria Math&quot;">𝑥</span><span> and </span><span style="font-family: &quot;Cambria Math&quot;">𝑦</span><span> coordinates are distance</span></p></li><li><p><span>To avoid naïve </span><span style="font-family: &quot;Cambria Math&quot;">𝑂</span><span>(</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span>^2) time solution, transform/reduce orthogonal rectangle insertion to 1D interval search problem instances</span></p><ul><li><p><span>Use Sweep-line algorithm</span></p></li></ul></li><li><p><span>Interval searches when there are </span><span style="font-family: &quot;Cambria Math&quot;">𝑅</span><span> intersections: </span><span style="font-family: &quot;Cambria Math&quot;">𝑂</span><span>(</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> + </span><span style="font-family: &quot;Cambria Math&quot;">𝑅</span><span> log </span><span style="font-family: &quot;Cambria Math&quot;">𝑛</span><span>) worst-case time complexity</span></p></li><li><p><span>Compared to orthogonal line segment intersection -&gt; extra complexity due to the interval search tree implementation</span></p></li></ul><p></p>
16
New cards

2-Dimension Range Search

Problem definition:

  • Given 𝑁 points on 2D space, and a 2D range (here, an axis-aligned rectangle), output the points contained in the range

  • (Count version): … output the number of points contained in the range

Extend the Dictionary ADT to 2D keys:

  • Insert a 2D key

  • Delete a 2D key

  • Search for a 2D key

  • rangeSearch: find all keys that lie in a 2D range

  • rangeCount: number of keys that lie in a 2D range

For our use, Keys are points in the plane and we want to find/count points in a given axis-aligned rectangle

17
New cards

Grids

  • Extend Dictionary ADT to 2D keys via a grid implementation (array analogue):

    • Divide space into an M-by-M grid (i.e., decide on grid “granularity”)

    • Create list of points contained in each square

    • Use 2D array to directly index each square

    • Insert(x,y): add (x,y) to list for corresponding square

    • rangeSearch: examine only squares that intersect 2D range query

    • To compute intersecting squares - Use top right and bottom left points defining the range query

  • Space: 𝑀^2 + 𝑁

  • Time: 1 + 𝑁/𝑀^2 per square examined, on average

  • Grid square size impacts performance:

    • Small size (bigger 𝑀^2) wastes space

    • Large size means you have many points per square (on average)

    • Good tradeoff: √𝑁 x √𝑁 grid

      • For √𝑁 x √𝑁 grid, if points are evenly distributed, then:

        • Initializing the data structure takes N space 

        • Inserting a point takes 𝑂(1) time

        • Range Search also takes 𝑂(1) time because 1 point per square

  • In practice: Clustering is a common behaviour in geometric (& other) data

  • Implications here: Lists for some square are too long

  • Can we design a data structure that adapts to data density?

<ul><li><p><span>Extend Dictionary ADT to 2D keys via a grid implementation (array analogue):</span></p><ul><li><p><span>Divide space into an M-by-M grid (i.e., decide on grid “granularity”)</span></p></li><li><p><span>Create list of points contained in each square</span></p></li><li><p><span>Use 2D array to directly index each square</span></p></li><li><p><span>Insert(x,y): add (x,y) to list for corresponding square</span></p></li><li><p><span>rangeSearch: examine only squares that intersect 2D range query</span></p></li><li><p><span>To compute intersecting squares - Use top right and bottom left points defining the range query</span></p></li></ul></li><li><p><span>Space: </span><span style="font-family: &quot;Cambria Math&quot;">𝑀</span><span>^2 + </span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span></p></li><li><p><span>Time: 1 + </span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span>/</span><span style="font-family: &quot;Cambria Math&quot;">𝑀</span><span>^2 per square examined, on average</span></p></li><li><p><span>Grid square size impacts performance:</span></p><ul><li><p><span>Small size (bigger </span><span style="font-family: &quot;Cambria Math&quot;">𝑀</span><span>^2) wastes space</span></p></li><li><p><span>Large size means you have many points per square (on average)</span></p></li><li><p><span>Good tradeoff: √</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> x √</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> grid</span></p><ul><li><p><span>For √</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> x √</span><span style="font-family: &quot;Cambria Math&quot;">𝑁</span><span> grid, if points are evenly distributed, then:</span></p><ul><li><p><span>Initializing the data structure takes N space&nbsp;</span></p></li><li><p><span>Inserting a point takes </span><span style="font-family: &quot;Cambria Math&quot;">𝑂</span><span>(1) time</span></p></li><li><p><span>Range Search also takes </span><span style="font-family: &quot;Cambria Math&quot;">𝑂</span><span>(1) time because 1 point per square</span></p></li></ul></li></ul></li></ul></li><li><p><span>In practice: Clustering is a common behaviour in geometric (&amp; other) data</span></p></li><li><p><span>Implications here: Lists for some square are too long</span></p></li><li><p><span>Can we design a data structure that adapts to data density?</span></p></li></ul><p></p>
18
New cards

Clustering

  • In practice: Clustering is a common behaviour in geometric (& other) data

  • In:

    • Human population data

    • Other geospatial data

      • Cultural points of interest

      • Rental houses

      • Shops, …

    • Also in data used for machine learning

19
New cards

Space-Partitioning Trees

Rather than dividing the space evenly into squares, you can divide in several other ways:

  • Into two half spaces (not necessarily even sized)

  • Into four squares (even sized)

  • Into two regions

<p>Rather than dividing the space evenly into squares, you can divide in several other ways:</p><ul><li><p><span>Into two half spaces (not necessarily even sized)</span></p></li><li><p><span>Into four squares (even sized)</span></p></li><li><p><span>Into two regions</span></p></li></ul><p></p>
20
New cards

2D Tree Construction

Recursively partition plane into two halfplanes. Create 2D tree at same time

  • Even level points partition in two subsets: to their left, and to their right, respectively

  • Odd level points partition in two subsets: below them, and above them, respectively

<p>Recursively partition plane into two halfplanes. Create 2D tree at same time</p><ul><li><p><span>Even level points partition in two subsets: to their left, and to their right, respectively</span></p></li><li><p><span>Odd level points partition in two subsets: below them, and above them, respectively</span></p></li></ul><p></p>
21
New cards

2D Tree Construction Analysis

  • Space: Θ(𝑁)

  • Worst case Time: 𝑂(𝑁 log 𝑁)

22
New cards

2D Tree Range Search

Find all points in a query axis-aligned rectangle:

  • Check if root point is in the query rectangle

  • Check left/bottom recursively (if rectangle extends into that part)

  • Check right/above recursively (if rectangle extends into that part)

23
New cards

2D Tree Range Search Analysis

  • Average (Expected) case: 𝑂(𝑅 + log 𝑁) time

  • Worst case: 𝑂(𝑅 + 𝑁) time

24
New cards

2D Tree Nearest Neighbour

Find closest point to query point

  • Check distance from point 1 to query point

  • If left/bottom could contain a closer point, recurse left

  • If right/top could contain a closer point, recurse right

Output current closest point to query point

25
New cards

2D Tree Nearest Neighbour Analysis

  • Average (Expected) case: 𝑂(log 𝑁) time

  • Worst case: 𝑂(𝑁) time

26
New cards

KD Trees

Extension of 2D trees to more dimensions is widely used

  • Good for high-dimensional and clustered data

  • Can be used for N-body simulation (physics)

3D tree example:

  • 𝑥-dim

  • 𝑦-dim

  • 𝑧-dim

27
New cards

Quadtree Construction

Recursively partition plane into four squares

  • Each square holds up to some 𝑘 = 2 points,

  • Any square that has more points is split up into four sub squares

  • Tree structure follows the partition structure

In quadtrees, regular partition that adapts to point density

Can be used on images, and pixels take the “role of points”

  • Compression of images

  • Check connected components in image (image processing)

<p>Recursively partition plane into four squares</p><ul><li><p><span>Each square holds up to some </span><span style="font-family: &quot;Cambria Math&quot;">𝑘</span><span> = 2 points,</span></p></li><li><p><span>Any square that has more points is split up into four sub squares</span></p></li><li><p><span>Tree structure follows the partition structure</span></p></li></ul><p>In quadtrees, regular partition that adapts to point density</p><p>Can be used on images, and pixels take the “role of points”</p><ul><li><p><span>Compression of images</span></p></li><li><p><span>Check connected components in image (image processing)</span></p></li></ul><p></p>
28
New cards

Replacing BSTs with Skip Lists

  • Skip lists: probabilistic data structures

  • Another way to implement Dictionary ADT with fast insertion, deletion and lookup

  • Main idea: Multiple layers of linked lists over 16 elements

Skip lists contains log1/p (𝑛) lists:

  • Layer 1: Bottom layer & a simple linked list (here, over 16 elements)

  • Layer 2: Each element of layer 1 is in layer 2 with probability 𝑝 (here, 𝑝 = 1/4 )

  • Layer 3: Each element of layer 2 is in layer 3 with probability 𝑝

<ul><li><p><span>Skip lists: probabilistic data structures</span></p></li><li><p><span>Another way to implement Dictionary ADT with fast insertion, deletion and lookup</span></p></li><li><p><span>Main idea: Multiple layers of linked lists over 16 elements</span></p></li></ul><p><span>Skip lists contains log<sub>1/p</sub> (</span><span style="font-family: &quot;Cambria Math&quot;">𝑛</span><span>) lists:</span></p><ul><li><p><span>Layer 1: Bottom layer &amp; a simple linked list (here, over 16 elements)</span></p></li><li><p><span>Layer 2: Each element of layer 1 is in layer 2 with probability </span><span style="font-family: &quot;Cambria Math&quot;">𝑝</span><span> (here, </span><span style="font-family: &quot;Cambria Math&quot;">𝑝</span><span> = 1/4 )</span></p></li><li><p><span>Layer 3: Each element of layer 2 is in layer 3 with probability </span><span style="font-family: &quot;Cambria Math&quot;">𝑝</span></p></li></ul><p></p>
29
New cards

Skip List Insertion Example

add node to layer 1 (bottom) list. Flip a coin with probability 𝑝. If tails, add node to layer 2 list, repeat for layer 3

30
New cards

Skip List Indexing Example

access 𝑖th element

  • As it is, slow: 𝑂(𝑛) time

  • But if you add the width of the link, then 𝑂(log 𝑛) time

31
New cards

Replacing BSTs with Skip Lists Advantages

Simple to implement and efficient on average

  • Average case complexity: 𝑂(log 𝑛 )

  • Worst-case complexity: 𝑂(𝑛)

  • Space complexity: 𝑂(𝑛 log 𝑛)

In practice, skips lists are used:

  • MemSQL

  • Discord

  • RocksDB

Good for concurrency, and in that context, has been a research focus

  • Concurrent? For example, when multiple threads access same data structure