def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    # TODO
    #the node state represent person_id and action represents movie_id
    node=Node(state=source,parent=None,action=None)

    frontier=QueueFrontier()
    frontier.add(node)
    explored=[]
    path=[]

    while True:
    	if frontier.empty():
    		return None

    	new=frontier.remove()
    	if new.state==target:
    		while new.parent is not None:
    			path.append([new.action,new.state])
    			new=new.parent
    		return path[::-1]

    	explored.append(new.state)
    	for movie,actor in neighbors_for_person(new.state):
    		if actor not in explored and not  frontier.contains_state(new.state):
    			frontier.add(Node(state=actor,action=movie,parent=new))
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    start = Node(state = source, parent = None, action = None)
    
    frontier = QueueFrontier()
    frontier.add(start)
    
    explored = set()
    
    while True:
        if frontier.empty():
            return None
                 
        node = frontier.remove()
        if node.state == target:
            solution = []
            while node.parent is not None:
                pair = (node.action, node.state)
                solution.append(pair)
                node = node.parent
            solution.reverse()
            return solution
        
        explored.add(node.state)
        
        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state=state, parent=node, action=action)
                frontier.add(child)
Example #3
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)
    examined = set()
    path = []

    while True:
        if frontier.empty():
            return None
        node = frontier.remove()
        examined.add(node.state)
        neighbors = neighbors_for_person(node.state)

        for starred, person in neighbors:
            if person == target:
                path.append((starred, person))
                while node.parent is not None:
                    path.append((node.action, node.state))
                    node = node.parent
                path.reverse()
                return path
            else:
                if not frontier.contains_state(
                        person) and person not in examined:
                    child = Node(state=person, parent=node, action=starred)
                    frontier.add(child)
Example #4
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)
    explored = set()
    """
    check if the negihtbors have the target if do return
    """
    while True:

        # If nothing left in frontier, then no path
        if frontier.empty():
            return []
        # Choose a node from the frontier
        node = frontier.remove()

        # Mark node as explored
        explored.add(node.state)

        # Add neighbors to frontier
        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state=state, parent=node, action=action)
                if child.state == target:
                    return return_paths(child)
                frontier.add(child)
Example #5
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    # TODO
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)
    explored = set()
    while True:
        if frontier.empty():
            return None
        node = frontier.remove()
        explored.add(node.state)
        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state=state, parent=node, action=action)
                if child.state == target:
                    path = []
                    while child.parent is not None:
                        path.insert(0, (child.action, child.state))
                        child = child.parent
                    return path
                else:
                    frontier.add(child)
Example #6
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    explore = set()
    frontier = QueueFrontier()
    #start = neighbors_for_person(source)
    #for state in start:
    node = Node(source, parent=None, id=source)
    frontier.add(node)
    while True:
        if frontier.empty():
            return None
        node = frontier.remove()
        explore.add(node.id)
        if node.id == target:
            parents = []
            while node.parent is not None:
                parents.append(node.state)
                node = node.parent
            parents.reverse()
            return parents
        for state in neighbors_for_person(node.id):
            if not frontier.contains_state(id) and state[1] not in explore:
                child = Node(state=state, parent=node, id=state[1])
                frontier.add(child)
Example #7
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    if source == target:
        return []

    start = Node(source, None, None)
    queue = QueueFrontier()

    queue.add(start)

    explored = set()

    while True:
        if queue.empty():
            return None

        node = queue.remove()

        explored.add(node.state)

        for action, state in neighbors_for_person(node.state):
            if state == target:
                path = []
                path.insert(0, (action, state))
                while node.parent is not None:
                    path.insert(0, (node.action, node.state))
                    node = node.parent
                return path
            if not queue.contains_state(state) and state not in explored:
                queue.add(Node(state, node, action))
