示例#1
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     # pass  # TODO
     # set up queue
     q = Queue()
     # Traver from node to node " Linked list { whiles current } <- trav"
     # Graphs Trav = Html Href
     traversed = []
     q.enqueue(starting_vertex)
     # while the queue still has values in it
     while q.size() > 0:
         cur_val = q.dequeue()
         traversed.append(cur_val)
         for val in self.vertices[cur_val]:
             # make sure we've not gone that way
             if val not in traversed:
                 q.enqueue(val)
         print(cur_val)
示例#2
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():
         path = q.dequeue()
         last_node = path[-1]
         if last_node in visited:
             continue
         elif last_node == destination_vertex:
             return path
         visited.add(last_node)
         for neighbor in self.get_neighbors(last_node):
             new_path = path.copy()
             new_path.append(neighbor)
             q.enqueue(new_path)
示例#3
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.
        """
        queue = Queue()
        queue.enqueue([starting_vertex])
        visited = set()

        while queue.size() > 0:
            path = queue.dequeue()
            vertex = path[-1]
            if vertex not in visited:
                if vertex == destination_vertex:
                    return path
                visited.add(vertex)
                for next_vert in self.get_neighbors(vertex):
                    new_path = list(path)
                    new_path.append(next_vert)
                    queue.enqueue(new_path)
示例#4
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        queue = Queue()
        queue.enqueue(starting_vertex)

        explored = []

        while queue.size() > 0:
            u = queue.queue[0]

            for v in self.vertices[u]:
                if v not in explored:
                    queue.enqueue(v)

            explored.append(u)
            queue.dequeue()

        print(explored)
示例#5
0
 def bft(self, starting_vertex):
     """Print each vertex in breadth-first order beginning from starting_vertex"""
     # Create a queue
     q = Queue()
     # Enqueue the starting vertex
     q.enqueue(starting_vertex)
     # Create a set to store visited vertices
     visited = set()
     # While the queue is not empty...
     while q.size() > 0:
         # Dequeue the first vertex
         v = q.dequeue()
         # Check if it's been visited
         # If it hasn't been visited...
         if v not in visited:
             # Mark it as visited
             visited.add(v)
             print(v)
             # Enqueue all of its neighbors
             for neighbor in self.get_neighbors(v):
                 q.enqueue(neighbor)
示例#6
0
文件: graph.py 项目: ajb85/Graphs
 def bfs(self, starting_vertex, destination_vertex):
     print("BFS")
     visited = set()
     q = Queue()
     q.enqueue([1])
     paths = {}
     while(q.size() > 0):
         path = q.dequeue()
         v = path[-1]
         if(v == destination_vertex):
             length = len(path)
             if(length in paths):
                 paths[length].append(path)
             else:
                 paths[length] = [path]
         elif(not v in visited):
             visited.add(v)
             for neighbor in self.vertices[v]:
                 q.enqueue([*path, neighbor])
     smallest = min(paths.keys())
     return None if len(paths[smallest]) == 0 else paths[smallest] if len(paths[smallest]) > 1 else paths[smallest][0]
示例#7
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        q = Queue()

        q.enqueue(starting_vertex)

        visited = set()

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

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

                print(current_node)

                for neighbor in self.get_neighbors(current_node):
                    q.enqueue(neighbor)
示例#8
0
 def bft(self, starting_vertex):
     # Create a q and enqueue starting vertex
     qq = Queue()
     qq.enqueue([starting_vertex])
     # Create a set of traversed vertices
     visited = set()
     # While queue is not empty:
     while qq.size() > 0:
         # dequeue/pop the first vertex
         path = qq.dequeue()
         # if not visited
         if path[-1] not in visited:
             # DO THE THING!!!!!!!
             print(path[-1])
             # mark as visited
             visited.add(path[-1])
             # enqueue all neighbors
             for next_vert in self.get_neighbors(path[-1]):
                 new_path = list(path)
                 new_path.append(next_vert)
                 qq.enqueue(new_path)
示例#9
0
文件: social.py 项目: brellin/Graphs
    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 = {}
        s = Queue()
        s.enqueue([user_id])
        while s.size():
            path = s.dequeue()
            curr_friend = path[-1]
            if curr_friend not in visited:
                visited[curr_friend] = path
                for friend in self.friendships[curr_friend]:
                    s.enqueue(list(path) + [friend])

        return visited
示例#10
0
    def bft(self, starting_vertex):
        to_visit = Queue()
        visited = set()

        to_visit.enqueue(starting_vertex)
        while to_visit.size() > 0:
            # dequeue first entry
            v = to_visit.dequeue()

            # if not visited:
            if v not in visited:
                # Visit the node (print it out)
                print(v)

                # Add it to the visited set
                visited.add(v)

                # enqueue all its neighbors
                for n in self.get_neighbors(v):
                    #print(f"Adding: {n}")
                    to_visit.enqueue(n)
示例#11
0
文件: words.py 项目: justman00/Graphs
def find_ladders(beginWord, endWord):
    visited = set()
    q = Queue()

    q.enqueue([beginWord])

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

        if node == endWord:
            return len(path)

        if node not in visited:
            visited.add(node)
            for val in get_neighbours(node):
                copy_path = path.copy()
                copy_path.append(val)
                q.enqueue(copy_path)

    pass
示例#12
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 node ID
     q = Queue()
     q.enqueue(starting_vertex)
     # create a set to store the visited nodes
     visited = set()
     # While the queue is not empty
     while q.size() > 0:
         v = q.dequeue()
         # if the current node has not been visited
         if v not in visited:
             # mark as visted. print v and add v to visited set
             print(v)
             visited.add(v)
             # then add all of it's neougbours to the back of the queue
             for next_node in self.vertices[v]:
                 q.enqueue(next_node)
示例#13
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()
        qq.enqueue(starting_vertex)

        visited = set()
        while qq.size() > 0:
            path = qq.dequeue()
            if path[-1] == destination_vertex:
                return path
            print(path[-1])
            visited.add(path[-1])

            for next_vertex in self.get_neighbors(path[-1]):
                new_path = path.copy()  # can also do list(path)
                new_path.append(next_vertex)
                qq.enqueue(new_path)
示例#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.
     """
     q = Queue()
     visited = []
     q.enqueue([starting_vertex])
     while q.size() > 0:
         curPath = q.dequeue()
         curNode = curPath[-1]
         print('bfs curNode; ', curNode)
         if curNode is destination_vertex:
             return curPath
         if curNode not in visited:
             visited.append(curNode)
             for neighbor in self.get_neighbors(curNode):
                 tempPath = curPath.copy()
                 tempPath.append(neighbor)
                 q.enqueue(tempPath)
