Faster and More Robust Mesh-based Algorithms for Obstacle k-Nearest Neighbour

Faster and More Robust Mesh-based Algorithms for Obstacle -Nearest Neighbour

\nameShizhe Zhao \
\addrMonash University
\addrMelbourne, Australia
\AND\nameDavid Taniar \
\addrMonash University
\addrMelbourne, Australia
\AND\nameDaniel D. Harabor \
\addrMonash University
\addrMelbourne, Australia

We are interested in the problem of finding nearest neighbours in the plane and in the presence of polygonal obstacles (OkNN). Widely used algorithms for OkNN are based on incremental visibility graphs, which means they require costly and online visibility checking and have worst-case quadratic running time. Recently Polyanya, a fast point-to-point pathfinding algorithm was proposed which avoids the disadvantages of visibility graphs by searching over an alternative data structure known as a navigation mesh. Previously, we adapted Polyanya to multi-target scenarios by developing two specialised heuristic functions: the Interval heuristic and the Target heuristic . Though these methods outperform visibility graph algorithms by orders of magnitude in all our experiments they are not robust: expands many redundant nodes when the set of neighbours is small while performs poorly when the set of neighbours is large. In this paper, we propose new algorithms and heuristics for OkNN which perform well regardless of neighbour density.

Faster and More Robust Mesh-based Algorithms for Obstacle -Nearest Neighbour Shizhe Zhao
Monash University
Melbourne, Australia
David Taniar
Monash University
Melbourne, Australia
Daniel D. Harabor
Monash University
Melbourne, Australia

1 Introduction

The Obstacle (equiv. Obstructed) k-Nearest Neighbour Problem (OkNN) is a generalised variant of the well known Euclidean k-Nearest Nearest Problem (kNN) in two dimensions. In both cases the objective is to return the closest neighbours (equiv. targets) to an a priori unknown query point . In the case of OkNN however polygonal obstacles are introduced and the objective is to return shortest distances to each of the closest neighbours without crossing any obstacles. We refer to this metric as the obstacle distance between traversable points. Fig 1 highlights the differences between the two problem settings.

Figure 1: We aim to find the nearest neighbour of point from among the set of target points . Notice is the nearest neighbor of under the Euclidean metric but also the furthest neighbor of when obstacles are considered.

OkNN problems appear in a number of different application areas and are of interest in several different research fields. For example, in the context of spatial databases, it is often desirable to perform clustering queries in the presence of natural or man-made obstacles (such as rivers, trees or buildings) (?). In the context of computer games meanwhile, e.g. as described in (?), agents often rely on nearest-neighbour information, such as the location of enemies or resource points, for creating higher-level plans. Besides being a problem of direct interest, OkNN is also a fundamental sub-problem for a number of related spatial queries such as:

  • Obstacle Range Query (OR) (?): given a query point and a range , find all targets whose obstacle distance to at most equal to . OkNN algorithms can be applied to range queries by setting to be infinite and terminating when the distance to the next closest target is greater than .

  • Obstacle Reverse k-Nearest Neighbor (ORkNN) and variants (?, ?)): given a query point and a value , return a set of targets that have as one of their nearest neighbours: i.e. . ORkNN typically involes two stages: (i) search stage, which explores the space to obtain a set of candidates; (ii) refine stage, which removes from the set of candidates those for which is not in the top neighbours. Each of the two stages benefits directly from an efficient routine for OkNN.

  • Continuous Obstacle k-Nearest Neighbor (COkNN) (?): similar to OkNN, but the query point is generalised to a line segment. Solving such queries typically involves generating a list of so-called “split points”, and then invoking an OkNN query for each of these points.

Two popular algorithms for OkNN, which can deal with obstacles, are local visibility graphs (?) and fast filter (?). Though different in details, both of these methods are similar in that they depend on the incremental and online construction of a graph of co-visible points followed by an online Dijkstra search. Algorithms of this type are simple to understand, provide optimality guarantees and the promise of fast performance. Such advantages make incremental visibility graphs attractive to researchers. However, incremental visibility graphs also suffer from a number of notable disadvantages including: (i) online visibility checks; (ii) an incremental construction process that has up to quadratic space and time complexity for the worst case; (iii) duplicated effort, since the graph is discarded each time the query point changes.

In a previous paper (?) we develop a new method for computing OkNN which avoids these disadvantages and which can improve runtime performance by several orders of magnitude. Our work extends Polyanya (?): a recent and very fast algorithm for computing Euclidean shortest paths on a navigation mesh: a data structure comprised of convex polygons which taken together represent the entire traversable space. Compared to visibility graphs, navigation meshes are much cheaper to construct and sometimes available as input “for free” (e.g. in computer game settings, navigation meshes are often created, at least in part, by human designers). In a range of experiments we show that each of our previously proposed techniques can be several orders of magnitude faster than LVG (?), a state-of-the-art algorithm based on incremental visibility graphs.

Limitations in Our Previous Work. Our previous work described three multi-target variants of Polyanya, each of which offers state of the art performance but only in specific experimental scenarios:

  • Brute-force Polyanya, a simple algorithm that invokes point-to-point Polyanya repeatedly, once for each target. Though extremely naive this algorithm was undominated in our experiments in scenarios with few targets and for values of .

  • Interval Heuristic , which replaces the point-to-point heuristic function of the original algorithm with a consistent and online alternative that minimises cost-to-go distance for all targets in the candidate set. In our experiments this algorithm was undominated in dense-target scenarios where the map contains many nearest neighbour candidates.

  • Target Heuristic , a costly pre-processing-based heuristic function which always chooses the Euclidean-nearest candidate for computing cost-to-go estimates. In our experiments this algorithm was undominated in sparse-target scenarios with small values of .

Contributions. In this paper, we propose new preprocessing-based algorithms and heuristics for OkNN which improve on the performance of our previous work and which are more robust, in the sense that they perform well across a larger range of experimental scenarios including different target densities and for a wider range of values for :

  • We combine Polyanya with a technique known as Incremental Euclidean Restriction to derive a simple but very effective OkNN algorithm based on repeatedly solving point-to-point queries.

  • We develop an efficient preprocessing framework that involves computing labels that we store with the edges of the navigation mesh. We exploit these labels during a subsequent online phase and derive two new OkNN query algorithms: (i) Fence checking which is very fast but only works when ; (ii) Fence Heuristic which works in the general case and which performs as well as and sometimes better than and , regardless of target density.

2 Related work

2.1 R-tree and traditional kNN

Traditional kNN queries in the plane (i.e. no obstacles) is a well studied problem for which there exists many well known and efficient spatial indexing schemes. Perhaps the most successful and well known of these is the R-tree (?), a hierarchical data structure which organises a collection of spatial objects embedded in the plane (e.g. points or polygons) into a height-balanced tree. Though many variants exist (?, ?, ?), each of which improves the basic idea in some way, they all operate similarly and provide similar functionality. For this reason we opt to describe only the original work.

There are two types of nodes in an R-tree: leaves, which represent single objects, and interior nodes which are associated with a Minimal Bounding Rectangle (MBR). The MBR of an interior node contains the objects of all of its children and their associated MBRs. Meanwhile, nearest-neighbour queries involve a branch-and-bound traversal that depends on two important metrics:

  • mindist, which bounds from below the minimum distance from the query point to the closest object in the MBR of the current node;

  • minmaxdist, which bounds from below the minimum distance from the query point to the furthest object in the MBR of the current node.

The query algorithm starts at the root of the tree node and proceeds down, prioritizing nodes by mindist and pruning the search space by minmaxdist. Though improvements to this basic algorithm exist they employ a similar schema, albeit with different prioritisation and pruning strategies; e.g. (?, ?).

2.2 OkNN Queries and Visibility Graphs

Solving point-to-point obstacle distance queries in main-memory is a well studied problem for which many algorithms exist (?). Optimal methods usually pre-compute a visibility graph (VG), which adds edges between pairs of vertices that are co-visible. Figure 3 shows an example.

Figure 2: Example of a visibility graph. Black lines are edges in the visibility graph.
Figure 3: Example of a navigation mesh. Grey lines define individual mesh polygons.

Once the graph is created (including all candidate target points in the case of OkNN), queries can be resolved by way of A* or using Dijkstra’s well known algorithm. In case the query point is not one of the existing vertices of the graph, a further online insertion operation is performed before search can begin. The main drawback of visibility graphs is their worst-case time and space complexity which can be up where is the number of vertices in the map (?). In spatial database scenarios, for example, can be more than , so in-main-memory approaches are not suitable. Researchers in this field are thus motivated to design variant algorithms that only consider and process obstacles relevant to the current query. Two popular methods based on this idea are Local Visibility Graphs(LVG) (?) and Fast Filter (?). We have previously discussed the strengths and weaknesses of these methods in Section 1.

2.3 Pathfinding on Navigation Mesh

A navigation mesh is a data structure that divides the traversable space into a set of convex polygons. Originally proposed by Arkin (?), this spatial representation technique has been widely applied to pathfinding problems in areas such as robotics and computer games. Navigation meshes can be generated easily and efficiently. For example, Constrained Delaunay Triangulations (?), a popular starting point for many mesh-based algorithms, can be constructed in time. In other settings, such as computer games, navigation meshes are available as input “for free”, having been created, at least in part, by human designers.

Until recently pathfinding algorithms developed for navigation meshes have typically lacked both strong performance and optimality gurantees  (?, ?). Polyanya (?) is a recent navigation mesh algorithm which changes the status quo by being compromise-free: i.e. simultaneously fast, optimal and (assuming the mesh is given as input) entirely online. We give a technical description of this algorithm in Section 4. Our previous work on the topic of OkNN  (?) extends Polyanya, from point-to-point pathfinding to the multi-target case.

3 Problem Statement

OkNN is a nearest-neighbour search in two dimensions that can be formalised as follows:

Definition 3.1.

Obstacle k-Nearest Neighbour (OkNN): Given a set of points , a set of obstacles , a distinguished point and and an integer : return a set such that for all .


  • is a set of non-traversable polygonal obstacles.

  • is a set of traversable points called targets.

  • is a traversable point called the query point.

  • is an input parameter that controls the number of nearest neighbours that will be returned.

  • and are functions that measure the shortest distance between two points, as discussed below.

  • is the nearest neighbour of .

Stated in simple words, the objective is to find the set of targets which are closest to from among all possible candidates in . When discussing distances between two points and we distinguish between two metrics: which is the well known Euclidean metric (i.e. “straight-line distance”) and which measures the length of a shortest path between points and such that no pairwise segment of the path intersects any point inside an obstacle (i.e. “obstacle avoiding distance”).

Solution approaches for OkNN can be broadly categorised into two schemas:

  • Linear search methods, which involve the repeated application of a point-to-point pathfinding algorithm, from to selected . Linear search methods terminate when it can be proven that the obstacle distance to all nearest neighbours has beeen found. Instatiations of this schema include the OkNN algorithms known as LVG (?) and fast filter (?).

  • Spatial partitioning methods, which consider all candidate targets simultaneously and which attempt to solve a given instance of kNN in a single integrated search. Such methods apply prioritised search (e.g. best first or branch-and-bound) in combination with specialised heuristics that prune the set of candidates and focus attention toward the most promising target first. Instantiating this schema are a variety of Euclidean kNN methods such as R-tree (?) and Spatial KD-tree (?).

