Abstract
Knearest neighbours (kNN) is a very popular instancebased classifier due to its simplicity and good empirical performance. However, largescale datasets are a big problem for building fast and compact neighbourhoodbased classifiers. This work presents the design and implementation of a classification algorithm with index data structures, which would allow us to build fast and scalable solutions for large multidimensional datasets. We propose a novel approach that uses navigable smallworld (NSW) proximity graph representation of largescale datasets. Our approach shows 2–4 times classification speedup for both average and 99th percentile time with asymptotically close classification accuracy compared to the 1NN method. We observe two orders of magnitude better classification time in cases when method uses swap memory. We show that NSW graph used in our method outperforms other proximity graphs in classification accuracy. Our results suggest that the algorithm can be used in largescale applications for fast and robust classification, especially when the search index is already constructed for the data.
1. Introduction
Proximity graphs are a practical class of graphs with applications in multiple areas. For example, they are used for motion planning, as rapidly exploring random trees in [1, 2] and minimum spanning trees in clustering [3]. Most importantly, they lay in the core of search time data structures for largescale multidimensional data indexing, where stands for dataset cardinality.
Instancebased classification (IbC) methods store items (instances) from the training dataset as part of the classifier. Unlike other methods such as decision trees and artificial neural networks, the IbC algorithms do not estimate the classifier function from the training data in advance; instead, they store training data and derive a class label from an examination of the unseen sample’s nearest neighbours at test time [4]. Such methods easily adopt to unseen data by extending the list of stored samples.
Among pure IbC methods, we can identify knearest neighbours (kNN) with different variations [5–7], piecewise functions (e.g., splines [8]), and kernel approximators, such as radial basis function (RBF) interpolation methods. Splines and kernel approximation are frequently used in numerical methods for equation solving. At the same time, kNN is considered both a good basis for novel machine learning approaches [5] and useful tool for complex applied machine learning tasks [9].
Decision trees, support vector machines (SVM), selforganizing maps [10], learning vector quantization [11], and RBF networks [12] can also be attributed to instancebased methods. However, we avoid such wide interpretation, as these methods do not require storing original samples for classification.
In this paper, we address the problem of classification speed in the context of IbC using proximity graphs. Large datasets often appear in content recommendation tasks of internet services: search, online shops, streaming, or social networks. Classification accompanies recommendations in such tasks as sentiment analysis [13] or autolabelling [14]. As such systems serve millions or even billions of requests per day, this makes a millisecond algorithm overhead scale into hours and days of CPU time every day. This is a notable financial load.
The Knearest neighbours method estimates the class label of a test sample based on the labels of its closest neighbours from the training set. Distance is defined with some metric function. To avoid computing the distance of the test sample to every item in the training data, indexing is employed. This allows achieving sublinear classification time with various data structures such as trees, graphs, and inverted indices.
Graphbased indexing utilizes the idea that a dataset can often be represented in a metric space. Thus, adding a distance metric for nodes and requiring edges to represent close neighbourhoods, we can benefit from greedylike search algorithms, traversing the graph with preliminary knowledge about the desired direction towards a query sample.
In instancebased methods, algorithm execution time depends on the number of stored instances, while modelbased methods depend on the number of model parameters. Thus, IbC should offer both asymptotically and practically fast methods even for very large datasets, which requires constructing additional structures to navigate the data, such as search indices. We consider the case where index creation is indispensable and try to reduce the classification wall time. More specifically, we show how navigable smallworld (NSW) [15] and hierarchical navigable smallworld (HNSW) graphs [16] properties can be utilized in machine learning. We propose an improvement to NSW and HNSW index data structures, which results in 2–4 times sustainable speedup on average compared to 1NN classification baseline.
The contribution of this work can be summarized as follows:(i)We propose a new instancebased classification approach, which utilizes properties of NSW and HNSW index data structures to achieve 2–4 times 1NN classification speedup.(ii)Our proposed methods show a 2order time improvement when used with a memory swap file.
The rest of the paper is organized as follows. Section 2 discusses different indexing strategies for large multidimensional datasets. Section 3 covers both algorithm construction and theoretical justification of the proposed idea. Section 4 describes the experimental setup, datasets, hardware, and ways of comparison of our method to other approaches. Section 5 is devoted to the numerical results of our experiments. In this section, a proposed classifier is assessed in terms of speed and accuracy, and the NSW graph is compared to other proximity graphs. Section 6 analyses obtained numbers and state conditions in which using our method is beneficial and discusses interesting properties. Section 7 closes the paper with a highlight of major outcomes.
Our experiments, results, and code are available in the GitHub repository (https://github.com/IUCVLab/proximitycut).
2. Related Work
This section overviews how a problem of large dataset indexing is solved in the industry right now and considers indexing application to instancebased learning.
The problem of largescale indexing for multidimensional data arose together with efficient methods of document embedding using artificial neural networks [17–19]. The Internet became an endless source of data, including web pages, Wikipedia articles (https://dumps.wikimedia.org/), scientific papers (https://en.wikipedia.org/wiki/Web_of_Science) and images (https://en.wikipedia.org/wiki/Google_Photos) which form collections with to items in each. Contemporary research in natural language processing also requires bigger datasets to prove robustness [20]. As there is no exact borderline, we address these sizes as large. Search on such a scale can no longer be exhaustive. To be practical, it requires sublinear time. For an unsorted collection, this means that, on classical computers, we need to use approximate methods, also known as approximate nearest neighbour search (ANNS). Exact and approximate nearest neighbour searches are the core tool of many metricbased machine learning algorithms, including kNN classification, kmeans, kcentroids, and DBSCAN clustering. We discuss three approaches to building large dataset indices to guarantee fast ANNS. In this paper, we assume that the data can be represented in a metric or in a vector space depending on indexing method.
2.1. Tree Based
The invention of AVLtrees and Btrees made search trees a powerful tool to build indices for numerical data. Quadtrees [21] and KDtrees [22] have been used for indexing multidimensional vector data. Unfortunately, their usage is limited to low dimensions because they suffer from the curse of dimensionality. For example, indexing items with KDtree will utilize at most first dimensions of the vector, while contemporary deep models produce 100–1000 dimensional vectors, such as 768dimensional BERT embeddings as in [17]. For such big vectors, the search procedure will not account the majority of dimensions. Thus, it cannot guarantee a low distance from the query to the obtained “neighbours.” To solve this problem, authors of annoy (https://github.com/spotify/annoy) apply random projections instead of predefined vector dimensions and multiple search trees, which is proven an efficient way of reducing data dimensions for large datasets [23]. A collection of trees can achieve high ANNS accuracy with a small search time. However, it comes with a significant memory overhead as each tree consumes memory proportional to dataset size.
2.2. Inverted Index Based
An inverted index file (IVF) is an efficient method for text indexing, as it utilizes statistical properties of human language and discrete word representation. Since multidimensional vector data is continuous, various metricbased discretization approaches, such as vector quantization and vector clustering, are used to prepare the socalled vocabularies—finite collections of vectors, representing data clusters [24, 25]. Current works discuss methods to avoid the problem from which IVF suffers in natural language processing. Word frequencies in natural language are different, leading to a skewed index. The proposed methods include different kmeans implementations to form a vocabulary and product quantization technique for a better space partitioning as in [24]. Though IVF is a fast and scalable method with promising search speed and ANNS accuracy, it requires significant additional memory [26].
2.2.1. Proximity GraphBased
A proximity graph is a graph with a distance metric defined for vertices [27]. In practice, the metric can be defined not for all pairs of vertices, and edge in such a graph exists if and only if (or with higher probability if) its vertices satisfy particular geometric requirements; for example, if they are close in metric space. Building a proximity graph with dataset items as vertices can be understood as building a road network. It allows the search algorithm to travel starting from the arbitrary vertex in the direction of the search query by following some greedy strategy.
There are multiple types of deterministic and probabilistic proximity graphs, including minimum spanning trees (MST), relative neighbourhood graphs (RNG), Gabriel graphs, and Delaunay triangulations [28]. Among them, there are a group of data structures based on the idea of smallworld graphs. A major feature of smallworld (SW) networks [29] compared to other graphs is that together with edges connecting tight neighbourhoods (compare with local roads), they also include “distant” edges (compare with flights). In this example, “distant” means the edge which connects nearclique clusters which do not share any nodes. The existence of such “distant” edges leads to expected shortest path length (edges count) between arbitrary pair of vertices which was proven in [30]. NSW and HNSW graphs [16] place graph vertices into a metric space, introducing a highly efficient greedylike algorithm to traverse the graph. The authors claim that their data structure approximates Delaunay triangulations in high dimensions and propose a novel method of constructing SW graphs in metric space, which has construction time complexity and memory overhead, where represents the number of dimensions in data vectors.
3. Methodology
This work is dedicated to an improvement of the IbC methods. Given a big multidimensional dataset, we can achieve good results with the kNN classifier: using existing search indices, we can guarantee search time without sacrificing accuracy. These methods are competitive and are used in recent applied research as in [5, 9]. Still, we must consider speed in terms of both theoretical complexity and wall time, as largescale services are sensitive to even a millisecond overhead in a single function. Proximity graphs (NSW and HNSW in our case) built upon the unlabelled collection achieve expected logarithmic nearest neighbours search time with a greedylike algorithm. Therefore using a graphbased index, the kNN classifier can run in logarithmic time, which can hardly be improved in terms of theoretical complexity. On the contrary, we concentrate our efforts on utilizing label information to reduce practical computation time and preserve classification accuracy.
Our work is limited to the assumption that the dataset has a property of a metric space, with high probability nearest neighbours (in terms of the metric) of an item belonging to the same class as the item. This assumption is sometimes called the compactness hypothesis [31]. This assumption is general for all metricbased machine learning methods, including both unsupervised (e.g., kmeans and DBSCAN) and supervised (e.g., kNN and linear models) approaches.
The core theoretical idea of the proposed method lies in the fact that a proximity graph cut can be used to approximate the class boundaries. A graph cut is a set of edges where the source and destination vertices belong to different classes. A graph cut example is given in Figure 1.
The outline of our methodology is the following:(i)The same as in kNN classification, we accept the compactness hypothesis. This allows making an assumption that classes are closed volumes in .(ii)kNN classifier assigns a class to an unseen sample based on implicit class border estimation with neighbours voting. Border estimation can be replaced with faster border crossing detection, based on The Jordan Curve theorem [32] and its extension [33].(iii)The proposed border crossing detection technique is based on traversing NSW and HNSW graphs with a greedy algorithm (beam search). This algorithm produces the nearshortest path between the starting point and unseen sample, which is shown to be long [30].
Further paragraphs expand the listed ideas.
The Jordan curve theorem guarantees that if there are two classes in where one class is surrounded by a closed curve, then an arbitrary path between two points belonging to different classes intersects this border an odd number of times and at least once. We apply multidimensional consequence [33] of the theorem following the compactness hypothesis: for an arbitrary path (edges sequence) in proximity graph, single class border crossing can be used to indicate class change. An exact crossing point location is not needed. It is enough to account edges where vertices have different labels, that is, which belong to the graph cut. The method also works even if the class is not a single cluster but a set of disconnected clusters.
Speed characteristics of our implementation are derived from two properties of NSW and HNSW graphs. Firstly, in smallworld graphs (by definition), the shortest path between two arbitrary vertices has expected logarithmic length. Thus, any query search algorithm can start from a random graph node and find the nodes closest to the query node in time on average if the shortest path is known. Secondly, the greedy beam search algorithm in a dense enough NSW graph produces a path with edges with a probability of , which is shown by the experiments in [15] and theoretical proof in [30]. Greediness here is defined as selecting the next vertex from the neighbours, such that it is the closest (in metric space) to the destination. Euclidean and angular metrics are the most popular for vector space datasets. In other words, if algorithms search for query node starting in node , at each step, it should move to such a neighbor of , which has the smallest distance to in terms of metric. The aforementioned properties guarantee that this search, on average, will successfully converge in time.
To sum up, for a classification task, class boundary estimation is not needed. Instead, it is enough to detect the event of boundarycrossing. This useful observation allows reducing computational overhead, which is valuable for large systems. For implementation, we use both properties of NSW graphs to efficiently obtain a path in a graph and combine them with the Jordan curve theorem.
A formal description of our method is as follows: let a class be a set of volumes in a multidimensional metric space. As we mentioned earlier, we approximate class boundaries with a graph cut. Boundarycrossing occurs if an edge in a path belongs to a cut. Thus, we propose the following algorithm of classification. Given a sample vector that needs to be classified, a search is started from a graph vertex with a random index taken from a uniform distribution. A vertex choice procedure does not influence the result as any shorted path in the NSW graph has a logarithmic length. Then, graph nodes are greedily traversed towards the given vector, and the algorithm stops if it cannot find a closer neighbour. If class labels are available for all vertices, only the last border crossing is needed (if any happened) to assign a class to the sample.
The algorithm works for both binary and multiclass problems. Generalization to multiclass comes from applying the onevsall technique: the last border crossing can be considered as moving from the united “all” class to “one” class.
The proposed approach for NSW graphs is summarized in algorithm 1, which is an approximate equivalent to 1NN classification. The only difference for HNSW implementation requires to start at the top level of the graph and repeat the same algorithm at the lower levels until convergence. This also means that the choice of vertex for HNSW graphs is deterministic.
The Euclidean distance for normalized features is used as a metric in tests if other is not mentioned explicitly. This choice is reasonable in many practical applications as it captures the human perception of “closeness”: a significant change in the value of one feature or insignificant changes in multiple features should not dramatically influence the distance metric.

NSW graph allows using the proposed algorithm together with the kNN voting procedure. That is, classification can be run multiple times to achieve better accuracy. In the case of the HNSW graph, the search procedure always uses a predefined starting point. Thus applying voting will not bring any benefit.
4. Experiments
We implement our method to improve the original NSW and HNSW graph search procedure. Our experiments study our method from three points of view:(i)NSW graph ANNS quality compared to other proximity graphs,(ii)Classification accuracy compared to 1NN,(iii)Time improvement compared to baseline 1NN classification with HNSW.
We understand graph quality criterion as an ability to provide a better approximation for the ANNS problem. Application of proximity graphs is always a tradeoff between speed of neighbourhood exploration and percentage of actual neighbours retrieved (which can be referred to as recall metric). Experiments show that graph choice is good. The other two criteria are devoted to the method assessment for both accuracy and time. 1NN classification is used as a baseline. The first reason is that a proposed method is an approximation for this classification technique, so we assess our solution compared to the best achievable nonexhaustive 1NN classification method done with NHSW. Our target is to achieve better practical time with acceptable accuracy loss. The second reason is that based on 1NN results, one can easily extrapolate the time cost for an arbitrary kNN classification method.
To compare a graph type used in our method with all graph types presented in survey [28], we run experiments with 3 UCI datasets mentioned in a paper: Dermatology (https://archive.ics.uci.edu/ml/datasets/dermatology), Isolet (https://archive.ics.uci.edu/ml/datasets/isolet), and Image Segmentation (https://archive.ics.uci.edu/ml/datasets/Image + Segmentation). Nonnormalized Euclidean distance is used for Isolet and Image Segmentation to reproduce original accuracy results. For the Dermatology dataset, as it contains both categorical and numerical features, we implement and use the Heterogeneous Value Difference Metric (HVDM) defined in [34] (implemented in https://github.com/IUCVLab/proximitycut/blob/master/modules/tools/hvdm.py). For this set of experiments, NSW implementation from our repository was used.
By construction, the HNSW graph contains the NSW graph as a subset on level 0. Thus, all remaining experiments are conducted with hnswlib implementation where NSW graphs are extracted from the parent HNSW graph.
The speed and accuracy of classification are compared to the 1NN classifier on the mediumsize road signs dataset [35] with 43 classes (images are resized to 256dimensional representation, 10% test set) and two large binary classification datasets HIGGS ( items) and SUSY ( items) [36] (5% test set). Detailed speedup statistics are measured using another mediumsize Cover Type dataset [37].
In this work, we do not claim to invent or improve existing classification algorithm(s). These types of work require exhaustive testing for all marginal cases. We aim to apply stateoftheart indexing infrastructure and show what can we gain from it (in speedup) and at what cost (in accuracy). As NSW and HNSW’s time complexity characteristics have already been studied [15, 16] and proven [30], in this paper, we focus on practical improvement. Since algorithm time is shown to depend on dataset dimensionality and size, we cover both aspects.
All experiments were conducted at a 64bit Windows 10 laptop using a single CPU core. The laptop has AMD Rizen 3 3200U chip with 2.6 GHz frequency and 2 physical cores. 6 GB RAM is installed in the machine with 3.5 GB available for experiments. Python implementations were launched at Jupyter notebooks with Python 3.7.4. C++ implementation was compiled with GCC 7.4.0 using Windows Subsystem for Linux (Ubuntu 18.04).
5. Results
5.1. Graphs Comparison
The choice of NSW graph was validated by comparing accuracy results with other proximity graphs, namely, relative neighbourhood graphs (RNG), Gabriel graphs, and minimum spanning trees (MST). We compare our results against implementations from [28] on UCI datasets proposed in the paper. The authors intentionally focused only on classification accuracy and omitted speed comparison. Thus, we can compare our results by accuracy only. On Isolet, our method outperformed RNG graphs classification with 88.5% accuracy against 88.1%. With Dermatology data, it achieved the same 95.65% accuracy as RNG graphs, which can be the result of very small dataset and almost complete graph. With Image Segmentation data, our method achieved 87.5% accuracy which is only slightly worse than 1NN (90.3%) and RNG (88.8%). Detailed results are given in Table 1.
5.2. Average Classification Accuracy and Time
In NSW and HNSW graphs, the construction phase depends on hyperparameter , which linearly influences the number of graph edges. According to original papers, increasing this parameter can bring better accuracy results paying with additional index memory. We compared how this parameter influences baseline 1NN classification and the proposed method on two large datasets. Results are provided in Figure 2.
We also studied how dataset size and graph edges density controlled by NSW hyperparameter influence average classification time and accuracy. We compared baseline 1NN classification with the proposed method on three datasets with different hyperparameter values. With comparable accuracy number, our method showed sustainable speedup on both graphs. Time and accuracy results are given in Table 2.
NSW and HNSW graphs are built by a deterministic procedure, but their properties depend on the order of inserting and the structure of the dataset itself. Search and classification time for such graphs can only be estimated in terms of expected values. We used a mediumsize Cover Type dataset to compare classification times distributions. While both baseline 1NN classification and NSWbased proposed method show comparable time spread growth, the HNSWbased method shows extremely good numbers. For visual comparison, please refer to Figure 3.
5.3. Service Reliability Comparison
Indexing structures are used in different search tasks to improve the quality of service. Service reliability is frequently assessed in terms of or percentiles. Thus, we prepared a percentilebased comparison of the proposed method to the 1NN baseline, which shows 1.5–2 times speedup for NSWbased implementation and 4+ times comparison for NSWbased. The numbers are given in Table 3.
6. Discussion
The experiments on graph comparison show that NSWbased classification accuracy outperforms sparse Gabriel and MST graphs in all experiments. Also, the resulting classifier shows a behavior very similar to RNGbased implementation for each of the experiments. Considering this, we refer to [38], which states that although 2dimensional case RNG construction requires operations, the dimensional and nonEuclidean metric spaces will require operations. NSW graphs are constructed in which is a significant gain for largescale datasets.
Comparison of the proposed method accuracy to the baseline 1NN classifier shows that the proposed method is slightly under the baseline, HNSWbased implementation in all tests 0–7 percent points worse than 1NN. But we also observed that NSWbased implementation asymptotically tends to the baseline (see Figure 2) with the growth of graph density, defined by hyperparameter. For the small size of the dataset, the NSWbased implementation showed significant speedup, thus using –NN classifiers where built upon the proposed method will achieve asymptotically better accuracy for the smaller time. HNSWbased implementation at the same time shows consistent speedup for all graph sizes and densities. The loss in accuracy can be explained by the fact that the final step of our method returns only an approximation of the nearest neighbour. Thus, in future studies, improvement of the algorithm for a better approximation at the last search iterations can be addressed to compete with the baseline in accuracy while preserving similar time. We can also say that speedup was observed at all experiments for all sizes of the datasets and graph densities. A general observation is that speedup tends to be bigger for smaller datasets, but for scale datasets, it remains on a significant level.
We separately stop on the speedup value for the SUSY dataset with high density (M = 128), which shows 1.17 times improvement for NSW and enormous 148.1 times improvement for HNSW (see Table 2). The experiment was conducted multiple times with different system parameters showing the same result. We found out that this behavior fully depends on using swap memory. For the small Road Signs dataset, doubling graph density (parameter ) implies linear absolute time growth for 1NN classification, whereas for SUSY, we observe 3order time growth compared to 16 times density growth ( = 8, 128). We detected that, at the test machine, the process could only allocate 3.5 GB of physical RAM, while the data structure required almost 5 GB of virtual memory. Thus, a significant part of the data was dumped into HDD. This slowed down both the index construction phase and classification. The HNSW graph architecture uses exponentially smaller parts of memory for higher graph levels according to the construction process. Thus, only the last steps of the algorithm require 0level graph traversal, while the level with higher numbers can easily fit into physical RAM. This makes HNSWbased classification very promising in cases of low physical RAM.
Considering potential service quality, we state that both NSW and HNSWbased implementations of the proposed method show comparable speed improvement for the average and highpercentile classification time. We also observe that this speedup does not depend on graph density as shown in Table 3.
To sum up, we define two potential applications for our proposed method. Firstly, both NSW and HNSWbased algorithms can be used as dedicated classifiers for datasets of all sizes to improve absolute classification time still sacrificing a few percent points of accuracy. NSWbased classifier, in this case, will offer asymptotically growing accuracy for denser graphs, HNSWbased version will be extremely timeefficient when RAM does not fit the dataset, and a swap file is used. Secondly, for small and mediumsized datasets, NSWbased kNN implementation can offer better accuracy for the same classification time.
We want to mention that although we discussed competing indexing approaches (IVF, trees) in Section 2, we cannot implement our methods with these data structures, as they do not produce the graph cut we use in both our methods.
7. Conclusion
In this paper, we introduced the novel approach to instancebased classification. The approach improves existing NSW and HNSW data structures for faster classification of unseen items. It simplifies the original search algorithm and connects it with the Jordan curve theorem. The method achieved sustainable 4x speedup at real mediumscale datasets and more than 2x speedup for a large dataset using production hnswlib C++ library while preserving asymptotically close accuracy. It also showed extremely good time improvement if used with swap file.
We analysed our solution execution time, and we can say that it provides significantly better reliability in terms of and classification time percentile compared to the 1NN classification baseline.
Our future research can target improvement of nearest neighbour estimation in the end of the proposed search algorithm which will improve classification accuracy while keeping time smaller.
Data Availability
The machine learning classification datasets (Dermatology, Isolet, Image Segmentation, Cover Type, SUSY, and HIGGS) used to support the findings of this study have been deposited in the UCI repository (https://archive.ics.uci.edu/ml/datasets/dermatology, https://archive.ics.uci.edu/ml/datasets/isolet, archive.ics.uci.edu/ml/datasets/Image + Segmentation,https://archive.ics.uci.edu/ml/datasets/HIGGS, https://archive.ics.uci.edu/ml/datasets/SUSY, https://archive.ics.uci.edu/ml/datasets/covertype). These prior studies (and datasets) are cited at relevant places within the text as references [28, 36, 37]. The machine learning classification dataset with road signs used to support the findings of this study has been deposited in the INI Benchmark Website https://benchmark.ini.rub.de. These prior studies (and datasets) are cited at relevant places within the text as references [35]. The code related to synthetic dataset generation used to support the findings of this study has been deposited in the GitHub repository https://github.com/IUCVLab/proximitycut.
Conflicts of Interest
Adil Mehmood Khan acts as an editor of Complexity and Robustness TradeOff for Traditional and Deep Models special issue.
Acknowledgments
This research has been financially supported by The Analytical Center for the Government of the Russian Federation (Agreement No. 70202100143 dd. 01.11.2021, IGK 000000D730321P5Q0002).