Beispiel #1
0
 def test_init_with_list(self):
     q = PriorityQueue()
     q.enqueue('B', 3)
     q.enqueue('C', 5)
     q.enqueue('A', 1)
     assert q.front() == 'A'
     assert q.length() == 3
     assert q.is_empty() is False
Beispiel #2
0
 def test_length(self):
     pq = PriorityQueue()
     assert pq.length() == 0
     pq.enqueue('A', 1)
     assert pq.length() == 1
     pq.enqueue('B', 2)
     assert pq.length() == 2
     assert pq.dequeue() == (1, 'A')
     assert pq.length() == 1
     assert pq.dequeue() == (2, 'B')
     assert pq.length() == 0
Beispiel #3
0
 def test_length(self):
     q = PriorityQueue()
     assert q.length() == 0
     q.enqueue('A', 5)
     assert q.length() == 1
     q.enqueue('B', 1)
     assert q.length() == 2
     q.dequeue()
     assert q.length() == 1
     q.dequeue()
     assert q.length() == 0
Beispiel #4
0
 def test_front(self):
     q = PriorityQueue()
     assert q.front() is None
     q.enqueue('A', 6)
     assert q.front() == 'A'
     q.enqueue('B', 10)
     assert q.front() == 'A'
     q.dequeue()
     assert q.front() == 'B'
     q.dequeue()
     assert q.front() is None
Beispiel #5
0
 def test_is_empty(self):
     p = PriorityQueue()
     assert p.is_empty() is True
     p.enqueue('A', 1)
     assert p.is_empty() is False
     p.enqueue('B', 1)
     assert p.is_empty() is False
     p.dequeue()
     assert p.is_empty() is False
     p.dequeue()
     assert p.is_empty() is True
Beispiel #6
0
 def test_front(self):
     pq = PriorityQueue()
     assert pq.front() is None
     pq.enqueue('A', 1)
     assert pq.front() == (1, 'A')
     pq.enqueue('B', 2)
     assert pq.front() == (1, 'A')
     pq.dequeue()
     assert pq.front() == (2, 'B')
     pq.dequeue()
     assert pq.front() is None
Beispiel #7
0
 def test_dequeue(self):
     q = PriorityQueue()
     q.enqueue('A', 1)
     q.enqueue('B', 2)
     q.enqueue('C', 3)
     assert q.dequeue() == 'A'
     assert q.length() == 2
     assert q.dequeue() == 'B'
     assert q.length() == 1
     assert q.dequeue() == 'C'
     assert q.length() == 0
     assert q.is_empty() is True
     with self.assertRaises(ValueError):
         q.dequeue()
Beispiel #8
0
def djkistra(sourceId: int, vertices: list, vertexDict: dict, edgeList: list):
    sourcePair = Pair(sourceId)

    visited = set()
    unvisited = PriorityQueue(contents=[sourcePair])

    # build mapping labels -> vertexId & vertexId -> labels
    labelsDict = {
        int(v.attributes["label"].value): int(v.attributes["vertexId"].value)
        for v in vertices
    }
    labels = [labelsDict[i] for i in range(len(labelsDict))]
    previousLabelsDict = {v: k for k, v in labelsDict.items()}

    vertexDict[sourceId].setCost(0)

    vertexDict[sourceId].setPrevious(sourceId)

    while not unvisited.isEmpty():
        currentPair = unvisited.dequeue()

        visited.add(currentPair.getVertexId())

        currentVertex = vertexDict[currentPair.getVertexId()]
        # grab adjacents.
        adjacents = currentVertex.getAdjacents(edgeList)

        for e in adjacents:
            dist = vertexDict[currentVertex.vertexId].getCost() + e.weight
            for vertex in [e.v1, e.v2]:
                if vertex not in visited:
                    if vertexDict[vertex].getCost() > dist:
                        vertexDict[vertex].setCost(dist)
                        vertexDict[vertex].setPrevious(
                            currentVertex.getVertexId())
                        # heapq.heappush(unvisited, Pair(vertex, dist))
                        unvisited.enqueue(Pair(vertex, dist))

    for i in range(len(visited)):
        print("Vertex:")
        print("  label: {}".format(i))
        print("  cost: {:.2f}".format(vertexDict[labels[i]].getCost()))
        print("  previous: {}\n".format(
            previousLabelsDict[vertexDict[labelsDict[i]].getPrevious()]))

    return labelsDict
Beispiel #9
0
def dijkstra(source, graph):
    pQueue = PriorityQueue()
    graph[source]['dist'] = 0

    for v in graph:
        pQueue.enqueue(v, graph[v]['dist'])

    while not pQueue.isEmpty():
        u = pQueue.dequeue()
        baseDist = graph[u]['dist']
        for w in graph[u]['edgeTo']:
            edgeLen = graph[u]['edgeTo'][w]
            newDist = baseDist + edgeLen
            currentDist = graph[w]['dist']
            if newDist < currentDist:
                graph[w]['dist'] = newDist
                pQueue.changePriority(w, newDist)

    distanceList = []
    for v in graph:
        distanceList.append((v, graph[v]['dist']))

    return distanceList
def Astar(puzzle8, came_from):
    frontier = PriorityQueue()
    cost_so_far = {}
    frontier.enqueue(puzzle8, 0)
    cost_so_far[puzzle8.ToString()] = puzzle8.cost
    came_from[puzzle8.ToString()] = None
    while not frontier.is_empty():
        puzzle8 = frontier.dequeue()
        
        if puzzle8.isGoal():
            return puzzle8
        else:
            moves = puzzle8.getAllMoves()
            for move in moves:
                newpuzzle8 = puzzle8.clone()
                newpuzzle8.move(*move)
                new_cost = newpuzzle8.cost
                if cost_so_far.get(newpuzzle8.ToString()) == None or \
                   new_cost < cost_so_far[newpuzzle8.ToString()]:
                    cost_so_far[newpuzzle8.ToString()] = new_cost
                    priority = new_cost + newpuzzle8.heuristics
                    frontier.enqueue(newpuzzle8, priority)
                    came_from[newpuzzle8.ToString()] = puzzle8
    return None
Beispiel #11
0
 def test_enqueue(self):
     q = PriorityQueue()
     q.enqueue('A', 3)
     assert q.front() == 'A'
     assert q.length() == 1
     q.enqueue('B', 1)
     assert q.front() == 'B'
     assert q.length() == 2
     q.enqueue('C', 4)
     assert q.front() == 'B'
     assert q.length() == 3
     assert q.is_empty() is False
Beispiel #12
0
 def test_enqueue(self):
     pq = PriorityQueue()
     pq.enqueue('B', 2)
     assert pq.front() == (2, 'B')
     assert pq.length() == 1
     pq.enqueue('A', 1)
     assert pq.front() == (1, 'A')
     assert pq.length() == 2
     pq.enqueue('C', 3)
     assert pq.front() == (1, 'A')
     assert pq.length() == 3
     assert pq.is_empty() is False
Beispiel #13
0
 def test_front(self):
     p = PriorityQueue()
     assert p.front() is None
     p.enqueue('A', 2)
     assert p.front() == 'A'
     p.enqueue('B', 2)
     assert p.front() == 'A'
     p.enqueue('C', 1)
     assert p.front() == 'C'
     p.dequeue()
     assert p.front() == 'A'
     p.dequeue()
     assert p.front() == 'B'
Beispiel #14
0
 def test_length(self):
     p = PriorityQueue()
     p.enqueue('A', 1)
     assert p.length() == 1
     p.enqueue('B', 1)
     assert p.length() == 2
     p.enqueue('C', 2)
     assert p.length() == 3
     p.dequeue()
     assert p.length() == 2
     p.dequeue()
     assert p.length() == 1
     p.dequeue()
     assert p.length() == 0
Beispiel #15
0
 def test_push_pop(self):
     p = PriorityQueue()
     assert p.heap.items == []
     p.enqueue('A', 2)
     assert p.heap.items == [(2, 'A')]
     p.enqueue('B', 2)
     assert p.heap.items == [(2, 'A'), (2, 'B')]
     p.enqueue('C', 1)
     assert p.heap.items == [(1, 'C'), (2, 'B'), (2, 'A')]
     p.enqueue('D', 1)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B')]
     p.enqueue('E', 3)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B'),
                             (3, 'E')]
     p.enqueue('F', 2)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B'),
                             (3, 'E'), (2, 'F')]
     assert p.push_pop('G', 1) == 'C'
     assert p.heap.items == [(1, 'D'), (1, 'G'), (2, 'A'), (2, 'B'),
                             (3, 'E'), (2, 'F')]
     assert p.push_pop('H', 2) == 'D'
     assert p.heap.items == [(1, 'G'), (2, 'B'), (2, 'A'), (2, 'H'),
                             (3, 'E'), (2, 'F')]
     assert p.push_pop('I', 3) == 'G'
     assert p.heap.items == [(2, 'A'), (2, 'B'), (2, 'F'), (2, 'H'),
                             (3, 'E'), (3, 'I')]
     assert p.push_pop('J', 4) == 'A'
     assert p.heap.items == [(2, 'B'), (2, 'H'), (2, 'F'), (4, 'J'),
                             (3, 'E'), (3, 'I')]
     assert p.push_pop('K', 1) == 'B'
     assert p.heap.items == [(1, 'K'), (2, 'H'), (2, 'F'), (4, 'J'),
                             (3, 'E'), (3, 'I')]
     assert p.push_pop('L', 3) == 'K'
     assert p.heap.items == [(2, 'F'), (2, 'H'), (3, 'I'), (4, 'J'),
                             (3, 'E'), (3, 'L')]