示例#15
0
    def bft(self, starting_vertex):
        q = Queue()
        visited = set()
        # ancestor problem:
        # need list of earlier nodes
        ancestors = []
        # initialize
        q.enqueue(starting_vertex)

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

            if v not in visited:
                # print(v) # original purpose
                ancestors.append(v)  # ancestor purpose
                visited.add(v)

                for neighbor in self.get_neighbors(v):
                    q.enqueue(neighbor)
        # print(ancestors)
        return ancestors
示例#16
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        q = Queue()
        visited = set()

        q.enqueue(starting_vertex)

        while q.size() > 0:

            v = q.dequeue()

            if v not in visited:
                print(v)

                visited.add(v)

                for neighbor in self.get_neighbors(v):
                    q.enqueue(neighbor)
示例#17
0
    def find_new_room(self):
        q = Queue()
        q.enqueue((self.current_room, []))

        visited = set()

        while q.size() > 0:
            room, path = q.dequeue()

            if room.id not in self.seen:
                return path

            elif room.id not in visited:
                visited.add(room.id)

                for exit in room.get_exits():
                    path_copy = path.copy()
                    path_copy.append(exit)
                    q.enqueue((room.get_room_in_direction(exit), path_copy))

        raise ValueError('Room not found')
示例#18
0
def word_ladder(begin_word, end_word):
    q = Queue()
    visited = set()

    q.enqueue([begin_word])

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

        if word == end_word:
            return path

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

            for neighbor in get_neighbors(word):
                path_copy = path.copy()
                path_copy.append(neighbor)

                q.enqueue(path_copy)
示例#19
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        q = Queue()

        q.enqueue(starting_vertex)

        found = [
            starting_vertex,
        ]

        while q.size() > 0:
            for vertex in self.vertices[q.queue[0]]:
                if vertex not in found:
                    q.enqueue(vertex)
                    found.append(vertex)
            q.dequeue()
            # TODO format this more nicely
        print(f"BFT: {found}")
示例#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 ID and the value is the path.
     """
     q  = Queue()
     q.enqueue([user_id]) 
     # I made a dictionary instead of set
     visited = {} 
     
     while q.size() > 0:
         cur = q.dequeue()
         last = cur[-1]
         visited[last] = cur
         for f in self.friendships[last]:
             if f not in visited:
                 new_path = cur + [f]
                 q.enqueue(new_path)
     return visited
示例#21
0
 def bfs(self, starting_vertex):  # , destination_vertex
     q = Queue()
     q.enqueue([(starting_vertex.id, None)])
     visited = set()
     while q.size() > 0:
         path = q.dequeue()
         v = path[-1][0]
         unseen = self.get_neighbors(v)
         # if v not in visited:
         #     if v == destination_vertex:
         #         return path
         #     visited.add(v)
         #     for next_v in self.get_neighbors(v):
         #         path_copy = list(path)
         #         path_copy.append(next_v)
         #         q.enqueue(path_copy)
         if len(unseen) > 0:
             return path[1:]
         for dirxn, room in self.vertices[v].items():
             q.enqueue(path + [(room, dirxn)])
     return None
示例#22
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        queue = Queue()
        queue.enqueue(starting_vertex)
        result = []
        visited = {}
        visited[starting_vertex] = True

        while queue.size():
            current_vertex = queue.dequeue()
            result.append(current_vertex)

            for neighbor in self.vertices[current_vertex]:
                if neighbor not in visited:
                    visited[neighbor] = True
                    queue.enqueue(neighbor)
        for vertex in result:
            print(vertex)
示例#23
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()
     # Create and empty 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 visited...
         if v not in visited:
             # Mark is as visited
             print(v)
             visited.add(v)
             # Then add all of its neighbors to the back of the queue
             for neighbor in self.vertices[v]:
                 q.enqueue(neighbor)
示例#24
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """
        pass  # TODO

        visited = {i: 0 for i in self.vertices}

        my_queue = Queue()
        my_queue.enqueue(starting_vertex)
        while my_queue.size() > 0:

            current_node = my_queue.dequeue()

            if visited[current_node] == 0:
                print(current_node)

                visited[current_node] = 1
                for node in self.vertices[current_node]:
                    my_queue.enqueue(node)
示例#25
0
 def bfs(self, starting_vertex, destination_vertex):
     """
     Return a list containing the shortest path from
     starting_vertex to destination_vertex (target) in
     breath-first order.
     """
     q = Queue()
     q.enqueue( [starting_vertex] )
     visited = set()
     while q.size() > 0:
         path = q.dequeue()
         v = path[-1]
         if v == destination_vertex:
             return path
         if v not in visited:
             visited.add(v)
             for neighbor in self.get_neighbors(v):
                 path_copy = path.copy()
                 path_copy.append(neighbor)
                 q.enqueue(path_copy)
     print("bfs------------")
示例#26
0
文件: graph.py 项目: tolaked/Graphs
 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 a queue to hold the vertex ids
     q = Queue()
     q.enqueue([starting_vertex])
     visited = set()
     while q.size() > 0:
         p = q.dequeue()
         v = p[-1]
         if v not in visited:
             if v == destination_vertex:
                 return p
             visited.add(v)
             for next_vertex in self.get_neighbors(v):
                 new_p = list(p)
                 new_p.append(next_vertex)
                 q.enqueue(new_p)
示例#27
0
def earliest_ancestor(ancestors, starting_node):
    graph = Graph()
    for pair in ancestors:
        graph.add_vertex(pair[0])
        graph.add_vertex(pair[1])
        graph.add_edge(pair[1], pair[0])
    earliestAncestor = -1
    maxLength = 1
    queue = Queue()
    queue.enqueue([starting_node])
    while queue.size() > 0:
        path = queue.dequeue()
        vertex = path[-1]
        if (len(path) >= maxLength and earliestAncestor != -1) or (len(path) > maxLength):
            earliestAncestor = vertex
            maxLength = len(path)
        for neighbor in graph.vertices[vertex]:
            path_copy = list(path)
            path_copy.append(neighbor)
            queue.enqueue(path_copy)
    return earliestAncestor
示例#28
0
    def bft(self, starting_vertex):
        """
        Print each vertex in breadth-first order
        beginning from starting_vertex.
        """

        queue = Queue()
        already_explored = {}
        queue.enqueue(starting_vertex)
        already_explored[starting_vertex] = True

        while queue.size():
            current_vertex = queue.dequeue()            
            print(f'{current_vertex} ', end='')

            for edge in self.vertices[current_vertex]:
                if edge not in already_explored:
                    queue.enqueue(edge)
                    already_explored[edge] = True

        print()                
示例#29
0
文件: graph.py 项目: emilyelri/Graphs
 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()
     path_list = [starting_vertex]
     q.enqueue(path_list)
     visited = set()
     while q.size() > 0:
         path = q.dequeue()
         last = path[-1]
         if last not in visited:
             visited.add(last)
             if last is destination_vertex:
                 return path
             for neighbor in self.get_neighbors(last):
                 new_path = path.copy()
                 new_path.append(neighbor)
                 q.enqueue(new_path)
示例#30
0
 def bft(self, starting_vertex):
     """
     Print each vertex in breadth-first order
     beginning from starting_vertex.
     """
     # create a queue, enqueue starting vertex
     qq = Queue()
     qq.enqueue([starting_vertex])
     # create set of traversed vertices
     visited = set()
     # while queue is not empty: dequeue/pop first vertex
     while qq.size() > 0:
         path = qq.dequeue()
         # if not visited, visit and mark as visited
         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)
                 qq.enqueue(new_path)