Exemple #1
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        q = Queue()

        # Enqueue A PATH TO the starting vertex
        q.enqueue([starting_vertex])
        visited = set()
        while q.size() != 0:
            path= q.dequeue()
            vertex = path[-1]
            if vertex not in visited:
                visited.add(vertex)
                if vertex == destination_vertex:
                    return path
            for e in self.get_neighbors(vertex):
                    copy = path.copy()
                    copy.append(e)
                    q.enqueue(copy)
Exemple #2
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     # Create an empty queue and enqueue the starting vertex ID
     q = Queue()
     q.enqueue(starting_vertex)
     # Create an empty Set to store visited vertices
     visited = set()
     # While the queue is not empty...
     while q.size() > 0:
         # Dequeue the first vertex
         v = q.dequeue()
         # If that vertex has not been visited...
         if v not in visited:
             # Mark it as visited
             print(v)
             visited.add(v)
             # Then add all of it's neighbors to the back of the queue
             for neighbor in self.vertices[v]:
                 q.enqueue(neighbor)
Exemple #3
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     #use a queue
     #visited hash table
     #stop when queue is empty
     #add to queue (if not in visited)
     #queue all neighbors
     #dequeue, and add to visited
     visited = set()
     btf_queue = Queue()
     btf_queue.enqueue(starting_vertex)
     while btf_queue.size() > 0:
         print(btf_queue.queue[0])
         neighbors = self.get_neighbors(btf_queue.queue[0])
         visited.add(btf_queue.queue[0])
         btf_queue.dequeue()
         for neighbor in neighbors:
             if neighbor not in visited:
                 btf_queue.enqueue(neighbor)
Exemple #4
0
    def getAllSocialPaths(self, userID):
        """
        Takes a user's userID as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        visited = {}  # Note that this is a dictionary, not a set
        queue = Queue()
        queue.enqueue([userID])
        while queue.size() > 0:
            path = queue.dequeue()
            vertex = path[-1]
            if vertex not in visited:
                visited[vertex] = path
                for friendship in self.friendships[vertex]:
                    new_path = list(path)
                    new_path.append(friendship)
                    queue.enqueue(new_path)
        return visited
Exemple #5
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        q = Queue()
        visited = set()
        q.enqueue([starting_vertex])

        while q.size() > 0:
            path = q.dequeue()
            current = path[-1]

            if current == destination_vertex:
                return path

            if current not in visited:
                visited.add(current)

                for i in self.get_neighbors(current):
                    q.enqueue(path + [i])
Exemple #6
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        # Create an empty queue and enqueue A PATH TO the starting vertex ID
        q = Queue()
        # q.enqueue(starting_vertex)

        # Create a Set to store visited vertices
        visited = set()
        q.enqueue([starting_vertex])

        # While the queue is not empty...
        while q.size() > 0:
            # Dequeue the first PATH
            v = q.dequeue()

            # Grab the last vertex from the PATH
            n = v[-1]

            # If that vertex has not been visited...
            if n not in visited:
                # CHECK IF IT'S THE TARGET
                if n == destination_vertex:
                    # IF SO, RETURN PATH
                    return v

                # Mark it as visited...
                visited.add(n)

                # Then add A PATH TO its neighbors to the back of the queue
                for neighbor in self.vertices[n]:
                    # COPY THE PATH
                    new = v.copy()
                    # APPEND THE NEIGHOR TO THE BACK
                    new.append(neighbor)
                    q.enqueue(new)
Exemple #7
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     # create empty queue and enqueue starting vertex Id
     q = Queue()
     q.enqueue(starting_vertex)
     # create set to store for visited vertices
     visited = set()
     # while queue not empty
     while q.size() > 0:
         # deque first vertex
         v = q.dequeue()
         # if vertex has not been visited
         if v not in visited:
             # mark as visited
             print(v)
             visited.add(v)
             # add all neighbors to back of queue
             for neighbor in self.vertices[v]:
                 q.enqueue(neighbor)
Exemple #8
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        q = Queue()
        q.enqueue([starting_vertex])

        visited = set()

        while q.size() > 0:
            path = q.dequeue()
            last_vertex = path[-1]
            if last_vertex not in visited:
                if last_vertex == destination_vertex:
                    return path
                else:
                    visited.add(last_vertex)
                    for next_vertex in self.vertices[last_vertex]:
                        new_path = [*path, next_vertex]
                        q.enqueue(new_path)
Exemple #9
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        queue = Queue()
        visited = set()

        queue.enqueue(starting_vertex)

        while queue.size() > 0:
            vertex = queue.dequeue()

            if vertex in visited:
                continue
            else:
                visited.add(vertex)

            print(vertex)

            for neighbor in self.get_neighbors(vertex):
                queue.enqueue(neighbor)
Exemple #10
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        # create an empty queue and enqueue a starting vertex
        queue = Queue()
        queue.enqueue(starting_vertex)

        # create a set to store the visited vertices
        visited = {starting_vertex}

        # while the queue is not empty
        while queue.size() > 0:
            u = queue.dequeue()
            print(u)
            for v in self.get_neighbors(u):
                # if vertex has not been visited
                if v not in visited:
                    queue.enqueue(v)
                    # mark the vertex as visited
                    visited.add(v)
Exemple #11
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        # create an empty queue and enqueue PATH To the Starting Vertex ID
        queue = Queue()

        queue.enqueue([starting_vertex])

        # create a set to store visited vertices
        visited = set()

        # while the queue is not empty
        while queue.size() > 0:
            # dequeue the first PATH
            path = queue.dequeue()
            # grab the last vertex from the Path
            vert = path[-1]

            # is this vertex the target?
            if vert == destination_vertex:
                return path
            else:
                # check if the vertex has not been visited
                if vert not in visited:
                    print(vert)
                    # mark the vertex as visited
                    visited.add(vert)
                    # then add A Path to its neighbors to the back of the queue
                    back = self.get_neighbors(vert)
                    for next_vert in back:
                        # make a copy of the path
                        path_copy = list(path)
                        # append the neighbor to the back of the path
                        path_copy.append(next_vert)
                        # enqueue out new path
                        queue.enqueue(path_copy)
Exemple #12
0
def path_to_room_id(player, target_room_id):
    room_id = player.cur_room
    neighbors = g.get_neighbors(room_id)
    q = Queue()

    # FORMAT OF QUEUE: [ [directions], [room_ids] ]
    # Initialize queue
    for direction in neighbors:
        if neighbors[direction] == target_room_id:
            # Return to main calling function
            print(f'Found {target_room_id} 1 move away')
            return [direction]
        else:
            # [directions to new location, new room id]
            room_id = neighbors[direction]
            q.enqueue([[direction], [room_id]])

    # START SEARCH
    while q.size() > 0:
        directions, room_ids = q.dequeue()
        last_room_id = room_ids[-1]
        # Get neighbors
        neighbors = g.get_neighbors(last_room_id)
        neighbors_keys = list(neighbors.keys())
        random.shuffle(neighbors_keys)
        for direction in neighbors_keys:
            r_id = neighbors[direction]
            if r_id == target_room_id:
                directions.append(direction)
                print(
                    f'Found room {target_room_id} {len(directions)} moves away.'
                )
                return directions
            elif r_id not in room_ids:
                new_directions = list(directions)
                new_directions.append(direction)
                new_room_ids = list(room_ids)
                new_room_ids.append(r_id)
                q.enqueue([new_directions, new_room_ids])
Exemple #13
0
    def get_all_social_paths(self, user_id):
        """
        Takes a user's user_id as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        visited = {}  # Note that this is a dictionary, not a set
        # !!!! IMPLEMENT ME
        friends_to_check = Queue()
        friends_to_check.enqueue(user_id)
        print("printing whats in friends_to_check:", friends_to_check)

        for user in self.friendships:
            if user == user_id:
                print(f"hey it's the user {user} and it equals {user_id}")
                for friend in self.friendships[user]:
                    print(user, "is friends with:", friend)

        return visited
Exemple #14
0
 def bfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing the shortest path from
     starting_vertex to destination_vertex in
     breath-first order.
     """
     paths = []
     visited = set()
     bft_queue = Queue()
     bft_queue.enqueue([starting_vertex])
     while bft_queue.size() > 0:
         vertex_path = bft_queue.dequeue()
         print(vertex_path)
         vertex = vertex_path[-1]
         if vertex not in visited:
             visited.add(vertex)
             for neighbour in self.get_neighbors(vertex):
                 if neighbour == destination_vertex:
                     path.append(vertex_path.append(neighbour))
                 else:
                     bft_queue.enqueue(vertex_path.append(neighbour))
     print(path)
Exemple #15
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        q = Queue()
        q.enqueue([starting_vertex])
        visited = set()

        while q.size() > 0:
            path = q.dequeue()
            last_item = path[-1]

            if last_item not in visited:
                visited.add(last_item)

                if last_item is destination_vertex:
                    return path

                for neighbor in self.get_neighbors(last_item):
                    q.enqueue([*path, neighbor])
Exemple #16
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     temp = []
     #make queue
     queue = Queue()
     #visited set
     visited = set()
     #put starting in queue
     queue.enqueue(starting_vertex)
     #while not empty, dequeue item, and mark item as visited
     while queue.size():
         node = queue.dequeue()
         visited.add(node)
         temp.append(node)
         #for each dequeued item edges, place in queue if not visited
         for edge in self.vertices[node]:
             if edge not in visited:
                 queue.enqueue(edge)
     print(temp)
Exemple #17
0
def get_shortest_path(start, end):
    # return list of shortest path id's from start to end using BFS
    qq = Queue()
    qq.enqueue([start])
    visited = set()

    while qq.size() > 0:
        path = qq.dequeue()
        if path[-1] not in visited:
            # mark room as visited
            visited.add(path[-1])
            if path[-1] == end:
                return path
            else:
                # find exits and find neig
                exits = world.rooms[path[-1]].get_exits()
                for direction in exits:
                    # add the neighbor to the queue
                    neighbor = world.rooms[path[-1]].get_room_in_direction(
                        direction)
                    print("unpack path? ", *path, "neigbor.id", neighbor.id)
                    qq.enqueue([*path, neighbor.id])
Exemple #18
0
    def bfs(self, starting_vertex, destination_vertex):
        q = Queue()
        visited = []
        q.enqueue([starting_vertex])

        while q.size():
            path = q.dequeue()
            # print("Path", path)
            vertex = path[-1]

            if vertex not in visited:
                if vertex == destination_vertex:
                    return path

                visited.append(vertex)

                for next_vertex in self.vertices[vertex]:
                    new_path = list(path)
                    new_path.append(next_vertex)
                    q.enqueue(new_path)

        return None
Exemple #19
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breadth-first order.
        """
        # make a queue
        q = Queue()
        # make a set to track nodes we've visited
        visited = set()

        path = [starting_vertex]
        q.enqueue(path)

        # while queue isn't empty
        while q.size() > 0:
            ## dequeue the path at the front of the line
            current_path = q.dequeue()
            current_node = current_path[-1]

            ### if this node is our target node
            if current_node == destination_vertex:
                #### return it!! return TRUE
                return current_path

        ### if not visited
            if current_node not in visited:
                #### mark as visited
                visited.add(current_node)
                #### get its neighbors
                neighbors = self.get_neighbors(current_node)
                #### for each neighbor
                for neighbor in neighbors:
                    ## copy path so we don't mutate the original path for different nodes
                    path_copy = current_path[:]
                    path_copy.append(neighbor)

                    ##### add to our queue
                    q.enqueue(path_copy)
Exemple #20
0
    def get_all_social_paths(self, user_id):
        """
        Takes a user's user_id as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        q = Queue()
        q.enqueue([user_id])
        visited = {}  # Note that this is a dictionary, not a set
        while q.size() > 0:
            path = q.dequeue()
            u = path[-1]
            if u not in visited:
                visited[u] = path
                for friend in self.friendships[u]:
                    path_copy = path.copy()
                    path_copy.append(friend)
                    q.enqueue(path_copy)
        return visited
Exemple #21
0
    def bfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing the shortest path from
        starting_vertex to destination_vertex in
        breath-first order.
        """
        # create set to hold visited vertex
        visited = set()

        # Initialize queue
        queue = Queue()

        # enqueue starting_vertex in a list
        queue.enqueue([starting_vertex])

        # while there is path in queue
        while queue.size():
            # dequeue path
            path = queue.dequeue()

            # get the last vertex in the dequeued path
            last_vertex = path[-1]

            # if last vertex is not already visited
            if last_vertex not in visited:
                # loop through the neighbors of the last_vertex
                for neighbor in self.vertices[last_vertex]:
                    # create new path list
                    new_path = list(path)
                    # append all the neighbors of current vertex to list
                    new_path.append(neighbor)
                    # check if the current neighbor is the destination
                    if neighbor == destination_vertex:
                        # return the new path
                        return new_path
                    # enqueue the new path
                    queue.enqueue(new_path)
                # add the last vertex to visited
                visited.add(last_vertex)
Exemple #22
0
    def getAllSocialPaths(self, userID):
        """
        Takes a user's userID as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        q = Queue()
        q.enqueue(userID)
        visited = {}

        while q.size() > 0:
            node = q.dequeue()

            for user in self.friendships[node]:
                if user not in visited:
                    visited[user] = self.bfs(user, userID)
                    q.enqueue(user)

        return visited
Exemple #23
0
    def get_all_social_paths(self, user_id):
        """
        Takes a user's user_id as an argument

        Returns a dictionary containing every user in that user's
        extended network with the shortest friendship path between them.

        The key is the friend's ID and the value is the path.
        """
        q = Queue()
        #Return extended network of users 
        visited = {}  # Note that this is a dictionary, not a set
        # Dictionary will have a key and value pair 
        # !!!! IMPLEMENT ME
        q.enqueue([user_id])
        # For every user_id we need to traverse and find connecting nodes
        # for each node find the shortest path 
        # counter = 0
        while q.size() > 0:
            # counter += 1
            # print("Degree",counter)
            path = q.dequeue()
            # print(path)
            v = path[-1]
            if v not in visited:
                # print(v)
                visited[v] = path
                for friend_id in self.friendships[v]:
                    path_copy = path.copy()
                    path_copy.append(friend_id)
                    q.enqueue(path_copy)
                    
                
        # set the shortest path as the value in key value 
        # Track visited 
        # If not visited ...
            #run our bft for shortest path 

        return visited
Exemple #24
0
def bft(destination):
    q = Queue()
    q.enqueue([(player.current_room,None)])
    newly_visited = set()
    newly_visited.add(player.current_room)
    while q.size() > 0:
        path = q.dequeue()
        current = path[-1][0]
        if current == destination:
            break
        neighbors = get_neighbors(current)
        neighbors = list(neighbors).sort(reverse=True)
        for direction,next_room in neighbors:
            if next_room not in newly_visited:
                new_path = path + [(next_room,direction)]
                q.enqueue(new_path)
                newly_visited.add(next_room)

    for i in range(1,len(path)):
        player.travel(path[i][1])
        traversal_path.append(path[i][1])
        visited.add(path[i][0])
Exemple #25
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """

        # initialize queue with starting vertex
        q = Queue()
        q.enqueue(starting_vertex)

        # set to keep track of vertexes already seen
        visited = set()

        # while queue is not empty if we haven't seen the element frome the
        # queue, add to visited, print, and add neighbors to queue
        while q.size() > 0:
            vertex = q.dequeue()
            if vertex not in visited:
                visited.add(vertex)
                print(vertex)
                for edge in self.get_neighbors(vertex):
                    q.enqueue(edge)
