def test_graph_from_edges(): edges = [(1, 2, 3), (4, 3)] G = graph_from_edges(*edges) assert set(G.nodes) == {1, 2, 3, 4} assert set(G.edges) == {(1, 2), (2, 3), (4, 3)} edges = [("1", "2", "3"), ("4", "3")] G = graph_from_edges(*edges) assert set(G.nodes) == {"1", "2", "3", "4"} assert set(G.edges) == {("1", "2"), ("2", "3"), ("4", "3")} edges = [("A", "B", "C")] G = graph_from_edges(*edges) assert set(G.nodes) == {"A", "B", "C"} assert set(G.edges) == {("A", "B"), ("B", "C")} edges = [("A", "B", "D"), ("A", "C", "D"), ("E", "C")] G = graph_from_edges(*edges) assert set(G.nodes) == {"A", "B", "C", "D", "E"} assert set(G.edges) == {("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"), ("E", "C")} edges = [("A", "B", "D"), ("A", "C", "D"), ("E", "C", "D")] G = graph_from_edges(*edges) assert set(G.nodes) == {"A", "B", "C", "D", "E"} assert set(G.edges) == {("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"), ("E", "C")}
def test_graph_from_edges_string(): edges_strings = [ "A - B - C ; D - C", "A - B ; B - C ; D - C", "A -> B -> C ; D -> C", "A-B;B-C;D-C", "A->B;B->C;D-C;", ] for edges_string in edges_strings: G = graph_from_edges_string(edges_string) assert set(G.nodes) == {"A", "B", "C", "D"} assert set(G.edges) == {("A", "B"), ("B", "C"), ("D", "C")} edges2 = edges_from_graph(G) G2 = graph_from_edges(*edges2) assert set(G2.nodes) == set(G.nodes) assert set(G2.edges) == set(G.edges) edges_strings = ["1 - 2 - 3 ; 4 - 3", "1 - 2 ; 2 - 3 ; 4 - 3"] for edges_string in edges_strings: G = graph_from_edges_string(edges_string) assert set(G.nodes) == {1, 2, 3, 4} assert set(G.edges) == {(1, 2), (2, 3), (4, 3)} edges2 = edges_from_graph(G) G2 = graph_from_edges(*edges2) assert set(G2.nodes) == set(G.nodes) assert set(G2.edges) == set(G.edges)
def test_iter_graph(): G1 = nx.DiGraph() G1 = add_node_after(G1, 1) G1 = add_node_after(G1, 2, 1) G1 = add_node_after(G1, 3, 2) G1 = add_node_after(G1, 4) G1 = add_node_after(G1, 5, 4) G1 = add_node_after(G1, 6, 5, 3) G2 = graph_from_edges((1, 2), (3, 4)) for G in (G1, G2): done = set() for n in iter_graph(G): for p in G.predecessors(n): assert p in done assert n not in done done.add(n) assert done == set(G.nodes) G3 = nx.DiGraph() G3.add_node(1) G3.add_node(2) # 2 un-connexted nodes assert list(iter_graph(G3)) in ([1, 2], [2, 1])
def test_subbranch_search(): Graph = graph_from_edges(("A", "B", "C"), ("B", "D")) nodes = list(subbranch_search("A", Graph)) assert nodes == ['A', 'B', 'C', 'D'] or nodes == ['A', 'B', 'D', 'C'] nodes = list(subbranch_search("B", Graph)) assert nodes == [] nodes = list(subbranch_search("B", Graph, visited={"A"})) assert nodes == ['B', 'C', 'D'] or nodes == ['B', 'D', 'C'] nodes = list(subbranch_search("C", Graph, visited={"B"})) assert nodes == ["C"] nodes = list(subbranch_search("D", Graph, visited={"B"})) assert nodes == ["D"] # Graph2 Graph = graph_from_edges(("A", "B", "C", "E"), ("B", "D", "E")) nodes = list(subbranch_search("A", Graph)) assert nodes == ['A', 'B', 'C', 'D', 'E' ] or nodes == ['A', 'B', 'D', 'C', 'E'] Graph = graph_from_edges(("A", "B", "C", "E"), ("B", "D", "E")) nodes = list(subbranch_search("B", Graph, visited={"A"})) assert nodes == ['B', 'C', 'D', 'E'] or nodes == ['B', 'D', 'C', 'E'] Graph = graph_from_edges(("A", "B", "C", "E"), ("B", "D", "E")) nodes = list(subbranch_search("C", Graph, visited={"B"})) assert nodes == ['C'] Graph = graph_from_edges(("A", "B", "C"), ("A", "C")) nodes = list(subbranch_search("A", Graph)) assert nodes == ["A", "B", "C"] nodes = list(subbranch_search("B", Graph, visited={"A"})) assert nodes == ['B', 'C'] nodes = list(subbranch_search("C", Graph, visited={"B"})) assert nodes == []
def test_merge_nodes(): Graph = graph_from_edges(("A", "B", "C"), ("B", "D")) mGraph = merge_nodes(Graph, {"B": "A"}) assert set(mGraph.nodes) == {"A", "C", "D"} assert set(mGraph.edges) == {("A", "C"), ("A", "D")} # check Graph wasn't modified assert set(Graph.nodes) == {"A", "B", "C", "D"} mGraph = merge_nodes(Graph, {"C": "B", "D": "B"}) assert set(mGraph.nodes) == {"A", "B"} assert set(mGraph.edges) == {('A', 'B')}
def test_edges_from_graph(): G = nx.DiGraph() G.add_node("A") G.add_node("B") assert edges_from_graph(G) == [("A", ), ("B", )] G = nx.DiGraph() G.add_node("B") G.add_node("A") assert edges_from_graph(G) == [("A", ), ("B", )] G = graph_from_edges(("A", "B"), ("C", "D")) assert set(G.nodes) == {'A', 'B', 'C', 'D'} assert set(G.edges) == {('A', 'B'), ('C', 'D')} assert edges_from_graph(G) == [('A', 'B'), ('C', 'D')]
def test_get_two_by_two_edges(): assert get_two_by_two_edges(("a", "c"), ("b", "c")) == [('a', 'c'), ('b', 'c')] assert get_two_by_two_edges(("b", "c"), ("a", "c")) == [('b', 'c'), ('a', 'c')] assert get_two_by_two_edges(("a", "c", "d"), ("b", "c")) == [('a', 'c'), ('c', 'd'), ('b', 'c')] assert get_two_by_two_edges(("a", "c", "d"), ("b", "c")) == [('a', 'c'), ('c', 'd'), ('b', 'c')] edges = [("A", ), ("B", "C", "D")] G = graph_from_edges(*edges) assert set(G.nodes) == {"A", "B", "C", "D"} assert set(G.edges) == {("B", "C"), ("C", "D")}
def test_edges_from_graph(): G = nx.DiGraph() G.add_node("A") G.add_node("B") assert edges_from_graph(G) == [("A", ), ("B", )] G = nx.DiGraph() G.add_node("B") G.add_node("A") assert edges_from_graph(G) == [("A", ), ("B", )] G = graph_from_edges(("A", "B"), ("C", "D")) assert set(G.nodes) == {"A", "B", "C", "D"} assert set(G.edges) == {("A", "B"), ("C", "D")} assert edges_from_graph(G) == [("A", "B"), ("C", "D")]
def create_graph(self): """ create the graphical structure """ self.complete_graph = graph_from_edges(*self._edges) self._verif_graph_structure() self._terminal_node = get_terminal_nodes(self.complete_graph)[0] self._nodes_order = list(iter_graph(self.complete_graph))