*3.3. Problem Definition*

After the above overview and basic definitions, we define the problem that is considered in this paper.

Question Definition: In this paper, we study direction-aware continuous moving KNN queries in road networks in dynamic environments.

In a dynamic environment, if there is a group of moving objects *P* and a query object *q* in the road network, the query is to retrieve the *K*-nearest neighbors from the road network at any timestamp in [*t*0, *t*1] towards the query object, e.g., "A moving passenger inquires about *K* taxis moving towards himself during [*t*0, *t*1] period".

For the example in Figure 3, the global problem is clarified. In edge *ei*(*<sup>w</sup>*,*<sup>s</sup>*), *w* represents the length of the edge and *s* represents the speed limit of *ei* indicating the maximum speed of the moving object on *ei*. For moving object *p*(*s*), *s* represents the speed of object *p*. The query point *q* requests the two nearest-neighbor objects in the time interval [0, 4].

**Figure 3.** Road network at timestamp 0.

#### **4. Direction-Aware Continuous Moving** *K-***Nearest Neighbor Query**

When the query object arrives at the road network node, it records the timestamp and divides the time interval into sub-intervals such that in each sub-interval, the query object moves on the same edge. First, we must execute the direction-aware KNN query algorithm at the starting timestamp to identify the *K*-nearest neighbor objects that are moving towards the query object. Then, the monitoring range of continuous queries is calculated; the moving objects in the monitoring range may affect the query results. After that, a local road network is established according to the monitoring range and then moving candidates are identified. Finally, the timestamp when the candidate objects replace the query results is determined. If the timestamp is within the specified period, the sub-interval is divided, and the result set is modified. Figure 4 shows the basic process of the algorithm.

#### *4.1. Direction-Aware K-Nearest Neighbor Query for Moving Objects*

In determining whether the moving object moves towards query object in road networks, two cases are considered. Case 1: the moving object and the query object are on the same edge. At this time, it is only necessary to determine whether the moving direction of the moving object points towards the query object or not. The moving direction of the moving object is determined according to the azimuth information. Case 2: the moving object and the query object are on the different edge. This paper adopts the method of Direction-aware K-Nearest Neighbor Query [27], in which road network expansion and azimuth angle are used to determine k neighbor objects moving toward the query point. The azimuth angle is used to determine if the direction of moving object is toward the query point, and the direction determination rule is the direction of road network expansion is opposite the direction towards the query object.

As shown in Figure 5, the horizontal angle between the reference point, which is denoted as *n*1, and the target directional line, which is denoted as *n*1*n*2, is α. In the road network, node *ni* is either the starting node or the ending node of edge; hence, the azimuth angle, namely, β, of the road network expansion is β = α or β = α + 180*mod*360. In Figure 6, if the current accessing node is *n*3, its extended adjacent edges are *e*3 and *e*4, the extended direction has two, when the extended edge *e*<sup>3</sup>(*n*3, *<sup>n</sup>*<sup>6</sup>), and the extended direction is *n*3 to *n*6, then β = β3; when the extended edge *e*<sup>4</sup>(*n*3, *n*<sup>4</sup>) and the extended direction is *n*3 to *n*4, β = β2.

**Figure 6.** Azimuth angle of road network expansion.

Using the direction of road network expansion, the rules for judging whether the moving object is moving on road network towards query object are as follows: when the road network extends to the adjacent edge, which is denoted as *ei*(*ni*, *nj*), the direction of moving object is denoted as γ, the direction of road network expansion is denoted as β. The moving object is moving towards the query object if it satisfies the inequality ε-180 ≤ <sup>γ</sup>-β<sup>≤</sup> 180-ε (ε is the minimum deviation of the azimuth).

Based on the method for identifying the moving objects of road network that are moving towards query object, this paper uses R-tree to quickly retrieve the road edges and an INE-based road network expansion method to query the *K*-nearest moving objects, which efficiently identifies the *K*-nearest objects that are moving towards the query object. The algorithm generates a priority queue according to the distance from each network node to the query object and the priority queue controls the expansion order of the network. The closer a node is, the earlier its adjacent edges will be expanded. If an adjacent edge *e* of node *v* is currently expanding and all the moving objects on the adjacent edge have been retrieved, then use the azimuth angle to judge whether these objects are moving towards the current road network node *v*. If *p* is moving towards *v*, the road network distance from *p* to the query object is calculated, and put *p* into the result set. Then, the adjacent edge *e* is marked as "expanded" and this process will continue until there are *K* objects in the result set. Only when the distance from the next road network node to the query object is smaller than the distance from the farthest neighbor of the result set to the query object; otherwise, the algorithm directly returns the *K* objects that are moving towards the query object.

For the problem in Figure 3, since the speed of *q* is 1, the time to reach road network node *n*2 is 8; therefore, in the time interval [0, 4], *q* always moves on the same road edge. In this stage, we must identify the *K*-nearest neighbor object that is moving towards the query object at timestamp 0. Combined with the INE road network extension and the direction determination method, the algorithm process is as follows: first, edge *e*1 is retrieved, object *p*1 is acquired, *p*1 is moved towards the query object, and the distance between *p*1 and the query object is inserted into the result set: *R* = {(*p*1,6)}. Node *n*2 is closest to the query object and adjacent edges *e*2, *e*3, and *e*4 of node *n*2 are extended, *p4* is retrieved on edge *e*2 and the direction of *p*4 is opposite the direction of expansion. Thus, *R* = {(*p*1, 6), (*p*4, 16)}. Next, *p*3 at edge *e*3 is retrieved, since the distance from the query object is 14, which is smaller than p4, R = {(*p*1, 6), (*p*3, 14)} is inserted; on edge *e*4, *p*6 is retrieved and it is determined that *p*6 is consistent with the expansion direction; the moving direction is far from the query object, and the condition is not satisfied. Then, the other end node, namely, *n*1 of *<sup>e</sup>*1*,* is expanded and the moving object on the adjacent edge of *n*1 is retrieved. The algorithm terminates when the distance of the next node of the algorithm is larger than the distance of the second-nearest-neighbor object to the query object. The final result is R = {(*p*1, 6), (p3, 14)}.

#### *4.2. Evaluation of Monitoring Range in Continuous Queries*

The objective of this stage is to obtain the road network monitoring range of the query request. For the time sub-interval, namely [*ti*, *tj*], of the query, the direction-aware road network moving object KNN query technology is used to identify the KNN object that is moving towards the query point and the result set is {*p*1, *p*2 ... , *NNk*}. In this result set, *NNk* is the object farthest from the query point. The monitoring distance (MD) is calculated according to the result set, and it ensures that only an object that is within the monitoring distance can become the final continuous query result. Knowing that the objects in the KNN result set are all moving towards the query point and the distance between *NNk* and the query point is the largest at timestamp *ti*. The MD is calculated as follows:

$$MD\_{q(\text{ti},t\bar{j})} = ND\_{qN\text{Nk}(\text{ti})} + q.s.\*(tj - t\dot{t}) + AD \tag{3}$$

There are three parts as follows: (1) *NDq*,*NNk*(*ti*) represents the distance between *NNk* and the query point at timestamp *ti*; (2) *q*.*s*<sup>∗</sup> (*tj* − *ti*) represents the distance that query point *q* moves between [*ti*, *tj*]; and (3) AD represents an additional distance to ensure that all potential candidates for the KNN results are monitored in the query process within the time interval [*ti*, *tj*]. AD is calculated as follows: firstly, the set of edges arrived after extending the distance of *NDq*,*NNk*(*ti*) + *q*.*s*<sup>∗</sup> (*tj* − *ti*) from the query point is determined and the additional distances of these edges are calculated as *ei*.*<sup>s</sup>* ∗ (*tj* − *ti*). Then, the maximum additional distance is *AD* = max*ei*.*s*<sup>∗</sup> (*tj* − *ti*) .

If the distance from an object on an edge to a query point exceeds *NDq*,*NNk*(*ti*) + *q*.*<sup>s</sup>* ∗ (*tj* − *ti*), but this object moves towards the query point at the maximum speed for this edge, the moving object may be the result of the continuous KNN query. Therefore, we must add an additional distance to avoid missing all possible KNN results.

We consider the example in the previous section. After the DAKNN query processing stage, we obtain *K*-nearest neighbor object R = {(*p*1, 6), (*p*3, 14)}, *NDq*,*p*<sup>1</sup>(0) = 6, *NDq*,*p*<sup>3</sup>(0) = 14. The distance that *q* moves in time interval [0, 4] is 4 and the road network monitoring distance is MD = 14 + 4 + AD, as shown in Figure 7. The objects *p*1 and *p*3 in the result set move towards the query point; hence, the distance between *p*1 or *p*3 and query point must be less than 18 in the time interval [0, 4]. That is, at timestamp 4, the query point arrives at *q*' and a distance of 18 is extended from the query point *q*' and marked with dotted lines. In Figure 7, the marks fall on edges *e*1, *e*2, *<sup>e</sup>*3*,* and *e*4 and AD = max {2\*4, 2\*4, 3\*4, 1\*4} = 12. Therefore, the monitoring range of the road network is 30 and by expanding by a distance of 30 from query point *q*', the monitoring range is obtained and marked with solid lines in Figure 7.

**Figure 7.** An example of monitoring range.

#### *4.3. Identifying Candidate Objects*

This stage is a process to prune the search space and gets all moving objects for which the road network distances are less than MD. The task at this stage is mainly to build a local road network and to obtain candidate objects.

