~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/algorithms/bipartite/__init__.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
6
The bipartite algorithms are not imported into the networkx namespace
 
7
at the top level so the easiest way to use them is with:
 
8
 
 
9
>>> import networkx as nx
 
10
>>> from networkx.algorithms import bipartite
 
11
 
 
12
NetworkX does not have a custom bipartite graph class but the Graph()
 
13
or DiGraph() classes can be used to represent bipartite graphs. However,
 
14
you have to keep track of which set each node belongs to, and make
 
15
sure that there is no edge between nodes of the same set. The convention used
 
16
in NetworkX is to use a node attribute named "bipartite" with values 0 or 1 to
 
17
identify the sets each node belongs to.
 
18
 
 
19
For example:
 
20
 
 
21
>>> B = nx.Graph()
 
22
>>> B.add_nodes_from([1,2,3,4], bipartite=0) # Add the node attribute "bipartite"
 
23
>>> B.add_nodes_from(['a','b','c'], bipartite=1)
 
24
>>> B.add_edges_from([(1,'a'), (1,'b'), (2,'b'), (2,'c'), (3,'c'), (4,'a')])
 
25
 
 
26
Many algorithms of the bipartite module of NetworkX require, as an argument, a
 
27
container with all the nodes that belong to one set, in addition to the bipartite
 
28
graph `B`. If `B` is connected, you can find the node sets using a two-coloring 
 
29
algorithm: 
 
30
 
 
31
>>> nx.is_connected(B)
 
32
True
 
33
>>> bottom_nodes, top_nodes = bipartite.sets(B)
 
34
>>> list(top_nodes)
 
35
[1, 2, 3, 4]
 
36
>>> list(bottom_nodes)
 
37
['a', 'c', 'b']
 
38
 
 
39
However, if the input graph is not connected, there are more than one possible
 
40
colorations. Thus, the following result is correct:
 
41
 
 
42
>>> B.remove_edge(2,'c')
 
43
>>> nx.is_connected(B)
 
44
False
 
45
>>> bottom_nodes, top_nodes = bipartite.sets(B)
 
46
>>> list(top_nodes)
 
47
[1, 2, 4, 'c']
 
48
>>> list(bottom_nodes)
 
49
['a', 3, 'b']
 
50
 
 
51
Using the "bipartite" node attribute, you can easily get the two node sets:
 
52
 
 
53
>>> top_nodes = set(n for n,d in B.nodes(data=True) if d['bipartite']==0)
 
54
>>> bottom_nodes = set(B) - top_nodes
 
55
>>> list(top_nodes)
 
56
[1, 2, 3, 4]
 
57
>>> list(bottom_nodes)
 
58
['a', 'c', 'b']
 
59
 
 
60
So you can easily use the bipartite algorithms that require, as an argument, a
 
61
container with all nodes that belong to one node set:
 
62
 
 
63
>>> print(round(bipartite.density(B, bottom_nodes),2))
 
64
0.42
 
65
>>> G = bipartite.projected_graph(B, top_nodes)
 
66
>>> G.edges()
 
67
[(1, 2), (1, 4)]
 
68
 
 
69
All bipartite graph generators in NetworkX build bipartite graphs with the 
 
70
"bipartite" node attribute. Thus, you can use the same approach:
 
71
 
 
72
>>> RB = nx.bipartite_random_graph(5, 7, 0.2)
 
73
>>> nx.is_connected(RB)
 
74
False
 
75
>>> RB_top = set(n for n,d in RB.nodes(data=True) if d['bipartite']==0)
 
76
>>> RB_bottom = set(RB) - RB_top
 
77
>>> list(RB_top)
 
78
[0, 1, 2, 3, 4]
 
79
>>> list(RB_bottom)
 
80
[5, 6, 7, 8, 9, 10, 11]
 
81
 
 
82
For other bipartite graph generators see the bipartite section of
 
83
:doc:`generators`.
 
84
 
 
85
"""
 
86
 
 
87
from networkx.algorithms.bipartite.basic import *
 
88
from networkx.algorithms.bipartite.centrality import *
 
89
from networkx.algorithms.bipartite.cluster import *
 
90
from networkx.algorithms.bipartite.projection import *
 
91
from networkx.algorithms.bipartite.redundancy import *
 
92
from networkx.algorithms.bipartite.spectral import *