예제 #1
0
파일: Test_Graph.py 프로젝트: pmusset/DIRAC
    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")
예제 #2
0
  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" )
예제 #3
0
파일: Test_Graph.py 프로젝트: pmusset/DIRAC
    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")
예제 #4
0
  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" )
예제 #5
0
파일: Test_Graph.py 프로젝트: pmusset/DIRAC
    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})
예제 #6
0
  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 } )