如何从python字典中找到到特定节点到叶子节点以及前面节点的所有路径?

时间:2019-04-08 12:02:28

标签: python algorithm dictionary

我有一个字典['V. ', 'Divakar ', 'Botcha', '1', ',', '2', ', ', 'Mengdie ', 'Zhang', '1', ', ', 'Kuilong ', 'Li', '1', ',', '2', ', ', 'Hong ', 'Gu', '1', ',', '2', ', ', 'Zhonghui ', 'Huang', '1', ', ', 'Jianhui ', 'Cai', '3', ', ', 'Youming ', 'Lu', '1', ', ', 'Wenjie ', 'Yu', '3', ', ', 'and ', 'Xinke ', 'Liu', '1', '* '] fname作为键),与每个节点关联的父子节点(代码中的字典引用)的列表。我将输入一个键(对于下面的代码段B是键,即fname:fname)。我必须考虑父节点。对于B,我希望路径为['B']

我试图找到叶子节点,但是下面的代码没有任何输出。

[A,B,C,C1,X],[A,B,C,C2],[A,B,D,D1],[A,B,D,D2],[R1,B,C,C1,X],[R,B,C,C2],[R1,B,D,D1],[R2,B,D,D2],[R1,B,P]

预期输出为:def neighbor_find(graph, key, neighbor, current_path, paths_list): for v in range(0,len(graph)-1): #access the values in list if d[v]['fname'] == key: #checks if the key input is present in the list if not d[v][neighbor]: #if not the path is appended as it is paths_list.append(current_path) else: # if key present in list, checks its parent/child for node in d[v][neighbor]: for i in range(0,len(d[v]['parent'])): print(d[v]['parent'][i]['name']) neighbor_find(graph, node, neighbor, [node] + current_path, paths_list) def path_find(graph, key, path_list): parents_list, children_list = [], [] neighbor_find(graph, key, 'parent', [], parents_list) neighbor_find(graph, key, 'child', [], children_list) for parent_path in parents_list: for children_path in children_list: path_list.append(parent_path + [key] + children_path[::-1]) for i in range (0,len(path_list)): print("path" ,i+1 , ":", path_list[i]) d = [ { 'file' : 'user_auth.py', 'fname': 'B', 'parent': [{'name': 'A','filename': 'user_auth.py'},{'name': 'R1','filename': 'user_registration.py'}], 'child': [{'name': ['C', 'D'], 'filename' : 'user_auth.py'}], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname': 'C', 'parent': [{'name': ['B', 'M'], 'filename': 'user_auth.py'}], 'child': [{'name':['C1', 'C2'], 'filename' : 'user_auth.py' }], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_004'] }, { 'file' : 'user_auth.py', 'fname': 'D', 'parent': [{'name': 'B' , 'filename' : 'user_auth.py'}], 'child': [{'name':['D1', 'D2'], 'filename' : 'user_auth.py' }], 'tags': ['TC_HRM_API_002','TC_HRM_UI_002'] }, { 'file' : 'out.js', 'fname': 'C1', 'parent': [{'name':'C', 'filename' : 'user_auth.py'}], 'child': [{'name': 'X', 'filename' : 'user_auth.py'}], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname': 'M', 'parent': [], 'child': [{'name': 'C', 'filename' : 'user_auth.py'} , { 'name' : 'N' , 'filename' : 'user_registration.py'}], 'tags': ['TC_HRM_API_001','TC_HRM_API_001','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname': 'A', 'parent': [], 'child': [{'name': 'B', 'filename' : 'user_auth.py'}], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_005'] }, {'file' : 'user_registration.py', 'fname': 'N', 'parent': [{'name': 'M', 'filename' : 'user_auth.py'}], 'child': [], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, {'file' : 'user_auth.py', 'fname': 'D1', 'parent': [{'name': 'D', 'filename' : 'user_auth.py'} ], 'child': [], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname': 'D2', 'parent': [{'name': 'D', 'filename' : 'user_auth.py'}], 'child': [], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname': 'C2', 'parent': [{'name': 'C', 'filename' : 'user_auth.py'} ], 'child': [], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file': 'user_auth.py', 'fname': 'X', 'parent': [{'name': 'C1', 'filename' : 'user_auth.py'} ], 'child': [], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname' : 'B', 'parent' : [{'name': 'R1', 'filename' : 'user_registration.py'} ], 'child' : [{'name': 'P', 'filename' : 'user_auth.py'} ], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_registration.py', 'fname' : 'R1', 'parent' : [], 'child' : [{'name': 'B', 'filename' : 'user_auth.py'} ], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }, { 'file' : 'user_auth.py', 'fname' : 'P', 'parent' : [{'name': 'B', 'filename' : 'user_auth.py'} ], 'child' : [], 'tags': ['TC_HRM_API_001','TC_HRM_API_002','TC_HRM_UI_003'] }] key = 'B' path_list = [] path_find(d, key, path_list) 实际输出为:[A,B,C,C1,X],[A,B,C,C2],[A,B,D,D1],[A,B,D,D2],[R1,B,C,C1,X],[R,B,C,C2],[R1,B,D,D1],[R2,B,D,D2],[R1,B,P]

1 个答案:

答案 0 :(得分:1)

我们可以将这个问题分解为:

  1. 找到从current nodeparent(根)的所有路径
  2. 找到从current nodechild(叶子)的所有路径
  3. 将所有parents路径和chilren路径合并在一起,就像product

因此,代码中的主要问题是您仅遍历子级,可以通过dfs尝试上述想法:

def neighbor_find(graph, key, neighbor, current_path, paths_list):
    for v in graph.values():
        if v['fname'] == [key]:
            if not v[neighbor]:
                paths_list.append(current_path)
            else:
                for node in v[neighbor]:
                    neighbor_find(graph, node, neighbor, [node] + current_path, paths_list)


def path_find(graph, key, path_list):
    parents_list, children_list = [], []
    neighbor_find(graph, key, 'parent', [], parents_list)
    neighbor_find(graph, key, 'child', [], children_list)

    for parent_path in parents_list:
        for children_path in children_list:
            path_list.append(parent_path + [key] + children_path[::-1])

测试代码:

d = {1: {'fname': ['B'],
         'parent': ['A'],
         'child': ['C', 'D']},
     2: {'fname': ['C'],
         'parent': ['B', 'M'],
         'child': ['C1', 'C2']},
     3: {'fname': ['D'],
         'parent': ['B'],
         'child': ['D1', 'D2']},
     4: {'fname': ['C1'],
         'parent': ['C'],
         'child': ['X']},
     5: {'fname': ['M'],
         'parent': [],
         'child': ['C', 'N']},
     6: {'fname': ['A'],
         'parent': [],
         'child': ['B']},
     7: {'fname': ['N'],
         'parent': ['M'],
         'child': []},
     8: {'fname': ['D1'],
         'parent': ['D'],
         'child': []},
     9: {'fname': ['D2'],
         'parent': ['D'],
         'child': []},
     10: {'fname': ['C2'],
          'parent': ['C'],
          'child': []},
     11: {'fname': ['X'],
          'parent': ['C'],
          'child': []},
     }
key = 'C'

path_list = []
path_find(d, key, path_list)

print(path_list)

输出:

[['A', 'B', 'C', 'C1', 'X'], ['A', 'B', 'C', 'C2'], ['M', 'C', 'C1', 'X'], ['M', 'C', 'C2']]

希望对您有所帮助,如果还有其他问题,请发表评论。 :)