简体   繁体   English

如何在 Python 递归图 function 中返回多条路径?

[英]How to get multiple paths returned in a Python recursive graph function?

1) Suppose A1, B1, and C1 are nodes in this graph. 1) 假设 A1、B1 和 C1 是该图中的节点。 C1 is upstream to B1, and B1 is upstream to A1. C1 在 B1 的上游,B1 在 A1 的上游。 See this picture: https://imgur.com/fHxuCpH见这张图片: https://imgur.com/fHxuCpH

If I call get_path_to_most_upstream(A1,[]) then I get [[A1, B1, C1]] .如果我调用get_path_to_most_upstream(A1,[])那么我得到[[A1, B1, C1]]

2) However, this doesn't work when a node has more than one parent upstream to it. 2) 但是,当一个节点的上游有多个父节点时,这不起作用。 See this picture: https://imgur.com/YQ5Q1zx见这张图片: https://imgur.com/YQ5Q1zx

When I call get_path_to_most_upstream(A1,[]) , I am getting an empty list [] but I want to get [[A1, B1, C1],[A1,B1,C2],[A1,B2,C3],[A1,B2,C4]] .当我调用get_path_to_most_upstream(A1,[])时,我得到一个空列表[]但我想得到[[A1, B1, C1],[A1,B1,C2],[A1,B2,C3],[A1,B2,C4]] How can I adjust this function to return that?我该如何调整这个 function 以返回它?

def get_path_to_most_upstream(start_key, path):
    current_top = start_key
    path = path + [current_top] #add top node to path

    parents = get_nodes_upstream_one_hop(current_top) #returns list of nodes directly above
    #parents = [B1] in first case
    #parents = [B1, B2] in second

    if not parents: #base case
        return [path]

    paths = [] 
    for parent in parents:
        if parent not in path:
            extended_paths = get_path_to_most_upstream(parent, path)
            for p in extended_paths:
                paths.append(p)
    return paths

Your code works with small mod.您的代码适用于小型 mod。

Mods模组

  1. In function get_path_to_most_upstream give path default value of None在 function get_path_to_most_upstream 中给出路径默认值 None

    get_path_to_most_upstream(start_key, path = None): get_path_to_most_upstream(start_key, path = None):

  2. Set path to list, if path is None如果路径为无,则将路径设置为列表

    if path is None: path = []如果路径为无:路径 = []

Refactored Code重构代码

def get_nodes_upstream_one_hop(node):
  return [neighbor for neighbor in graph[node]]

 def get_path_to_most_upstream(start_key, path = None):
    if path is None:
      path = []
    current_top = start_key

    path = path + [current_top] #add top node to path

    parents = get_nodes_upstream_one_hop(current_top) #returns list of nodes directly above
    #parents = [B1] in first case
    #parents = [B1, B2] in second

    if not parents: #base case
        return [path]

    paths = [] 
    for parent in parents:
        if parent not in path:
            extended_paths = get_path_to_most_upstream(parent, path)
            for p in extended_paths:
                paths.append(p)
    return paths

Graph Code图形代码

Reference 参考

from collections import defaultdict 

# function for adding edge to graph 
graph = defaultdict(list) 
def addEdge(graph,u,v): 
    graph[u].append(v) 

# definition of function 
def generate_edges(graph): 
    edges = [] 

    # for each node in graph 
    for node in graph: 

        # for each neighbour node of a single node 
        for neighbour in graph[node]: 

            # if edge exists then append 
            edges.append((node, neighbour)) 
    return edges 

Testing测试

Test 1测试 1

graph = defaultdict(list)
addEdge(graph, 'A1', 'B1')
addEdge(graph, 'B1', 'C1')
print(get_path_to_most_upstream('A1')) # [['A1', 'B1', 'C1']]

Test 2测试 2

graph = defaultdict(list)
addEdge(graph, 'A1', 'B1')
addEdge(graph, 'A1', 'B2')
addEdge(graph, 'B1', 'C1')
addEdge(graph, 'B1', 'C2')
addEdge(graph, 'B2', 'C3')
addEdge(graph, 'B2', 'C4')
print(get_path_to_most_upstream('A1')) 
# [['A1', 'B1', 'C1'], 
#  ['A1', 'B1', 'C2'], 
#  ['A1', 'B2', 'C3'], 
#  ['A1', 'B2', 'C4']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 python 图形数据结构中找到所有可能的路径而不使用递归 function - Find all possible paths in a python graph data structure without using recursive function 修改一个没有计数的递归函数。 路径,获取所有路径的序列 - Modifying a recursive function that counts no. of paths, to get sequence of all paths 如何让我的递归函数在Python中返回0 - How to Get My Recursive Function to Return 0 in Python 如何在Python 2.7中将此递归函数(将3X3矩阵的路径列表返回)更改为迭代函数? - How to change this recursive function (return a list of paths for a 3X3 matrix) into iterative function in Python 2.7? Python 从图中获取所有路径 - Python get all paths from graph 递归图遍历:如何生成和返回所有路径? - Recursive graph traversal: how to generate and return all paths? 如何存储递归图函数中的值? - How to store values from a recursive graph function? 如何在Python中获取与图形中特定长度的所有可能路径相对应的边值列表? - How to get the list of edge values corresponding to all possible paths of a particular length in a graph in Python? 如何在机器人文件中使用python函数的多个返回值? - How to use multiple returned value from a python function in a robot file? 如何获取从 class function 返回的多个数据帧? - How can I get multiple dataframes returned from a class function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM