def test_directed_connected_components(self): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h", "i"]) digr.add_edges([("b", "a"), ("a", "c"), ("c", "b"), ("d", "b")]) digr.add_edges([("d", "f"), ("f", "e"), ("e", "d"), ("g", "e")]) digr.add_edges([("g", "h"), ("h", "i"), ("i", "g")]) self.assertEqual(len(directed_connected_components(digr)), 3) digr2 = digraph() digr2.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]) digr2.add_edges([("a", "b"), ("b", "c"), ("c", "a"), ("b", "d"), ("d", "e")]) digr2.add_edges([("e", "f"), ("f", "g"), ("g", "e"), ("d", "g"), ("i", "f")]) digr2.add_edges([("h", "g"), ("c", "h"), ("c", "k"), ("h", "i"), ("i", "j")]) digr2.add_edges([("h", "j"), ("j", "k"), ("k", "h")]) self.assertEqual(len(directed_connected_components(digr2)), 4)
def test_directed_connected_components(): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h", "i"]) digr.add_edges([("b", "a"), ("a", "c"), ("c", "b"), ("d", "b")]) digr.add_edges([("d", "f"), ("f", "e"), ("e", "d"), ("g", "e")]) digr.add_edges([("g", "h"), ("h", "i"), ("i", "g")]) assert len(directed_connected_components(digr)) == 3 digr2 = digraph() digr2.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]) digr2.add_edges([("a", "b"), ("b", "c"), ("c", "a"), ("b", "d"), ("d", "e")]) digr2.add_edges([("e", "f"), ("f", "g"), ("g", "e"), ("d", "g"), ("i", "f")]) digr2.add_edges([("h", "g"), ("c", "h"), ("c", "k"), ("h", "i"), ("i", "j")]) digr2.add_edges([("h", "j"), ("j", "k"), ("k", "h")]) assert len(directed_connected_components(digr2)) == 4
def setup_digraph(): digr = digraph() digr.add_nodes(['s', 'a', 'b', 'c', 'd', 'e', 'f']) digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")]) digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")]) digr.add_edges([("b", "f"), ("e", "f")]) return digr
def test_topological_ordering(setup_graph): gr = setup_graph dag = digraph() # directed acyclic graph dag.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h"]) dag.add_edges([("a", "b"), ("a", "c"), ("a", "e"), ("d", "a")]) dag.add_edges([("g", "b"), ("g", "f"), ("f", "e"), ("h", "f"), ("h", "a")]) order = {o[0]: o[1] for o in topological_ordering(dag)} assert sum([order[u] < order[v] for (u, v) in dag.edges()]) == len(dag.edges()) # all comparisons are True
def test_topological_ordering(self): dag = digraph() # directed acyclic graph dag.add_nodes(["a", "b", "c", "d", "e", "f", "g", "h"]) dag.add_edges([("a", "b"), ("a", "c"), ("a", "e"), ("d", "a")]) dag.add_edges([("g", "b"), ("g", "f"), ("f", "e"), ("h", "f"), ("h", "a")]) order = {o[0]: o[1] for o in topological_ordering(dag)} self.assertEqual(sum([order[u] < order[v] for (u, v) in dag.edges()]), len(dag.edges())) # all comparisons are True
def setUp(self): self.gr = graph() self.gr.add_nodes(["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"]) self.gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")]) self.gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")]) self.gr.add_edges([("g", "h"), ("f", "g")]) self.gr.add_edges([("j", "k"), ("j", "l")]) self.digr = digraph() self.digr.add_nodes(["s", "a", "b", "c", "d", "e", "f"]) self.digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")]) self.digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")]) self.digr.add_edges([("b", "f"), ("e", "f")])
def setUp(self): self.gr = graph() self.gr.add_nodes(["s", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l"]) self.gr.add_edges([("s", "a"), ("s", "b"), ("a", "c"), ("c", "e")]) self.gr.add_edges([("e", "d"), ("d", "b"), ("a", "b"), ("c", "d")]) self.gr.add_edges([("g", "h"), ("f", "g")]) self.gr.add_edges([("j", "k"), ("j", "l")]) self.digr = digraph() self.digr.add_nodes(['s', 'a', 'b', 'c', 'd', 'e', 'f']) self.digr.add_edges([("s", "a"), ("a", "b"), ("b", "a"), ("c", "b")]) self.digr.add_edges([("b", "s"), ("s", "d"), ("d", "e"), ("e", "d")]) self.digr.add_edges([("b", "f"), ("e", "f")])
def test_shortest_path_in_directed_graph(): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f"]) digr.add_edge(("a", "b"), 7) digr.add_edge(("a", "c"), 9) digr.add_edge(("a", "f"), 14) digr.add_edge(("f", "e"), 9) digr.add_edge(("c", "f"), 2) digr.add_edge(("c", "d"), 11) digr.add_edge(("b", "c"), 10) digr.add_edge(("b", "d"), 15) digr.add_edge(("d", "e"), 6) assert shortest_path(digr, "a")["a"] == 0 assert shortest_path(digr, "a")["b"] == 7 assert shortest_path(digr, "a")["c"] == 9 assert shortest_path(digr, "a")["d"] == 20 assert shortest_path(digr, "a")["e"] == 20 assert shortest_path(digr, "a")["f"] == 11
def test_shortest_path_in_directed_graph(self): digr = digraph() digr.add_nodes(["a", "b", "c", "d", "e", "f"]) digr.add_edge(("a", "b"), 7) digr.add_edge(("a", "c"), 9) digr.add_edge(("a", "f"), 14) digr.add_edge(("f", "e"), 9) digr.add_edge(("c", "f"), 2) digr.add_edge(("c", "d"), 11) digr.add_edge(("b", "c"), 10) digr.add_edge(("b", "d"), 15) digr.add_edge(("d", "e"), 6) self.assertEqual(shortest_path(digr, "a")["a"], 0) self.assertEqual(shortest_path(digr, "a")["b"], 7) self.assertEqual(shortest_path(digr, "a")["c"], 9) self.assertEqual(shortest_path(digr, "a")["d"], 20) self.assertEqual(shortest_path(digr, "a")["e"], 20) self.assertEqual(shortest_path(digr, "a")["f"], 11)
def setUp(self): self.gr = digraph() self.gr.add_nodes(["a", "b", "c", "d", "e", "f"]) self.gr.add_edges([("a","b"), ("b", "c"), ("a", "d"), ("d", "e"), ("d", "f")]) self.gr.add_edge(("f", "b"))
def setup_graph(): gr = digraph() gr.add_nodes(["a", "b", "c", "d", "e", "f"]) gr.add_edges([("a","b"), ("b", "c"), ("a", "d"), ("d", "e"), ("d", "f")]) gr.add_edge(("f", "b")) return gr