mercredi 10 juin 2020

'NoneType' object is not iterable error in Python

I am trying to find the shortest list inside of the Paths list but I receive an error ('NoneType' object is not iterable error in Python). This function is for "graph pattern" or "searching algorithm" finding the shortest path to a goal. Please let me know what I am doing wrong here. Thanks!

graph = {'A':['B','C'],'B':['F','D','I'],'C':['D','E','G'],'D':['H'],'F':['I'],'E':['G'],'I':['H'],'D':['H'],'H':['G']} 




def find_all_paths(graph, start, end, path=[]):

        path = path + [start]
        if start == end:
            return [path]
        if start not in graph:
            return []
        paths = []
        for node in graph[start]:
            if node not in path:
                newpaths = find_all_paths(graph, node, end, path)
                for newpath in newpaths:
                    paths.append(newpath)

        minList=min(paths, key= len)
        print(minList)


Aucun commentaire:

Enregistrer un commentaire