def prim2(graph: GraphCore): pq = Heap() visited = set() src = next(iter(graph.vertices)) visited.add(src) edges = set() for edge in graph.get_edges(src).items(): dst, edge_props = edge weight = edge_props['weight'] pq.push((weight, src, dst)) while len(visited) != len(graph.vertices) and pq.size() > 0: top = pq.pop() while pq.size() > 0: weight, src, dst = top if dst not in visited: break top = pq.pop() visited.add(dst) edges.add(top) if len(visited) == len(graph.vertices): break src = dst for edge in graph.get_edges(src).items(): dst, edge_props = edge weight = edge_props['weight'] if dst not in visited: pq.push((weight, src, dst)) return edges
def get_cycle_nodes(graph: GraphCore): ''' 1. Append all the vertices whose degree is 1 to a queue. 2. Pop the queue front and mark it visited. 3. Then decrement neighbours degree by 1. If it becomes 1, add neigh to queue. 4. Repeat 2-3. 5. Whichever nodes are not visited, they are in the cycle. ''' degrees = {} que = deque() for src in graph.vertices: edges = graph.get_edges(src) degrees[src] = len(edges.keys()) if degrees[src] == 1: que.append(src) visited = set() while que: front = que.popleft() assert (front not in visited) visited.add(front) neighbours = graph.get_edges(front).keys() for neigh in neighbours: degrees[neigh] -= 1 if degrees[neigh] == 1: que.append(neigh) return graph.vertices - visited
def main(): graph = GraphCore() graph.add_edge(0, 1, 4) graph.add_edge(0, 2, 1) graph.add_edge(1, 2, 2) graph.add_edge(1, 4, 3) graph.add_edge(2, 1, 2) graph.add_edge(2, 3, 2) graph.add_edge(3, 4, 3) distances = dijkstra(graph, 0) print(distances)
def compare_words(word1, word2, graph: GraphCore): idx1 = 0 idx2 = 0 while idx1 < len(word1) and idx2 < len(word2): char1 = word1[idx1] char2 = word2[idx2] if char1 == char2: idx1 += 1 idx2 += 1 continue graph.add_edge(char1, char2) break
def main(): graph = GraphCore(True) graph.add_edge(1, 2) graph.add_edge(1, 3) graph.add_edge(2, 3) graph.add_edge(2, 5) graph.add_edge(3, 2) graph.add_edge(3, 4) graph.add_edge(4, 5) print(graph) state = get_dfs_edges(graph) print(state)
def main(): graph = GraphCore(False) graph.add_edge(1, 2) graph.add_edge(1, 3) graph.add_edge(2, 3) graph.add_edge(2, 5) graph.add_edge(3, 2) graph.add_edge(3, 4) graph.add_edge(4, 5) print(graph) path = dfs_iterative(graph, 1) print(path)
def main(): graph = GraphCore() graph.add_edge(0, 1, 4) graph.add_edge(0, 2, 1) graph.add_edge(1, 2, 2) graph.add_edge(1, 4, 3) graph.add_edge(2, 1, 2) graph.add_edge(2, 3, 2) graph.add_edge(3, 4, 3) distances, paths = floyd_warshall(graph) print(distances) print(paths)
def main(): graph = GraphCore(False) graph.add_edge(1, 2) graph.add_edge(1, 3) graph.add_edge(2, 3) graph.add_edge(2, 5) graph.add_edge(3, 2) graph.add_edge(3, 4) graph.add_edge(4, 5) print(graph) visited = set() dfs(graph, 1, 4, visited) print(visited)
def main(): graph = GraphCore(False) graph.add_edge(1, 2) graph.add_edge(2, 3) graph.add_edge(3, 1) graph.add_edge(5, 6) graph.add_edge(6, 5) graph.add_edge(1, 5) graph.add_edge(7, 6) print(graph) parent_mapping = get_strongly_connected_components(graph) print(parent_mapping) cond_graph = get_condensation_graph(graph, parent_mapping) print(cond_graph)
def get_connected_components(graph: GraphCore): disjoint_set = DisjointSet() for src in graph.vertices: disjoint_set._add_node_if_absent(src) for src, edges in graph.get_all_edges().items(): for dst in edges.keys(): disjoint_set.join(src, dst) return disjoint_set.get_groups()
def main(): graph = GraphCore(False) graph.add_edge(1, 2) graph.add_edge(2, 1) graph.add_edge(2, 3) graph.add_edge(3, 4) graph.add_edge(4, 2) print(graph) parent_mapping = get_strongly_connected_components(graph) print(parent_mapping)
def main(): graph = GraphCore(True) graph.add_edge(1, 2) graph.add_edge(2, 3) graph.add_edge(3, 4) graph.add_edge(4, 3) graph.add_edge(4, 2) print(graph) ans = get_bridges_cut_points(graph) print(ans)
def main(): graph = GraphCore(True) graph.add_edge(1, 2) graph.add_edge(1, 3) graph.add_edge(2, 4) graph.add_edge(3, 4) graph.add_edge(4, 5) print(graph) is_it_really = is_bipartite(graph) print(is_it_really)
def main(): graph = GraphCore() graph.add_edge(2, 3) graph.add_edge(3, 1) graph.add_edge(4, 0) graph.add_edge(4, 1) graph.add_edge(5, 0) graph.add_edge(5, 2) topo_order = topo_sort(graph) print(topo_order)
def main(): graph = GraphCore(True) graph.add_edge(1, 2) graph.add_edge(1, 3) graph.add_edge(2, 4) graph.add_edge(3, 4) graph.add_edge(4, 5) print(graph) dist = bfs(graph, 3, 1) print(dist)
def main(): graph = GraphCore() graph.add_edge(2, 3, 1) graph.add_edge(3, 1, 1) graph.add_edge(4, 0, 1) graph.add_edge(4, 1, 1) graph.add_edge(5, 0, 1) graph.add_edge(5, 2, 1) longest_path = longest_path_dag_topo(graph, 2) print(graph) print(longest_path)
def main(): graph = GraphCore(True) graph.add_edge(1, 2) graph.add_edge(1, 3) graph.add_edge(1, 4) graph.add_edge(5, 2) graph.add_edge(5, 3) graph.add_edge(5, 4) print(graph) dist = bfs_bidirectional(graph, 1, 5) print(dist)
def main(): graph = GraphCore() graph.add_edge(2, 3, 1) graph.add_edge(3, 1, 1) graph.add_edge(4, 0, 1) graph.add_edge(4, 1, 1) graph.add_edge(5, 0, 1) graph.add_edge(5, 2, 1) distances = longest_path_dag_negate(graph, 2) print(graph) print(distances)
def compute_longest_path_starting_at(graph: GraphCore, src, longest_distances): if src in longest_distances: return longest_distances[src] longest_path_distance = 1 for neigh, edge_weight in graph.get_edges(src).items(): longest_path_distance = max( longest_path_distance, compute_longest_path_starting_at(graph, neigh, longest_distances) + edge_weight["weight"], ) longest_distances[src] = longest_path_distance return longest_path_distance
def get_strongly_connected_components(graph: GraphCore): visit_order = [] visited = set() for src in graph.vertices: if src not in visited: visit(src, graph, visited, visit_order) graph_reverse = graph.get_transpose() visit_order.reverse() parent_mapping = {} for src in visit_order: assign_component(src, graph_reverse, parent_mapping, src) return parent_mapping
def main(): graph1 = GraphCore(True) graph1.add_edge(1, 2, 5) graph1.add_edge(2, 3, 6) graph1.add_edge(1, 3, 2) mst = kruskal(graph1) print(mst)
def main(): debt_graph = GraphCore() debt_graph.add_edge(0, 1, 1000) debt_graph.add_edge(1, 2, 5000) debt_graph.add_edge(0, 2, 2000) simplified = simplify_debt_graph(debt_graph) print(simplified)
def main(): # Will give wrong result if the graph is not bi-directional !!! graph1 = GraphCore(True) graph1.add_edge(1, 2, 5) graph1.add_edge(2, 3, 6) graph1.add_edge(1, 3, 2) mst = prim(graph1) print(mst)
def main(): graph = GraphCore(False) graph.add_edge(0, 1) graph.add_edge(2, 3) graph.add_edge(3, 4) print(graph) ans = get_topo_sort(graph) print(ans)
def main(): graph = GraphCore(False) graph.add_edge(0, 1) graph.add_edge(2, 3) graph.add_edge(3, 4) graph.add_edge(4, 2) print(graph) ans = is_cyclic(graph) print(ans)
def main(): graph = GraphCore(True) graph.add_edge(1, 2) graph.add_edge(2, 3) graph.add_edge(3, 1) graph.add_edge(5, 6) print(graph) ans = get_connected_components(graph) print(ans)
def main(): graph = GraphCore(True) graph.add_edge(0, 1) graph.add_edge(2, 3) graph.add_edge(3, 4) graph.add_edge(4, 2) print(graph) ans = get_cycle_nodes(graph) print(ans)
def longest_path_dag_negate(graph: GraphCore, src): """ Computes the single-source longest path from a source vertex src. Works for negative edges also! First negate all the edges (5 becomes -5). Then find the shortest path from src to all nodes. Multipy the distances with -1 to reverse negation. """ for _, edges in graph.get_all_edges().items(): for __, edge in edges.items(): edge["weight"] = -edge["weight"] distances = shortest_path_dag_topo(graph, src) for node in distances: distances[node] = -distances[node] return distances
def main(): graph = GraphCore(False) num_vertices = 4 edges_input = [] edges_input.append((1, 2)) edges_input.append((3, 2)) edges_input.append((4, 3)) edges_input.append((2, 1)) for edge in edges_input: # Reverse edges. graph.add_edge(edge[1], edge[0]) for vert in range(num_vertices): graph.add_vertex(vert + 1) ans = get_first_component_nodes(graph) print(ans)
def get_condensation_graph(graph, parent_mapping): cond_graph = GraphCore(False) for src, edges in graph.get_all_edges().items(): parent_src = parent_mapping[src] for dst in edges: parent_dst = parent_mapping[dst] if parent_src == parent_dst: continue else: cond_graph.add_edge(parent_src, parent_dst) # For all the vertices which are disconnected from the rest of the graph. for src in graph.vertices: if parent_mapping[src] == src: cond_graph.add_vertex(src) return cond_graph