我的图形计算周期有问题。 我想
graph = {('S','A'): ('2','2'), ('A','S'): ('2','2'), ('S','B'): ('3','5'),
('B','S'): ('3','5'), ('S','C'): ('3','6'), ('C','S'): ('3','6'),
('B','D'): ('4','5'), ('D','B'): ('4','5'), ('A','C'): ('2','2'),
('D','C'): ('3','4'), ('C','D'): ('3','4'), ('T','C'): ('5','8'),
('C','T'): ('8','5'), ('T','D'): ('4','7'), ('D','T'): ('4','7')}
print(graph)
{('S', 'A'): ('2', '2'), ('S', 'C'): ('3', '6'), ('T', 'C'): ('5', '8'), ('A', 'S'): ('2', '2'), ('D', 'T'): ('4', '7'), ('C', 'D'): ('3', '4'), ('D', 'B'): ('4', '5'), ('S', 'B'): ('3', '5'), ('C', 'T'): ('8', '5'), ('C', 'S'): ('3', '6'), ('B', 'S'): ('3', '5'), ('T', 'D'): ('4', '7'), ('B', 'D'): ('4', '5'), ('D', 'C'): ('3', '4'), ('A', 'C'): ('2', '2')}
我想扩展图形,但不存在循环。例如:
vs = 'S'
vt = 'T'
('S', 'A'): ('2', '2'), ('S', 'B'): ('3', 5'), ('S', 'C'): ('3', '6')
展开:
('S', 'A', 'C') : ('S', 'A') + ('A', 'C') = ('2', '2') + ('2', '2') =
('4', '4')
('S', 'B', 'D') : ('S', 'B') + ('B', 'D') = ('3', '5') + ('4', '5') =
('7', '10')
('S', 'C', 'A') : not expand
('S', 'C', 'S') : not expand
('S', 'C', 'T') : ('S', 'C') + ('C', 'T') = ('3', '6') + ('5', '8') =
('8', '14')
('S', 'C', 'D') : ('S', 'C') + ('C', 'D') = ('3', '6') + ('3', '4') =
('6', '10')
输出(预期):
('S', 'A', 'C') : ('4', '4')
('S', 'B', 'D') : ('7', '10')
('S', 'C', 'A') : not expand
('S', 'C', 'S') : not expand
('S', 'C', 'T') : ('8', '14')
('S', 'C', 'D') : ('6', '10')
您能帮我解决上述问题吗?非常感谢您。
答案 0 :(得分:0)
我没有收到您的问题,但这是您希望的吗?
graph = {('S','A'): ('2','2'), ('A','S'): ('2','2'), ('S','B'): ('3','5'),
('B','S'): ('3','5'), ('S','C'): ('3','6'), ('C','S'): ('3','6'),
('B','D'): ('4','5'), ('D','B'): ('4','5'), ('A','C'): ('2','2'),
('D','C'): ('3','4'), ('C','D'): ('3','4'), ('T','C'): ('5','8'),
('C','T'): ('8','5'), ('T','D'): ('4','7'), ('D','T'): ('4','7')}
sub = {}
for keys, values in graph.items():
for keys2, values2 in graph.items():
if keys[1] == keys2[0]:
key = keys + keys2[1:]
valueB= int(values[0]) + int(values2[0])
valueE = int(values[1]) + int(values2[1])
value = (str(valueB), str(valueE))
sub[key] = value
print(sub)
输出:
{('S', 'A', 'S'): ('4', '4'), ('S', 'A', 'C'): ('4', '4'), ('A', 'S', 'A'): ('4', '4'), ('A', 'S', 'B'): ('5', '7'), ('A', 'S', 'C'): ('5', '8'), ('S', 'B', 'S'): ('6', '10'), ('S', 'B', 'D'): ('7', '10'), ('B', 'S', 'A'): ('5', '7'), ('B', 'S', 'B'): ('6', '10'), ('B', 'S', 'C'): ('6', '11'), ('S', 'C', 'S'): ('6', '12'), ('S', 'C', 'D'): ('6', '10'), ('S', 'C', 'T'): ('11', '11'), ('C', 'S', 'A'): ('5', '8'), ('C', 'S', 'B'): ('6', '11'), ('C', 'S', 'C'): ('6', '12'), ('B', 'D', 'B'): ('8', '10'), ('B', 'D', 'C'): ('7', '9'), ('B', 'D', 'T'): ('8', '12'), ('D', 'B', 'S'): ('7', '10'), ('D', 'B', 'D'): ('8', '10'), ('A', 'C', 'S'): ('5', '8'), ('A', 'C', 'D'): ('5', '6'), ('A', 'C', 'T'): ('10', '7'), ('D', 'C', 'S'): ('6', '10'), ('D', 'C', 'D'): ('6', '8'), ('D', 'C', 'T'): ('11', '9'), ('C', 'D', 'B'): ('7', '9'), ('C', 'D', 'C'): ('6', '8'), ('C', 'D', 'T'): ('7', '11'), ('T', 'C', 'S'): ('8', '14'), ('T', 'C', 'D'): ('8', '12'), ('T', 'C', 'T'): ('13', '13'), ('C', 'T', 'C'): ('13', '13'), ('C', 'T', 'D'): ('12', '12'), ('T', 'D', 'B'): ('8', '12'), ('T', 'D', 'C'): ('7', '11'), ('T', 'D', 'T'): ('8', '14'), ('D', 'T', 'C'): ('9', '15'), ('D', 'T', 'D'): ('8', '14')}
我的代码所做的只是在字典中搜索每个键,然后查找第一个键的结束字符并与第二个键的开始字符进行比较。如果相同,那么我将其合并。然后,我将合并这些字符的值并将其存储在集合中。
我也在学习,但是我想尝试提供帮助,所以也许有些专家可以帮助我们。