In our previous work we describe three algorithms for OkNN: Brute-force Polyanya, which instantiates linear search, and Polyanya in combination with the heuristics and , both of which instatiate the spatial partioning search schema.

4 Polyanya and heuristic functions: h, h, h

In its canonical form Polyanya is a point-to-point Euclidean shortest path algorithm which can be seen as an instance of A*: it performs a best-first search using an consistent heuristic function to prioritise nodes for expansion. The mechanical details are however quite different. Since we will employ a similar search methodology to Polyanya, we give herein a brief description of that algorithm. There are three key components:

Figure 4: Search nodes in Polyanya. Notice that the interval is a contiguous subset of points drawn from an edge of the navigation mesh. The corresponding root point, , is either the query point itself or the vertex of an obstacle. Taken together they form the search node .
Figure 5: From (?). We expand the node which has and as observable successors. In addition, the nodes , and are non-observable. All other potential successors can be safely pruned (more details in (?)).
  • Search Nodes: Conventional search algorithms proceed from one traversable point to the next. Polyanya, by comparison, searches from one edge of the navigation mesh to another. In this model search nodes are tuples where each is a contiguous interval of points and is a distinguished point called the root. Nodes are constructed such that each point is visible from . Meanwhile, itself corresponds to the last turning point on the path: from to any . Fig 5 shows an example.

  • Successors: Successor nodes are generated by “pushing” the current interval away from its root and through the interior of an adjacent and traversable polygon. A successor is said to be observable if each point is visible from . The successor node in this case is formed by the tuple . By contrast, a successor is said to be non-observable if the taut (i.e. locally optimal) path from to each must pass through one of the endpoints of current interval . The successor node in this case is formed by the tuple with as one of the points or . Fig 5 shows an example.

    Note that the target point is inserted in the open list as a special case (observable or non-observable) successor whenever the search reaches its containing polygon. The interval of this successor contains only the target.

  • Evaluation using h: When prioritising nodes for expansion, Polyanya makes use of an -value estimation for a given search node , and target :

    where represents the length of a concrete shortest path from to , and represents the lower-bound from to via some . There are three cases to consider which describe the relative positions of the in relation to the . These are illustrated in Fig 6. The objective in each case is to choose the unique that minimises the estimate. The three cases together are sufficient to guarantee that the estimator is consistent.

Similar to A*, Polyanya terminates when the target is expanded or when the open list is empty. In (?) this algorithm is shown to outpeform a range of optimal and suboptimal competitors, often by orders of magnitude.

Figure 6: Polyanya -value estimator. The current node is with and each of are possible target locations. Case 1: the target is . In this case the point with minimum -value is at the intersection of the interval and the line . Case 2: the target is . In this case the with minimum -value is one of two endpoints of . Case 3: the target is . In this case the with minimum -value is obtained by first mirroring through and applying Case 1 or Case 2 to the mirrored point (here, ). Notice that in this case, simply to doesn’t give us the h-value, based on Definition, it must reach the interval first.

4.1 Interval Heuristic h

In some OkNN settings targets are myriad and one simply requires a fast algorithm to explore the local area. The idea we introduce for such settings takes the form of a specialised heuristic function which we combine with Polyanya. It can be formalised as follows:

Definition 4.1.

Given search node , the interval heuristic is the minimum Euclidean distance from to any point .

The Interval Heuristic is consistent (?), and applying it only requires solving a simple geometric problem: finding the closest point on a line. Since this operation is trivial has a low computation overhead and can be applied entirely online. Stated differently, discards all spatial information about any target and performs instead a Dijkstra-like search which can quickly explore surrounding polygons. When targets are many and randomly distributed this approach can be highly efficient. The main drawback is redundant search effort in non-target areas, such as can occur when targets are few and sparsely distributed.

4.2 Target Heuristic h

In some OkNN settings the targets are sparse, so we need an effective method to prune the search space. The idea we introduce for such settings again takes the form of a specialised and consistent heuristic, , which we again combine with Polyanya.

Definition 4.2.

Given search node , the target heuristic is:

The idea is simple: each time Polyanya expands a node computes a cost-to-go it does so with respect to the Euclidean-nearest candidate which has not yet been added to the nearest neighbour set. Selecting such a target involves four nearest neighbour queries which we solve using an R-tree. This approach computes all nearest neighbours in a single run. Compared to , this heuristic can significantly reduce the number of node expansions because it always drives the search in the direction of the most promising candidate point. This approach performs well when target are sparse. The main drawback is the high cost of computing nearest neighbours when targets are numerous, dense and found in any direction.

5 Proposed Methods

We propose two distinct ideas for improving OkNN Search. The first idea involves computing, during an offline pre-processing step, a set of “fence labels” for each edge of the navigation mesh. The labels bound the distance from the edge to the nearest point in the target candidates set. We exploit the labels to improve the performance of a subsequent online search. The second idea we propose involves running point-to-point Polyanya, from the query location and to each target in the candidate set, in the order specified by a preprocessing-based heuristic technique known as Incremental Euclidean Restriction.

5.1 Fence Labelling

Interval heuristic can give us the minimum obstacle distance from the root to an edge of the mesh, an interesting observation is that if the algorithm have explored the entire map, then each edge of meshes must contains such information. Furthermore, if we ran such full-map exploration starting with each target and store obtained information, we would utilize such information for actual queries.

