geometric median
The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing the sum of distances to the sample points. This generalizes the median, which has the property of minimizing the sum of distances for one-dimensional data, and provides a central tendency in higher dimensions.
也就是说,中位数就是一个数组里到所有其他数据点的距离之和达到最小值的点。n维的也一样。
一维的中位数满足这个性质,证明的话可以用反证法。可以证明的到的是,中位数往左一点或者往右一点都会造成距离之和增加,所以中位数是到其他点的距离之和最小。
$Geometric Median =\underset{y \in \mathbb{R}^n}{\operatorname{arg\,min}} \sum_{i=1}^m \left \| x_i-y \right \|_2$
然后,问题来了。。。
Q:Given set of points in 2d grid space. Find a grid point such that sum of distance from all the points to this common point is minimum.
eg: p1: [0, 0] p2: [3, 0] p3: [0, 3]
ans: r: [0,0]
sum: 0 + 3 + 3 = 6
这题naive 方法就是$O(n^2)$,求出所有点到其他点的距离之和,再取最小。
这里指的是曼哈顿距离。manhattan distance. 欧式距离不好求,网上人家直接用kmeans。。
参考:
- http://stackoverflow.com/questions/12934213/how-to-find-out-geometric-median
- http://stackoverflow.com/questions/12905663/given-list-of-2d-points-find-the-point-closest-to-all-other-points/12905913#12905913
对于曼哈顿距离,可以先通过预处理,算出在x轴上,每个点到其他x的值的距离之和,这个开销在O(nlgn+2*n)。y轴的同理。
现在我们就能够在O(1)得到所有点到其他点的距离之和(曼哈顿距离)。所以就能够在O(n)中求出最小值了。(最大值都行啊)
bool compareByX(const Point &p1, const Point &p2) {
return p1.x < p2.x;
}
bool compareByY(const Point &p1, const Point &p2) {
return p1.y < p2.y;
}
int maxDistance(vector<Point> &points) {
if (points.empty()) return ;
sort(points.begin(), points.end(), compareByX);
int n = points.size();
vector<int> xdistances(n, ), ydistances(n, );
for (int i = ; i < n; ++i) {
xdistances[i] = xdistances[i - ] + i * (points[i].x - points[i - ].x);
}
int right = ;
for (int i = n - ; i >= ; --i) {
right = right + (n - i - ) * (points[i + ].x - points[i].x);
xdistances[i] += right;
}
// preprocessing based on y
sort(points.begin(), points.end(), compareByY);
for (int i = ; i < n; ++i) {
ydistances[i] = ydistances[i - ] + i * (points[i].y - points[i - ].y);
}
int top = ;
for (int i = n - ; i >= ; --i) {
top = top + (n - i - ) * (points[i + ].y - points[i].y);
ydistances[i] += top;
}
int max = ;
for (int i = ; i < n; ++i) {
if (xdistances[i] + ydistances[i] > max) {
max = xdistances[i] + ydistances[i];
}
}
return max;
}
q神好叼,给他mock interview的时候答出O(n)的。
geometric median的更多相关文章
- 论文笔记(Filter Pruning via Geometric Median for Deep Convolutional Neural Networks Acceleration)
这是CVPR 2019的一篇oral. 预备知识点:Geometric median 几何中位数 \begin{equation}\underset{y \in \mathbb{R}^{n}}{\ar ...
- postgis几何操作函数集
管理操作函数 AddGeometryColumn - Adds a geometry column to an existing table of attributes. By default use ...
- [第四篇] PostGIS:“我让PG更完美!”
概要 本篇文章主要分为几何图形处理函数.仿生变换函数.聚类函数.边界分析函数.线性参考函数.轨迹函数.SFCGAL 函数.版本函数这八部分. Geometry Processing ST_Buffer ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
随机推荐
- Swift3.0语言教程使用字符串创建和初始化字符串
Swift3.0语言教程使用字符串创建和初始化字符串 Swift3.0语言教程使用字符串创建和初始化字符串,在编程语言中,字面值是很常见的数据描述形式.人们可以通过字面所表达的意思,获知其含义,尤其是 ...
- 转 : 用Delphi编写安装程序
http://www.okbase.net/doc/details/931 还没有亲自验证过,仅收藏 当你完成一个应用软件的开发后,那么你还需要为该软件做一个规范化的安装程序,这是程序设计的最后一步 ...
- 疯狂java笔记(五) - 系统交互、System、Runtime、Date类
一.程序与用户交互(Java的入口方法-main方法): 运行Java程序时,都必须提供一个main方法入口:public static void main(String[] args){} publ ...
- # asp.net core 1.0 项目结构
1.环境 开发:VS2015 平台:window 7 2.解决方案级别结构 创建一个ASP.NET 5 Template Empty项目: src:存放项目(projects) global.json ...
- gulp下静态资源的合并、压缩、MD5后缀
var gulp = require('gulp'); var RevAll = require('gulp-rev-all'); var uglify = require('gulp-uglify' ...
- Android入门第八篇之GridView(九宫图)
本文来自http://blog.csdn.net/hellogv/ GridView跟ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选!本文就是介绍如何使用GridVi ...
- Button 设置适应不同版本 旋转以后大小相应的改变
很多时候对于不同的版本,随设备的旋转以后,相应的Button的大小如果不做相应的改变,这很影响视图的美观和布局:下面是小编的个人看法 UIButton *button = [[UIBtton all ...
- 【BZOJ】3456: 城市规划
http://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意:求n个点的无向连通图的方案.(n<=130000) #include <bi ...
- 用clip剪裁实现半圆
定义和用法 clip 属性剪裁绝对定位元素. 说明 这个属性用于定义一个剪裁矩形.对于一个绝对定义元素,在这个矩形内的内容才可见.出了这个剪裁区域的内容会根据 overflow 的值来处理.剪裁区域可 ...
- pdf转word
一.刚需 pdf转word,这个需求肯定是有的.但是大家都知道,pdf是用来排版打印的,所以编辑起来会比较麻烦,所以,大家都会尝试将pdf的内容转成word,然后再进行编辑. 二.方法 1.用offi ...