SlideShare a Scribd company logo
kd-Trees
CMSC 420
kd-Trees
• Invented in 1970s by Jon Bentley
• Name originally meant “3d-trees, 4d-trees, etc”
where k was the # of dimensions
• Now, people say “kd-tree of dimension d”
• Idea: Each level of the tree compares against 1
dimension.
• Let’s us have only two children at each node
(instead of 2d)
kd-trees
• Each level has a “cutting
dimension”
• Cycle through the dimensions
as you walk down the tree.
• Each node contains a point
P = (x,y)
• To find (x’,y’) you only
compare coordinate from the
cutting dimension
- e.g. if cutting dimension is x,
then you ask: is x’ < x?
x
y
x
y
x
10,12
35,45
kd-tree example
x
y
x
y
5,25
50,30
70,70
30,40
(30,40)
(5,25)
(70,70)
(10,12)
(50,30)
(35,45)
insert: (30,40), (5,25), (10,12), (70,70), (50,30), (35,45)
insert(Point x, KDNode t, int cd) {
if t == null
t = new KDNode(x)
else if (x == t.data)
// error! duplicate
else if (x[cd] < t.data[cd])
t.left = insert(x, t.left, (cd+1) % DIM)
else
t.right = insert(x, t.right, (cd+1) % DIM)
return t
}
Insert Code
FindMin in kd-trees
• FindMin(d): find the point with the smallest value in
the dth dimension.
• Recursively traverse the tree
• If cutdim(current_node) = d, then the minimum
can’t be in the right subtree, so recurse on just the
left subtree
- if no left subtree, then current node is the min for tree
rooted at this node.
• If cutdim(current_node) ≠ d, then minimum could
be in either subtree, so recurse on both subtrees.
- (unlike in 1-d structures, often have to explore several
paths down the tree)
FindMin
60,80
70,70
50,50
1,10
35,90
x
y
x
y
10,30
25,40
51,75
(51,75)
55,1
(25,40)
(10,30)
(55,1)
(1,10)
(70,70)
(60,80)
(35,90)
FindMin(x-dimension):
(50,50)
FindMin
60,80
70,70
50,50
1,10
35,90
x
y
x
y
10,30
25,40
51,75
(51,75)
55,1
(25,40)
(10,30)
(55,1)
(1,10)
(70,70)
(60,80)
(35,90)
FindMin(y-dimension):
1,10
55,1
(50,50)
FindMin
60,80
70,70
50,50
1,10
35,90
x
y
x
y
10,30
25,40
51,75
(51,75)
55,1
(25,40)
(10,30)
(55,1)
(1,10)
(70,70)
(60,80)
(50,50)
(35,90)
FindMin(y-dimension): space searched
Point findmin(Node T, int dim, int cd):
// empty tree
if T == NULL: return NULL
// T splits on the dimension we’re searching
// => only visit left subtree
if cd == dim:
if t.left == NULL: return t.data
else return findmin(T.left, dim, (cd+1)%DIM)
// T splits on a different dimension
// => have to search both subtrees
else:
return minimum(
findmin(T.left, dim, (cd+1)%DIM),
findmin(T.right, dim, (cd+1)%DIM)
T.data
)
FindMin Code
Delete in kd-trees
Q P
A
Want to delete node A.
Assume cutting
dimension of A is cd
In BST, we’d
findmin(A.right).
Here, we have to
findmin(A.right, cd)
cd
cd B
Everything in Q has
cd-coord < B, and
everything in P has cd-
coord ≥ B
Delete in kd-trees --- No Right Subtree
• What is right subtree is
empty?
• Possible idea: Find the max
in the left subtree?
- Why might this not
work?
• Suppose I findmax(T.left)
and get point (a,b):
Q
(x,y)
x
cd (a,b)
(a,c)
It’s possible that T.left
contains another point
with x = a.
Now, our equal
coordinate invariant is
violated!
No right subtree --- Solution
• Swap the subtrees of node to
be deleted
• B = findmin(T.left)
• Replace deleted node by B
Q
(x,y)
x
cd (a,b)
(a,c)
Now, if there is another
point with x=a, it
appears in the right
subtree, where it should
Point delete(Point x, Node T, int cd):
if T == NULL: error point not found!
next_cd = (cd+1)%DIM
// This is the point to delete:
if x = T.data:
// use min(cd) from right subtree:
if t.right != NULL:
t.data = findmin(T.right, cd, next_cd)
t.right = delete(t.data, t.right, next_cd)
// swap subtrees and use min(cd) from new right:
else if T.left != NULL:
t.data = findmin(T.left, cd, next_cd)
t.right = delete(t.data, t.left, next_cd)
else
t = null // we’re a leaf: just remove
// this is not the point, so search for it:
else if x[cd] < t.data[cd]:
t.left = delete(x, t.left, next_cd)
else
t.right = delete(x, t.right, next_cd)
return t
Nearest Neighbor Searching in kd-trees
• Nearest Neighbor Queries are very common: given a point Q find the
point P in the data set that is closest to Q.
• Doesn’t work: find cell that would contain Q and return the point it
contains.
- Reason: the nearest point to P in space may be far from P in the
tree:
- E.g. NN(52,52):
60,80
70,70
50,50
1,10
35,90
10,30
25,40
51,75
55,1
(51,75)
(25,40)
(10,30)
(55,1)
(1,10)
(70,70)
(60,80)
(35,90)
(50,50)
kd-Trees Nearest Neighbor
• Idea: traverse the whole tree, BUT make two
modifications to prune to search space:
1. Keep variable of closest point C found so far.
Prune subtrees once their bounding boxes say
that they can’t contain any point closer than C
2. Search the subtrees in order that maximizes the
chance for pruning
Nearest Neighbor: Ideas, continued
d
Query
Point Q
T
Bounding box
of subtree
rooted at T
If d > dist(C, Q), then no
point in BB(T) can be
closer to Q than C.
Hence, no reason to search
subtree rooted at T.
Recurse, but start with the subtree “closer” to Q:
First search the subtree that would contain Q if we were
inserting Q below T.
Update the best point so far, if T is better:
if dist(C, Q) > dist(T.data, Q), C := T.data
Nearest Neighbor, Code
def NN(Point Q, kdTree T, int cd, Rect BB):
// if this bounding box is too far, do nothing
if T == NULL or distance(Q, BB) > best_dist: return
// if this point is better than the best:
dist = distance(Q, T.data)
if dist < best_dist:
best = T.data
best_dist = dist
// visit subtrees is most promising order:
if Q[cd] < T.data[cd]:
NN(Q, T.left, next_cd, BB.trimLeft(cd, t.data))
NN(Q, T.right, next_cd, BB.trimRight(cd, t.data))
else:
NN(Q, T.right, next_cd, BB.trimRight(cd, t.data))
NN(Q, T.left, next_cd, BB.trimLeft(cd, t.data))
Following Dave Mount’s Notes (page 77)
best, best_dist are global var
(can also pass into function calls)
Nearest Neighbor Facts
• Might have to search close to the whole tree in the
worst case. [O(n)]
• In practice, runtime is closer to:
- O(2d + log n)
- log n to find cells “near” the query point
- 2d to search around cells in that neighborhood
• Three important concepts that reoccur in range /
nearest neighbor searching:
- storing partial results: keep best so far, and update
- pruning: reduce search space by eliminating irrelevant trees.
- traversal order: visit the most promising subtree first.

