__init__.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. r""" This module provides functions and operations for bipartite
  2. graphs. Bipartite graphs `B = (U, V, E)` have two node sets `U,V` and edges in
  3. `E` that only connect nodes from opposite sets. It is common in the literature
  4. to use an spatial analogy referring to the two node sets as top and bottom nodes.
  5. The bipartite algorithms are not imported into the networkx namespace
  6. at the top level so the easiest way to use them is with:
  7. >>> from networkx.algorithms import bipartite
  8. NetworkX does not have a custom bipartite graph class but the Graph()
  9. or DiGraph() classes can be used to represent bipartite graphs. However,
  10. you have to keep track of which set each node belongs to, and make
  11. sure that there is no edge between nodes of the same set. The convention used
  12. in NetworkX is to use a node attribute named `bipartite` with values 0 or 1 to
  13. identify the sets each node belongs to. This convention is not enforced in
  14. the source code of bipartite functions, it's only a recommendation.
  15. For example:
  16. >>> B = nx.Graph()
  17. >>> # Add nodes with the node attribute "bipartite"
  18. >>> B.add_nodes_from([1, 2, 3, 4], bipartite=0)
  19. >>> B.add_nodes_from(["a", "b", "c"], bipartite=1)
  20. >>> # Add edges only between nodes of opposite node sets
  21. >>> B.add_edges_from([(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c"), (4, "a")])
  22. Many algorithms of the bipartite module of NetworkX require, as an argument, a
  23. container with all the nodes that belong to one set, in addition to the bipartite
  24. graph `B`. The functions in the bipartite package do not check that the node set
  25. is actually correct nor that the input graph is actually bipartite.
  26. If `B` is connected, you can find the two node sets using a two-coloring
  27. algorithm:
  28. >>> nx.is_connected(B)
  29. True
  30. >>> bottom_nodes, top_nodes = bipartite.sets(B)
  31. However, if the input graph is not connected, there are more than one possible
  32. colorations. This is the reason why we require the user to pass a container
  33. with all nodes of one bipartite node set as an argument to most bipartite
  34. functions. In the face of ambiguity, we refuse the temptation to guess and
  35. raise an :exc:`AmbiguousSolution <networkx.AmbiguousSolution>`
  36. Exception if the input graph for
  37. :func:`bipartite.sets <networkx.algorithms.bipartite.basic.sets>`
  38. is disconnected.
  39. Using the `bipartite` node attribute, you can easily get the two node sets:
  40. >>> top_nodes = {n for n, d in B.nodes(data=True) if d["bipartite"] == 0}
  41. >>> bottom_nodes = set(B) - top_nodes
  42. So you can easily use the bipartite algorithms that require, as an argument, a
  43. container with all nodes that belong to one node set:
  44. >>> print(round(bipartite.density(B, bottom_nodes), 2))
  45. 0.5
  46. >>> G = bipartite.projected_graph(B, top_nodes)
  47. All bipartite graph generators in NetworkX build bipartite graphs with the
  48. `bipartite` node attribute. Thus, you can use the same approach:
  49. >>> RB = bipartite.random_graph(5, 7, 0.2)
  50. >>> RB_top = {n for n, d in RB.nodes(data=True) if d["bipartite"] == 0}
  51. >>> RB_bottom = set(RB) - RB_top
  52. >>> list(RB_top)
  53. [0, 1, 2, 3, 4]
  54. >>> list(RB_bottom)
  55. [5, 6, 7, 8, 9, 10, 11]
  56. For other bipartite graph generators see
  57. :mod:`Generators <networkx.algorithms.bipartite.generators>`.
  58. """
  59. from networkx.algorithms.bipartite.basic import *
  60. from networkx.algorithms.bipartite.centrality import *
  61. from networkx.algorithms.bipartite.cluster import *
  62. from networkx.algorithms.bipartite.covering import *
  63. from networkx.algorithms.bipartite.edgelist import *
  64. from networkx.algorithms.bipartite.matching import *
  65. from networkx.algorithms.bipartite.matrix import *
  66. from networkx.algorithms.bipartite.projection import *
  67. from networkx.algorithms.bipartite.redundancy import *
  68. from networkx.algorithms.bipartite.spectral import *
  69. from networkx.algorithms.bipartite.generators import *