I have to complete a project which have an text file and finds path between 2 actors(kevin bacin game) that you input by going through the movies in the text file.

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

Code

# Class for node
class node: slots = ('movie','neighbor')

# Node constructor
def mkNode(name):
    n = node()
    n.name = name
    n.neighbor = []
    return n

# Find if node is in the graph
def findNode(nodelist, name):
    for n in nodelist:
        if n.name == name:
            return n

#Creates graph
def loadGraphFile(file):
    graph = []
    for line in file:
        contents = line.split()
        movieName = contents[0]
        actorNames = [contents[i]+ " " + contents[i+1] for i in range(1, len(contents), 2)]        
        movieNode = findNode(graph, movieName)
        if movieNode == None:
            movieNode = mkNode(movieName)
            graph.append(movieNode)
        for actorName in actorNames:            
            actorNode = findNode(graph,actorName)
        if actorNode == None:
            actorNode = mkNode(actorName)
            graph.append(actorNode)
        actorNode.neighbor.append(movieNode)
        movieNode.neighbor.append(actorNode)
        return graph

def loadGraphFileName(file = "C:/Users/******/Desktop"):
    return loadGraphFile(open(file))

# 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)

Recommended Answers

All 2 Replies

... and your problem is?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.