More Related Content

What's hot

Red black tree
Red black treeRed black tree
Red black tree
uos lahore
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Zafar Ayub
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
Krish_ver2
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
Syed Zaid Irshad
 
Red Black Tree Insertion & Deletion
Red Black Tree Insertion & DeletionRed Black Tree Insertion & Deletion
Red black trees
Red black treesRed black trees
Red black trees
Amit Kumar Rathi
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
chauhankapil
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
Anand Ingle
 
Heap tree
Heap treeHeap tree
Heap tree
Shankar Bishnoi
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
Trupti Agrawal
 
Heaps
HeapsHeaps
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
vinaykhimsuriya1
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
Kuber Chandra
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
ArunaP47
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 

What's hot (20)

Red black tree
Red black treeRed black tree
Red black tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Red Black Tree Insertion & Deletion
Red Black Tree Insertion & DeletionRed Black Tree Insertion & Deletion
Red Black Tree Insertion & Deletion
 
Red black trees
Red black treesRed black trees
Red black trees
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Heap tree
Heap treeHeap tree
Heap tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Heaps
HeapsHeaps
Heaps
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Similar to kdtrees.pdf

Search data structures
Search data structuresSearch data structures
Search data structures
Ravi Pathak
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
Suneel61
 
trees.ppt
trees.ppttrees.ppt
trees.ppt
jawraabdul
 
