def test_topological_sort(self): # cp3 4.4 dag in https://visualgo.net/en/dfsbfs dg = Digraph() dg.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (2, 5), (3, 4), (7, 6)]) self.assertEqual([7, 6, 0, 1, 2, 5, 3, 4], topological_sort(dg)) # cp3 4.17 dag in https://visualgo.net/en/dfsbfs dg = Digraph() dg.add_edges_from([(0, 1), (0, 2), (0, 3), (1, 3), (1, 4), (2, 4), (3, 4)]) self.assertEqual([0, 2, 1, 3, 4], topological_sort(dg)) # cp3 4.18 dag bipartite in https://visualgo.net/en/dfsbfs dg = Digraph() dg.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4)]) self.assertEqual([0, 2, 1, 3, 4], topological_sort(dg))
if v == 0] # vertices with in-degree = 0 for u in list0: for v in g.neighbors(u): in_degrees[v] -= 1 if in_degrees[v] == 0: list0.append(v) return len(list0) == len(g.vertices) if __name__ == '__main__': dress_order = [['shirt', 'tie'], ['tie', 'jacket'], ['belt', 'jacket'], ['shirt', 'belt'], ['undershorts', 'pants'], ['pants', 'shoes'], ['socks', 'shoes']] g = Digraph() g.add_edges_from(dress_order) g.add_vertex('jacket') g.add_vertex('watch') g.add_vertex('shoes') print(g) print(is_directed_acyclic_graph(g)) print(is_directed_acyclic_dfs(g)) # print(is_directed_acyclic_bfs(g)) # g1 = Digraph() # g1.add_vertex(1) # g1.add_vertex(2) # print(is_directed_acyclic_bfs(g1)) # g2 = Digraph() # g2.add_edge(1, 2)
g = Graph() g.add_edges_from([(1, 2), (1, 5), (2, 3), (2, 5), (3, 6), (4, 5)]) print(f'connected: {is_connected(g)}') # print(connected_components_dj(g)) print(connected_components(g)) assert connected_components(g) == [[1, 2, 3, 6, 5, 4]] dg = Digraph() dg.add_edges_from([ ('a', 'b'), ('b', 'c'), ('b', 'e'), ('c', 'd'), ('c', 'g'), ('d', 'c'), ('d', 'h'), ('e', 'a'), ('e', 'f'), ('f', 'g'), ('g', 'f'), ('g', 'h'), ('h', 'h'), ]) print(strongly_connected_components(dg)) assert strongly_connected_components(dg) == [['a', 'e', 'b'], ['c', 'd'], ['g', 'f'], ['h']] dg = Digraph() dg.add_edges_from([(0, 1), (1, 3), (2, 1), (3, 2), (3, 4), (4, 5), (5, 7), (6, 4), (7, 6)]) print(strongly_connected_components(dg))
from Graph import Digraph def topological_sort(g): def dfs(g, u): seen.add(u) for v in g.neighbors(u): if v not in seen: dfs(g, v) sortedv.appendleft(u) seen = set() sortedv = deque() for u in g.vertices: if u not in seen: dfs(g, u) return list(sortedv) if __name__ == '__main__': dress_order = [['shirt', 'tie'], ['tie', 'jacket'], ['belt', 'jacket'], ['shirt', 'belt'], ['undershorts', 'pants'], ['pants', 'shoes'], ['socks', 'shoes']] g = Digraph() g.add_edges_from(dress_order) g.add_vertex('jacket') g.add_vertex('watch') g.add_vertex('shoes') print(g) print(topological_sort(g))
for v in g.neighbors(u): queue.append((v, path + [v])) return paths # def all_simple_paths_bfs(g, s, t): # seen = set() # paths = [] # queue = deque([(s, s)]) # while queue: # u, path = queue.popleft() # if u == t: # paths.append(path) # else: # for v in g.neighbors(u): # if v not in seen: # queue.append((v, path + v)) # seen.add(v) # return paths if __name__ == '__main__': g = Digraph() # clrs Figure 22.8 g.add_edges_from([ ('m', 'q'), ('m', 'r'), ('m', 'x'), ('n', 'o'), ('n', 'q'), ('n', 'u'), ('o', 'r'), ('o', 's'), ('o', 'v'), ('p', 'o'), ('p', 's'), ('p', 'z'), ('q', 't'), ('r', 'u'), ('r', 'y'), ('s', 'r'), ('v', 'w'), ('v', 'x'), ('y', 'v') ]) print(all_simple_paths_dfs(g, 'p', 'v')) print(all_simple_paths_bfs(g, 'p', 'v'))
def dfs_tree(g, src): def dfs(g, u): for v in g.neighbors(u): if v not in seen: tree.add_edge(u, v) seen.add(v) dfs(g, v) tree = Graph() tree.add_vertex(src) seen = {src} dfs(g, src) return tree if __name__ == '__main__': # dg = Digraph() # clrs book example # dg.add_edges_from([('u', 'v'), ('u', 'x'), ('v', 'y'), ('w', 'y'), # ('w', 'z'), ('x', 'v'), ('y', 'x'), ('z', 'z')]) # t = dfs_tree(dg, 'u') # print(t.vertices) # print(t.edges) dg = Digraph() # dg.add_edges_from([(0, 1), (1, 0), (2, 1)]) dg.add_edges_from([(1, 0), (1, 4), (4, 0), (4, 2)]) t = dfs_tree(dg, 0) print(t.vertices) print(t.edges)
# The transpose of a directed graph G=(V, E) is the graph Gᵀ = (V, Eᵀ) , where # Eᵀ = {(v,u) ∈ V x V : (u,v) ∈ E}. Thus, GT is G with all its edges reversed. from Graph import Digraph def transpose(g): gt = Digraph() for u in g.vertices: gt.add_vertex(u) for u, v in g.edges: gt.add_edge(v, u, g.weight(u, v)) return gt if __name__ == '__main__': g = Digraph() g.add_edges_from([ ('a', 'b'), ('b', 'c'), ('b', 'e'), ('b', 'f'), ('c', 'd'), ('c', 'g'), ('d', 'c'), ('d', 'h'), ('e', 'a'), ('e', 'f'), ('f', 'g'), ('g', 'f'), ('g', 'h'), ('h', 'h') ]) gt = transpose(g) print(gt.edges)