Source http://blog.csdn.net/shunan/archive/2007/05/28/1628238.aspx
Typedef struct Node
{
Int X;
Int y;
} Mynode;
Typedef struct nodel
{
Int start;
Int num;
Edge * firstedge;
} Mynodel;
Typedef struct edge
{
Int mainnum;
Int adherenum;
Double angle;
Struct edge * pre;
Struct edge * next;
Struct edge * Twin;
} Edge;
Typedef struct mpiedge
{
Int mainnum;
Int adherenum;
Double angle;
} Myedge;
1. mynode structure. The Node array stores the abscissa and ordinate of the point set on the plane.
2. Edge Structure, edge node. Save the vertex information adjacent to the vertex with the serial number mainnum: the serial number of the vertex adjacent to the vertex adherenum; radian (0 <= ,,, <= 2 *); Pre and next fields are the first and last edge nodes in the two-way linked list; the twin field is the edge node corresponding to this node (that is, the vertex with the serial number mainnum becomes an adjacent vertex with the serial number adherenum)
3. mynodel structure: Start is the sequence number of the vertex adjacent to this vertex; num is the number of the vertex adjacent to this vertex; the firstedge field points to the node with the smallest angle in the edge node adjacent to this vertex (the angle of the adjacent edge node with the serial number start is 0)
4. The myedge structure only extracts the first three elements of the edge structure to form a node type, mainly to meet the requirements of MPI functions during communication (custom communication element types) used to determine the edge type
If you only need to implement the serial KNN graph algorithm, you can merge the mynode and mynodel structures, or use the myedge structure.
Input: Point Set p composed of n> 1 Non-overlapped point in the Coordinate Plane
Output: The constructed canvas V (P)
1. Sort the N points by the X coordinate value as the primary keyword, And the Y coordinate value as the secondary keyword.
2. Start construction:
2.1 If n = 2 then: Construct the canvas of the two points and return
2.2 If n = 3 then: Construct the three vertex's KNN graph and return
2.3 divide N points into two parts, PL, PR
2.4 construct a point set PL into a canvas V (PL)
2.5 construct a point set PR into a canvas V (PR)
2.6 merge V (PL) and V (PR) into V (P)
2.7 return V (P)
Input: generated V (PL), V (PR)
Output: merged V (P), P = plupr
1 calculate the convex hull CH (PL) and CH (PR) of PL and pr respectively)
2 BCT unzip bottomconnected_tangent (CH (PL), CH (PR ))
3 uct upconnected_tangent (CH (PL), CH (PR ))
4 l left point of BCT
R branch right point of BCT
5 while (not (BCT = UCT) do
5.1 isright outer isleft outer false
5.2 initedge (edgel, edger, L, R)
5.3 insertedge (L, edgel)
Insertedge (R, Edger)
5.4/* tests the points adjacent to R in PR (clockwise)
5.4.1 edger in pre-R is directed clockwise to the next half edge node
5.4.2 if pre! = NULL and pre-correlated points are located on the left side of the LR vector.
5.4.2.1 the next half edge node in the PRE clockwise direction in the pre1 half R
5.4.2.2 while pre1! = NULL and pre1: The point associated with do in the outer circle of a triangle consisting of L, R, and Pre
5.4.2.2.1 deleteedge (pre)
5.4.2.2.2 pre-defined pre1
5.4.2.2.3 in pre1 half R, the PRE is clockwise toward the next half edge node.
5.4.3 else isright enabled true
5.5/* perform a test on the point adjacent to L in PL (in the clockwise direction)
5.5.1 In next half l, edgel is directed to the next half edge node counterclockwise.
5.5.2 if next! = NULL and next
5.5.2.1 in next1 limit L, next is directed to the next half edge node counterclockwise.
5.5.2.2 while next1! = NULL and next1 the point associated with do in the outer circle of a triangle consisting of L, R, and next
5.5.2.2.1 deleteedge (next)
5.5.2.2.2 next generation next1
5.5.2.2.3 in next1 limit L, next is directed to the next half edge node counterclockwise.
5.5.3 else isleft limit true
5.6 If isright then l then next
5.7 else if isleft then r semi pre correlated vertex number
5.8 The points associated with else if next are the then in the outer circle of a triangle consisting of L, R, And pre.
L vertex sequence number associated with interval next
5.9 else R semi pre correlation vertex number
6 initedge (edgel, edger, L, R)
7 insertedge (L, edgel)
Insertedge (R, Edger)
The initedge (edgel, edger, L, R) function initializes Two Half Edge Nodes, edgel and edger. edgel will be inserted into the edge linked list of L in the future, make r the adjacent vertex of L (edgel-> mainnum 1_l, edgel-> adherenum 1_r); edger is the opposite.
The insertedge (number, edge) function inserts the edge of the half edge node into the edge linked list of the vertex with the serial number, because a circular edge linked list maintained by each vertex is stored in a counter-clockwise direction of the adjacent vertex, to maintain this feature after each insertion, before insertion, calculate the point to be inserted to the first vertex (as shown in the Start sequence number in the mynodel structure, the value is assigned during the first insertion, And the node will not be changed even if it is deleted for reference) according to the angle to determine the inserted place (the angle order means that those adjacent points are arranged in the counterclockwise direction ).
The edge eedge (edge) function deletes the edge of the half edge node and its corresponding Half Edge (the half edge node pointed to by the twin field.
Because the length is too long, the description of the algorithm is omitted here (such as convex hull, BCT, uct, etc)
Parallelization of KNN graph Splitting Algorithm
Currently, there are two parallel algorithms used to construct a canvas: one is to use the number of P = O (n) processors of the same magnitude as the plane point set, the MPI-based parallel algorithm can achieve O (lgn * lgn) Complexity and is suitable for scenarios with very high time requirements. Obviously, such a processing environment costs a lot, when the number of plane point sets is small, it does not reflect the superiority of such algorithms, because communication takes up most of the time. For example, bitonic sort can be used to describe such situations, because my parallel machine has 32 processors, when I run the bitonic sort algorithm implemented by MPI to sort 64 data (each processor stores two data, the time is already in milliseconds, and the fast sorting time for 64 data can be ignored. In microseconds, however, bitonic sort is theoretically analyzed, its time complexity is only O (lgn * lgn), and it is a sort algorithm that is very suitable for parallel processing. It can be seen that, because it uses many processors, compared with the data processing time, the communication time between processors occupies the entire algorithm. The execution time is very large. Another method is to use a constant P processor to process N points in a flat point set. This algorithm is easy to implement and significantly increases speed, which is very suitable for general computing.
In theory, we can also refer to the preceding Method for fast sorting and parallelization for constructing the divide and conquer algorithm of The KNN graph. However, when constructing a graph and processing it in parallel, there is a lot of information to be exchanged between processors. Unlike parallel fast sorting, it is convenient to divide the data into two parts and send the whole part to the corresponding processor.
Gridded
In the sense of the nearest vertex, the dual line graph of The KNN graph is the segmentation algorithm of the previously described structure.
Incremental algorithm: this algorithm is easy to implement and widely used. It is suitable for the triangle of small-scale point sets. The specific process is as follows:
1. traverse all the Scattered Points and generate a large triangle containing all the scattered points (the vertex is not in the vertices concentration)
2. point P has not been processed. insert it. Otherwise, end the algorithm and exit.
3. In the split Triangle network, find the triangle T containing point P, and connect the three vertices of point P to form three triangles.
4. Optimize the locally generated triangles based on the optimization criteria (Interchange diagonal lines, etc)
5. Return to Step 1
Local transformation method: according to the nature of the Adaboost triangle division 2, we first construct a triangle network that does not meet the conditions of the Adaboost triangle division, then, the convex quadrilateral iteration edges made up of two common edge triangles satisfy the conditions of the DCT (mainly the method of exchanging diagonal lines)