algorithm - 如何找到连接不同联合之间不同节点的最佳方式?
问题描述
描述:
有 1000 个联合,每个联合包含 x 个节点(x 是 1~100 之间的随机值)。现在我们可以创建从联合 A 中的一个节点到联合 B 中的另一个节点的连接。
规则是:
1. one node only accepts just one connection.
2. The connection must be across different unions.
同样,尽可能多地创建此类连接。
最后,可能还有几个节点无法连接,因为其他联合中没有其他可用节点。
例如:
Union 1: a b c
Union 2: d e
Union 3: f g h i j
如果我们选择以下连接:
U1.a <-> U2.d
U1.b <-> U2.e
U1.c <-> U3.f
h i j
in union 3 将被留下。
但是如果我们使用另一种连接:
U1.a <-> U3.f
U1.b <-> U3.g
U1.c <-> U3.h
U2.d <-> U3.i
U2.e <-> U3.j
那么就没有节点了。
所以问题是:
我们如何设计算法来尝试找到使无连接节点最少的最优解?
解决方案
这相当于分区问题,其中输入多重集中的每个元素都是联合的长度。此外,这个问题总是可以通过在 O(n) 时间内运行的简单贪心实现来解决。例如,考虑以下输入:
Union 1: a a a
Union 2: b b b b b b b b b
Union 3: c c c c c c c c c c
Union 4: d d d d d d d d d d
Union 5: e e
简单的贪心算法创建两个输出列表。对于每个联合(从具有最多元素的联合开始),联合的元素被添加到较短的输出列表中。结果是两个这样的列表:
c c c c c c c c c c b b b b b b b b b
d d d d d d d d d d a a a e e
下一步是从较长列表的末尾取出一些项目并将它们添加到较短列表的开头。在此示例b
中,移动了两个 s:
c c c c c c c c c c b b b b b b b
b b d d d d d d d d d d a a a e e
那么,它会一直有效吗?是的,唯一的例外是当一个联合包含超过一半的项目总数时。在这种情况下,不会从较长的列表中移动任何项目。
这是python中的示例实现:
inputList = [['a','a','a'],
['b','b','b','b','b','b','b','b','b'],
['c','c','c','c','c','c','c','c','c','c'],
['d','d','d','d','d','d','d','d','d','d'],
['e','e']]
topCount = 0
botCount = 0
topList = []
botList = []
# sort the input in descending order based on length
inputList = sorted(inputList, key=lambda x:len(x), reverse=True)
# greedy partitioning into two output lists
for itemList in inputList:
if topCount <= botCount:
topList += itemList
topCount += len(itemList)
else:
botList += itemList
botCount += len(itemList)
# move some elements from the end of the longer list to the beginning of the shorter list
if topList[0] != topList[-1]:
if topCount > botCount+1:
excess = (topCount - botCount) // 2
botList = topList[-excess:] + botList
topList = topList[:-excess]
elif botCount > topCount+1:
excess = (botCount - topCount) // 2
topList = botList[-excess:] + topList
botList = botList[:-excess]
print topList
print botList
输出是:
['c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'b', 'b']
['b', 'b', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'a', 'a', 'a', 'e', 'e']
推荐阅读
- mongodb - 从 mongodb 中的三个集合中获取结果
- node.js - AWS lamda 在回调命中之前退出
- android - 找不到符号方法 getArguments()
- javascript - Thymeleaf 内联 javascript 表达式与变量
- rest - 注册期间现有电子邮件的 422 或 409 状态代码
- maven - 覆盖配置文件中的插件配置
- php - 使用 cURL 发布 JSON 不起作用
- android - 每次都不显示键盘
- python - 我正在编写一个程序,它将用户输入的数字相加,然后输出总数。有了总数,我如何输出所有输入的数字
- sql - 奇怪的选择行为:加入有效,where 子句无效 - 为什么和从何时开始?