I have to complete a lab which basically takes a text file and finds path between 2 actors that you input by going through the movies in the text file. (For the labs purposes we are only doing 3 degrees of separation)
For example if the text file is:
Apollo13 Kevin Bacon Tom Hanks Gary sinise
HollowMan Elisabeth Shue Kevin Bacon Josh Brolin
AFewGoodMen Tom Cruise Demi Moore Jack Nicholson Kevin Bacon
OneCrazySummer John Cusack Demi Moore
DaVinciCode Tom Hanks Ian McKellen Audrey Tautou
and you searched from the start being John Cusack, and goal being Kevin Bacon it would return:
was in OneCrazySummer with
was in AFewGoodMen with
I am trying to do this by constructing a graph which has actors only neighboring movies and movies only neighboring actors, and doing a depth first search through them to find a path.
I am currently getting a lot of errors trying to load the graph in my code. Specifically at a point in my code where I try to access the neighbors of the node it returns that NoneType.neighbors does not exist, but I don't see how the actorNode or movieNode can be NoneType. I know that my code to this point probably does not work but I can't test past the construction of the graph.
Here's my code so far:
# Class for node class node: __slots__ = ('movie','neighbor') # Node constructor def mkNode(name): n = node n.name = name return # Find if node is in the graph def findNode(nodelist, name): for n in nodelist: if n == None: return None elif n.name == name: return #Creates graph def loadGraphFile(file): graph =  for line in file: contents = line.split() movieName = contents actorName = contents[1:] movieNode = findNode(graph, movieName) if movieNode == None: movieNode = mkNode(movieName) graph.append(movieNode) actorNode = findNode(graph,actorName) if actorNode == None: actorNode = mkNode(actorName) graph.append(actorNode) actorNode.neighbor.append(movieNode) movieNode.neighbor.append(actorNode) return graph # Searches graph for path def search(start,goal,visited): if start == goal: return [start] else: for n in start.neighbor: content = line.split() for x in range(0,(len(content)-1),2): z = (content[x] + content[x+1]) if not z in visited: visited.append(z) path = search(z,goal,visited) if path != None: return [start] + path visited.append(x)
specifically when it does actorNode.neighbor.append(movieNode) it returns the error that NoneType.neighbor doesn't exist. But I have a statement right above it to check if the actorNode is None and change it.