Пример #1
0
    def dfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing a path from
        starting_vertex to destination_vertex in
        depth-first order.
        """

        s = Stack()
        s.push(starting_vertex)

        # Repeat until stack is empty
        while s.size() > 0:

            # pop first vert
            v = s.pop()

            if v is not destination_vertex:
                for next_vert in self.get_neighbors(v):
                    s.push(next_vert)
Пример #2
0
def dft_islands(start_x, start_y, matrix, visited):
    '''
    Returns updated visited matrix after a
    dft of matrix starting from x,y
    '''
    s = Stack()
    s.push((start_x,start_y))
    visited = set()

    while s.size() > 0:
        v = s.pop()
        x = v[0]
        y = v[1]

        if not visited[y][x]:
            visited[y][x] = True
            for neighbor in get_island_neighbors(x, y, matrix):
                s.push(neighbor)
    return visited
Пример #3
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     s = Stack()
     s.push([starting_vertex])
     v = set()
     while s.size() > 0:
         path = s.pop()
         last_vertex = path[-1]
         if last_vertex == destination_vertex:
             return path
         v.add(last_vertex)
         for neighbor in self.vertices[last_vertex]:
             copy = path.copy()
             copy.append(neighbor)
             s.push(copy)
Пример #4
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     s = Stack()
     s.push((starting_vertex, ))
     visited = set()
     while s.size() > 0:
         p = s.pop()
         v = p[len(p) - 1]
         while v not in visited:
             if v == destination_vertex:
                 return list(p)
             visited.add(v)
             for neighbor in self.vertices[v]:
                 cp = p + (neighbor, )
                 s.push(cp)
Пример #5
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     stack = Stack()
     stack.push(starting_vertex)
     visited = set()
     path = []
     while stack.size() > 0:
         current_vertex = stack.pop()
         path.append(current_vertex)
         if current_vertex not in visited:
             if current_vertex == destination_vertex:
                 return path
             visited.add(current_vertex)
             for next_vertex in self.vertices[current_vertex]:
                 stack.push(next_vertex)
Пример #6
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        stack = Stack()
        visited = set()

        # set the starting vertex
        stack.push(starting_vertex)

        while stack.size():
            current_vertex = stack.pop()
            if current_vertex not in visited:
                print(current_vertex)
                visited.add(current_vertex)
                neighbors = self.get_neighbors(current_vertex)
                for neighbor in neighbors:
                    stack.push(neighbor)
Пример #7
0
 def dft(self, starting_vertex):
     """Print each vertex in depth-first order
     beginning from starting_vertex.
     """
     # Create stack and push starting vertex
     stack = Stack()
     stack.push([starting_vertex])
     # Create set of traversed vertices
     visited = set()
     while stack.size() > 0:
         path = stack.pop()  # Pop first vertex
         # If not in traversed
         if path[-1] not in visited:
             print(path[-1])  # DO THE THANG
             visited.add(path[-1])  # Mark as visited
             # Push all neighbors
             for ngbr in self.get_neighbors(path[-1]):
                 new_path = path + [ngbr]
                 stack.push(new_path)
Пример #8
0
    def dfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing a path from
        starting_vertex to destination_vertex in
        depth-first order.
        """
        stack, path = Stack(), []
        stack.push(starting_vertex)

        while stack.size() > 0:
            vertex = stack.pop()
            if vertex in path:
                continue
            path.append(vertex)
            for adj_vertex in self.vertices[vertex]:
                stack.push(adj_vertex)
            if vertex == destination_vertex:
                return path
        return path
Пример #9
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     stack = Stack()
     visited = set()
     stack.push([starting_vertex])
     while stack.size() > 0:
         path = stack.pop()
         vertex = path[-1]
         if vertex not in visited:
             if vertex == destination_vertex:
                 return path
             for connection in self.vertices[vertex]:
                 copy_path = path[:]
                 copy_path.append(connection)
                 stack.push(copy_path)
