recursion - Getting all combinations recursively [Python] -


consider dictionary:

 {  "a": [ ["b"], ["c"] ],     "b": [ ["d"], ["e"] ],     "c": [ ["h"] ],     "d":[["i"],["j"]]  } 

i want find possible paths lead not in dictionary keys. example

a = [ [b], [c] ] 

we can expand to

a = [ [b, d, i],[b, d, j], [b, e], [c, h] ] 

i trying come recursive solution can't work fully. suggestions how approach problem?

you can find path following function ,but dont post code , dont know try self dont know have problem , couldn't explain thing till tell try , code :

def find_all_paths(graph, start,path=[]):        path=path+[start]        if not graph.has_key(start):           return [path]        paths = []        nodes in graph[start]:           n in nodes :              if n[0] not in path:                   newpaths = find_all_paths(graph, n[0], path)                   newpath in newpaths:                       paths.append(newpath)        return paths  g={  "a": [ ["b"], ["c"] ],     "b": [ ["d"], ["e"] ],     "c": [ ["h"] ],     "d":[["i"],["j"]]  } 

demmo :

print find_all_paths(g,'a') [['a', 'b', 'd', 'i'], ['a', 'b', 'd', 'j'], ['a', 'b', 'e'], ['a', 'c', 'h']]  print find_all_paths(g,'b') [['b', 'd', 'i'], ['b', 'd', 'j'], ['b', 'e']] 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -