简体   繁体   English

找出列表中有多少组

[英]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.

相关问题 找出给定邻接矩阵中有多少个连接的节点组 - Find how many connected groups of nodes in a given adjacency matrix 如何使用Java在列表中找到所有可能的4组? - How can one find all possible groups of 4 in a list using Java? 如何找到对元素列表进行分组所需的最小组数? - How to find the minimum number of groups needed for grouping a list of elements? 如何在数字列表中找到“奇数” - How to find the "odd one out" in a list of numbers 找出有多少值小于数组中的一个值 - Find out how many values are smaller up to a value in an array 在具有评级的人员列表中建立两个人的小组 - Build groups of two out of a list of people with rating 组之间共享多少个对象? - How many objects are shared between groups? 按组对列表进行排序的效率如何? - How efficiently sort a list by groups? 如何比较两个列表并找出“已添加”“已删除”“未更改”部分 - how to compare two list and find out `added` `deleted` `unchanged ` part 如何有效地计算数组项之间的差异,并找出多少差异小于某个特定值? - How to efficienlty calculate the difference between array items and find out how many differences are less than a certain value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM