使用Python从树中提取所有可能的最长路径

使用Python从树中提取所有可能的最长路径,python,graph,python-2.7,tree,pygraphviz,Python,Graph,Python 2.7,Tree,Pygraphviz,我使用Pygraphviz生成了一个关键字树,如下所示。我将树的边缘作为元组列表。我正在寻找一种算法或最佳方法,从这棵树中提取所有可能的最长路径,以便它覆盖所有可能的关系 因为我使用的是Pygraphviz,所以我一直在寻找一种直接而简单的方法来实现这一点。但Pygraphviz似乎没有这样的选择 edges = [('Sara', 'chocolate'), ('chocolate', 'loves'), ('oranges', 'chocolate'), ('Jessi', 'chocol

我使用Pygraphviz生成了一个关键字树,如下所示。我将树的边缘作为元组列表。我正在寻找一种算法或最佳方法,从这棵树中提取所有可能的最长路径,以便它覆盖所有可能的关系

因为我使用的是Pygraphviz,所以我一直在寻找一种直接而简单的方法来实现这一点。但Pygraphviz似乎没有这样的选择

edges = 
[('Sara', 'chocolate'),
('chocolate', 'loves'),
('oranges', 'chocolate'),
('Jessi', 'chocolate'),
('best', 'best'),
('friends', 'chocolate'),
('Aron', 'chocolate')]

预期结果:

[['Sara', 'chocolate', 'loves'],   
 ['oranges', 'chocolate', 'loves'],   
 ['Jessi', 'chocolate', 'loves'],  
 ['friends', 'chocolate', 'loves'],  
 ['Aron', 'chocolate', 'loves'],  
 ['Sara', 'chocolate', 'oranges'],  
 ['Sara', 'chocolate', 'Jessi'],  
 ['Sara', 'chocolate', 'friends'],  
 ['Sara', 'chocolate', 'Aron'],  
 ['oranges', 'chocolate', 'Jessi'],  
 ['oranges', 'chocolate', 'friends'],  
 ['oranges', 'chocolate', 'Aron'],  
 ['Jessi', 'chocolate', 'friends'],
 ['Jessi', 'chocolate', 'Aron'],  
 ['friends', 'chocolate', 'Aron'],
 ['best', 'best']]
from collections import defaultdict
import itertools
edges = [('Sara', 'chocolate'),
         ('chocolate', 'loves'),
         ('oranges', 'chocolate'),
         ('Jessi', 'chocolate'),
         ('best', 'best'),
         ('friends', 'chocolate'),
         ('Aron', 'chocolate')]
neighbors = {}
rev_neighbors = {}
for edge in edges:
    neighbors[edge[0]] = edge[1]

for edge in edges:
    neighbor = neighbors.get(edge[1])
    if neighbor:
        print edge[0], edge[1], neighbor

v = defaultdict(list)
for key, value in sorted(neighbors.iteritems()):
    v[value].append(key)
for key, value in v.iteritems():
    if not len(list(itertools.combinations(value, 2))) == 0:
        for item in list(itertools.combinations(value, 2)):
            print item[0], key, item[1]
我的当前代码:

[['Sara', 'chocolate', 'loves'],   
 ['oranges', 'chocolate', 'loves'],   
 ['Jessi', 'chocolate', 'loves'],  
 ['friends', 'chocolate', 'loves'],  
 ['Aron', 'chocolate', 'loves'],  
 ['Sara', 'chocolate', 'oranges'],  
 ['Sara', 'chocolate', 'Jessi'],  
 ['Sara', 'chocolate', 'friends'],  
 ['Sara', 'chocolate', 'Aron'],  
 ['oranges', 'chocolate', 'Jessi'],  
 ['oranges', 'chocolate', 'friends'],  
 ['oranges', 'chocolate', 'Aron'],  
 ['Jessi', 'chocolate', 'friends'],
 ['Jessi', 'chocolate', 'Aron'],  
 ['friends', 'chocolate', 'Aron'],
 ['best', 'best']]
from collections import defaultdict
import itertools
edges = [('Sara', 'chocolate'),
         ('chocolate', 'loves'),
         ('oranges', 'chocolate'),
         ('Jessi', 'chocolate'),
         ('best', 'best'),
         ('friends', 'chocolate'),
         ('Aron', 'chocolate')]
neighbors = {}
rev_neighbors = {}
for edge in edges:
    neighbors[edge[0]] = edge[1]

for edge in edges:
    neighbor = neighbors.get(edge[1])
    if neighbor:
        print edge[0], edge[1], neighbor

v = defaultdict(list)
for key, value in sorted(neighbors.iteritems()):
    v[value].append(key)
for key, value in v.iteritems():
    if not len(list(itertools.combinations(value, 2))) == 0:
        for item in list(itertools.combinations(value, 2)):
            print item[0], key, item[1]
注意:请忽略数字xlabel