The described idea cannot be efficient, in spite of the time complexity, we need at least space to store obtained information, where is the number of mesh edges, and is the number targets.

However, similar to traditional nearest-neighbour query (?, ?), we can apply minmaxdist pruning in this case.

Definition 5.1.

Given a mesh edge , a search node where and (resp. ) is the endpoint of closer to (resp. ), then:

  • mindist ,

  • minmaxdist

In other words, for a given root , the length of shortest path to any point must in range , see an example in Fig 7.

Figure 7: are endpoints of the mesh edge, the length of blue path is the , the length of one of green path is the

To distinguish the g-value in preprocessing and search, let’s define as follow:

Definition 5.2.

Let be a search node produced by target in preprocessing:

Definition 5.3.

Dominate: Given two search nodes and , where and are on same mesh edge , dominates iff .

Lemma 5.1.

Let and be two search nodes in preprocessing stage, where and are on same mesh edge , and dominates ; for a given query point , let be any point on . Then:


We have two equations:


then is:

according to definition 5.1, we have:


and because dominates , we have:


thus . ∎

Lemma 5.1 implies that, in preprocessing, we can discard those search nodes who is dominated by others, let’s call this fence pruning.

Definition 5.4.

Fence lies on each mesh edge, it contains a collection of search nodes produced by preprocessing, let’s call those search nodes labels 111To make discussion clear, we call this ”search node” in context of preprocessing, and ”label” in context of query processing. Fence has a property (default value is INF), and in preprocessing stage, it blocks all passing search nodes that:


Finally we propose a floodfill-like algorithm: in initialization, for each target , generate successors on the edges of mesh that contains , and store those search nodes in a open-list; similar to OkNN search, we expand search nodes in the order of their f-value, where ; meanwhile, we apply fence pruning and update upper-bound of the fence befor the expansion; the algorithm terminates when open-list becomes empty, see in algorithm 1.

Input :  (targets set), (open list)
1 foreach t in  do
2       foreach suc in successor(t) do
3             q.push(suc);
5       end foreach
7 end foreach
8while q not empty do
9       n = q.pop();
10       // fence pruning;
11       fence = get_fence(n.I);
12       lowerbound = (n.r) + mindist(n);
13       if lowerbound fence.upperbound then
14             continue;
16       end if
17      upperbound = (n.r) + minmaxdist(n);
18       fence.upperbound = min(fence.upperbound, upperbound);
19       foreach suc in successors(n) do
20             q.push(suc);
22       end foreach
24 end while
Algorithm 1 Preprocessing

Notice that the total number of search nodes can still up to , one case is that targets are distributed in a single cluster, and all obstacles line in two rows, so that all targets produce labels on mesh edges in the middle (see in Fig 7(b)). However, our experiment in section 6 indicates that most of fence only contains 2 to 3 labels (search nodes), and there are two observations to convince us the effectiveness of preprocessing (see in Fig 7(b)):

  • If two targets are very close, their may produce labels on same fence, but their successors must be pruned by one of other when root changes (known as root pruning (?));

  • If two targets are not close, they will dominate surrounding mesh edges and block search nodes from further targets.

(a) The worst case

(b) General case
Figure 8: (a): have same distance to edges in the middle, so fence of these edges contain labels from all targets. (b): are close and produce labels on same fence in green area, but has no labels after root change since all vertices are dominated by ; search nodes of (resp. ) can not reach brown (resp. green) area which is dominated by (resp. ).

5.2 Apply on nearest-neighbour query

After preprocessing, for a given query point , there are tow cases:

  • Case 1: there’s no labels on surrounding fences;

  • Case 2: all surrounding fences contain labels;

The Case 1 implies that there’s no path from to any target ;

Lemma 5.2.

In the Case 2, at least one of label is produced by the obstacle nearest-neighbour of .


Assume the obstacle nearest neighbour of is , and surrounding fences of not contain any label produced by , then:

  • Case 1: there’s no path from to , which contradict to the assumption;

  • Case 2: all successors of are dominated by others, and according to lemma 5.1, can not be the nearest-neighbour of , which contradict to the assumption as well.

Thus, such doesn’t exists. ∎

Therefore, we can run point-to-point search from to all roots of search nodes in surrounding fences, and find the nearest-neighbour, we call this algorithm Fence Checking, see details in algorithm 2. Notice that algorithm 2 only search to the last vertex instead of the entire path, so it is very fast.

Input :  (query point), (mesh polygon contains )
1 roots = set();
2 min_dist = INF;
3 nearest_neighbor = null;
4 foreach edge: poly.edges do
5       foreach node: edge.fence do
6             roots.add(node.r);
8       end foreach
10 end foreach
11foreach r: roots do
12       = g(r) +, r);
13       if  min_dist then
14             min_dist = ;
15             nearest_neighbor = start_point(r);
17       end if
19 end foreach
Algorithm 2 Fence Checking algorithm

5.3 Generalize to k-nearest neighbours

When , search nodes of next nearest-neighbour may be dominated by a visited nearest-neighbour and thus no labels on surrounding fences. So we still need a search process to explore the map, and we need to design heuristic function to prioritise search nodes in query processing. We first present an admissible but not consistent heuristic naive fence heuristic: ; then we show how to adapt it to a consistent heuristic fence heuristic: .

Definition 5.5.

: let be the fence lies on mesh edge , be a label on , be a search node produced by query point , where on , then

Stated in simple words, evaluates the distance from current root to the nearest target through interval and .

Theorem 5.3.

is admissible, more specific: given a search node , for :


