使用python networkx模块查找图中的链

使用python networkx模块查找图中的链,python,networkx,Python,Networkx,我有一个带有节点和边列表的图 nodes=['a','b','c','d','e','f','g'] G.add_edges_from ([('a','f'),('a','d'),('a','b'),('a','e'),('b','g'), ('b','e'),('b','c'),('c','b'),('c','d'),('d','a'), ('d','c'),('e','b'),('e','a'), ('f','a

我有一个带有节点和边列表的图

nodes=['a','b','c','d','e','f','g'] 
G.add_edges_from ([('a','f'),('a','d'),('a','b'),('a','e'),('b','g'),
                   ('b','e'),('b','c'),('c','b'),('c','d'),('d','a'),
                   ('d','c'),('e','b'),('e','a'),  ('f','a'),('g','b')]. 
我的问题是识别每个节点的邻居,我使用
G.neights(node[I])

我得到的结果是

a=>['e', 'b', 'f', 'd']
b=>['e', 'c', 'g', 'a']
c=>['b', 'd']
d=>['c', 'a']
e=>['b', 'a']
f=>['a']
g=>['b'].

现在,我必须将链排序为
f,正如我所理解的,您需要列出所有节点,这些节点在邻居方面是彼此的子集。因此,对于示例图,链应为:


f请使用适当的代码标记,使其更易于阅读。要清楚的是:“f是d的子集,是b的子集”是指“f的邻居构成d的邻居的子集,是b的邻居的子集”?hai joel,是的,我的意思是f的邻居是d的邻居的子集,b的邻居中有谁的邻居。
tmp = [] # temporary storage for chains
chains = [] # stores all found chains
nodes = G.nodes() # save the labels of nodes
prev_node =  None # Holds the node which other nodes will be checked if they are a subset of.
for i in xrange(len(nodes)): # each node is considered as a superset and others will be compared to it
    tmp.append(nodes[i]) # append this node as the head of the chain
    prev_node = nodes[i] # store it to compare other nodes to it
    for j in xrange (i+1,len(nodes)): # looping the rest of the nodes
        if set(G.neighbors(nodes[j])).issubset(set(((G.neighbors(prev_node))))) : # if it is a subset
            tmp.append(nodes[j]) # append to the chain
            prev_node = nodes[j] # store to check if the rest of the nodes are subset of this one

    if not (is_subset(chains,tmp)): # After finishing the chain check it is not a subset of an already created chain
        chains.append(list(reversed(tmp))) # add it to the chains
    tmp =[] # reset
    prev_node = None
print chains
def is_subset(chains,tmp):
    for i in xrange(len(chains)):
        if set(tmp).issubset(chains[i]):
                return True
    return False