I have some points like (g1, g2). I want to find median point by following formula:
a naive solution is to use a nested loop but it's so inefficient for a large space (about 1 million points). does anyone know a linear time algorithm/code or near linear time for doing this in c++?
I implement the naive solution as following but it does not efficient for my problem:
vector<double> get_dists(unordered_map<string, label*> myGraph) {
vector<double> sum_dists;
for (unordered_map<string, label*>::iterator it = myGraph.begin(); it != myGraph.end(); it++) {
double d = 0;
for (unordered_map<string, label*>::iterator it2 = myGraph.begin(); it2 != myGraph.end(); it2++)
{
d += dist(it->second->num, it2->second->num)
}
sum_dists.push_back(d);
}
return sum_dists;
}
it return a vector of sum of distances and the it can use to get minimum element index.