按拓扑分组
如果这个问题已经在以下位置得到回答,我深表歉意: 带分组的拓扑排序
但是,我不完全理解答案,因为我是图论新手。
我有以下项目:
c01,a11,b12,a21, b22,c23, c31,b32, a33.
每一项都是一个三元组。
Tup[0]
: '分组依据字母'
Tup[1]
: '依赖项有效的组号'
Tup[2]
: “依赖项的排序顺序”
我希望尽可能地按 tup[0]
进行分组,同时保持 item[1]
和 item[1]
中的组描述的排序顺序代码>项目[2]。第 1,2 项允许我们创建依赖项,从这里我们只需要创建组。
所以我们可以创建以下依赖关系:
a11<-b12
a21<-b22, b22<-c23
c31<-b32, b32<-a33
c01
从这里我想按字母分组,同时保持依赖关系。一种这样的解决方案是
a11, a21, b12, b22, c01, c23, c31, b32, a33
我们可以看到 a11<-b12, a21<-b22<-c23, c31<-b32<-a33, c01
任何想法将不胜感激, 谢谢, 抢
一个解决方案:
def groupPreserveSorted(listOfPairs):
"""
we want to group by tup[0], but maintain the order passed in according to tup[1]
>>> lop = [['A',0], ['B',1], ['C',0], ['D',2], ['E',2]]
>>> print groupPreserveSorted(lop)
[('A', 0), ('B', 1), ('C', 0), ('D', 2), ('E', 2)]
>>> lop = [['c',0], ['a',1], ['b',1], ['a',2], ['b',2], ['a', 3], ['b', 3], ['c', 3], ['a', 4], ['b', 4]]
>>> print groupPreserveSorted(lop)
[('c', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4), ('b', 1), ('b', 2), ('b', 3), ('b', 4), ('c', 3)]
>>> lop = [['c',0], ['a',1], ['b',1], ['a',2], ['b',2], ['a', 3], ['b', 3], ['c', 3], ['c', 4], ['a', 4], ['b', 4]]
>>> print groupPreserveSorted(lop)
[('c', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 3), ('c', 4), ('a', 4), ('b', 4)]
"""
groupCount = 0
groupMap = {} #map contains the "level" to the highest group
maxGroupDic = {} #this contains a map from tup[1] to the highest level attained by tup[1]
groupTypeToMapItem = {} #this contains all the levels that items in tup[0] are placed on
for groupType, dependencyGroup in listOfPairs:
if groupCount == 0:
groupMap[0] = [(groupType, dependencyGroup)]
maxGroupDic[dependencyGroup] = 0
groupTypeToMapItem[groupType] = [0]
groupCount+=1
else:
if groupType not in groupTypeToMapItem:#need to make new group
groupMap[groupCount] = [(groupType, dependencyGroup)]
maxGroupDic[dependencyGroup] = groupCount
groupTypeToMapItem[groupType] = [groupCount]
groupCount+=1
else:
maxGroupTypeItem = groupTypeToMapItem[groupType][-1]
if dependencyGroup in maxGroupDic: #then we just need to check where to add to a new level or to an old level
maxItem = maxGroupDic[dependencyGroup]
if maxItem>maxGroupTypeItem: #then we need to make a enw group
groupMap[groupCount] = [(groupType, dependencyGroup)]
maxGroupDic[dependencyGroup] = groupCount
groupTypeToMapItem[groupType] = [groupCount]
groupCount+=1
else:
countToUse = [item for item in groupTypeToMapItem[groupType] if item>=maxItem][0]
groupMap[countToUse].append((groupType, dependencyGroup))
maxGroupDic[dependencyGroup]=countToUse
else: #we haven't added this groupType yet just add to lowest level
countToUse = groupTypeToMapItem[groupType][0]
groupMap[countToUse].append((groupType, dependencyGroup))
maxGroupDic[dependencyGroup]=countToUse
return flatten([groupMap[count] for count in xrange(groupCount)], depth = 1)
这是一个很好的解决方案,因为它是 o(n),但它绝对不是最干净的答案:)
I apologize if this question has already been answered in:
Topological Sort with Grouping
However, I do not completely understand the answer as I am new to graph theory.
I have the following items:
c01,a11,b12,a21, b22,c23, c31,b32, a33.
Each of these is a three tuple.
Tup[0]
: 'letter to group by'
Tup[1]
: 'group number where dependencies are valid'
Tup[2]
: 'sort order of dependencies'
I would like to group by tup[0]
as closely as possible while maintaining the sort order described by the groups in item[1]
and item[2]
. Items 1,2 allow us to create the dependencies, from here we just need to create the groups.
so we can create the following depencies:
a11<-b12
a21<-b22, b22<-c23
c31<-b32, b32<-a33
c01
From here I would like to group by letter while maintaining the dependencies. One such solution would be
a11, a21, b12, b22, c01, c23, c31, b32, a33
We can see that a11<-b12, a21<-b22<-c23, c31<-b32<-a33, c01
Any thoughts would be greatly appreciated,
Thanks,
Rob
one solution:
def groupPreserveSorted(listOfPairs):
"""
we want to group by tup[0], but maintain the order passed in according to tup[1]
>>> lop = [['A',0], ['B',1], ['C',0], ['D',2], ['E',2]]
>>> print groupPreserveSorted(lop)
[('A', 0), ('B', 1), ('C', 0), ('D', 2), ('E', 2)]
>>> lop = [['c',0], ['a',1], ['b',1], ['a',2], ['b',2], ['a', 3], ['b', 3], ['c', 3], ['a', 4], ['b', 4]]
>>> print groupPreserveSorted(lop)
[('c', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4), ('b', 1), ('b', 2), ('b', 3), ('b', 4), ('c', 3)]
>>> lop = [['c',0], ['a',1], ['b',1], ['a',2], ['b',2], ['a', 3], ['b', 3], ['c', 3], ['c', 4], ['a', 4], ['b', 4]]
>>> print groupPreserveSorted(lop)
[('c', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 3), ('c', 4), ('a', 4), ('b', 4)]
"""
groupCount = 0
groupMap = {} #map contains the "level" to the highest group
maxGroupDic = {} #this contains a map from tup[1] to the highest level attained by tup[1]
groupTypeToMapItem = {} #this contains all the levels that items in tup[0] are placed on
for groupType, dependencyGroup in listOfPairs:
if groupCount == 0:
groupMap[0] = [(groupType, dependencyGroup)]
maxGroupDic[dependencyGroup] = 0
groupTypeToMapItem[groupType] = [0]
groupCount+=1
else:
if groupType not in groupTypeToMapItem:#need to make new group
groupMap[groupCount] = [(groupType, dependencyGroup)]
maxGroupDic[dependencyGroup] = groupCount
groupTypeToMapItem[groupType] = [groupCount]
groupCount+=1
else:
maxGroupTypeItem = groupTypeToMapItem[groupType][-1]
if dependencyGroup in maxGroupDic: #then we just need to check where to add to a new level or to an old level
maxItem = maxGroupDic[dependencyGroup]
if maxItem>maxGroupTypeItem: #then we need to make a enw group
groupMap[groupCount] = [(groupType, dependencyGroup)]
maxGroupDic[dependencyGroup] = groupCount
groupTypeToMapItem[groupType] = [groupCount]
groupCount+=1
else:
countToUse = [item for item in groupTypeToMapItem[groupType] if item>=maxItem][0]
groupMap[countToUse].append((groupType, dependencyGroup))
maxGroupDic[dependencyGroup]=countToUse
else: #we haven't added this groupType yet just add to lowest level
countToUse = groupTypeToMapItem[groupType][0]
groupMap[countToUse].append((groupType, dependencyGroup))
maxGroupDic[dependencyGroup]=countToUse
return flatten([groupMap[count] for count in xrange(groupCount)], depth = 1)
this is a nice solution as it is o(n), but it is definitely not the cleanest answer:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的解决方案
或作为单行解决方案
This is my solution
or as a single line solution