예제 #1
0
 def __init__(self,
              createGraphTraverser:
              "undirected IDigraph<T> -> IBreadthFirstTraverser<T>" = lambda
              G: BreadthFirstTraverser()):
     self.createGraphTraverser = createGraphTraverser
예제 #2
0
 def __init__(
     self,
     createDigraphTraverser:
     "IDigraph<T> -> IDigraphTraverser<T>"  #?create?¶create?
     = lambda G: BreadthFirstTraverser()):  #?=?»=?
     self.createDigraphTraverser = createDigraphTraverser
예제 #3
0
#coding: latin1
#< full
from algoritmia.datastructures.digraphs import AdjacencyDigraph
from algoritmia.datastructures.sets import IntSet
from algoritmia.datastructures.maps import IntKeyMap
from algoritmia.problems.traversals import BreadthFirstTraverser

G = AdjacencyDigraph(
    E=[(0, 1), (0, 3), (1, 4), (2, 5), (3, 1), (4, 3)],  #?E=?¶E=?
    createMap=lambda V: IntKeyMap(capacity=max(V) + 1),  #?mapF?»mapF?
    createSet=lambda V: IntSet(capacity=max(V) + 1))  #?setF?»setF?
bft = BreadthFirstTraverser(createSet=lambda V: IntSet(capacity=max(V) + 1))
print(list(bft.traverse(G, 0)))
#> full
예제 #4
0
 def setUp(self):
     self.G = Digraph(E={0: [1, 2], 1: [3], 2: [3], 3: [4], 5: [6]})
     self.bft = BreadthFirstTraverser()
     self.rdft = RecursiveDepthFirstTraverser()
     self.dft = DepthFirstTraverser()
     seed(0)
예제 #5
0
class TestTraversers(unittest.TestCase):
    def setUp(self):
        self.G = Digraph(E={0: [1, 2], 1: [3], 2: [3], 3: [4], 5: [6]})
        self.bft = BreadthFirstTraverser()
        self.rdft = RecursiveDepthFirstTraverser()
        self.dft = DepthFirstTraverser()
        seed(0)

    def test_breadth_first_traversal(self):
        self.assertTrue(
            tuple(self.bft.traverse(self.G, 0)) in ((0, 1, 2, 3, 4), (0, 2, 1,
                                                                      3, 4)))

    def test_breadth_first_full_traversal(self):
        self.assertTrue(tuple(self.bft.full_traverse(self.G)) in \
                        ((0,1,2,3,4,5,6),
                         (0,2,1,3,4,5,6),
                         (5,6,0,1,2,3,4),
                         (5,6,0,2,1,3,4),
                         ))

    def test_recursive_preorder_traversal(self):
        self.assertTrue(
            tuple(self.rdft.traverse(self.G, 0)) in ((0, 1, 3, 4, 2), (0, 2, 3,
                                                                       4, 1)))

    def test_recursive_postorder_traversal(self):
        self.assertTrue(
            tuple(
                self.rdft.traverse(
                    self.G, 0, postorder_visitor=lambda u, v: v)) in ((4, 3, 1,
                                                                       2, 0),
                                                                      (4, 3, 2,
                                                                       1, 0)))

    def test_preorder_traversal(self):
        self.assertTrue(
            tuple(self.dft.traverse(self.G, 0)) in ((0, 1, 3, 4, 2), (0, 2, 3,
                                                                      4, 1)))

    def test_postorder_traversal(self):
        self.assertTrue(
            tuple(
                self.dft.traverse(self.G, 0, postorder_visitor=lambda u, v: v))
            in ((4, 3, 1, 2, 0), (4, 3, 2, 1, 0)))

    def test_preorder_full_traversal(self):
        self.assertTrue(
            tuple(self.dft.full_traverse(self.G)) in ((0, 1, 3, 4, 2, 5, 6),
                                                      (0, 2, 3, 4, 1, 5, 6)))

    def test_postorder_full_traversal(self):
        self.assertTrue(
            tuple(
                self.dft.full_traverse(
                    self.G, postorder_visitor=lambda u, v: v)) in ((4, 3, 1, 2,
                                                                    0, 6, 5),
                                                                   (4, 3, 2, 1,
                                                                    0, 6, 5)))

    def test_breadth_first_traversal_from_some(self):
        self.assertTrue(tuple(self.bft.traverse_from_some(self.G, [0, 5])) in \
                        ((0,5,1,2,6,3,4),
                         ))
예제 #6
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import UndirectedGraph
from algoritmia.problems.traversals import BreadthFirstTraverser

G = UndirectedGraph(E=[(0,1), (0,3), (1,4), (2,5), (3,1), (4,3)])
print(list(BreadthFirstTraverser().traverse(G, 0)))
#> full
예제 #7
0
#coding: latin1

#< full
from algoritmia.datastructures.digraphs import UndirectedGraph
from algoritmia.problems.traversals import BreadthFirstTraverser

G = UndirectedGraph(E=[(0, 1), (0, 3), (1, 4), (2, 5), (3, 1), (4, 3)])
print(list(BreadthFirstTraverser().full_traverse(G)))
#> full