Fixed database typo and removed unnecessary class identifier.

This commit is contained in:
Batuhan Berk Başoğlu 2020-10-14 10:10:37 -04:00
parent 00ad49a143
commit 45fb349a7d
5098 changed files with 952558 additions and 85 deletions

View file

@ -0,0 +1,68 @@
import pytest
import networkx as nx
from networkx import NetworkXNotImplemented
class TestAttractingComponents:
@classmethod
def setup_class(cls):
cls.G1 = nx.DiGraph()
cls.G1.add_edges_from(
[
(5, 11),
(11, 2),
(11, 9),
(11, 10),
(7, 11),
(7, 8),
(8, 9),
(3, 8),
(3, 10),
]
)
cls.G2 = nx.DiGraph()
cls.G2.add_edges_from([(0, 1), (0, 2), (1, 1), (1, 2), (2, 1)])
cls.G3 = nx.DiGraph()
cls.G3.add_edges_from([(0, 1), (1, 2), (2, 1), (0, 3), (3, 4), (4, 3)])
cls.G4 = nx.DiGraph()
def test_attracting_components(self):
ac = list(nx.attracting_components(self.G1))
assert {2} in ac
assert {9} in ac
assert {10} in ac
ac = list(nx.attracting_components(self.G2))
ac = [tuple(sorted(x)) for x in ac]
assert ac == [(1, 2)]
ac = list(nx.attracting_components(self.G3))
ac = [tuple(sorted(x)) for x in ac]
assert (1, 2) in ac
assert (3, 4) in ac
assert len(ac) == 2
ac = list(nx.attracting_components(self.G4))
assert ac == []
def test_number_attacting_components(self):
assert nx.number_attracting_components(self.G1) == 3
assert nx.number_attracting_components(self.G2) == 1
assert nx.number_attracting_components(self.G3) == 2
assert nx.number_attracting_components(self.G4) == 0
def test_is_attracting_component(self):
assert not nx.is_attracting_component(self.G1)
assert not nx.is_attracting_component(self.G2)
assert not nx.is_attracting_component(self.G3)
g2 = self.G3.subgraph([1, 2])
assert nx.is_attracting_component(g2)
assert not nx.is_attracting_component(self.G4)
def test_connected_raise(self):
G = nx.Graph()
pytest.raises(NetworkXNotImplemented, nx.attracting_components, G)
pytest.raises(NetworkXNotImplemented, nx.number_attracting_components, G)
pytest.raises(NetworkXNotImplemented, nx.is_attracting_component, G)

View file

@ -0,0 +1,244 @@
import pytest
import networkx as nx
from networkx import NetworkXNotImplemented
def assert_components_edges_equal(x, y):
sx = {frozenset([frozenset(e) for e in c]) for c in x}
sy = {frozenset([frozenset(e) for e in c]) for c in y}
assert sx == sy
def assert_components_equal(x, y):
sx = {frozenset(c) for c in x}
sy = {frozenset(c) for c in y}
assert sx == sy
def test_barbell():
G = nx.barbell_graph(8, 4)
nx.add_path(G, [7, 20, 21, 22])
nx.add_cycle(G, [22, 23, 24, 25])
pts = set(nx.articulation_points(G))
assert pts == {7, 8, 9, 10, 11, 12, 20, 21, 22}
answer = [
{12, 13, 14, 15, 16, 17, 18, 19},
{0, 1, 2, 3, 4, 5, 6, 7},
{22, 23, 24, 25},
{11, 12},
{10, 11},
{9, 10},
{8, 9},
{7, 8},
{21, 22},
{20, 21},
{7, 20},
]
assert_components_equal(list(nx.biconnected_components(G)), answer)
G.add_edge(2, 17)
pts = set(nx.articulation_points(G))
assert pts == {7, 20, 21, 22}
def test_articulation_points_repetitions():
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (1, 3)])
assert list(nx.articulation_points(G)) == [1]
def test_articulation_points_cycle():
G = nx.cycle_graph(3)
nx.add_cycle(G, [1, 3, 4])
pts = set(nx.articulation_points(G))
assert pts == {1}
def test_is_biconnected():
G = nx.cycle_graph(3)
assert nx.is_biconnected(G)
nx.add_cycle(G, [1, 3, 4])
assert not nx.is_biconnected(G)
def test_empty_is_biconnected():
G = nx.empty_graph(5)
assert not nx.is_biconnected(G)
G.add_edge(0, 1)
assert not nx.is_biconnected(G)
def test_biconnected_components_cycle():
G = nx.cycle_graph(3)
nx.add_cycle(G, [1, 3, 4])
answer = [{0, 1, 2}, {1, 3, 4}]
assert_components_equal(list(nx.biconnected_components(G)), answer)
def test_biconnected_components1():
# graph example from
# http://www.ibluemojo.com/school/articul_algorithm.html
edges = [
(0, 1),
(0, 5),
(0, 6),
(0, 14),
(1, 5),
(1, 6),
(1, 14),
(2, 4),
(2, 10),
(3, 4),
(3, 15),
(4, 6),
(4, 7),
(4, 10),
(5, 14),
(6, 14),
(7, 9),
(8, 9),
(8, 12),
(8, 13),
(10, 15),
(11, 12),
(11, 13),
(12, 13),
]
G = nx.Graph(edges)
pts = set(nx.articulation_points(G))
assert pts == {4, 6, 7, 8, 9}
comps = list(nx.biconnected_component_edges(G))
answer = [
[(3, 4), (15, 3), (10, 15), (10, 4), (2, 10), (4, 2)],
[(13, 12), (13, 8), (11, 13), (12, 11), (8, 12)],
[(9, 8)],
[(7, 9)],
[(4, 7)],
[(6, 4)],
[(14, 0), (5, 1), (5, 0), (14, 5), (14, 1), (6, 14), (6, 0), (1, 6), (0, 1)],
]
assert_components_edges_equal(comps, answer)
def test_biconnected_components2():
G = nx.Graph()
nx.add_cycle(G, "ABC")
nx.add_cycle(G, "CDE")
nx.add_cycle(G, "FIJHG")
nx.add_cycle(G, "GIJ")
G.add_edge("E", "G")
comps = list(nx.biconnected_component_edges(G))
answer = [
[
tuple("GF"),
tuple("FI"),
tuple("IG"),
tuple("IJ"),
tuple("JG"),
tuple("JH"),
tuple("HG"),
],
[tuple("EG")],
[tuple("CD"), tuple("DE"), tuple("CE")],
[tuple("AB"), tuple("BC"), tuple("AC")],
]
assert_components_edges_equal(comps, answer)
def test_biconnected_davis():
D = nx.davis_southern_women_graph()
bcc = list(nx.biconnected_components(D))[0]
assert set(D) == bcc # All nodes in a giant bicomponent
# So no articulation points
assert len(list(nx.articulation_points(D))) == 0
def test_biconnected_karate():
K = nx.karate_club_graph()
answer = [
{
0,
1,
2,
3,
7,
8,
9,
12,
13,
14,
15,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
},
{0, 4, 5, 6, 10, 16},
{0, 11},
]
bcc = list(nx.biconnected_components(K))
assert_components_equal(bcc, answer)
assert set(nx.articulation_points(K)) == {0}
def test_biconnected_eppstein():
# tests from http://www.ics.uci.edu/~eppstein/PADS/Biconnectivity.py
G1 = nx.Graph(
{
0: [1, 2, 5],
1: [0, 5],
2: [0, 3, 4],
3: [2, 4, 5, 6],
4: [2, 3, 5, 6],
5: [0, 1, 3, 4],
6: [3, 4],
}
)
G2 = nx.Graph(
{
0: [2, 5],
1: [3, 8],
2: [0, 3, 5],
3: [1, 2, 6, 8],
4: [7],
5: [0, 2],
6: [3, 8],
7: [4],
8: [1, 3, 6],
}
)
assert nx.is_biconnected(G1)
assert not nx.is_biconnected(G2)
answer_G2 = [{1, 3, 6, 8}, {0, 2, 5}, {2, 3}, {4, 7}]
bcc = list(nx.biconnected_components(G2))
assert_components_equal(bcc, answer_G2)
def test_null_graph():
G = nx.Graph()
assert not nx.is_biconnected(G)
assert list(nx.biconnected_components(G)) == []
assert list(nx.biconnected_component_edges(G)) == []
assert list(nx.articulation_points(G)) == []
def test_connected_raise():
DG = nx.DiGraph()
pytest.raises(NetworkXNotImplemented, nx.biconnected_components, DG)
pytest.raises(NetworkXNotImplemented, nx.biconnected_component_edges, DG)
pytest.raises(NetworkXNotImplemented, nx.articulation_points, DG)
pytest.raises(NetworkXNotImplemented, nx.is_biconnected, DG)

View file

@ -0,0 +1,111 @@
import pytest
import networkx as nx
from networkx import convert_node_labels_to_integers as cnlti
from networkx import NetworkXNotImplemented
class TestConnected:
@classmethod
def setup_class(cls):
G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted")
G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
cls.G = nx.union(G1, G2)
cls.G = nx.union(cls.G, G3)
cls.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
cls.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1)
cls.gc = []
G = nx.DiGraph()
G.add_edges_from(
[
(1, 2),
(2, 3),
(2, 8),
(3, 4),
(3, 7),
(4, 5),
(5, 3),
(5, 6),
(7, 4),
(7, 6),
(8, 1),
(8, 7),
]
)
C = [[3, 4, 5, 7], [1, 2, 8], [6]]
cls.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
C = [[2, 3, 4], [1]]
cls.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
C = [[1, 2, 3]]
cls.gc.append((G, C))
# Eppstein's tests
G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
C = [[0], [1], [2], [3], [4], [5], [6]]
cls.gc.append((G, C))
G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
C = [[0, 1, 2], [3, 4]]
cls.gc.append((G, C))
G = nx.DiGraph()
C = []
cls.gc.append((G, C))
def test_connected_components(self):
cc = nx.connected_components
G = self.G
C = {
frozenset([0, 1, 2, 3]),
frozenset([4, 5, 6, 7, 8, 9]),
frozenset([10, 11, 12, 13, 14]),
}
assert {frozenset(g) for g in cc(G)} == C
def test_number_connected_components(self):
ncc = nx.number_connected_components
assert ncc(self.G) == 3
def test_number_connected_components2(self):
ncc = nx.number_connected_components
assert ncc(self.grid) == 1
def test_connected_components2(self):
cc = nx.connected_components
G = self.grid
C = {frozenset([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])}
assert {frozenset(g) for g in cc(G)} == C
def test_node_connected_components(self):
ncc = nx.node_connected_component
G = self.grid
C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
assert ncc(G, 1) == C
def test_is_connected(self):
assert nx.is_connected(self.grid)
G = nx.Graph()
G.add_nodes_from([1, 2])
assert not nx.is_connected(G)
def test_connected_raise(self):
pytest.raises(NetworkXNotImplemented, nx.connected_components, self.DG)
pytest.raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
pytest.raises(NetworkXNotImplemented, nx.node_connected_component, self.DG, 1)
pytest.raises(NetworkXNotImplemented, nx.is_connected, self.DG)
pytest.raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
def test_connected_mutability(self):
G = self.grid
seen = set()
for component in nx.connected_components(G):
assert len(seen & component) == 0
seen.update(component)
component.clear()

View file

@ -0,0 +1,53 @@
from itertools import chain
import networkx as nx
import pytest
class TestIsSemiconnected:
def test_undirected(self):
pytest.raises(nx.NetworkXNotImplemented, nx.is_semiconnected, nx.Graph())
pytest.raises(nx.NetworkXNotImplemented, nx.is_semiconnected, nx.MultiGraph())
def test_empty(self):
pytest.raises(nx.NetworkXPointlessConcept, nx.is_semiconnected, nx.DiGraph())
pytest.raises(
nx.NetworkXPointlessConcept, nx.is_semiconnected, nx.MultiDiGraph()
)
def test_single_node_graph(self):
G = nx.DiGraph()
G.add_node(0)
assert nx.is_semiconnected(G)
def test_path(self):
G = nx.path_graph(100, create_using=nx.DiGraph())
assert nx.is_semiconnected(G)
G.add_edge(100, 99)
assert not nx.is_semiconnected(G)
def test_cycle(self):
G = nx.cycle_graph(100, create_using=nx.DiGraph())
assert nx.is_semiconnected(G)
G = nx.path_graph(100, create_using=nx.DiGraph())
G.add_edge(0, 99)
assert nx.is_semiconnected(G)
def test_tree(self):
G = nx.DiGraph()
G.add_edges_from(
chain.from_iterable([(i, 2 * i + 1), (i, 2 * i + 2)] for i in range(100))
)
assert not nx.is_semiconnected(G)
def test_dumbbell(self):
G = nx.cycle_graph(100, create_using=nx.DiGraph())
G.add_edges_from((i + 100, (i + 1) % 100 + 100) for i in range(100))
assert not nx.is_semiconnected(G) # G is disconnected.
G.add_edge(100, 99)
assert nx.is_semiconnected(G)
def test_alternating_path(self):
G = nx.DiGraph(
chain.from_iterable([(i, i - 1), (i, i + 1)] for i in range(0, 100, 2))
)
assert not nx.is_semiconnected(G)

View file

@ -0,0 +1,216 @@
import pytest
import networkx as nx
from networkx import NetworkXNotImplemented
class TestStronglyConnected:
@classmethod
def setup_class(cls):
cls.gc = []
G = nx.DiGraph()
G.add_edges_from(
[
(1, 2),
(2, 3),
(2, 8),
(3, 4),
(3, 7),
(4, 5),
(5, 3),
(5, 6),
(7, 4),
(7, 6),
(8, 1),
(8, 7),
]
)
C = {frozenset([3, 4, 5, 7]), frozenset([1, 2, 8]), frozenset([6])}
cls.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
C = {frozenset([2, 3, 4]), frozenset([1])}
cls.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
C = {frozenset([1, 2, 3])}
cls.gc.append((G, C))
# Eppstein's tests
G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
C = {
frozenset([0]),
frozenset([1]),
frozenset([2]),
frozenset([3]),
frozenset([4]),
frozenset([5]),
frozenset([6]),
}
cls.gc.append((G, C))
G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
C = {frozenset([0, 1, 2]), frozenset([3, 4])}
cls.gc.append((G, C))
def test_tarjan(self):
scc = nx.strongly_connected_components
for G, C in self.gc:
assert {frozenset(g) for g in scc(G)} == C
def test_tarjan_recursive(self):
scc = nx.strongly_connected_components_recursive
for G, C in self.gc:
assert {frozenset(g) for g in scc(G)} == C
def test_kosaraju(self):
scc = nx.kosaraju_strongly_connected_components
for G, C in self.gc:
assert {frozenset(g) for g in scc(G)} == C
def test_number_strongly_connected_components(self):
ncc = nx.number_strongly_connected_components
for G, C in self.gc:
assert ncc(G) == len(C)
def test_is_strongly_connected(self):
for G, C in self.gc:
if len(C) == 1:
assert nx.is_strongly_connected(G)
else:
assert not nx.is_strongly_connected(G)
def test_contract_scc1(self):
G = nx.DiGraph()
G.add_edges_from(
[
(1, 2),
(2, 3),
(2, 11),
(2, 12),
(3, 4),
(4, 3),
(4, 5),
(5, 6),
(6, 5),
(6, 7),
(7, 8),
(7, 9),
(7, 10),
(8, 9),
(9, 7),
(10, 6),
(11, 2),
(11, 4),
(11, 6),
(12, 6),
(12, 11),
]
)
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
# DAG
assert nx.is_directed_acyclic_graph(cG)
# nodes
assert sorted(cG.nodes()) == [0, 1, 2, 3]
# edges
mapping = {}
for i, component in enumerate(scc):
for n in component:
mapping[n] = i
edge = (mapping[2], mapping[3])
assert cG.has_edge(*edge)
edge = (mapping[2], mapping[5])
assert cG.has_edge(*edge)
edge = (mapping[3], mapping[5])
assert cG.has_edge(*edge)
def test_contract_scc_isolate(self):
# Bug found and fixed in [1687].
G = nx.DiGraph()
G.add_edge(1, 2)
G.add_edge(2, 1)
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
assert list(cG.nodes()) == [0]
assert list(cG.edges()) == []
def test_contract_scc_edge(self):
G = nx.DiGraph()
G.add_edge(1, 2)
G.add_edge(2, 1)
G.add_edge(2, 3)
G.add_edge(3, 4)
G.add_edge(4, 3)
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
assert sorted(cG.nodes()) == [0, 1]
if 1 in scc[0]:
edge = (0, 1)
else:
edge = (1, 0)
assert list(cG.edges()) == [edge]
def test_condensation_mapping_and_members(self):
G, C = self.gc[1]
C = sorted(C, key=len, reverse=True)
cG = nx.condensation(G)
mapping = cG.graph["mapping"]
assert all(n in G for n in mapping)
assert all(0 == cN for n, cN in mapping.items() if n in C[0])
assert all(1 == cN for n, cN in mapping.items() if n in C[1])
for n, d in cG.nodes(data=True):
assert set(C[n]) == cG.nodes[n]["members"]
def test_null_graph(self):
G = nx.DiGraph()
assert list(nx.strongly_connected_components(G)) == []
assert list(nx.kosaraju_strongly_connected_components(G)) == []
assert list(nx.strongly_connected_components_recursive(G)) == []
assert len(nx.condensation(G)) == 0
pytest.raises(
nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph()
)
def test_connected_raise(self):
G = nx.Graph()
pytest.raises(NetworkXNotImplemented, nx.strongly_connected_components, G)
pytest.raises(
NetworkXNotImplemented, nx.kosaraju_strongly_connected_components, G
)
pytest.raises(
NetworkXNotImplemented, nx.strongly_connected_components_recursive, G
)
pytest.raises(NetworkXNotImplemented, nx.is_strongly_connected, G)
pytest.raises(
nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph()
)
pytest.raises(NetworkXNotImplemented, nx.condensation, G)
# Commented out due to variability on Travis-CI hardware/operating systems
# def test_linear_time(self):
# # See Issue #2831
# count = 100 # base case
# dg = nx.DiGraph()
# dg.add_nodes_from([0, 1])
# for i in range(2, count):
# dg.add_node(i)
# dg.add_edge(i, 1)
# dg.add_edge(0, i)
# t = time.time()
# ret = tuple(nx.strongly_connected_components(dg))
# dt = time.time() - t
#
# count = 200
# dg = nx.DiGraph()
# dg.add_nodes_from([0, 1])
# for i in range(2, count):
# dg.add_node(i)
# dg.add_edge(i, 1)
# dg.add_edge(0, i)
# t = time.time()
# ret = tuple(nx.strongly_connected_components(dg))
# dt2 = time.time() - t
# assert_less(dt2, dt * 2.3) # should be 2 times longer for this graph

View file

@ -0,0 +1,78 @@
import pytest
import networkx as nx
from networkx import NetworkXNotImplemented
class TestWeaklyConnected:
@classmethod
def setup_class(cls):
cls.gc = []
G = nx.DiGraph()
G.add_edges_from(
[
(1, 2),
(2, 3),
(2, 8),
(3, 4),
(3, 7),
(4, 5),
(5, 3),
(5, 6),
(7, 4),
(7, 6),
(8, 1),
(8, 7),
]
)
C = [[3, 4, 5, 7], [1, 2, 8], [6]]
cls.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
C = [[2, 3, 4], [1]]
cls.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
C = [[1, 2, 3]]
cls.gc.append((G, C))
# Eppstein's tests
G = nx.DiGraph({0: [1], 1: [2, 3], 2: [4, 5], 3: [4, 5], 4: [6], 5: [], 6: []})
C = [[0], [1], [2], [3], [4], [5], [6]]
cls.gc.append((G, C))
G = nx.DiGraph({0: [1], 1: [2, 3, 4], 2: [0, 3], 3: [4], 4: [3]})
C = [[0, 1, 2], [3, 4]]
cls.gc.append((G, C))
def test_weakly_connected_components(self):
for G, C in self.gc:
U = G.to_undirected()
w = {frozenset(g) for g in nx.weakly_connected_components(G)}
c = {frozenset(g) for g in nx.connected_components(U)}
assert w == c
def test_number_weakly_connected_components(self):
for G, C in self.gc:
U = G.to_undirected()
w = nx.number_weakly_connected_components(G)
c = nx.number_connected_components(U)
assert w == c
def test_is_weakly_connected(self):
for G, C in self.gc:
U = G.to_undirected()
assert nx.is_weakly_connected(G) == nx.is_connected(U)
def test_null_graph(self):
G = nx.DiGraph()
assert list(nx.weakly_connected_components(G)) == []
assert nx.number_weakly_connected_components(G) == 0
pytest.raises(nx.NetworkXPointlessConcept, nx.is_weakly_connected, G)
def test_connected_raise(self):
G = nx.Graph()
pytest.raises(NetworkXNotImplemented, nx.weakly_connected_components, G)
pytest.raises(NetworkXNotImplemented, nx.number_weakly_connected_components, G)
pytest.raises(NetworkXNotImplemented, nx.is_weakly_connected, G)