class GraphTest(unittest.TestCase): graph = None adj = None mutex = 0 def setUp(self): if self.mutex == 0: self.graph = Graph([1,2,3,4,6]) self.graph.addEdge(1,3) self.graph.addEdge(2,4) self.graph.addEdge(2,6) self.graph.addEdge(6,4) self.adj = self.graph.getAdj() self.mutex = 1 def tearDown(self): self.graph = None def testDegree (self): assert self.graph.degree(1) == len(self.adj[0]), 'degree = x\n' assert self.graph.degree(4) == len(self.adj[3]), 'degree = x\n' def testMaxDegree(self): assert self.graph.maxDegree() == len(self.adj[4]), 'max degree = x\n' def testAvgDegree(self): assert self.graph.avgDegree() == (sum(map(len,self.adj))/2), 'avg degree = \n' def testSelfLoops(self): #assert self.graph.selfLoops() == , 'self loops = %d' % self.graph.selfLoops() return
for i in graph.Adj(s): if i == None: return if not self.__visited[graph.getIndex(i)]: self.__depthFirst(g,i) self.__components[graph.getIndex(i)] = self.__count return def count(self): return self.__count def componentsId(self): return self.__components def connected(self, v1, v2): if v1 not in self.graph.vertices or v2 not in self.graph.vertices: return False return self.__components[self.graph.getIndex(v1)] == self.__components[self.graph.getIndex(v2)] g = Graph([1,2,3,4,13,12,6]) g.addEdge(1,3) g.addEdge(2,4) g.addEdge(2,6) g.addEdge(6,4) g.addEdge(13,12) print g.getAdj() cc = GraphCC(g) print cc.count() print cc.componentsId() print cc.connected(12,13)