def testBFS(self): """ bfs walk """ global clock def walk(graph): """ bfs walk """ global clock nodes = graph.nodes() for node in nodes: node.makeProperty("clockC", 0) def postVisit(node): global clock node.clockC = clock clock += 1 nodes = graph.bfs(postVisit=postVisit) nodes.sort(key=lambda node: node.clockC) return nodes clock = 0 gr = Graph("testGraph", self.nodes, self.edges) gr.addNode(self.aloneNode) gr.reset() nodesSorted = walk(gr) nodes = gr.nodes() nodes.sort(key=lambda node: node.clockC) self.assertEqual(nodesSorted, nodes, "bfs failed")
def testBFS( self ): """ bfs walk """ global clock def walk( graph ): """ bfs walk """ global clock nodes = graph.nodes() for node in nodes: node.makeProperty( "clockC", 0 ) def postVisit( node ): global clock node.clockC = clock clock += 1 nodes = graph.bfs( postVisit = postVisit ) nodes.sort( key = lambda node: node.clockC ) return nodes clock = 0 gr = Graph( "testGraph", self.nodes, self.edges ) gr.addNode( self.aloneNode ) gr.reset() nodesSorted = walk( gr ) nodes = gr.nodes() nodes.sort( key = lambda node: node.clockC ) self.assertEqual( nodesSorted, nodes, "bfs failed" )
def testDFS(self): """ dfs """ global clock def topoA(graph): """ topological sort """ global clock nodes = graph.nodes() for node in nodes: node.makeProperty("clockA", 0) def postVisit(node): global clock node.clockA = clock clock += 1 graph.dfs(postVisit=postVisit) nodes = graph.nodes() nodes.sort(key=lambda node: node.clockA) return nodes def topoB(graph): """ topological sort """ global clock nodes = graph.nodes() for node in nodes: node.makeProperty("clockB", 0) def postVisit(node): global clock node.clockB = clock clock += 1 graph.dfsIter(postVisit=postVisit) nodes = graph.nodes() nodes.sort(key=lambda node: node.clockB) return nodes clock = 0 gr = Graph("testGraph", self.nodes, self.edges) gr.addNode(self.aloneNode) nodesSorted = topoA(gr) nodes = gr.nodes() nodes.sort(key=lambda node: node.clockA, reverse=True) self.assertEqual(nodes, nodesSorted, "topoA sort failed") clock = 0 gr = Graph("testGraph", self.nodes, self.edges) gr.addNode(self.aloneNode) gr.reset() nodesSorted = topoB(gr) nodes = gr.nodes() nodes.sort(key=lambda node: node.clockB, reverse=True) self.assertEqual(nodes, nodesSorted, "topoB sort failed")
def testDFS( self ): """ dfs """ global clock def topoA( graph ): """ topological sort """ global clock nodes = graph.nodes() for node in nodes: node.makeProperty( "clockA", 0 ) def postVisit( node ): global clock node.clockA = clock clock += 1 graph.dfs( postVisit = postVisit ) nodes = graph.nodes() nodes.sort( key = lambda node: node.clockA ) return nodes def topoB( graph ): """ topological sort """ global clock nodes = graph.nodes() for node in nodes: node.makeProperty( "clockB", 0 ) def postVisit( node ): global clock node.clockB = clock clock += 1 graph.dfsIter( postVisit = postVisit ) nodes = graph.nodes() nodes.sort( key = lambda node: node.clockB ) return nodes clock = 0 gr = Graph( "testGraph", self.nodes, self.edges ) gr.addNode( self.aloneNode ) nodesSorted = topoA( gr ) nodes = gr.nodes() nodes.sort( key = lambda node: node.clockA, reverse = True ) self.assertEqual( nodes, nodesSorted, "topoA sort failed" ) clock = 0 gr = Graph( "testGraph", self.nodes, self.edges ) gr.addNode( self.aloneNode ) gr.reset() nodesSorted = topoB( gr ) nodes = gr.nodes() nodes.sort( key = lambda node: node.clockB, reverse = True ) self.assertEqual( nodes, nodesSorted, "topoB sort failed" )
def testGraph(self): """ ctor nodes edges connect walk """ # # create graph gr = Graph("testGraph", self.nodes, self.edges) # # nodes and edges for node in self.nodes: self.assertEqual(node in gr, True) for edge in self.edges: self.assertEqual(edge in gr, True) self.assertEqual(sorted(self.nodes, key=lambda x: x.name), sorted(gr.nodes(), key=lambda x: x.name)) self.assertEqual(sorted(self.edges, key=lambda x: x.name), sorted(gr.edges(), key=lambda x: x.name)) # # getNode for node in self.nodes: self.assertEqual(gr.getNode(node.name), node) # # connect aloneEdge = gr.connect(self.nodes[0], self.aloneNode) self.assertEqual(self.aloneNode in gr, True) self.assertEqual(aloneEdge in gr, True) # # addNode anotherNode = Node("5") anotherEdge = anotherNode.connect(self.aloneNode) gr.addNode(anotherNode) self.assertEqual(anotherNode in gr, True) self.assertEqual(anotherEdge in gr, True) # # walk no nodeFcn ret = gr.walkAll() self.assertEqual(ret, {}) for node in gr.nodes(): self.assertEqual(node.visited, True) gr.reset() for node in gr.nodes(): self.assertEqual(node.visited, False) # # walk with nodeFcn def nbEdges(node): """ dummy node fcn """ return len(node.edges()) ret = gr.walkAll(nodeFcn=nbEdges) self.assertEqual(ret, {'1': 3, '2': 0, '3': 0, '4': 0, '5': 1})
def testGraph( self ): """ ctor nodes edges connect walk """ # # create graph gr = Graph( "testGraph", self.nodes, self.edges ) # # nodes and edges for node in self.nodes: self.assertEqual( node in gr, True ) for edge in self.edges: self.assertEqual( edge in gr, True ) self.assertEqual( sorted( self.nodes ), sorted( gr.nodes() ) ) self.assertEqual( sorted( self.edges ), sorted( gr.edges() ) ) # # getNode for node in self.nodes: self.assertEqual( gr.getNode( node.name ), node ) # # connect aloneEdge = gr.connect( self.nodes[0], self.aloneNode ) self.assertEqual( self.aloneNode in gr, True ) self.assertEqual( aloneEdge in gr, True ) # # addNode anotherNode = Node( "5" ) anotherEdge = anotherNode.connect( self.aloneNode ) gr.addNode( anotherNode ) self.assertEqual( anotherNode in gr, True ) self.assertEqual( anotherEdge in gr, True ) # # walk no nodeFcn ret = gr.walkAll() self.assertEqual( ret, {} ) for node in gr.nodes(): self.assertEqual( node.visited, True ) gr.reset() for node in gr.nodes(): self.assertEqual( node.visited, False ) # # walk with nodeFcn def nbEdges( node ): """ dummy node fcn """ return len( node.edges() ) ret = gr.walkAll( nodeFcn = nbEdges ) self.assertEqual( ret, { '1': 3, '2' : 0, '3': 0, '4' : 0, '5': 1 } )