Given a search node , let be the label on fence that used by : and is produced by a target ; and let be the shortest path from to any target pass through and ; there are three cases:

  • If , then we have

    and according to definition of (Fig 6), we have , thus ;

  • If and has label on the , according to definition 5.5, we have ;

  • If and none of ’s label on the , then they must be dominated by some fences, according to lemma 5.1, we still have .

Thus, is admissible. ∎

Notice that when are co-visible, and otherwise it’s an underestimation. Such observation indicates that may not consistent, Fig 9 shows an example.

Figure 9: is the edge of mesh, the current search node is where , both and have labels on the fence that lies on , red segments indicate the actual path, while the blue segment shows the path after the expansion. Before the expansion, , since , after the expansion, the estimation becomes which is less than the previous value.

Therefore, when we expand a search node , those labels who lay on the fence but are not co-visible may ruin the consistency. To fix this problem, instead of checking visibility in , we can simply keeping the maximum f-value during the search.

Definition 5.6.

Fence heuristic : for a given search node , guarantees

where is the f-value of the parent search node.

Theorem 5.4.

The is consistent, more specific, is admissible and for a given search node and it’s successor :


Let be the current search node, and be any successor. First, we show that is admissible by mathematic induction:

  • Initially, when doesn’t has any parent, which is admissible according to theorem 5.3;

  • Assume for is admissible during the search;

  • For successor , if then is admissible;

  • Otherwise which is also admissible;

Thus is admissible, and according to definition 5.6, is always true, so it is consistent. ∎

Finally, we implement the fence heuristic search in algorithm 3.

1 while heap not empty do
2       node = heap.pop();
3       if node is a target that not yet reached then
4             result.add(node);
5             if result.size is k then
6                   return;
8             continue;
10       if node is a target that has been reached then
11             continue;
13       successors = genSuccessors(node);
14       foreach suc in successors do
15             suc.g = node.g + (node.root, suc.root);
16             suc.f = max(node.f, suc. g + (suc));
17             heap.push(suc);
19       end foreach
21 end while
22 Function ():
23       res = INF foreach s in Fence(node.I) do
24             res = min(res, (node, s.root) + s.root.g)
25       end foreach
26      return res
Algorithm 3 OkNN search with

5.4 IER-Polyanya: Polyanya with Incremental Euclidean Restriction

From previous experiments (?), we notice that the brute-force Polyanya, a naive adaption of Polyanya that running point-to-point search for each target, outperforms other sophisticated comptitors under certian scenarios. Such positive results motivate us to design more efficient algorithm.

Notice that for same pair of points , we always have , this is called Euclidean lower-bound property (?). Utilize such property, we can prune the search space, similar ideas also appeared in previous literatures (?, ?, ?). We implement this idea as follow: given query point , the algorithm use incremental nearest neighbor query in R-tree (?) to visit target in order of , then use Polyanya to compute and keep track k-nearest neighbours with metric; the algorithm terminates when where is the current -th obstacle nearest neighbour of , see in algorithm 4.

Input :  (targets set), (query point)
1 candidates = Heap();
2 rtree = Rtree();
3 = rtree.iNearestNeighbor();
4 while  is not null do
5       =, );
6       if less than candidates then
7             candidates.add();
9      else
10             if max(candidates)  then
11                  break;
12             end if
13            if max(candidates)  then
14                   candidates.remove_max();
15                   candidates.add();
17             end if
19       end if
20       = rtree.iNearestNeighbor();
22 end while
Algorithm 4 Repeated Polyanya
Lemma 5.5.

When algorithm 4 terminates, the candidates stores top- smallest obstacle distances.


Assume there is an unvisited that , where is the -th nearest neighbor. Since is s unvisited, we have ; according to Euclidean lower-bound property, we have and , therefore we have which contradict to the assumption, so such doesn’t exist. ∎

The effectiveness of IER-Polyanya affected by two factors:

  • False hit: a searched target not being retrieved is called false hit. False hit is more likely happen when Euclidean metric is misleading, for example in Fig 1, is the first target to search but wouldn’t be retrieved when .

  • Overlapping search space: since each search is independent, same search space may be explored multiple times.

In section 6, we will examine the performance of IER-Polyanya based these considerations.

6 Empirical Analysis

OkNN problem often appears in both AI pathfinding and spatial query preprocessing, so we examine the performance of proposed methods on different maps that corresponding two these two application scenarios. Fig 10 shows the set of maps that we consider in our experimental evaluation.

(a) AR0602SR. This map has 47 obstacles and 5504 vertices. The navigation mesh mesh comprises 5594 polygons.
(b) brc202d. This map has 75 obstacles and 4035 vertices. The navigation mesh comprises 4164 polygons.
(c) CatwalkAlley. This map has 157 obstacles and 15301 vertices. The navigation mesh comprises 15482 polygons.
(d) 8K tiled obstacles. This map has 8385 obstacles and 109171 vertices. The navigation mesh comprises 125937 polygons.
Figure 10: Maps and summaries. NB: black and green indicates non-traversable areas.

We choose three maps from a well known AI pathfinding benchmark sets (?): AR0602SR, brc202d, CatwalkAlley; and a synthetic map 8K tiled obstacles from our previous work (?)222There are few real datasets (e.g., but all expired that not publicly available, the way we created such synthetic map is described below:

  • Tiled obstacles map. we extract the shape of all parks in Australia from OpenStreetMap(?) and use these shapes as polygonal obstacles. There are initially 9000 such polygons333In our previous (?), it’s called 9000 obstacles map., after removing invalid polygons, there are 8385 polygons left. Next we generate a map by tiling all obstacles in the empty square plane. For the tiling, we first divide the square plane into grid having number of rows and columns. Then we assign each polygon to a single grid cell and normalize the shape of polygon by to fit inside the cell.

The navigation mesh of map is generated by Constrained Delaunay Triangulation, which is ; the implementation of such algorithm is in library Fade2D 444, the total time on such preprocessing is about 6s.

Parameters Values
1, 5, 10, 25, 50
Maps AR0602SR, brc202d, CatwalkAlley, 8K-tiled-obs
Target Density () 0.0001, 0.001, 0.01, 0.1
Target distribution random, clustered
Table 1: Parameters (default in bold)

In each query processing experiment, we choose parameters from Table 1. Notice that we define target density by where is the number of vertices, while other works (?, ?, ?) used as their density where is the number of obstacles. The reason is that game maps usually have few large continuous obstacles.

We’re using random query points for each setting, grouping results by x-axis, and computing average; the size of each bucket is at least 10; All algorithms appear in experiments are:

  • LVG: Local Visibility Graph (?);

  • h: Interval heuristic (?);

  • h: Target heuristic (?);

  • h: Fence heuristic (Algorithm 3);

  • fc: Fence Checking (Algorithm 2);

  • IER-Poly: Polyanya with Incremental Euclidean Restriction (Algorithm 4);

These algorithms are implemented in C++ and compiled with clang-902.0.39.1 using -O3 flag, under x86_64-apple-darwin17.5.0 platform. All of our source code and test data set are publicly available 555 All experiments are performed on a 2.5 GHz Intel Core i7 machine with 16GB of RAM and running OSX 10.13.4.

6.1 Experiment 1: preprocessing

This experiment is to examine the performance of preprocessing. For each map, we generated random distributed target set with four density and run floodfill with fence pruning (algorithm 1).

Time cost. We collect the execution time of each instance and group by map, each group contains data from different density. Tabel 2 shows that the std of each group is relative small but the mean of groups are very different, so we can conclude that the time cost of preprocessing is not sensitive to density of targets but the map itself (e.g. number of vertices).

map vertices mean std min 25% 50% 75% max
AR0602SR 5504 6.92 0.14 6.72 6.84 6.92 6.98 7.21
brc202d 4164 6.11 0.25 5.63 5.98 6.09 6.25 6.54
CatwalkAlley 15301 26.73 2.35 22.83 25.55 26.15 28.49 30.87
8K-tiled-obs 109171 537.16 24.94 499.93 522.24 537.60 552.43 580.43
Table 2: Processing time (ms)

Label size. This metric indicates the space cost and the performance during the query processing. We collect the number of labels on each mesh edge and group by map, then in each group, we further group data by density666There are of edges have more than 10 and up to 300 labels, we filter these out for better visualisation. Fig 11 shows that for all maps and all densities, medians of label size per edge are simlar.

Figure 11: Number of labels per edge

6.2 Experiment 2: random distributed targets

The aim of this experiment is to examine the performance of proposed algorithms when and (density) change, by default, and . The effectiveness of heuristic function is measured by total search time divide by number of generated node, so for same search time, larger heuristic cost meaning smaller search space. We also include the previous state-of-the-art LVG in comparison, and since this method may reach quadratic time complexity under some scenarios, we clip the execution time by ms.

From Fig 12 and Table 3, we make the following observations:

  • IER-Polyanya always outperforms others, and when is fixed it’s nearly order of magnitude faster than and . The reason is that Euclidean metric in this map with random distributed targets is very good, so it has small number of false hit and thus get benefit from the fast point-to-point search.

  • Fence heuristic has simlar number of generated search nodes to regarding the heuristic cost, and in general, it also has similar time cost to . Besides, it significantly outperforms when and is fixed. So we can conclude that has both good time cost and small number of generated nodes.

  • LVG is completely dominated by others in all cases, so we can remove this competitor in further experiments.

(a) varying ()
(b) varying ()
Figure 12: Performance on the default map (8K tiled obstacles)
heuristic mean std min 25% 50% 75% max
0.82 0.19 0.05 0.72 0.80 0.91 2.83
0.26 0.07 0.02 0.23 0.25 0.29 1.77
162.19 262.96 9.23 71.29 93.23 112.16 8191.00
0.42 0.12 0.08 0.34 0.39 0.44 1.42
Table 3: The average cost (s) of each heuristic function

6.3 Experiment 3: clustered targets

One drawback of IER-Polyanya is that each repeated search is independent, so that it makes many redundant computation when those searches are overlapped. The aim of this experiment is to examine the performance of IER-Polyanya when search space overlapped. We create a single cluster of targets, and to get rid of the influence of false hit, we let the size of cluster be , so that when the false hit becames .

From Figs 12(a),12(b) we make following observations:

  • when , IER-Polyanya is outpeformed by because the false hit;

  • when , false hit is , but IER-Polyanya is still significantly outpeformed by , while in previous experiment, it’s faster than for all . So we can conclude that IER-Polyanya has bad performance when search space overlapped.

Figure 13: Performance on clustered targets

6.4 Experiment 4: varying maps

Another drawback of IER-Polyanya is that the Euclidean metric can be misleading in some maps. The aim of this experiment is to examine the performance of IER-Polyanya on different types of maps.

We run random queries with default parameter where targets are randomly distributed. To show the influence of misleadingness, we analyze results as follow:

  • We use the number of nodes generated by measure the difficulty of search;

  • For each search, we evaluate the speed-up factor of each method by time cost of such method divide by time cost of ;

  • We plot the number of false-hit and the speed-up factor with increasing difficulty for each map.

Such speed-up comparison also appears in other pathfinding literature (?).

From fig 15 we make following observations:

  • In all maps, when false hit increases the speed-up factor decreases, meaning that false hit affect the performance of IER-Polyanya.

  • AR0602SR,brc202d,CatwalkAlley have more false hit than 8K tiled obstacles, meaning that Euclidean metric is misleading in those maps.

  • In those misleading maps, speed-up factor of IER-Polyanya is relative small, and sometimes less than (worse than ), meaning that IER-Polyanya doesn’t work well when Euclidean metric becomes misleading.

6.5 Experiment 5: Nearest neighbor query

Figure 14: Performance on nn query

The advantage of fence checking is only searching the last vertex of the shortest path, the aim of this experiment is to examine when it performs well. In the experiment, we run random queries with different densities, then we analyze results in two aspects: (i) grouping results by number of vertices on path to examine the performance when it gets advantage; (ii) grouping results by to examine the general performance in different density.

Fig 13(a) shows that fence checking is not sensitive to the number of vertices on path and can be orders of magnitude faster than other competitors when it gets advantage.

Fig 13(b) shows that fence checking outperforms others when , the reason is that when density increases, the number of vertices on shortest path decrease, thus fence checking gradually lose the advantage.

(a) AR0602SR
(b) brc202d
(c) CatwalkAlley
(d) 8K tiled obstacles
Figure 15: Performance on different maps

7 Conclusion and Future Work

We study the obstacle k-nearest neighbours problem which appears in both AI pathfinding and spatial database areas. In this paper, we proposed two efficient methods: fence heuristic and IER-Polyanya.

Fence heuristic utilizes precomputed information to guide the search process, compare to and in our previous work, it has both cheap heuristic cost and small search space, and it performs well in whatever target density. The preprocessing for Fence heuristic has bad theoretical upper bound, but it is efficient in practical when a pruning strategy be applied. IER-Polyanya utilizes a fast point-to-point search to compute obstacle distance and Euclidean restriction to prune candidate set, it sometimes outperforms Fence heuristic order of magnitude, but such performance is not stable that sensitive to the distribution of targets and obstacles.

There are several possible directions for future work. Firstly, current Fence heuristic performs a linear search for labels on fence to compute the heuristic value, an obvious improvement is to store labels by their interval, and during the search, only retrieval labels with overlapped interval (meaning visible). Secondly, notice that adding or deleting targets only affect a small area of the map, so that we can develop these operations to efficiently maintain fence labels when targets change. Thirdly, one possible way to deal the worst case in preprocessing is reconstructing part of the navigation mesh.


  • Abeywickrama et al. Abeywickrama, T., Cheema, M. A., & Taniar, D. (2016). k-nearest neighbors on road networks: A journey in experimentation and in-memory implementation.  PVLDB, 9(6), 492–503.
  • Aha et al. Aha, D. W., Molineaux, M., & Ponsen, M. (2005). Learning to win: Case-based plan selection in a real-time strategy game.  In Muñoz-Ávila, H., & Ricci, F. (Eds.), Proceedings of the International Conference on Case-Based Reasoning (ICCBR), pp. 5–20.
  • Arkin Arkin, R. C. (1989). Navigational path planning for a vision-based mobile robot.  Robotica, 7(1), 49–63.
  • Beckmann et al. Beckmann, N., Kriegel, H., Schneider, R., & Seeger, B. (1990). The r*-tree: An efficient and robust access method for points and rectangles.  In Garcia-Molina, H., & Jagadish, H. V. (Eds.), Proceedings of the 1990 ACM SIGMOD International Conference on Management of Data, Atlantic City, NJ, USA, May 23-25, 1990., pp. 322–331. ACM Press.
  • Cheung & Fu Cheung, K. L., & Fu, A. W. (1998). Enhanced nearest neighbour search on the r-tree.  SIGMOD Record, 27(3), 16–21.
  • Chew Chew, L. P. (1989). Constrained delaunay triangulations.  Algorithmica, 4(1), 97–108.
  • Cui et al. Cui, M., Harabor, D. D., & Grastien, A. (2017). Compromise-free pathfinding on a navigation mesh.  In Sierra, C. (Ed.), Proceedings of the Twenty-Sixth International Joint Conference on Artificial Intelligence, IJCAI 2017, Melbourne, Australia, August 19-25, 2017, pp. 496–502.
  • de Berg de Berg, M. (2000). Computational geometry: algorithms and applications, 2nd Edition. Springer.
  • Demyen & Buro Demyen, D., & Buro, M. (2006). Efficient triangulation-based pathfinding.  In Proceedings, The Twenty-First National Conference on Artificial Intelligence and the Eighteenth Innovative Applications of Artificial Intelligence Conference, July 16-20, 2006, Boston, Massachusetts, USA, pp. 942–947. AAAI Press.
  • Gao et al. Gao, Y., Liu, Q., Miao, X., & Yang, J. (2016). Reverse k-nearest neighbor search in the presence of obstacles.  Inf. Sci., 330, 274–292.
  • Gao et al. Gao, Y., Yang, J., Chen, G., Zheng, B., & Chen, C. (2011). On efficient obstructed reverse nearest neighbor query processing.  In Cruz, I. F., Agrawal, D., Jensen, C. S., Ofek, E., & Tanin, E. (Eds.), 19th ACM SIGSPATIAL International Symposium on Advances in Geographic Information Systems, ACM-GIS 2011, November 1-4, 2011, Chicago, IL, USA, Proceedings, pp. 191–200. ACM.
  • Gao & Zheng Gao, Y., & Zheng, B. (2009). Continuous obstructed nearest neighbor queries in spatial databases.  In Çetintemel, U., Zdonik, S. B., Kossmann, D., & Tatbul, N. (Eds.), Proceedings of the ACM SIGMOD International Conference on Management of Data, SIGMOD 2009, Providence, Rhode Island, USA, June 29 - July 2, 2009, pp. 577–590. ACM.
  • Ghosh & Mount Ghosh, S. K., & Mount, D. M. (1991). An output-sensitive algorithm for computing visibility graphs.  SIAM J. Comput., 20(5), 888–910.
  • Guttman Guttman, A. (1984). R-trees: A dynamic index structure for spatial searching.  In Yormark, B. (Ed.), SIGMOD’84, Proceedings of Annual Meeting, Boston, Massachusetts, USA, June 18-21, 1984, pp. 47–57. ACM Press.
  • Harabor et al. Harabor, D. D., Grastien, A., Öz, D., & Aksakalli, V. (2016). Optimal any-angle pathfinding in practice.  J. Artif. Intell. Res., 56, 89–118.
  • Hjaltason & Samet Hjaltason, G. R., & Samet, H. (1999). Distance browsing in spatial databases.  ACM Trans. Database Syst., 24(2), 265–318.
  • Kallmann Kallmann, M. (2005). Path planning in triangulations.  In Proceedings of the IJCAI workshop on reasoning, representation, and learning in computer games, pp. 49–54.
  • Kamel & Faloutsos Kamel, I., & Faloutsos, C. (1994). Hilbert r-tree: An improved r-tree using fractals.  In Bocca, J. B., Jarke, M., & Zaniolo, C. (Eds.), VLDB’94, Proceedings of 20th International Conference on Very Large Data Bases, September 12-15, 1994, Santiago de Chile, Chile, pp. 500–509. Morgan Kaufmann.
  • Ooi Ooi, B. C. (1987). Spatial kd-tree: A data structure for geographic database.  In Schek, H., & Schlageter, G. (Eds.), Datenbanksysteme in Büro, Technik und Wissenschaft, GI-Fachtagung, Darmstadt, 1.-3. April 1987, Proceedings, Vol. 136 of Informatik-Fachberichte, pp. 247–258. Springer.
  • OpenStreetMap contributors OpenStreetMap contributors (2017). Planet dump retrieved from
  • Roussopoulos et al. Roussopoulos, N., Kelley, S., & Vincent, F. (1995). Nearest neighbor queries.  In Carey, M. J., & Schneider, D. A. (Eds.), Proceedings of the 1995 ACM SIGMOD International Conference on Management of Data, San Jose, California, USA, May 22-25, 1995., pp. 71–79. ACM Press.
  • Sellis et al. Sellis, T. K., Roussopoulos, N., & Faloutsos, C. (1987). The r+-tree: A dynamic index for multi-dimensional objects.  In Stocker, P. M., Kent, W., & Hammersley, P. (Eds.), VLDB’87, Proceedings of 13th International Conference on Very Large Data Bases, September 1-4, 1987, Brighton, England, pp. 507–518. Morgan Kaufmann.
  • Sturtevant Sturtevant, N. R. (2012). Benchmarks for grid-based pathfinding.  IEEE Trans. Comput. Intellig. and AI in Games, 4(2), 144–148.
  • Tung et al. Tung, A. K. H., Hou, J., & Han, J. (2001). Spatial clustering in the presence of obstacles.  In Proceedings 17th International Conference on Data Engineering, pp. 359–367.
  • Xia et al. Xia, C., Hsu, D., & Tung, A. K. H. (2004). A fast filter for obstructed nearest neighbor queries.  In Williams, M. H., & MacKinnon, L. M. (Eds.), Key Technologies for Data Management, 21st British National Conference on Databases, BNCOD 21, Edinburgh, UK, July 7-9, 2004, Proceedings, Vol. 3112 of Lecture Notes in Computer Science, pp. 203–215. Springer.
  • Zhang et al. Zhang, J., Papadias, D., Mouratidis, K., & Zhu, M. (2004). Spatial queries in the presence of obstacles.  In Bertino, E., Christodoulakis, S., Plexousakis, D., Christophides, V., Koubarakis, M., Böhm, K., & Ferrari, E. (Eds.), Advances in Database Technology - EDBT 2004, 9th International Conference on Extending Database Technology, Heraklion, Crete, Greece, March 14-18, 2004, Proceedings, Vol. 2992 of Lecture Notes in Computer Science, pp. 366–384. Springer.
  • Zhao et al. Zhao, S., Taniar, D., & Harabor, D. D. (2018). Fast k-nearest neighbor on a navigation mesh.  In Bulitko, V., & Storandt, S. (Eds.), Proceedings of the Eleventh International Symposium on Combinatorial Search, SOCS 2018, Stockholm, Sweden - 14-15 July 2018, pp. 124–132. AAAI Press.
Comments 0
Request Comment
You are adding the first comment!
How to quickly get a good reply:
  • Give credit where it’s due by listing out the positive aspects of a paper before getting into which changes should be made.
  • Be specific in your critique, and provide supporting evidence with appropriate references to substantiate general statements.
  • Your comment should inspire ideas to flow and help the author improves the paper.

The better we are at sharing our knowledge with each other, the faster we move forward.
The feedback must be of minimum 40 characters and the title a minimum of 5 characters
Add comment
Loading ...
This is a comment super asjknd jkasnjk adsnkj
The feedback must be of minumum 40 characters
The feedback must be of minumum 40 characters

You are asking your first question!
How to quickly get a good answer:
  • Keep your question short and to the point
  • Check for grammar or spelling errors.
  • Phrase it like a question
Test description