The first task is to build a local road network; all the edges in the monitoring range are added to the list of influential edges for constructing a local road network. We use the road network expansion method to expand the road network from near and far from the road edge where the query point is located. For one road edge, if the distance from one of the nodes to the query point is less than MD, add it to influence edges to participate in the construction of the local road network. It is specifically stated that the Dijkstra algorithm is used to pre-calculate the shortest distance from the nodes in the local road network to the query point. The purpose of pre-calculation is to avoid inefficiencies caused by online distance calculation. That is to say, in the subsequent calculation process of the distance from the moving point to the query point, the pre-calculation improves the efficiency of the algorithm. The information in the local road network is useful information that may affect the query result and the moving object in the local road network is also the candidate object of the query result. Our next task is to obtain the moving object moving towards the query point in the local road network as the candidate object, then use the proposed method to determine the direction of moving object. As shown in Figure 8, the final set of candidate objects is {*p*1, *p*2, *p*3, *p*4}.

**Figure 8.** The Local Road Network.

#### *4.4. Validating Candidate Objects*

After the pruning stage, any object that cannot be the query result is excluded and a candidate object set, namely, {*p*1, *p*2 ... , *pm*} (*m* ≥ *k*), in the specified interval, namely [*t*0, *tn*], is obtained. Since the object is continuously moving, the query result may change within this time interval; if we cannot determine the time point at which the change occurs, queries at multiple timestamps maybe return the same query result. To reduce the cost of repeated query, we proposed the verification algorithm for determining the time points at which the time interval is divided into sub-intervals, which ensures that the query result set remains unchanged over each sub-interval.

In the verification algorithm, we first divided the specified time interval, namely, [*t*0, *tn*] into several sub-intervals, namely, [*t*0, *<sup>t</sup>*<sup>1</sup>], [*t*1, *t*2] ... [*tm* − 1, *tm*], [*tm*, *tn*], based on the time points when the candidates arrive at the nodes of the road network. When the moving objects arrive at the network nodes, the formula for calculating the distance between them and the query point will change. For each sub-interval, the distance between the moving object and the query point is determined via a specified linear function of time.

The objective of the verification algorithm is to identify the time point *tc* of the KNN result set change in each sub-interval [*tm* − 1, *tm*], so that the result set is the same at any timestamp within two consecutive *tcs*. Using the DAKNN method, which was introduced in Section 4.1, to obtain the query result set *Q* of initial time *tm* − 1, the first object to be replaced by other candidate objects in the query result set *Q* must be *NNk*. The distance between the moving object *NNk* and the query point should be recorded as *NDq*,*NNK*(*t*). For each of the candidate objects, the time point at which *NNk* is replaced by *pi* can be calculated via equation *NDq*,*NNk*(*t*) = *NDq*,*p<sup>i</sup>*(*t*)(*tm* − 1 < *t* < *tm*) and the minimum calculated time point can be selected as the time point of change *tc*1. There are two cases as follows: (1) if object *pi* is in the KNN result set, the query result only changes in order and *pi* becomes a new *NNk*; (2) If object *pi* is not in the KNN result set, time subinterval [*tm* − 1, *tm*] is divided into [*tm* − 1, *tc*1] and [*tc*1, *tm*]. For partitioned [*tm* − 1, *tc*<sup>1</sup>], the query results at any time in the subinterval are consistent with those at time *tm* − 1. For partitioned [*tc*1, *tm*], *NNk* of query result *Q* is replaced by *pi* as the query result at time t*c*1 and the above calculation process is repeated until the time of change exceeds *tm*.

We proceed with the example from the previous stage to verify that we have obtained the result set of candidate objects: {*p*1, *p*2, *p*3, *p*4}. In Figure 8, we divide the time interval, namely, [0, 4], into sub-intervals according to the time when the *p*2 arrives at the road network node: [0, 3] and [3, 4]. First, we verify subinterval [0, 3]. At time 0, KNNs = {*p*1, *p*3} and *NNk* = *p*3. Using the equation *NDq*,*NNk*(*t*) = *NDq*,*p*(*t*) to verify each candidate, it is concluded that object *p*4 replaces *p*3 at time 2 and *p*4 is not in the KNN result set; thus, the result set changes at time *tc* = 2. Therefore, time sub-interval [0, 3] is divided into [0, 2] and [2, 3] and the query results are <{*p*1, *p*3}, [0, 2]> and <{*p*1, *p*4}, [2, 3]<sup>&</sup>gt;. For time interval [3, 4], the candidates are validated via the same processes. At time 10/3, *p*1 replaces *p*4 and *p*1 is in the KNN set; the final result is R= <{*p*1, *p*3}, [0, 2]<sup>&</sup>gt;, <{*p*1, *p*4}, [2, 10/3]<sup>&</sup>gt;, and <{*p*4, *p*1}, [10/3, 4]<sup>&</sup>gt;.

#### **5. Results and Analysis**

This section evaluates the DACKNN query algorithm in road networks via detailed comparative test schemes. The performance of the algorithm is measured in terms of the execution time of the CPU. The results demonstrate that the proposed DACKNN method can efficiently process direction-aware continuous *K*-nearest neighbor queries of moving objects in road networks.