Пример #10
0
    def dfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing a path from
        starting_vertex to destination_vertex in
        depth-first order.
        """

        #1-make a stack ( using a placeholder s)
        s = Stack()

        #2- push our starting node
        s.push([starting_vertex])

        #3- make a set to track if we've been here before
        visited = set()

        #4-while our stack isn't empty
        while s.size() > 0:
            ##  pop off whatever's at the front of our line, this is our current node
            cur_path = s.pop()
            # get the last node of the path[-1]
            last_node = cur_path[-1]

            #if we haven't visited this node yet,
            if last_node not in visited:
                ### mark as visited
                visited.add(last_node)
                #print all the vertices
                print(last_node)

            ### get its neighbors
            neighbors = self.get_neighbors(last_node)

            ### for each of the neighbors,
            for neighbor in neighbors:
                next_path = cur_path.copy()
                next_path.append(neighbor)

                # check if neighbor is destination node
                if neighbor == destination_vertex:
                    return next_path
                #add the next node path to existing one
                s.push(next_path)
Пример #11
0
    def dfs(self, starting_vertex_id, target_value):
        s = Stack()
        s.push([starting_vertex_id])
        visited = set()

        while s.size() > 0:
            path = s.pop()

            vert = path[-1]

            if vert not in visited:
                if vert == target_value:
                    return path
                visited.add(vert)
                for next_vert in self.vertices[vert]:
                    new_path = list(path)
                    new_path.append(next_vert)
                    s.push(new_path)
        return None
Пример #12
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        s = Stack()
        v = set()

        s.push(starting_vertex)

        while s.size() > 0:
            x = s.pop()

            if x not in v:
                print(x)
                v.add(x)

                for n in self.get_neighbors(x):
                    s.push(n)
Пример #13
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     s = Stack()
     s.push([starting_vertex])
     visited = set()
     while s.size() > 0:
         path = s.pop()
         if path[-1] not in visited:
             visited.add(path[-1])
             for neighbor in self.get_neighbors(path[-1]):
                 new_path = [*path, neighbor]
                 if neighbor == destination_vertex:
                     return new_path
                 s.push(new_path)
     return
Пример #14
0
    def dfs(self, starting_vertex, destination_vertex):
        s = Stack()
        visited = []
        s.push([starting_vertex])

        while s.size() > 0:
            path = s.pop()
            node = path[-1]

            if node not in visited:
                neighbors = self.vertices[node]
                for neighbor in neighbors:
                    nextPath = list(path)
                    nextPath.append(neighbor)
                    s.push(nextPath)
                    if neighbor == destination_vertex:
                        return nextPath
                visited.append(node)
        return None
Пример #15
0
def go_travel():
    my_map = {0: {'w': '?', 's': '?', 'n': '?', 'e': '?'}}
    s = Stack()
    prev_room = 0
    direction_traveled = ''

    #Had to refactor it so that only the ids, not the entire room, was in the stack
    s.push(0)
    while s.size() > 0:
        cur_room = s.pop()
        map_it(player.current_room.id, prev_room, my_map, direction_traveled)
        if '?' in my_map[cur_room].values():
            for key, value in my_map[cur_room].items():
                if value == '?':
                    player.travel(key)
                    s.push(player.current_room.id)
                    direction_traveled = key
                    traversal_path.append(direction_traveled)
                    prev_room = cur_room
                    break
        else:
            if bfs(cur_room, my_map) is None:
                return
            path_to_exit = bfs(cur_room, my_map)
            new_traverse = []
            #Back to tuples for data management
            for index, room in enumerate(path_to_exit):
                #Manages +1 errors while checking to make sure that your room is in visited
                #There has to be a cleaner way than this, but I couldn't figure out how to do it without enumerate: That index helps you locate where you are in the path; this is where I kept getting stuck earlier
                if index < len(path_to_exit) - 1 and path_to_exit[
                        index + 1] in my_map[room].values():
                    for key, value in my_map[room].items():
                        if value == path_to_exit[index + 1]:
                            new_traverse.append(key)

            # Move there
            for move in new_traverse:
                prev_room = player.current_room.id
                player.travel(move)
                direction_traveled = move
                traversal_path.append(move)
            if '?' in my_map[player.current_room.id].values():
                s.push(player.current_room.id)
Пример #16
0
 def dft_recursive(self, starting_vertex, visited =None):
     """
     Print each vertex in depth-first order
     beginning from starting_vertex.
     This should be done using recursion.
     """
     if visited is None:
         visited = set()
     s = Stack()
     s.push(starting_vertex)
     while s.size() > 0 :
         v = s.pop()
         if v not in visited:
             visited.add(v)
             print("recursive", v)
             for neighbor in self.vertices[v]:
                 self.dft_recursive(neighbor, visited)
 
     pass  # TODO
Пример #17
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     s = Stack()
     s.push([starting_vertex])
     visited = set()
     while s.size() > 0:
         current_path = s.pop()
         ending_vertex = current_path[-1]
         if ending_vertex == destination_vertex:
             return current_path
         visited.add(ending_vertex)
         for neighbor in self.get_neighbors(ending_vertex):
             if neighbor not in visited:
                 new_path = current_path + [neighbor]
                 s.push(new_path)
Пример #18
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        stack = Stack()
        visited = set()

        stack.push(starting_vertex)

        while stack.size() > 0:
            current_node = stack.pop()

            if current_node not in visited:
                visited.add(current_node)
                print(current_node)
                edges = self.get_neighbors(current_node)
                for edge in edges:
                    stack.push(edge)
Пример #19
0
 def dft(self, starting_vertex):
     # create an empty stack
     s = Stack()  # import stack data structure
     # create an empty visited set
     visited = set()
     # push the starting vertex to the stack
     s.push(starting_vertex)
     # while stack is not empty
     while s.size() > 0:
         # pop the last vertex, this is the main difference between BFT and DFT
         v = s.pop()
         # if it has not been visited
         if v not in visited:
             # mark it as visited (add it to the visited set)
             visited.add(v)
             # then push its neighbors onto the stack
             for neighbor in self.vertices[v]:
                 s.push(neighbor)
     print("DFT", visited)
Пример #20
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        stack = Stack()
        stack.push(starting_vertex)
        visited = set()

        while stack.size() > 0:
            current_vertex = stack.pop()
            if current_vertex not in visited:
                # print(f'vertex: {current_vertex}')
                print(current_vertex)
                visited.add(current_vertex)

                edges = self.get_neighbors(current_vertex)
                for vertex in edges:
                    stack.push(vertex)
Пример #21
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        s = Stack()
        s.push(starting_vertex)

        visited = set()

        while s.size() > 0:
            v = s.pop()

            if v not in visited:
                print(v)
                visited.add(v)

                for next_vert in self.get_neighbors(v):
                    s.push(next_vert)
Пример #22
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """

        stack = Stack()  # create an empty stack
        stack.push(starting_vertex)  # push the starting_vertex onto the stack
        visited = set()  # create a visited set
        while stack.size() > 0:  # while our stack isn't empty:
            current_node = stack.pop(
            )  # pop off what's on top, this is our current_node
            if current_node not in visited:  # if it hasn't been visited:
                print(current_node)
                visited.add(current_node)  # mark it as visited
                neighbors = self.get_neighbors(
                    current_node)  # get its neighbors
                for neighbor in neighbors:  # and add each neighbor to the top of the stack
                    stack.push(neighbor)
Пример #23
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        path = []
        visited = set()
        to_visit = Stack()
        to_visit.push(starting_vertex)

        while to_visit.size() > 0:
            vertex = to_visit.pop()
            if vertex not in visited:
                visited.add(vertex)
                path.append(vertex)
                for neighbor in self.vertices[vertex]:
                    to_visit.push(neighbor)

        print(path)
Пример #24
0
    def dfs(self, starting_vertex, destination_vertex):
        tv = Stack()

        v = set()

        tv.push([starting_vertex])

        while tv.size() > 0:
            p = tv.pop()
            if p is None:
                continue
            if p[len(p) - 1] == destination_vertex:
                return p
            elif p[len(p) - 1] not in v:
                v.add(p[len(p) - 1])
                for a in self.vertices[p[len(p) - 1]]:
                    b = p.copy()
                    b.append(a)
                    tv.push(b)
Пример #25
0
    def dft(self, starting_vertex):
        """
        Print each vertex in depth-first order
        beginning from starting_vertex.
        """
        st = Stack()
        st.push([starting_vertex])

        visited = set()

        while st.size() > 0:
            path = st.pop()
            if path[-1] not in visited:
                print(path[-1])
                visited.add(path[-1])
                for next_vert in self.get_neighbors(path[-1]):
                    new_path = list(path)
                    new_path.append(next_vert)
                    st.push(new_path)
