Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster insertion and a static kdtree without node hierarchy #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

horasio
Copy link

@horasio horasio commented Feb 4, 2018

Added selection so that one can build the tree in n log n instead of n log^2 n, as is the case when sort()-ing at each level.
selection (of pivot) uses either median-of-median-of-5, or simply use the pivot value found in the middle.
The first is theoretically better but slower in practice, so it's commented out, in favour of the second option.

With 1M points in the "basic" example, tree construction goes from 12.5 seconds to 2.5 seconds.

Also added a staticKdTree, that only allows nearest search queries. No insertion, no deletion.
The advantage is that it does not build a node hierarchy, so there is no memory allocation.
I hoped the static version would be faster too, but it doesn't seem so. Might depend on the usage scenario.

kdTree-min.js is up to date.
The files package.json has not been updated, since I don't know how to.


// left is always included, right is always excluded,
// so the range [left,right) contains (right-left) elements.
function partition(points, left, right, pivIdx, dim) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give a detail documentation of this algorithm? Or provide some links about it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link
provides some information about the algorithm.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very standard algorithm whose description you can find in many place. For example wikipedia : https://en.wikipedia.org/wiki/Median_of_medians#Algorithm

@zhuang-hao-ming
Copy link

I think this is a very good improvement! 😄

@horasio
Copy link
Author

horasio commented Jun 23, 2020

ping. Is ubilabs not interested in much faster kd-tree construction?

kdTree.js Outdated
const pivot = points[pivIdx][dim]; // get pivot value
var storeIdx = left; // variable to store the final position of the pivot value.
swap(points, pivIdx, right-1); // Move pivot to end
for (i=left; i < right-1; ++i) { // check all values but the last

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (i=left; i < right-1; ++i) { // check all values but the last
for (let i=left; i < right-1; ++i) { // check all values but the last

Pretty sure i should be defined here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, all of the for loops I've seen have undefined variables.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much @TheColorman Is it standard practice nowadays in Javascript? (the code above is the only code I have ever written in JS)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the ability to use variables without declaration is considered a mistake in the language that exists due to backwards compatibility afaik.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I added let in every for loop. Anyway... after all these years, I doubt Ubilabs has any interest in my patch :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants