📚 The CoCalc Library - books, templates and other resources
License: OTHER
Kernel: Python 3
Bipartite graphs
Code examples from Think Complexity, 2nd edition.
Copyright 2019 Allen Downey, MIT License
In [1]:
The following examples are from the NetworkX documentation on bipartite graphs
In [2]:
In [3]:
Out[3]:
({1, 2, 3, 4}, {'a', 'b', 'c'})
In [4]:
Out[4]:
True
In [5]:
Out[5]:
False
Exercise: Write a generator function called cross_edges
that takes a NetworkX Graph
object, G
, and a Python set
object, top
, that contains nodes.
It should compute another set
called bottom
that contains all nodes in G
that are not in top
.
Then it should yield all edges in G
that connect a node in top
to a node in bottom
.
In [6]:
In [7]:
In [8]:
In [9]:
Out[9]:
True
In [10]:
In [11]:
Out[11]:
(1, 'a')
('a', 3)
(2, 'c')
('c', 3)
('c', 4)
(4, 'b')
In [12]:
Out[12]:
6
In [13]:
Out[13]:
6
In [14]:
Out[14]:
6
In [15]:
Out[15]:
6
Exercise: Write a function called is_bipartite
that takes a Graph
and a set of top nodes, and checks whether a graph is bipartite.
In [16]:
In [17]:
Out[17]:
True
In [18]:
Out[18]:
False
In [19]:
In [20]:
Out[20]:
True
In [21]:
Out[21]:
False
In [ ]: