test_decomposition.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import networkx as nx
  2. from networkx.algorithms.tree.decomposition import junction_tree
  3. def test_junction_tree_directed_confounders():
  4. B = nx.DiGraph()
  5. B.add_edges_from([("A", "C"), ("B", "C"), ("C", "D"), ("C", "E")])
  6. G = junction_tree(B)
  7. J = nx.Graph()
  8. J.add_edges_from(
  9. [
  10. (("C", "E"), ("C",)),
  11. (("C",), ("A", "B", "C")),
  12. (("A", "B", "C"), ("C",)),
  13. (("C",), ("C", "D")),
  14. ]
  15. )
  16. assert nx.is_isomorphic(G, J)
  17. def test_junction_tree_directed_unconnected_nodes():
  18. B = nx.DiGraph()
  19. B.add_nodes_from([("A", "B", "C", "D")])
  20. G = junction_tree(B)
  21. J = nx.Graph()
  22. J.add_nodes_from([("A", "B", "C", "D")])
  23. assert nx.is_isomorphic(G, J)
  24. def test_junction_tree_directed_cascade():
  25. B = nx.DiGraph()
  26. B.add_edges_from([("A", "B"), ("B", "C"), ("C", "D")])
  27. G = junction_tree(B)
  28. J = nx.Graph()
  29. J.add_edges_from(
  30. [
  31. (("A", "B"), ("B",)),
  32. (("B",), ("B", "C")),
  33. (("B", "C"), ("C",)),
  34. (("C",), ("C", "D")),
  35. ]
  36. )
  37. assert nx.is_isomorphic(G, J)
  38. def test_junction_tree_directed_unconnected_edges():
  39. B = nx.DiGraph()
  40. B.add_edges_from([("A", "B"), ("C", "D"), ("E", "F")])
  41. G = junction_tree(B)
  42. J = nx.Graph()
  43. J.add_nodes_from([("A", "B"), ("C", "D"), ("E", "F")])
  44. assert nx.is_isomorphic(G, J)
  45. def test_junction_tree_undirected():
  46. B = nx.Graph()
  47. B.add_edges_from([("A", "C"), ("A", "D"), ("B", "C"), ("C", "E")])
  48. G = junction_tree(B)
  49. J = nx.Graph()
  50. J.add_edges_from(
  51. [
  52. (("A", "D"), ("A",)),
  53. (("A",), ("A", "C")),
  54. (("A", "C"), ("C",)),
  55. (("C",), ("B", "C")),
  56. (("B", "C"), ("C",)),
  57. (("C",), ("C", "E")),
  58. ]
  59. )
  60. assert nx.is_isomorphic(G, J)