Exemple #26
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     # create empty queue and enqueue
     q = Queue()
     q.enqueue(starting_vertex)
     # create a set to store visited vertices
     visited = set()
     # while the queue isn't empty
     if q.size() > 0:
         # dequeue the first vertex
         v = q.dequeue()
         # if that vertex hasn't been visited
         if v not in visited:
             # mark it as visited
             print(v)
             visited.add(v)
             # add all of its neighbors to the back of the queue
             for neighbor in self.vertices[v]:
                 q.enqueue(neighbor)
Exemple #27
0
 def bfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing the shortest path from
     starting_vertex to destination_vertex in
     breath-first order.
     """
     qq = Queue()
     visited = set()
     qq.enqueue([starting_vertex])
     while qq.size() > 0:
         path = qq.dequeue()
         vertex = path[-1]
         if vertex not in visited:
             # Did we find the target vertex
             if vertex == destination_vertex:
                 return path
             visited.add(vertex)
             for next_vert in self.vertices[vertex]:
                 # create a new list and not just a reference or shallow copy
                 new_path = list(path)
                 new_path.append(next_vert)
                 qq.enqueue(new_path)
Exemple #28
0
 def bfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing the shortest path from
     starting_vertex to destination_vertex in
     breath-first order.
     """
     #TODO
     q = Queue()
     q.enqueue([starting_vertex])
     visited = set()
     while q.size() > 0:
         path = q.dequeue()
         vert = path[-1]
         if vert not in visited:
             if vert == destination_vertex:
                 return path
             visited.add(vert)
             for next_vert in self.get_neighbors(vert):
                 path_copy = list(path)
                 path_copy.append(next_vert)
                 q.enqueue(path_copy)
     return None
Exemple #29
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     # create an empty queue and enqueue the starting vertex ID
     q = Queue()
     q.enqueue(starting_vertex)
     # create a set to store the visited vertices
     visited = set()
     # while the queue is not empty
     while q.size() > 0:
         # Dequeue the first vertex
         v = q.dequeue()
         # if that vertex has not been vidited
         if v not in visited:
             # mark it as visited (printing for a representation)
             print(v)
             visited.add(v)
             # then add all of it's neighbors to the back of the queue
             for next_vertex in self.vertices[v]:
                 q.enqueue(next_vertex)
Exemple #30
0
def navigate_to(target_room_id):
    init_response = init()
    print(f"Waiting {init_response['cooldown']} second(s).")
    time.sleep(init_response["cooldown"])

    current_room_id = str(init_response["room_id"])
    target_room_id = str(target_room_id)

    visited = {}
    queue = Queue()
    queue.enqueue([current_room_id])

    while queue.size() > 0:
        path = queue.dequeue()
        last_room_id = str(path[-1])
        exits = map[last_room_id]["available_exits"]

        if last_room_id not in visited:
            visited[last_room_id] = path

            for exit in exits:
                queue.enqueue(path.copy() +
                              [str(map[last_room_id]["exits"][exit])])

    room_id_path = visited[target_room_id][1:]
    traversal_path = []

    current_room_exits = map[current_room_id]["exits"]

    for room_id in room_id_path:
        direction = list(current_room_exits.keys())[list(
            current_room_exits.values()).index(int(room_id))]
        traversal_path.append((direction, room_id))
        current_room_exits = map[str(room_id)]["exits"]

    for path in traversal_path:
        response = move(path[0], path[1])
        print(f"Waiting {response['cooldown']} second(s).")
        time.sleep(response["cooldown"])