예제 #1
0
 def dfs(self, graph, vert, marked, pre, post, reversepost):
     q.enqueue(pre, vert)
     m.put(marked, vert, True)
     lstadjacents = g.adjacents(graph, vert)
     adjiterator = it.newIterator(lstadjacents)
     while it.hasNext(adjiterator):
         adjvert = it.next(adjiterator)
         if not m.contains(marked, adjvert):
             self.dfs(graph, adjvert, marked, pre, post, reversepost)
     q.enqueue(post, vert)
     s.push(reversepost, vert)
예제 #2
0
def bfs(search, source):
    queue = q.newQueue()
    q.enqueue(queue, source)
    while not (q.isEmpty(queue)):
        v = q.dequeue(queue)
        adj_v = g.adjacents(search['graph'], v)
        adj_it = it.newIterator(adj_v)
        while it.hasNext(adj_it):
            w = it.next(adj_it)
            visited_w = map.get(search['visitedMap'], w)
            if visited_w == None:
                map.put(search['visitedMap'], w, {'marked': True, 'edgeTo': v})
                q.enqueue(queue, w)
예제 #3
0
 def dfs(self, graph, vert, marked, pre, post, reversepost):
     """
       Implementación del recorrido Depth First Search
     """
     q.enqueue(pre, vert)
     m.put(marked, vert, True)
     lstadjacents = g.adjacents(graph, vert)
     adjiterator = it.newIterator(lstadjacents)
     while it.hasNext(adjiterator):
         adjvert = it.next(adjiterator)
         if not m.contains(marked, adjvert):
             self.dfs(graph, adjvert, marked, pre, post, reversepost)
     q.enqueue(post, vert)
     s.push(reversepost, vert)
예제 #4
0
파일: Dfs_Bfs.py 프로젝트: vytis8/proyecto
def bfs(search, source):
    queue = q.newQueue()
    q.enqueue(queue, source)
    while not (q.isEmpty(queue)):
        v = q.dequeue(queue)
        list_ad = g.adjacents(search['graph'], v)
        for i in range(1, (lt.size(list_ad) + 1)):
            w = lt.getElement(list_ad, i)
            if map.get(search['visitedMap'], w) == None:
                map.put(search['visitedMap'], w, {
                    'marked': True,
                    'edgeTo': None,
                    'nodo': w
                })
                q.enqueue(queue, w)
예제 #5
0
def bfs(search, source):
    queue = q.newQueue()
    q.enqueue(queue, source)
    while not (q.isEmpty(queue)):
        v = q.dequeue(queue)
        visited_v = map.get(search['visitedMap'], v)['value']
        adjs = g.adjacents(search['graph'], v)
        adjs_iter = it.newIterator(adjs)
        while (it.hasNext(adjs_iter)):
            w = it.next(adjs_iter)
            visited_w = map.get(search['visitedMap'], w)
            if visited_w == None:
                map.put(search['visitedMap'], w, {
                    'marked': True,
                    'edgeTo': v,
                    'distTo': visited_v['distTo'] + 1
                })
                q.enqueue(queue, w)
예제 #6
0
def test_queueElements():
    """
    Se prueba la creacion de una nueva cola, se agregan todos los datos creados por sistema y se imprime su valor
    """
    queue = q.newQueue(list_type)

    q.enqueue(queue, book5)
    q.enqueue(queue, book6)
    q.enqueue(queue, book3)
    q.enqueue(queue, book10)
    q.enqueue(queue, book1)
    q.enqueue(queue, book2)
    q.enqueue(queue, book8)
    q.enqueue(queue, book4)
    q.enqueue(queue, book7)
    q.enqueue(queue, book9)
    iterator = it.newIterator(queue)
    while it.hasNext(iterator):
        element = it.next(iterator)
        result = "".join(str(key) + ": " + str(value) + ",  " for key, value in element.items())
        print(result)
예제 #7
0
def test_enqueue_dequeue():
    """
    Este test prueba que la cola pueda manejar inserciones y eliminaciones de forma correcta siguiendo
    un orden establecido, y que no quede referencia al objeto sacado despues de haberlo removido de la
    cola
    """
    queue = q.newQueue(list_type)
    assert (q.size(queue) == 0)
    assert (q.isEmpty(queue))

    q.enqueue(queue, book5)
    assert (q.size(queue) == 1)
    assert (q.peek(queue) == q.dequeue(queue))
    assert (q.size(queue) == 0)

    q.enqueue(queue, book6)
    assert (q.size(queue) == 1)
    assert (q.peek(queue) == q.dequeue(queue))
    assert (q.size(queue) == 0)

    q.enqueue(queue, book3)
    assert (q.size(queue) == 1)
    assert (q.peek(queue) == q.dequeue(queue))
    assert (q.size(queue) == 0)

    q.enqueue(queue, book10)
    assert (q.size(queue) == 1)
    assert (q.peek(queue) == q.dequeue(queue))
    assert (q.size(queue) == 0)

    q.enqueue(queue, book1)
    assert (q.size(queue) == 1)
    assert (q.peek(queue) == q.dequeue(queue))
    assert (q.size(queue) == 0)

    q.enqueue(queue, book2)
    assert (q.size(queue) == 1)
    assert (q.peek(queue) == q.dequeue(queue))
    assert (q.size(queue) == 0)

    q.enqueue(queue, book8)
    q.enqueue(queue, book4)
    q.enqueue(queue, book7)
    q.enqueue(queue, book9)

    assert (q.size(queue) == 4)
    assert book8 == q.dequeue(queue)
    assert book4 == q.dequeue(queue)
    assert book7 == q.dequeue(queue)
    assert book9 == q.dequeue(queue)

    assert (q.size(queue) == 0)
예제 #8
0
def test_peek_dequeue():
    """
    Este test prueba la creacion de una cola y que el orden de salida sea el correcto para la
    estructura en cuestion, y que el tamaño se reduzca para cada salida de objeto
    """

    queue = q.newQueue(list_type)
    assert q.size(queue) == 0
    assert q.isEmpty(queue)
    queue = q.newQueue(list_type)

    q.enqueue(queue, book5)
    q.enqueue(queue, book6)
    q.enqueue(queue, book3)
    q.enqueue(queue, book10)
    q.enqueue(queue, book1)
    q.enqueue(queue, book2)
    q.enqueue(queue, book8)
    q.enqueue(queue, book4)
    q.enqueue(queue, book7)
    q.enqueue(queue, book9)
    total = q.size(queue)
    while not (q.isEmpty(queue)):
        peek = q.peek(queue)
        assert (q.dequeue(queue) == peek)
        total -= 1
        assert (total == q.size(queue))
예제 #9
0
def loadCSVFile (file, queue):
    input_file = csv.DictReader(open(file))
    for row in input_file:  
        q.enqueue(queue,row)
예제 #10
0
 def test_emptyQueue (self):
     """
     """
     self.queue = q.newQueue(self.list_type)
     self.assertEqual (q.size(self.queue), 0)
     self.assertTrue (q.isEmpty(self.queue))
     q.enqueue  (self.queue, self.book5)
     q.enqueue  (self.queue, self.book6)
     q.enqueue  (self.queue, self.book3)
     q.enqueue  (self.queue, self.book10)
     q.enqueue  (self.queue, self.book1)
     q.enqueue  (self.queue, self.book2)
     q.enqueue  (self.queue, self.book8)
     q.enqueue  (self.queue, self.book4)
     q.enqueue  (self.queue, self.book7)
     q.enqueue  (self.queue, self.book9)
     self.assertEqual (q.size(self.queue), 10)
예제 #11
0
    def test_infoElements(self):
        """
        """
        self.queue = q.newQueue(self.list_type)
        self.assertEqual(q.size(self.queue), 0)
        self.assertTrue(q.isEmpty(self.queue))
        q.enqueue(self.queue, self.movie5)
        q.enqueue(self.queue, self.movie6)
        q.enqueue(self.queue, self.movie3)
        q.enqueue(self.queue, self.movie10)
        q.enqueue(self.queue, self.movie1)
        q.enqueue(self.queue, self.movie2)
        q.enqueue(self.queue, self.movie8)
        q.enqueue(self.queue, self.movie4)
        q.enqueue(self.queue, self.movie7)
        q.enqueue(self.queue, self.movie9)

        elem = q.dequeue(self.queue)
        self.assertEqual(q.size(self.queue), 9)
        self.assertDictEqual(elem, self.movie5)

        elem = q.dequeue(self.queue)
        self.assertEqual(q.size(self.queue), 8)
        self.assertDictEqual(elem, self.movie6)

        elem = q.peek(self.queue)
        self.assertEqual(q.size(self.queue), 8)
        self.assertDictEqual(elem, self.movie3)

        q.enqueue(self.queue, self.movie5)
        self.assertEqual(q.size(self.queue), 9)

        elem = q.peek(self.queue)
        self.assertDictEqual(elem, self.movie3)
예제 #12
0
    def test_emptyQueue(self):
        """
        """
        self.queue = q.newQueue(self.list_type)
        self.assertEqual(q.size(self.queue), 0)
        self.assertTrue(q.isEmpty(self.queue))
        q.enqueue(self.queue, self.movie5)
        q.enqueue(self.queue, self.movie6)
        q.enqueue(self.queue, self.movie3)
        q.enqueue(self.queue, self.movie1)
        q.enqueue(self.queue, self.movie2)
        q.enqueue(self.queue, self.movie4)

        self.assertEqual(q.size(self.queue), 6)
예제 #13
0
def bfs (search, source):
    queue = q.newQueue()
    q.enqueue(queue, source)
    while not (q.isEmpty(queue)):
        v = q.dequeue (queue)
        visited_v = map.get(search['visitedMap'], v)['value']
예제 #14
0
    def test_infoElements (self):
        """
        """
        self.queue = q.newQueue(self.list_type)
        self.assertEqual (q.size(self.queue), 0)
        self.assertTrue (q.isEmpty(self.queue))
        q.enqueue  (self.queue, self.book5)
        q.enqueue  (self.queue, self.book6)
        q.enqueue  (self.queue, self.book3)
        q.enqueue  (self.queue, self.book10)
        q.enqueue  (self.queue, self.book1)
        q.enqueue  (self.queue, self.book2)
        q.enqueue  (self.queue, self.book8)
        q.enqueue  (self.queue, self.book4)
        q.enqueue  (self.queue, self.book7)
        q.enqueue  (self.queue, self.book9)

        elem = q.dequeue (self.queue)
        self.assertEqual (q.size(self.queue), 9)
        self.assertDictEqual (elem, self.book5)

        elem = q.dequeue (self.queue)
        self.assertEqual (q.size(self.queue), 8)
        self.assertDictEqual (elem, self.book6)

        elem = q.peek (self.queue)
        self.assertEqual (q.size(self.queue), 8)
        self.assertDictEqual (elem, self.book3)

        q.enqueue  (self.queue, self.book5)
        self.assertEqual (q.size(self.queue), 9)
        
        elem = q.peek (self.queue)
        self.assertDictEqual (elem, self.book3)
예제 #15
0
def test_sizeStack():
    """
    Se prueba la creacion de una cola y la relacion con el tamaño al ingresar datos
    """

    queue = q.newQueue(list_type)
    assert (q.size(queue) == 0)
    assert (q.isEmpty(queue))
    queue = q.newQueue(list_type)

    q.enqueue(queue, book5)
    q.enqueue(queue, book6)
    q.enqueue(queue, book3)
    q.enqueue(queue, book10)
    q.enqueue(queue, book1)
    q.enqueue(queue, book2)
    q.enqueue(queue, book8)
    q.enqueue(queue, book4)
    q.enqueue(queue, book7)
    q.enqueue(queue, book9)
    assert q.size(queue) == 10
예제 #16
0
    def test_enqueueElements(self):
        """
        """
        self.queue = q.newQueue(self.list_type)
        q.enqueue(self.queue, self.movie5)
        q.enqueue(self.queue, self.movie6)
        q.enqueue(self.queue, self.movie3)
        q.enqueue(self.queue, self.movie1)
        q.enqueue(self.queue, self.movie2)
        q.enqueue(self.queue, self.movie4)

        iterator = it.newIterator(self.queue)
        while it.hasNext(iterator):
            element = it.next(iterator)
            result = "".join(
                str(key) + ": " + str(value) + ",  "
                for key, value in element.items())
            print(result)
예제 #17
0
def test_infoElements():
    """
    Este test busca confirmar que los datos se almacenen de forma correcta y que
    sean los valores correctos en el orden apropiado de la estructura.
    """
    queue = q.newQueue(list_type)
    assert (q.size(queue) == 0)
    assert (q.isEmpty(queue))
    queue = q.newQueue(list_type)

    q.enqueue(queue, book5)
    q.enqueue(queue, book6)
    q.enqueue(queue, book3)
    q.enqueue(queue, book10)
    q.enqueue(queue, book1)
    q.enqueue(queue, book2)
    q.enqueue(queue, book8)
    q.enqueue(queue, book4)
    q.enqueue(queue, book7)
    q.enqueue(queue, book9)

    elem = q.peek(queue)
    assert (q.size(queue) == 10)
    assert (elem == book5)

    elem = q.dequeue(queue)
    assert (q.size(queue) == 9)
    assert (elem == book5)

    elem = q.dequeue(queue)
    assert (q.size(queue) == 8)
    assert (elem == book6)

    elem = q.peek(queue)
    assert (q.size(queue) == 8)
    assert (elem == book3)

    q.enqueue(queue, book9)
    assert (q.size(queue) == 9)
    elem = q.peek(queue)
    assert (elem == book3)
예제 #18
0
    def test_enqueueElements (self):
        """
        """
        self.queue = q.newQueue(self.list_type)
        q.enqueue  (self.queue, self.book5)
        q.enqueue  (self.queue, self.book6)
        q.enqueue  (self.queue, self.book3)
        q.enqueue  (self.queue, self.book10)
        q.enqueue  (self.queue, self.book1)
        q.enqueue  (self.queue, self.book2)
        q.enqueue  (self.queue, self.book8)
        q.enqueue  (self.queue, self.book4)
        q.enqueue  (self.queue, self.book7)
        q.enqueue  (self.queue, self.book9)

        iterator = it.newIterator(self.queue)
        while  it.hasNext(iterator):
            element = it.next(iterator)
            result = "".join(str(key) + ": " + str(value) + ",  " for key, value in element.items())
            print (result)