Example #8
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    # TODO
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)

    Explored = set()

    while True:
        if frontier.empty():
            return None

        node = frontier.remove()
        Explored.add(node)

        Neighbors = neighbors_for_person(node.state)

        for movie_id, person_id in Neighbors:
            if not frontier.contains_state(
                    person_id) and person_id not in Explored:
                child = Node(state=person_id, parent=node, action=movie_id)
                if child.state == target:
                    solution = []
                    while node.parent is not None:
                        solution.append((node.action, node.state))
                        node = node.parent
                    solution.append((child.action, child.state))
                    return solution
                frontier.add(child)
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)

    while True:
        if frontier.empty():
            return None
        curr = frontier.remove()
        if curr.state == target:
            actions = []
            cells = []
            while curr.parent:  # is not None
                actions.append(curr.action)
                cells.append(curr.state)
                curr = curr.parent
            actions.reverse()
            cells.reverse()
            path = []
            for i in range(len(actions)):
                path.append((actions[i], cells[i]))
            return path
        for action, state in neighbors_for_person(curr.state):
            if not frontier.contains_state(state):
                child = Node(state=state, parent=curr, action=action)
                frontier.add(child)
Example #10
0
def shortest_path(source: int, target: int):
    """
    Returns the shortest list of (movie_id, person_id) pairs that connect the person with id source to the person with id target.

    If no possible path, returns None.
    """

    node = Node(state=source)  # state: person_id, action: movie_id
    explored = set()  # set of person_id

    frontier = QueueFrontier()
    frontier.add(node)

    while (not frontier.empty()):
        node = frontier.remove()
        explored.add(node.state)

        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state, node, action)
                frontier.add(child)

                if (child.state == target):
                    return recover(child)

    return None
Example #11
0
    def shortest_path(self, source, target):
        source = Node(state=source, parent=None, action=None)
        frontier = QueueFrontier()
        frontier.add(source)

        while True:

            if frontier.empty():
                raise Exception("No solution")

            node = frontier.remove()
            self.num_explored += 1

            if node.state == target:
                actions = []
                while node.parent is not None:
                    actions.append(node.action)
                    node = node.parent
                actions.reverse()
                return actions

            self.explored.append(node.state)

            for act in neighbors_for_person(node.state):
                print(act)
                actor = act[-1]
                if not frontier.contains_state(
                        actor) and actor not in self.explored:
                    child = Node(state=actor, parent=node, action=act)
                    frontier.add(child)
Example #12
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    InitialNode = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()

    frontier.add(InitialNode)
    explored = set()

    while not (frontier.empty()):

        node = frontier.remove()
        explored.add(node.state)
        neighbors = neighbors_for_person(node.state)
        for movie, person in neighbors:
            Next = Node(person, node, movie)
            if person not in explored or not frontier.contains_state(person):
                if person == target:
                    path = []
                    node = Next
                    while node.parent is not None:
                        path.append((node.action, node.state))
                        node = node.parent
                    path.reverse()
                    return path
                frontier.add(Next)
    return None
Example #13
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    queue = QueueFrontier()
    start = Node(state=source, parent=None, action=None)
    queue.add(start)
    explored = set()

    while True:
        if queue.empty():
            return None

        node = queue.remove()
        if node.state == target:
            path = []
            while node.parent is not None:
                path.append((node.action, node.state))
                node = node.parent
            path.reverse()
            return path
        explored.add(node.state)

        neighbors = neighbors_for_person(node.state)

        for action, state in neighbors:
            if not action or not state:
                return None
            elif state not in explored and not queue.contains_state(state):
                child = Node(state=state, parent=node, action=action)
                queue.add(child)
Example #14
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    # Initialize Frontier and add source (as node)
    moving_frontier = QueueFrontier()
    start_node = Node(source, None, None)
    moving_frontier.add(start_node)

    # BDF
    while not moving_frontier.empty():
        current = moving_frontier.remove()
        neighbors = neighbors_for_person(current.state)
        for neighbor in neighbors:
            if neighbor[1] == source:
                continue
            elif moving_frontier.contains_state(neighbor[1]):
                continue
            elif neighbor[1] == target:
                return node_path(Node(neighbor[1], current, neighbor[0]))
            else:
                moving_frontier.add(Node(neighbor[1], current, neighbor[0]))
    else:
        return None
