Last active
December 8, 2017 13:31
-
-
Save rkt2spc/3bedf01a5020310e82c3 to your computer and use it in GitHub Desktop.
Find the point closest to all other points in a given points-collection O(NlogN)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <stdio.h> | |
#include "Point2D.h" | |
using namespace std; | |
//Functions | |
Point2D FindMedianPoint(vector<Point2D> points); | |
vector<Point2D> GenerateRandomPoints(int count, int lowerH = 0, int upperH = 10, int lowerV = 0, int upperV = 10); | |
int main() | |
{ | |
vector<Point2D> points = GenerateRandomPoints(10); | |
for (int i = 0; i < 10; i++) | |
printf("[%d,%d] ", points[i].x, points[i].y); | |
Point2D medianPoint = FindMedianPoint(points); | |
printf("\n\nMedianPoint: [%d,%d]\n", medianPoint.x, medianPoint.y); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <algorithm> | |
#include "Point2D.h" | |
using namespace std; | |
Point2D FindMedianPoint(vector<Point2D> points) | |
{ | |
#pragma region Calculating Horizontal Cost | |
//=========================================================================================== | |
//O(NLogN) Sort - sort horizontal x-coor increasing | |
sort(points.begin(), points.end(), [](Point2D a, Point2D b) { | |
return a.x < b.x; | |
}); | |
//O(N) Calculate the total horizontal distance from each point to all the points from its left-side | |
vector<int> lDist(points.size(), 0); | |
for (int i = 1; i < points.size(); i++) | |
{ | |
lDist[i] += (points[i].x - points[i - 1].x) * i; | |
lDist[i] += lDist[i - 1]; | |
}; | |
//O(N) Calculate the total horizontal distance from each point to all the points from its right-side | |
vector<int> rDist(points.size(), 0); | |
for (int i = points.size() - 2; i >= 0; i--) | |
{ | |
rDist[i] += (points[i + 1].x - points[i].x) * (points.size() - (i + 1)); | |
rDist[i] += rDist[i + 1]; | |
}; | |
//O(N) hCost[i] = lDist[i] + rDist[i] | |
for (int i = 0; i < points.size(); i++) | |
points[i].hCost = lDist[i] + rDist[i]; | |
//=========================================================================================== | |
#pragma endregion | |
#pragma region Calculating Vertical Cost | |
//=========================================================================================== | |
//O(NLogN) Sort - sort vertical y-coor increasing | |
sort(points.begin(), points.end(), [](Point2D a, Point2D b) { | |
return a.y < b.y; | |
}); | |
//O(N) Calculate the total vertical distance from each point to all the points from its upper-side | |
vector<int> upDist(points.size(), 0); | |
for (int i = 1; i < points.size(); i++) | |
{ | |
upDist[i] += (points[i].y - points[i - 1].y) * i; | |
upDist[i] += upDist[i - 1]; | |
}; | |
//O(N) Calculate the total vertical distance from each point to all the points from its lower-side | |
vector<int> lwDist(points.size(), 0); | |
for (int i = points.size() - 2; i >= 0; i--) | |
{ | |
lwDist[i] += (points[i + 1].y - points[i].y) * (points.size() - (i + 1)); | |
lwDist[i] += lwDist[i + 1]; | |
}; | |
//O(N) hCost[i] = lDist[i] + rDist[i] | |
for (int i = 0; i < points.size(); i++) | |
points[i].vCost = upDist[i] + lwDist[i]; | |
//=========================================================================================== | |
#pragma endregion | |
//O(N) The Point closest to all other-points will have the smallest hCost & vCost | |
Point2D result = points[0]; | |
for (int i = 1; i < points.size(); i++) | |
{ | |
if (points[i].TotalCost() < result.TotalCost()) | |
result = points[i]; | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class Point2D | |
{ | |
public: | |
int hCost = 0; | |
int vCost = 0; | |
int x, y; | |
Point2D(int X, int Y) | |
{ | |
x = X; | |
y = Y; | |
} | |
int TotalCost() | |
{ | |
return hCost + vCost; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <stdlib.h> | |
#include <time.h> | |
#include "Point2D.h" | |
using namespace std; | |
vector<Point2D> GenerateRandomPoints(int count, int lowerH = 0, int upperH = 10, int lowerV = 0, int upperV = 10) | |
{ | |
srand(time(NULL)); | |
vector<Point2D> randomPoints; | |
for (int i = 0; i < count; i++) | |
{ | |
int x = rand() % upperH + lowerH; | |
int y = rand() % upperV + lowerV; | |
randomPoints.push_back(Point2D(x, y)); | |
} | |
return randomPoints; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment