view env/lib/python3.9/site-packages/networkx/algorithms/bipartite/tests/test_basic.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
line wrap: on
line source
```
import pytest

import networkx as nx
from networkx.algorithms import bipartite

class TestBipartiteBasic:
def test_is_bipartite(self):
assert bipartite.is_bipartite(nx.path_graph(4))
assert bipartite.is_bipartite(nx.DiGraph([(1, 0)]))
assert not bipartite.is_bipartite(nx.complete_graph(3))

def test_bipartite_color(self):
G = nx.path_graph(4)
c = bipartite.color(G)
assert c == {0: 1, 1: 0, 2: 1, 3: 0}

def test_not_bipartite_color(self):
with pytest.raises(nx.NetworkXError):
c = bipartite.color(nx.complete_graph(4))

def test_bipartite_directed(self):
G = bipartite.random_graph(10, 10, 0.1, directed=True)
assert bipartite.is_bipartite(G)

def test_bipartite_sets(self):
G = nx.path_graph(4)
X, Y = bipartite.sets(G)
assert X == {0, 2}
assert Y == {1, 3}

def test_bipartite_sets_directed(self):
G = nx.path_graph(4)
D = G.to_directed()
X, Y = bipartite.sets(D)
assert X == {0, 2}
assert Y == {1, 3}

def test_bipartite_sets_given_top_nodes(self):
G = nx.path_graph(4)
top_nodes = [0, 2]
X, Y = bipartite.sets(G, top_nodes)
assert X == {0, 2}
assert Y == {1, 3}

def test_bipartite_sets_disconnected(self):
with pytest.raises(nx.AmbiguousSolution):
G = nx.path_graph(4)
X, Y = bipartite.sets(G)

def test_is_bipartite_node_set(self):
G = nx.path_graph(4)
assert bipartite.is_bipartite_node_set(G, [0, 2])
assert bipartite.is_bipartite_node_set(G, [1, 3])
assert not bipartite.is_bipartite_node_set(G, [1, 2])
assert bipartite.is_bipartite_node_set(G, [0, 2, 10])
assert bipartite.is_bipartite_node_set(G, [0, 2, 20])
assert bipartite.is_bipartite_node_set(G, [1, 3, 10])
assert bipartite.is_bipartite_node_set(G, [1, 3, 20])

def test_bipartite_density(self):
G = nx.path_graph(5)
X, Y = bipartite.sets(G)
density = float(len(list(G.edges()))) / (len(X) * len(Y))
assert bipartite.density(G, X) == density
D = nx.DiGraph(G.edges())
assert bipartite.density(D, X) == density / 2.0
assert bipartite.density(nx.Graph(), {}) == 0.0

def test_bipartite_degrees(self):
G = nx.path_graph(5)
X = {1, 3}
Y = {0, 2, 4}
u, d = bipartite.degrees(G, Y)
assert dict(u) == {1: 2, 3: 2}
assert dict(d) == {0: 1, 2: 2, 4: 1}

def test_bipartite_weighted_degrees(self):
G = nx.path_graph(5)
X = {1, 3}
Y = {0, 2, 4}
u, d = bipartite.degrees(G, Y, weight="weight")
assert dict(u) == {1: 1.1, 3: 2}
assert dict(d) == {0: 0.1, 2: 2, 4: 1}
u, d = bipartite.degrees(G, Y, weight="other")
assert dict(u) == {1: 1.2, 3: 2}
assert dict(d) == {0: 0.2, 2: 2, 4: 1}

scipy = pytest.importorskip("scipy")
G = nx.path_graph(5)
X = [1, 3]
Y = [0, 2, 4]
assert M[0, 0] == 2
assert M[0, 0] == 4

scipy = pytest.importorskip("scipy")
tops = [2, 5, 10]
bots = [5, 10, 15]
for i in range(len(tops)):
G = bipartite.random_graph(tops[i], bots[i], 0.2)
top = [n for n, d in G.nodes(data=True) if d["bipartite"] == 0]