[英]find out how many groups are in a list
I have a list with each value in the interval 0..100. 我有一个列表,每个值的范围是0..100。
Ex: 例如:
[0,1,2,20,21,29,30,31,31,32,89,90,91,92,92,92,92]
Is there an algorithm that will determine how many groups are in this list? 是否有一种算法可以确定此列表中有多少个组? For the above list it should say that there are 3 groups (0..2), (20..32), (89..92)
. 对于上面的列表,应该说有3个组(0..2), (20..32), (89..92)
。
As far as I know there is the k-means algorithm that will split the list in a certain number of groups, but in my case, I need to determine how many groups are in the first place, so k-means doesn;t really help for this. 据我所知,有一种k-means算法会将列表分成一定数量的组,但是就我而言,我需要确定首先有多少个组,所以k-means并不是真的。为此。
I will need to do this in PHP if the language has any relevance. 如果该语言有任何相关性,则需要在PHP中执行此操作。
If you know that your groups are separable by a certain minimum distance d
, you can count the groups as by counting all consecutive elements that are not in the same group: 如果您知道您的组可分开一定的最小距离d
,则可以通过对不在同一组中的所有连续元素进行计数来对组进行计数:
Input: A, d
Output: num_groups
sort(A)
for i = 0 ... size(A) - 2
if A[i + 1] - A[i] >= d
num_groups++
If you have yet to determine the optimal minimum distance such that there are at most k
groups, you can determine this value by computing the adjacent differences and selecting the k
th largest element: 如果尚未确定最佳最小距离,使得最多有k
组,则可以通过计算相邻差并选择第k
个最大元素来确定该值:
Input: A of size n, k
Output: d
sort(A)
adj_diff = array of n - 1 elements
for i = 0 ... n - 2
adj_diff[i] = A[i + 1] - A[i]
sort(adj_diff)
d = adj_diff[n - 1 - k]
if d == adj_diff[n - 2 - k] // to make sure there are at most k groups,
d++ // otherwise we might have multiple groups
// with the same distance to their neighbors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.