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(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 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 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(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
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 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 snake_ladder_dijkstra(shortcuts): """ Assuming the snake and ladder board is of size 30. Create a graph such that every number 'num' has an outward edge going to num+1, num+2, ... num+6 of cost 1. Also, create a 0-cost edge for every shortcut. Then apply dijkstra directly. Total number of edges = 6*30 + num_shortcuts Note: We do not need to apply Dijkstra at all. See the BFS solution!! """ graph = GraphCore(False) for src in range(1, 31): for i in range(1, 7): dst = src + i if dst <= 30: graph.add_edge(src, dst, 1) for src, dst in shortcuts.items(): if dst > src: graph.add_edge(src, dst, 0) return dijkstra(graph, 1)[30]
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 simplify_debt_graph(debt_graph: GraphCore): credit_flow = {} for vert in debt_graph.vertices: credit_flow[vert] = 0 for src, edges in debt_graph.get_all_edges().items(): for neigh, edge in edges.items(): wt = edge["weight"] credit_flow[src] -= wt credit_flow[neigh] += wt debtors = [] creditors = [] for vert, flow in credit_flow.items(): if flow < 0: debtors.append([vert, -flow]) else: creditors.append([vert, flow]) assert sum(map(lambda item: item[1], debtors)) == sum(map(lambda item: item[1], creditors)) simplified_graph = GraphCore() for vert in debt_graph.vertices: simplified_graph.add_vertex(vert) debtors_idx = 0 creditors_idx = 0 while debtors_idx < len(debtors) and creditors_idx < len(creditors): debtor = debtors[debtors_idx] creditor = creditors[creditors_idx] min_flow = min(debtor[1], creditor[1]) simplified_graph.add_edge(debtor[0], creditor[0], min_flow) debtor[1] -= min_flow creditor[1] -= min_flow if debtor[1] == 0: debtors_idx += 1 if creditor[1] == 0: creditors_idx += 1 assert debtors_idx == len(debtors) assert creditors_idx == len(creditors) return simplified_graph
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(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(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() 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() 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 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)