Beispiel #16
0
 def test_enqueue(self):
     p = PriorityQueue()
     assert p.heap.items == []
     p.enqueue('A', 2)
     assert p.heap.items == [(2, 'A')]
     p.enqueue('B', 2)
     assert p.heap.items == [(2, 'A'), (2, 'B')]
     p.enqueue('C', 1)
     assert p.heap.items == [(1, 'C'), (2, 'B'), (2, 'A')]
     p.enqueue('D', 1)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B')]
     p.enqueue('E', 3)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B'),
                             (3, 'E')]
     p.enqueue('F', 2)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B'),
                             (3, 'E'), (2, 'F')]
Beispiel #17
0
 def test_dequeue(self):
     p = PriorityQueue()
     assert p.heap.items == []
     p.enqueue('A', 2)
     assert p.heap.items == [(2, 'A')]
     p.enqueue('B', 2)
     assert p.heap.items == [(2, 'A'), (2, 'B')]
     p.enqueue('C', 1)
     assert p.heap.items == [(1, 'C'), (2, 'B'), (2, 'A')]
     p.enqueue('D', 1)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B')]
     p.enqueue('E', 3)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B'),
                             (3, 'E')]
     p.enqueue('F', 2)
     assert p.heap.items == [(1, 'C'), (1, 'D'), (2, 'A'), (2, 'B'),
                             (3, 'E'), (2, 'F')]
     assert p.dequeue() == 'C'
     assert p.heap.items == [(1, 'D'), (2, 'B'), (2, 'A'), (2, 'F'),
                             (3, 'E')]
     assert p.dequeue() == 'D'
     assert p.heap.items == [(2, 'A'), (2, 'B'), (3, 'E'), (2, 'F')]
     assert p.dequeue() == 'A'
     assert p.heap.items == [(2, 'B'), (2, 'F'), (3, 'E')]
     assert p.dequeue() == 'B'
     assert p.heap.items == [(2, 'F'), (3, 'E')]
     assert p.dequeue() == 'F'
     assert p.heap.items == [(3, 'E')]
     assert p.dequeue() == 'E'
     assert p.heap.items == []
     with self.assertRaises(ValueError):
         p.dequeue()
class PQueueTestCase(unittest.TestCase):
    '''Test creation and use of a priority queue.'''
    
    def setUp(self):
        self.pq = PriorityQueue()
        self.r1 = Rectangle(0, 150, 200, 50, media.forestgreen, 1)
        self.o1= Oval(120, 160, 60, 60, media.white, 4)
        self.o2 = Oval(120, 115, 45, 45, media.yellow, 3)
        self.o3 = Oval(120, 80, 30, 30, media.orange, 2)

    def tearDown(self):
        self.pq = None
        self.r1 = None
        self.o1 = None
        self.o2 = None
        self.o3 = None
            
    def testSize(self):
        assert self.pq.size() == 0, 'mismatch in predicted pqueue size'
        
    def testEnqueueSize(self):
        self.pq.enqueue(self.r1)
        assert self.pq.size() == 1, \
               'mismatch in pqueue size after enqueue'

    def testDequeue(self):
        self.pq.enqueue(self.r1)
        result = self.pq.dequeue()
        assert result is self.r1, \
               'mismatch in dequeued value'

    def testMultipleEnqueueSize(self):
        self.pq.enqueue(self.o1)
        self.pq.enqueue(self.o2)
        self.pq.enqueue(self.o3)
        assert self.pq.size() == 3, \
               'mismatch in pqueue size after enqueues'

    def testMultipleDequeue(self):
        self.pq.enqueue(self.o1)
        self.pq.enqueue(self.o2)
        self.pq.enqueue(self.o3)
        result = self.pq.dequeue()
        assert result is self.o3, 'mismatch in dequeuing correct value'