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)
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------------")
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
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)
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)
def bfs(current_room, next_room): visited = {} q = Queue() q.enqueue([current_room]) while next_room not in visited: path = q.dequeue() current_room = str(path[-1]) if current_room not in visited: visited[str(current_room)] = path for direction in full_data[str(current_room)]['directions']: new_path = path.copy() new_path.append( full_data[str(current_room)]['directions'][direction]) q.enqueue(new_path) return visited[next_room]
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: # while the Queue is NOT empty # create a path of how we got to this point path = qq.dequeue() vertex = path[-1] if vertex not in visited: if vertex == destination_vertex: return path visited.add(vertex) for next_vert in self.vertices[vertex]: new_path = list(path) new_path.append(next_vert) qq.enqueue(new_path)
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)
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
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)
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
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)
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)
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)
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()
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)
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
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)
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')
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
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]
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)
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
def bft(self, starting_vertex): """ Print each vertex in breadth-first order beginning from starting_vertex. """ # Define set of vertices already traversed traversed_vertices = {starting_vertex} # Create a Queue since it's breadth-first queue = Queue() current_node = starting_vertex # While current_node isn't None while current_node: print(current_node) for neighbor in self.get_neighbors(current_node): if neighbor not in traversed_vertices: traversed_vertices.add(neighbor) queue.enqueue(neighbor) # Dequeue and set it as current_node. # If the queue is empty, current_node will be None # and the while-loop will end. current_node = queue.dequeue()
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)
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)
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)
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)
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)
def earliest_ancestor(ancestors, starting_node): queue = Queue() current_node = starting_node relationships = {} for node in ancestors: if node[1] not in relationships: relationships[node[1]] = set() relationships[node[1]].add(node[0]) if starting_node in relationships: queue.enqueue(relationships[current_node]) else: return -1 while True: relations = queue.dequeue() current_node = min(relations) if current_node not in relationships: return current_node else: queue.enqueue(relationships[current_node])