dsepcxw181184853 2018-06-28 15:16
浏览 61

php算法将一组数据排序成不超过四个的组

I currently have the problem that I have a dataset of about 1000 entries.

Each entry has two relevant features:

  • weight (float)
  • origin (string / another entity)

I have to sort those entries into groups of max. four entries. Groups can contain less entries, though.

  • group of four entries: very good
  • group of three entries: good
  • group of two entries: not so good
  • group of one entry: really bad (but possible if not avoidable)

Now, the way those entries are being sorted into the groups depend on their features in the following way:

  • the max. delta of weight within a group can be 10%.
  • there should be as many different values for origin as possible in each group. Having one duplicate is not so bad, but having three or more entries with the same origin should be avoided.

Within the dataset weight has a range of roughly 20.0 to 120.0. There are about 50 different possible values for origin.

I have to implement this in php, but answering with a php implementation is not necessary. The algorithm alone would be enough.

I have tried sorting all values for their weight and then simply split them every fourth entry. But the groups I then get are hard to rearrange with regard to the origin value. I think I could somehow get this done through a nasty implementation, but I hope there is a very elegant algorithm that can do just that.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doulouli8686 2018-06-28 16:48
    关注

    Here is a greedy that might give good results:

    Sort entried by weight
    groups = []
    used = array of length len(entries) initialized in false    
    For i = 0 to len(entries):
        if (used[i] == false):
            group = [entries[i]]
            j = i + 1
            while(j < len(entries) and delta(group[0], entries[j]) < 10 and len(group) < 4):
               if used[j] == false and entries[j].origin != all the origins in group:
                   group.add(entries[j])
                   used[j] = true
               j = j + 1
            if (len(group) < 4):
                //decide if you prefer a small group or a bigger group with repeated origins
            groups.add(group)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据