[Paper Review] Computing the k Nearest-Neighbors for all Vertices via Dijkstra
This paper presents an efficient algorithm to compute the k nearest-neighbors for every vertex in a weighted directed graph using a modified Dijkstra's algorithm. By running n parallel Dijkstra executions with a global priority queue and early termination once k neighbors are found per vertex, the method achieves O(k(n log n + m)) time complexity, significantly improving upon naive approaches and offering a deterministic variant with O(kn log n + km log k) time.
We are given a directed graph $G = (V,E)$ with $n$ vertices and $m$ edges, with positive weights on the edges, and a parameter $k >0$. We show how to compute, for every vertex $v \in V$, its $k$ nearest-neighbors. The algorithm runs in $O( k ( n \log n + m ) )$ time, and follows by a somewhat careful modification of Dijkstra's shortest path algorithm. This result is probably folklore, but we were unable to find a reference to it -- thus, this note.
Motivation & Objective
- To compute, for every vertex in a weighted directed graph, its k nearest-neighbors using a single, unified algorithm.
- To improve upon the naive approach of running Dijkstra independently from each vertex, which would take O(kn(n log n + m)) time.
- To design a method that shares computation across all source vertices while maintaining correctness and efficiency.
- To provide both a randomized and a deterministic version of the algorithm with provable time bounds.
- To demonstrate that the algorithm can be adapted to compute k nearest neighbors to a set of terminals, not just all vertices.
Proposed method
- Run n parallel instances of Dijkstra's algorithm, one from each vertex, using a global min-heap to manage all relaxation events.
- Each heap entry is a triple (vertex, source, distance), where the source indicates which Dijkstra execution it belongs to.
- For each vertex v, maintain a data structure (hash table or balanced BST) tracking the k nearest neighbors found so far.
- When a vertex v is extracted from the global heap, if its k nearest neighbors are not yet found, add the source s to v's neighbor set and relax all outgoing edges from v for that source.
- Once a vertex v has k nearest neighbors, disable further processing for v by removing its local queue from the global heap.
- Use a local queue Q_v for each vertex v to maintain the minimum distance from each source s to v, and update the global heap only when the minimum changes.
Experimental results
Research questions
- RQ1Can the k nearest-neighbors for all vertices in a weighted directed graph be computed more efficiently than running Dijkstra from each vertex independently?
- RQ2What is the optimal way to share computation across multiple Dijkstra runs while ensuring correctness and minimizing redundant work?
- RQ3Can the algorithm be made deterministic with a small overhead compared to the randomized version?
- RQ4How does the algorithm scale with respect to k, n, and m, and can it be adapted to compute k nearest neighbors to a subset of vertices (e.g., terminals)?
- RQ5What is the theoretical time complexity of a shared, multi-source Dijkstra-based approach that terminates early per vertex?
Key findings
- The algorithm computes the k nearest-neighbors for all vertices in O(k(n log n + m)) time using a shared global heap and early termination.
- A randomized version of the algorithm achieves the same time bound with high probability, using random sampling to guide neighbor discovery.
- A deterministic variant replaces hash tables with balanced binary search trees, increasing the time complexity to O(kn log n + km log k).
- The algorithm correctly maintains the k nearest neighbors per vertex by ensuring that each vertex is processed in the context of all relevant Dijkstra runs until k neighbors are found.
- The method naturally extends to computing k nearest neighbors to a set of terminals T ⊆ V, with the same time complexity.
- The algorithm avoids redundant relaxation operations by disabling vertices once they have k nearest neighbors, reducing the total number of operations to O(km).
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.