def is_bipartite(graph): """Runs breadth-first search through a graph to determine if a graph is bipartite. Uses 2-color test: https://en.wikipedia.org/wiki/Bipartite_graph#Testing_bipartiteness Objectives: O(m + n) running time (linear) Args: graph: the given graph, with nodes encoded as strings. Returns: True if a graph is bipartite, False otherwise. >>> g = graph(kind='undirected') >>> g.add_vertex('A') >>> g.add_vertex('B') >>> g.add_vertex('C') >>> g.add_vertex('D') >>> g.add_edge('A', 'B') >>> g.add_edge('A', 'C') >>> g.add_edge('B', 'D') >>> g.add_edge('C', 'D') >>> is_bipartite(g) True >>> g.add_vertex('E') >>> g.add_edge('B', 'E') >>> g.add_edge('D', 'E') >>> is_bipartite(g) False """ V = set(graph.vertices.keys()) explored = {node: 0 for node in V} q = queue() color = {} curr_color = 0 s = V.pop() explored[s] = 1 color[s] = curr_color q.push(s) while q.size: node = q.pop() curr_color = 0 if color[node] else 1 for neighbor in graph.vertices[node]: if not explored[neighbor]: explored[neighbor] = 1 color[neighbor] = curr_color q.push(neighbor) elif color[neighbor] == color[node]: return False return True
def bfs(graph, root): """Runs breadth-first search through a graph, starting at a given root. Objectives: O(m + n) running time (linear) Args: graph: the given graph, with nodes encoded as strings. root: the node from which to start the search. Returns: A dictionary containing the parent-child relationships of the bfs tree. >>> g = graph(kind='undirected') >>> g.add_vertex('A') >>> g.add_vertex('B') >>> g.add_vertex('C') >>> g.add_vertex('D') >>> g.add_vertex('E') >>> g.add_edge('A', 'B') >>> g.add_edge('A', 'C') >>> g.add_edge('B', 'D') >>> g.add_edge('C', 'D') >>> g.add_edge('B', 'E') >>> g.add_edge('D', 'E') >>> prev, dist = bfs(g, 'A') >>> prev['A'] >>> prev['D'] 'C' >>> prev['E'] 'B' >>> dist['C'] 1 >>> dist['E'] 2 """ V = set(graph.vertices.keys()) - {root} explored = {node: 0 for node in V} q = queue() prev = {root: None} dist = {root: 0} explored[root] = 1 q.push(root) while q.size: node = q.pop() for neighbor in graph.vertices[node]: if not explored[neighbor]: explored[neighbor] = 1 prev[neighbor] = node dist[neighbor] = dist[node] + 1 q.push(neighbor) return prev, dist
def run(): print "queue TEST STARTING" q = queue() i = 0 LIMIT = 1000 for j in xrange(0, LIMIT + 1): q.push(j) for j in xrange(0, LIMIT + 1): x = q.pop() assert x == j assert q.empty() print "queue TEST COMPLETED"
def run(): print "queue TEST STARTING" q = queue() i = 0 LIMIT = 1000 for j in xrange(0,LIMIT+1): q.push(j) for j in xrange(0, LIMIT+1): x = q.pop() assert x == j assert q.empty() print "queue TEST COMPLETED"
def is_bipartite(graph): """Runs breadth-first search through a graph to determine if a graph is bipartite. Uses 2-color test: https://en.wikipedia.org/wiki/Bipartite_graph#Testing_bipartiteness Objectives: O(m + n) running time (linear) Args: graph: the given graph, with nodes encoded as strings. Returns: True if a graph is bipartite, False otherwise. >>> g = graph(kind='undirected') >>> g.add_vertex('A') >>> g.add_vertex('B') >>> g.add_vertex('C') >>> g.add_vertex('D') >>> g.add_edge('A', 'B') >>> g.add_edge('A', 'C') >>> g.add_edge('B', 'D') >>> g.add_edge('C', 'D') >>> is_bipartite(g) True >>> g.add_vertex('E') >>> g.add_edge('B', 'E') >>> g.add_edge('D', 'E') >>> is_bipartite(g) False """ V = set(graph.vertices.keys()) explored = {node: 0 for node in V} q = queue() color = {} s = V.pop() explored[s] = 1 color[s] = 0 q.push(s) while q.size: node = q.pop() for neighbor in graph.vertices[node]: if not explored[neighbor]: explored[neighbor] = 1 color[neighbor] = 1 - color[node] # Opposite q.push(neighbor) elif color[neighbor] == color[node]: return False return True
def bfs(start, dest): dist = {start: 0} Q = queue() Q.push(start) while not Q.empty(): n = Q.pop() if n == dest: return dist[n] for o in n.getNeighbors(): if o not in dist: dist[o] = dist[n] + 1 Q.push(o) assert False