The strategy of rank-finding (maxium rank finding) based on divide-and-conquer method

Source: Internet
Author: User

A summary of rank finding's questions is as follows:

In a group of discrete points, check if the x and Y values are less than its points for each point, and if so, the rank of the point is the number of points less than it.

The thought of divide and conquer law:

Recursively divides the target area into two parts, based on the median of x values in all points on the interface, until the points in each region are below one, and for each small area, check the Y value of the point in its left child area and the Y value of the point in the right child area to The rank of the points in the child is counted.

This method eliminates the point of non-conforming horizontal position and effectively reduces the number of statistics.

The code examples are as follows:

#include <algorithm>#include<iostream>#include<vector>#include<cstring>Const intMax_n = +;structpoint{intx; inty;};BOOLcmpx (Point A, point B) {returnA.x <b.x;}BOOLCmpy (Point A, point B) {returnA.y <b.y;}intRank[max_n][max_n]; Point S[max_n];//Point set, start point, end +1voidRankfinding (Point s[],int  from,intTo ) {    intn = to- from; if(N <=0)    {        return; }    if(n = =1) {rank[s[ from].x][s[ from].Y] =0; return; } std::sort (S+ from, S + to, cmpx);//n*log (n)    intL = (to + from) /2; Rankfinding (S), from, l);//2*t (N/2)rankfinding (S, L, to); Std::sort (S+ from, S + L, cmpy);//(N/2) *log (N/2)Std::sort (S + L, S +to, Cmpy);  for(inti = l; I < to; ++i) { for(intj = from; J < L; ++j) {if(S[i].y >s[j].y) {RANK[S[I].X][S[I].Y]++; }        }    }}intMain () {memset (rank,-1,sizeof(rank)); memset (S),0,sizeof(S)); s[0] = {1,1 }; s[1] = {1,3 }; s[2] = {2,5 }; s[3] = {6,2 }; s[4] = {9,3 }; s[5] = {4,8 }; s[6] = {7,6 }; s[7] = {5,5 }; s[8] = {2,3 }; Rankfinding (S),0,9);  for(inti =0; i < max_n; ++i) { for(intj =0; J < Max_n; ++j) {if(Rank[i][j]! =-1) {Std::cout<< I <<' '<< J <<' '<< Rank[i][j] <<Std::endl; }        }    }}

tags in code, complexity

T (n) =2*t (N/2) +n*log (n) +2* (N/2) * (log (N/2))

=2*t (N/2) +n* (2*log (n)-1) =2* (T (N/2) +n*log (n)-N)

T (2^k) =2* (t (2^ (K-1)) + (k-1) * (2^k))) =2^ (k-1) * (t (1)) + (2^k) * (k-1) ^2= (2^k) * (t (1)/2+ (k-1) ^2)

T (n) =n* (t (1)/2+ (log (n)) ^2)

So the complexity is O (n (^2))

The strategy of rank-finding (maxium rank finding) based on divide-and-conquer method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.