Пример #26
0
    def dfs(self, starting_vertex, destination_vertex):
        # Return a list containing a path from
        # starting_vertex to destination_vertex in
        # depth-first order.

        visited = set()
        stack = Stack()
        stack.push([starting_vertex])
        while stack.size() > 0:
            path = stack.pop()
            vertex = path[-1]
            if vertex == destination_vertex:
                return path
            if vertex not in visited:
                visited.add(vertex)
                for neighbor in self.vertices[vertex]:
                    path_copy = list(path)
                    path_copy.append(neighbor)
                    stack.push(path_copy)
Пример #27
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     ss = Stack()
     ss.push([starting_vertex])
     visited = set()
     while ss.size() > 0:
         path = ss.pop()
         if path[-1] not in visited:
             if path[-1] == destination_vertex:
                 return path
             visited.add(path[-1])
             for next_vert in self.get_neighbors(path[-1]):
                 new_path = list(path)
                 new_path.append(next_vert)
                 ss.push(new_path)
Пример #28
0
 def dfs(self, starting_vertex, destination_vertex):
     stack = Stack()
     visited = set()
     stack.push([starting_vertex])
     while stack.size() > 0:
         path = stack.pop()
         vertex = path[-1]
         if vertex == destination_vertex:
             return path
         if vertex not in visited:
             visited.add(vertex)
             for vert in self.vertices[vertex]:
                 new_path = path.copy()
                 new_path.append(vert)
                 if new_path[-1] == destination_vertex:
                     return new_path
                 else:
                     stack.push(new_path)
     return None
Пример #29
0
 def dfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing a path from
     starting_vertex to destination_vertex in
     depth-first order.
     """
     stack = Stack()
     visited = set()
     stack.push([starting_vertex])
     while stack.size():
         path = stack.pop()
         node = path[-1]
         if node not in visited:
             if node == destination_vertex:
                 return path
             for neighbor in self.vertices[node]:
                 copy_path = path.copy()
                 copy_path.append(neighbor)
                 stack.push(copy_path)
Пример #30
0
    def dfs(self, starting_vertex, destination_vertex):
        """
        Return a list containing a path from
        starting_vertex to destination_vertex in
        depth-first order.
        """
        # Create an empty stack and enqueue the PATH TO starting_vertex
        s = Stack()
        s.push([starting_vertex])
        # Create an empty set to track visited verticies
        visited = set()
        # while the queue is not empty:
        while s.size() != 0:
            # get current vertex PATH (dequeue from queue)
            current_paths = s.pop()

            # set the current vertex to the LAST element of the PATH
            current = current_paths[-1]

            # Check if the current vertex has not been visited:
            if current not in visited:

                # CHECK IF THE CURRENT VERTEX IS DESTINATION
                if current == destination_vertex:
                    # IF IT IS, STOP AND RETURN
                    return current_paths

                # Mark the current vertex as visited
                # Add the current vertex to a visited_set
                visited.add(current)

                neighbors = self.get_neighbors(current)
                # Queue up NEW paths with each neighbor:

                for i in neighbors:
                    # take current path
                    new_path = []
                    new_path = new_path + current_paths
                    new_path.append(i)

                    # append the neighbor to it
                    # queue up NEW path
                    s.push(new_path)