Example #15
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.
    If no possible path, returns None.
    """
    frontier = QueueFrontier()
    frontier.add(Node(source, None, None))
    explored = set()

    if source == target:
        raise Exception("source should not be target")

    while True:
        if frontier.empty():
            return None
        node = frontier.remove()
        if node.state == target:
            answer = []
            while node.parent is not None:
                answer.append((node.action, node.state))
                node = node.parent
            answer.reverse()
            return answer
        explored.add(node.state)
        for movie_id, person_id in neighbors_for_person(node.state):
            if (not frontier.contains_state(person_id)) and (person_id
                                                             not in explored):
                child = Node(state=person_id, parent=node, action=movie_id)
                frontier.add(child)
Example #16
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    path = []
    node = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()

    if node.state == target:
        return path
    frontier.add(node)
    explored = set()

    while True:
        if frontier.empty():
            return None

        node = frontier.remove()
        explored.add(node.state)

        for movie, person in neighbors_for_person(node.state):
            if not frontier.contains_state(person) and person not in explored:
                child = Node(state=person, parent=node, action=movie)
                if child.state != target:
                    frontier.add(child)
                else:
                    while child.parent is not None:
                        path.append((child.action, child.state))
                        child = child.parent
                    path.reverse()
                    return path
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)
    visited_nodes = set()

    while True:
        if frontier.empty():
            raise Exception("no solution could be found")

        node = frontier.remove()
        visited_nodes.add(node)

        if node.state == target:
            path = []
            while node.parent is not None:
                path.append((node.action, node.state))
                node = node.parent
            path.reverse()
            return path

        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(
                    state) and state not in visited_nodes:
                child = Node(state=state, parent=node, action=action)
                frontier.add(child)
Example #18
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    # init frontier
    srcNode = Node(source, None, None)
    frontier = QueueFrontier()
    frontier.add(srcNode)

    visited = set()

    while True:
        if frontier.empty(): return None
        # pop off current node
        curNode = frontier.remove()
        # add surrounding neighbors
        for action, state in neighbors_for_person(curNode.state):
            if not frontier.contains_state(state) and state not in visited:
                childNode = Node(state, curNode, action)

                # check for goal
                if childNode.state == target:
                    path = []
                    while childNode.parent is not None:
                        path.append((childNode.action, childNode.state))
                        childNode = childNode.parent
                    path.reverse()
                    return path
                else:
                    frontier.add(childNode)
        visited.add(curNode.state)
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    # TODO
    queue = QueueFrontier()
    start = Node(source, None, None)
    queue.add(start)

    explored = set()

    while True:
        if queue.empty():
            return None

        node = queue.remove()

        explored.add(node.state)

        for movie, person in neighbors_for_person(node.state):
            if not queue.contains_state(person) and person not in explored:
                child = Node(person, node, (movie, person))
                if child.state == target:
                    return backtrack(child)
                queue.add(child)
Example #20
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    explored = set()
    frontier = QueueFrontier()
    node = Node(source, None, None)
    frontier.add(node)
    while 1:
        if (frontier.empty()):
            return None
        else:
            node = frontier.remove()
            if (node.state == target):
                # Return the list
                path = []
                while node.parent:
                    path.append((node.action, node.state))
                    node = node.parent
                path.reverse()
                return path
            else:
                # Add the removed node to the explored set
                explored.add(node)
                # Add all the neighbors to the frontier if they have not been explored and they are not in the frontier yet
                neighbors = neighbors_for_person(node.state)
                for neighbor in neighbors:
                    newNode = Node(neighbor[1], node, neighbor[0])
                    if (not (frontier.contains_state(newNode.state)
                             or any(oldNode.state == newNode.state
                                    for oldNode in explored))):
                        frontier.add(newNode)
Example #21
0
def shortest_path(source, target):
    solution = []

    if source == target:
        return solution

    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)
    explored = set()

    while True:

        if frontier.empty():
            return None

        node = frontier.remove()
        explored.add(node.state)

        for action, state in neighbors_for_person(node.state):
            child = Node(state=state, parent=node, action=action)
            if child.state == target:
                while child.parent is not None:
                    solution.append((child.action, child.state))
                    child = child.parent
                solution.reverse()
                return solution

            if not frontier.contains_state(state) and state not in explored:
                frontier.add(child)
Example #22
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    q = QueueFrontier()
    seen = set()

    start = Node(state=source, parent=None, action=None)

    q.add(start)
    seen.add(start.state)

    found_target = False
    while not found_target:
        if q.empty():
            return None

        temp_node = q.remove()
        seen.add(temp_node.state)
        if check_found(target, temp_node):
            return check_found(target, temp_node)

        for movie_id, actor_id in neighbors_for_person(temp_node.state):
            if actor_id not in seen and not q.contains_state(actor_id):
                child = Node(state=actor_id, parent=temp_node, action=movie_id)
                if check_found(target, child):
                    return check_found(target, child)
                q.add(child)

    return None
Example #23
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    frontier = QueueFrontier()
    root_node = Node(source, None, None)
    frontier.add(root_node)
    return bfs(frontier, target)
Example #24
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    # Create a node with source as the initial state
    start = Node(state=source, parent=None, action=None)

    # Creating a empty queue frontier aka BFS
    frontier = QueueFrontier()

    # Adding the initial state to the frontier
    frontier.add(start)

    # Initialize an empty unexplored state
    explored_state = set()

    while True:
        if frontier.empty():
            return None

        # Remove a node from the frontier
        current_node = frontier.remove()

        if current_node.state == target:
            path = []

            while current_node.parent is not None:
                path.append((current_node.action, current_node.state))
                current_node = current_node.parent

            path.reverse()

            return path

        explored_state.add(current_node)

        # A set of tuple of all the movies the current person is in and all stars starred in that movie
        for movie, person in neighbors_for_person(current_node.state):
            if not frontier.contains_state(
                    person) and person not in explored_state:

                new_node = Node(state=person,
                                parent=current_node,
                                action=movie)
                if new_node.state == target:
                    path = []

                    while new_node.parent is not None:
                        path.append((new_node.action, new_node.state))
                        new_node = new_node.parent

                    path.reverse()
                    return path

                frontier.add(new_node)
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    # TODO
    # raise NotImplementedError
    # Keep track of number of states explored
    num_explored = 0

    # Initialize frontier to just the starting position
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)

    # Initialize an empty explored set
    explored = set()

    # Keep looping until solution found
    while True:

        # If nothing left in frontier, then no path
        if frontier.empty():
            raise Exception("no solution")

        # Choose a node from the frontier
        node = frontier.remove()
        num_explored += 1

        # If node is the goal, then we have a solution
        if node.state == target:
            actions = []
            cells = []
            solution = []
            while node.parent is not None:
                actions.append(node.action)
                cells.append(node.state)
                node = node.parent
            actions.reverse()
            cells.reverse()

            for i in range(len(actions)):
                solution.append((actions[i], cells[i]))
            if len(solution) > 0:
                return solution
            else:
                return None

        # Mark node as explored
        explored.add(node.state)

        # Add neighbors to frontier
        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state=state, parent=node, action=action)
                frontier.add(child)
Example #26
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    num_explored = 0

    # Initialize frontier to just the starting position
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)

    # Initialize an empty explored set
    explored = set()

    # Keep looping until solution found
    while True:

        # If nothing left in frontier, then no path
        if frontier.empty():
            return None

        # Choose a node from the frontier
        node = frontier.remove()
        num_explored += 1

        # If node is the goal, then we have a solution
        if node.state == target:
            actions = []
            cells = []
            while node.parent is not None:
                actions.append(node.action)
                cells.append(node.state)
                node = node.parent
            actions.reverse()
            cells.reverse()
            solution = (actions, cells)
            return

        # Mark node as explored
        explored.add(node.state)

        # Add neighbors to frontier
        for action, state in neighbors_for_person(node.state):
            if state not in explored and not frontier.contains_state(action):
                child = Node(state=state, parent=node, action=action)
                if child.state == target:
                    action_path = []
                    node = child
                    while node.parent is not None:
                        action_path.append((node.action, node.state))
                        node = node.parent

                    action_path.reverse()
                    return action_path
                frontier.add(child)
Example #27
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    neighbors = neighbors_for_person(source)

    # Shortest path means BFS, then the frontier is a queue
    frontier = QueueFrontier()

    # Set of explored states
    explored = set()

    # Path to be returned (if any)
    path = []

    # Add all the actors staring with source in the frontier, with their movies
    for neighbor in neighbors:
        start = Node(state=neighbor, parent=None, action=None)

        # If the source and the target stare in the same movie :
        if target in start.state[1]:
            path.append([start.state[0], start.state[1]])
            return path

        frontier.add(start)

    while True:

        # If there is no path
        if frontier.empty():
            return None

        # Get a node from the fontier to be analyzed
        node = frontier.remove()

        explored.add(node.state)

        for neighbor in neighbors_for_person(node.state[1]):
            if not frontier.contains_state(
                    neighbor) and neighbor not in explored:
                child = Node(state=neighbor, parent=node, action=None)

                # If a path is found, add all the nodes to the variable path starting at the end (target -> source)
                if target in child.state[1]:
                    node = child
                    while node.parent is not None:
                        path.append([node.state[0], node.state[1]])
                        node = node.parent
                    path.append([node.state[0], node.state[1]])

                    # Put back path in the right order (source -> target)
                    path.reverse()
                    return path

                frontier.add(child)
Example #28
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    # TODO
    # Keep track of number of states explored
    num_explored = 0

    # Initialize frontier to just the starting position
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)

    explored = set()
    # Keep looping until solution found
    while True:

        # If nothing left in frontier, then no path
        if frontier.empty():
            raise Exception("no solution")

        # Choose a node from the frontier
        node = frontier.remove()
        num_explored += 1

        # If node is the goal, then we have a solution
        if node.state == target:
            answer = []
            while node.parent is not None:
                #action = movieid, state = personid
                answer.append((node.action, node.state))
                node = node.parent
            answer.reverse()
            return answer

        #Mark node as explored
        if node.state not in explored:
            explored.add(node.state)

        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state=state, parent=node, action=action)
                #if you detect a goal node, no need to add it to the frontier, you can simply return the solution immediately.
                if child.state == target:
                    answer = []
                    while child.parent is not None:
                        #action = movieid, state = personid
                        answer.append((child.action, child.state))
                        child = child.parent
                    answer.reverse()
                    return answer

                frontier.add(child)
                explored.add(child.state)
Example #29
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    # keep track of number of states explored
    num_explored = 0

    # state is person id
    # action is movie id

    # initialize frontier to starting position
    start = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(start)

    # initialize empty explored set
    explored = set()

    # keep looping until solution found
    while True:

        # if nothing in the frontier at start of loop, no path
        if frontier.empty():
            return (None)

        # choose a node from the frontier
        node = frontier.remove()
        num_explored += 1

        # if node is the goal, we have a solution
        # this should be the shortest path since we're using BFS
        if node.state == target:
            actions = []
            states = []
            while node.parent is not None:
                actions.append(node.action)
                states.append(node.state)
                node = node.parent
            actions.reverse()
            states.reverse()
            solution = []
            for i in range(len(actions)):
                solution.append((actions[i], states[i]))
            return (solution)

        explored.add(node.state)

        for action, state in neighbors_for_person(node.state):
            if not frontier.contains_state(state) and state not in explored:
                child = Node(state=state, parent=node, action=action)
                frontier.add(child)

    # TODO
    raise NotImplementedError
Example #30
0
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """

    #Initialize frontier to starting position
    source = Node(state=source, parent=None, action=None)
    frontier = QueueFrontier()
    frontier.add(source)

    #Explored Set
    explored = set()
    # TODO
    #Solution Loop
    while True:

        if frontier.empty():
            break

        node = frontier.remove()

        if node.state == target:
            solution = []
            while node.parent is not None:
                segment = (node.action, node.state)
                solution.append(segment)
                node = node.parent
            solution.reverse()

            return solution

        explored.add(node.state)

        #add neighbors to frontier
        neighbors = neighbors_for_person(node.state)
        for costars in neighbors:
            if costars[1] == target:
                solution = []
                finalnodes = Node(state=target, parent=node, action=costars[0])
                while finalnodes.parent is not None:
                    segment = (finalnodes.action, finalnodes.state)
                    solution.append(segment)
                    finalnodes = finalnodes.parent
                solution.reverse()

                return solution

            if not frontier.contains_state(
                    costars[1]) and costars[1] not in explored:
                child = Node(state=costars[1], parent=node, action=costars[0])
                frontier.add(child)

    return None
    raise NotImplementedError