09 binary trees
09 binary trees09 binary trees
09 binary trees
Andres Mendez-Vazquez
 
b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
SyedAhsan232061
 
Distance Based Indexing
Distance Based IndexingDistance Based Indexing
Distance Based Indexing
ketan533
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
Trees gt(1)
Trees gt(1)Trees gt(1)
Trees gt(1)
Gopi Saiteja
 
Cs345 cl
Cs345 clCs345 cl
Cs345 cl
mattriley
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
Krish_ver2
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Bresenhamcircle derivation
Bresenhamcircle derivationBresenhamcircle derivation
Bresenhamcircle derivation
Mazharul Islam
 
6.5 determinant x
6.5 determinant x6.5 determinant x
6.5 determinant x
math260
 
The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184
Mahmoud Samir Fayed
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
Trector Rancor
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics Math
Mark Kilgard
 
Bresenham circle
Bresenham circleBresenham circle
Bresenham circle
Taher Barodawala
 
Google BigQuery is a very popular enterprise warehouse that’s built with a co...
Google BigQuery is a very popular enterprise warehouse that’s built with a co...Google BigQuery is a very popular enterprise warehouse that’s built with a co...
Google BigQuery is a very popular enterprise warehouse that’s built with a co...
Abebe Admasu
 
lecture 12
lecture 12lecture 12
lecture 12
sajinsc
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
Dwight Sabio
 

Similar to kdtrees.pdf (20)

Search data structures
Search data structuresSearch data structures
Search data structures
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
trees.ppt
trees.ppttrees.ppt
trees.ppt
 
09 binary trees
09 binary trees09 binary trees
09 binary trees
 
b-tree.ppt
b-tree.pptb-tree.ppt
b-tree.ppt
 
Distance Based Indexing
Distance Based IndexingDistance Based Indexing
Distance Based Indexing
 
lecture 11
lecture 11lecture 11
lecture 11
 
Trees gt(1)
Trees gt(1)Trees gt(1)
Trees gt(1)
 
Cs345 cl
Cs345 clCs345 cl
Cs345 cl
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Bresenhamcircle derivation
Bresenhamcircle derivationBresenhamcircle derivation
Bresenhamcircle derivation
 
6.5 determinant x
6.5 determinant x6.5 determinant x
6.5 determinant x
 
The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
CS 354 Graphics Math
CS 354 Graphics MathCS 354 Graphics Math
CS 354 Graphics Math
 
Bresenham circle
Bresenham circleBresenham circle
Bresenham circle
 
Google BigQuery is a very popular enterprise warehouse that’s built with a co...
Google BigQuery is a very popular enterprise warehouse that’s built with a co...Google BigQuery is a very popular enterprise warehouse that’s built with a co...
Google BigQuery is a very popular enterprise warehouse that’s built with a co...
 
lecture 12
lecture 12lecture 12
lecture 12
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 

More from swapnilslide2019

Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
swapnilslide2019
 
Collectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.pptCollectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.ppt
swapnilslide2019
 
Flow Network Introduction
Flow Network IntroductionFlow Network Introduction
Flow Network Introduction
swapnilslide2019
 
AA_Unit_6.pptx
AA_Unit_6.pptxAA_Unit_6.pptx
AA_Unit_6.pptx
swapnilslide2019
 
AA_Unit_4.pptx
AA_Unit_4.pptxAA_Unit_4.pptx
AA_Unit_4.pptx
swapnilslide2019
 
AA_Unit_3.pptx
AA_Unit_3.pptxAA_Unit_3.pptx
AA_Unit_3.pptx
swapnilslide2019
 
AA_Unit_2.pptx
AA_Unit_2.pptxAA_Unit_2.pptx
AA_Unit_2.pptx
swapnilslide2019
 
