简体   繁体   中英

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

1) Suppose A1, B1, and C1 are nodes in this graph. C1 is upstream to B1, and B1 is upstream to A1. See this picture: https://imgur.com/fHxuCpH

If I call get_path_to_most_upstream(A1,[]) then I get [[A1, B1, C1]] .

2) However, this doesn't work when a node has more than one parent upstream to it. See this picture: 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]] . How can I adjust this function to return that?

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.

Mods

  1. In function get_path_to_most_upstream give path default value of 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

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

Test 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']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM