class TestDFS(unittest.TestCase): def setUp(self): self.pairs = UGraph(100) for x in range(0, 98, 2): self.pairs.addEdge(x, x + 2) graph = UGraph(4) graph.addEdge(0, 1) graph.addEdge(1, 2) graph.addEdge(2, 3) self.bipartite = BipartiteDFS(graph) nonbiGraph = UGraph(4) nonbiGraph.addEdge(0, 1) nonbiGraph.addEdge(1, 2) nonbiGraph.addEdge(2, 3) nonbiGraph.addEdge(0, 2) nonbiGraph.addEdge(0, 3) self.notbipartite = BipartiteDFS(nonbiGraph) def test_connecting(self): """Connect the pairs from 0 to 96, and see if they are connected.""" self.d = DepthFirstPaths(self.pairs, 0) self.assertTrue(self.d.hasPathTo(96)) for i in range(1, 99, 2): self.assertFalse(self.d.hasPathTo(i)) def test_bipartite(self): self.assertTrue(self.bipartite.isBipartite) self.assertFalse(self.notbipartite.isBipartite)
def test_cc_integrity(self): g = UGraph(20) count = 2 for i in range(1, 10): count += 1 g.addEdge(i, i + 1) self.integrity = ConnectedComponents(g) self.assertEqual(count, self.integrity.count())
class TestUGraph(unittest.TestCase): def setUp(self): self.a = UGraph(1001) self.b = UGraph(100) def test_connecting(self): "We'll connect 1 node to 999 nodes and expect the len of adj to be 999" self.assertEqual(len(self.a.adj[0]), 0) for i in range(1, 1000): self.a.addEdge(0, i) self.assertEqual(len(self.a.adj[0]), 999) self.assertEqual(UGraph.degree(self.a, 0), 999)
def dfs(self, g: UGraph, i: int, j: int): self.marked[i] = True for node in g.close(i): if not self.marked[node]: self.dfs(g, i, node) elif i != j: self.hasCycle = True
def test_connecting(self): "We'll connect 1 node to 999 nodes and expect the len of adj to be 999" self.assertEqual(len(self.a.adj[0]), 0) for i in range(1, 1000): self.a.addEdge(0, i) self.assertEqual(len(self.a.adj[0]), 999) self.assertEqual(UGraph.degree(self.a, 0), 999)
def setUp(self): self.graphArr = [] for g in range(1, 200): self.graphArr.append(UGraph(g)) self.ccarr = [] for c in range(0, 199): self.ccarr.append(ConnectedComponents(self.graphArr[c]))
def dfs(self, g: UGraph, v: int): self.marked[v] = True self.id[v] = self.curr_component for node in g.close(v): if not self.marked[node]: self.dfs(g, node)
def setUp(self): self.pairs = UGraph(100) for x in range(0, 98, 2): self.pairs.addEdge(x, x + 2) graph = UGraph(4) graph.addEdge(0, 1) graph.addEdge(1, 2) graph.addEdge(2, 3) self.bipartite = BipartiteDFS(graph) nonbiGraph = UGraph(4) nonbiGraph.addEdge(0, 1) nonbiGraph.addEdge(1, 2) nonbiGraph.addEdge(2, 3) nonbiGraph.addEdge(0, 2) nonbiGraph.addEdge(0, 3) self.notbipartite = BipartiteDFS(nonbiGraph)
def dfs(self, g: UGraph, v: int): self.marked[v] = True for node in g.close(v): if not self.marked[node]: self.edgeTo[node] = v self.dfs(g, node)
def setUp(self): self.a = UGraph(1001) self.b = UGraph(100)