예제 #1
0
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)
예제 #2
0
 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())
예제 #3
0
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)
예제 #4
0
 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
예제 #5
0
 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)
예제 #6
0
    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)
예제 #8
0
 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)
예제 #9
0
 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)
예제 #10
0
 def setUp(self):
     self.a = UGraph(1001)
     self.b = UGraph(100)