AA_Unit 1_part-II.pdf
AA_Unit 1_part-II.pdfAA_Unit 1_part-II.pdf
AA_Unit 1_part-II.pdf
swapnilslide2019
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
swapnilslide2019
 
Programming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdfProgramming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdf
swapnilslide2019
 
CI.pdf
CI.pdfCI.pdf

More from swapnilslide2019 (13)

Topic-G-JavaCollections Framework.ppt
Topic-G-JavaCollections    Framework.pptTopic-G-JavaCollections    Framework.ppt
Topic-G-JavaCollections Framework.ppt
 
Collectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.pptCollectionsand GenericsInJavaProgramming.ppt
Collectionsand GenericsInJavaProgramming.ppt
 
Flow Network Introduction
Flow Network IntroductionFlow Network Introduction
Flow Network Introduction
 
AA_Unit_6.pptx
AA_Unit_6.pptxAA_Unit_6.pptx
AA_Unit_6.pptx
 
AA_Unit_4.pptx
AA_Unit_4.pptxAA_Unit_4.pptx
AA_Unit_4.pptx
 
AA_Unit_3.pptx
AA_Unit_3.pptxAA_Unit_3.pptx
AA_Unit_3.pptx
 
AA_Unit_2.pptx
AA_Unit_2.pptxAA_Unit_2.pptx
AA_Unit_2.pptx
 
AA_Unit 1_part-II.pdf
AA_Unit 1_part-II.pdfAA_Unit 1_part-II.pdf
AA_Unit 1_part-II.pdf
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
Programming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdfProgramming Laboratory Unit 1.pdf
Programming Laboratory Unit 1.pdf
 
PL-I.pdf
PL-I.pdfPL-I.pdf
PL-I.pdf
 
CI.pdf
CI.pdfCI.pdf
CI.pdf
 
PL-I.pdf
PL-I.pdfPL-I.pdf
PL-I.pdf
 

Recently uploaded

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
mahaffeycheryld
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
ycwu0509
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
Yasser Mahgoub
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
AI for Legal Research with applications, tools
AI for Legal Research with applications, toolsAI for Legal Research with applications, tools
AI for Legal Research with applications, tools
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
morris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdfmorris_worm_intro_and_source_code_analysis_.pdf
morris_worm_intro_and_source_code_analysis_.pdf
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
Gas agency management system project report.pdf
Gas agency management system project report.pdfGas agency management system project report.pdf
Gas agency management system project report.pdf
 

kdtrees.pdf

  • 2. kd-Trees • Invented in 1970s by Jon Bentley • Name originally meant “3d-trees, 4d-trees, etc” where k was the # of dimensions • Now, people say “kd-tree of dimension d” • Idea: Each level of the tree compares against 1 dimension. • Let’s us have only two children at each node (instead of 2d)
  • 3. kd-trees • Each level has a “cutting dimension” • Cycle through the dimensions as you walk down the tree. • Each node contains a point P = (x,y) • To find (x’,y’) you only compare coordinate from the cutting dimension - e.g. if cutting dimension is x, then you ask: is x’ < x? x y x y x
  • 5. insert(Point x, KDNode t, int cd) { if t == null t = new KDNode(x) else if (x == t.data) // error! duplicate else if (x[cd] < t.data[cd]) t.left = insert(x, t.left, (cd+1) % DIM) else t.right = insert(x, t.right, (cd+1) % DIM) return t } Insert Code
  • 6. FindMin in kd-trees • FindMin(d): find the point with the smallest value in the dth dimension. • Recursively traverse the tree • If cutdim(current_node) = d, then the minimum can’t be in the right subtree, so recurse on just the left subtree - if no left subtree, then current node is the min for tree rooted at this node. • If cutdim(current_node) ≠ d, then minimum could be in either subtree, so recurse on both subtrees. - (unlike in 1-d structures, often have to explore several paths down the tree)
  • 10. Point findmin(Node T, int dim, int cd): // empty tree if T == NULL: return NULL // T splits on the dimension we’re searching // => only visit left subtree if cd == dim: if t.left == NULL: return t.data else return findmin(T.left, dim, (cd+1)%DIM) // T splits on a different dimension // => have to search both subtrees else: return minimum( findmin(T.left, dim, (cd+1)%DIM), findmin(T.right, dim, (cd+1)%DIM) T.data ) FindMin Code
  • 11. Delete in kd-trees Q P A Want to delete node A. Assume cutting dimension of A is cd In BST, we’d findmin(A.right). Here, we have to findmin(A.right, cd) cd cd B Everything in Q has cd-coord < B, and everything in P has cd- coord ≥ B
  • 12. Delete in kd-trees --- No Right Subtree • What is right subtree is empty? • Possible idea: Find the max in the left subtree? - Why might this not work? • Suppose I findmax(T.left) and get point (a,b): Q (x,y) x cd (a,b) (a,c) It’s possible that T.left contains another point with x = a. Now, our equal coordinate invariant is violated!
  • 13. No right subtree --- Solution • Swap the subtrees of node to be deleted • B = findmin(T.left) • Replace deleted node by B Q (x,y) x cd (a,b) (a,c) Now, if there is another point with x=a, it appears in the right subtree, where it should
  • 14. Point delete(Point x, Node T, int cd): if T == NULL: error point not found! next_cd = (cd+1)%DIM // This is the point to delete: if x = T.data: // use min(cd) from right subtree: if t.right != NULL: t.data = findmin(T.right, cd, next_cd) t.right = delete(t.data, t.right, next_cd) // swap subtrees and use min(cd) from new right: else if T.left != NULL: t.data = findmin(T.left, cd, next_cd) t.right = delete(t.data, t.left, next_cd) else t = null // we’re a leaf: just remove // this is not the point, so search for it: else if x[cd] < t.data[cd]: t.left = delete(x, t.left, next_cd) else t.right = delete(x, t.right, next_cd) return t
  • 15. Nearest Neighbor Searching in kd-trees • Nearest Neighbor Queries are very common: given a point Q find the point P in the data set that is closest to Q. • Doesn’t work: find cell that would contain Q and return the point it contains. - Reason: the nearest point to P in space may be far from P in the tree: - E.g. NN(52,52): 60,80 70,70 50,50 1,10 35,90 10,30 25,40 51,75 55,1 (51,75) (25,40) (10,30) (55,1) (1,10) (70,70) (60,80) (35,90) (50,50)
  • 16. kd-Trees Nearest Neighbor • Idea: traverse the whole tree, BUT make two modifications to prune to search space: 1. Keep variable of closest point C found so far. Prune subtrees once their bounding boxes say that they can’t contain any point closer than C 2. Search the subtrees in order that maximizes the chance for pruning
  • 17. Nearest Neighbor: Ideas, continued d Query Point Q T Bounding box of subtree rooted at T If d > dist(C, Q), then no point in BB(T) can be closer to Q than C. Hence, no reason to search subtree rooted at T. Recurse, but start with the subtree “closer” to Q: First search the subtree that would contain Q if we were inserting Q below T. Update the best point so far, if T is better: if dist(C, Q) > dist(T.data, Q), C := T.data
  • 18. Nearest Neighbor, Code def NN(Point Q, kdTree T, int cd, Rect BB): // if this bounding box is too far, do nothing if T == NULL or distance(Q, BB) > best_dist: return // if this point is better than the best: dist = distance(Q, T.data) if dist < best_dist: best = T.data best_dist = dist // visit subtrees is most promising order: if Q[cd] < T.data[cd]: NN(Q, T.left, next_cd, BB.trimLeft(cd, t.data)) NN(Q, T.right, next_cd, BB.trimRight(cd, t.data)) else: NN(Q, T.right, next_cd, BB.trimRight(cd, t.data)) NN(Q, T.left, next_cd, BB.trimLeft(cd, t.data)) Following Dave Mount’s Notes (page 77) best, best_dist are global var (can also pass into function calls)
  • 19. Nearest Neighbor Facts • Might have to search close to the whole tree in the worst case. [O(n)] • In practice, runtime is closer to: - O(2d + log n) - log n to find cells “near” the query point - 2d to search around cells in that neighborhood • Three important concepts that reoccur in range / nearest neighbor searching: - storing partial results: keep best so far, and update - pruning: reduce search space by eliminating irrelevant trees. - traversal order: visit the most promising subtree first.