concept priority queue in category algorithms

appears as: priority queues, priority queue, A priority queue, priority queue
Algorithms and Data Structures in Action

This is an excerpt from Manning's book Algorithms and Data Structures in Action.

In appendix C we also clarify how there is a difference between abstract data structures and concrete data structures. The former includes points (a) and (b) above, describing at a high level how clients will interact with it and the results and performance of operations. The latter builds on the principles and API expressed by the abstract description, adding a concrete implementation for its structure and algorithms (points (c) and (d)).

Figure 7.13 Using a synchronized implementation for the cache, we can avoid race conditions. In the example shown in figure 7.12, a synchronized data structure requires any thread to acquire a lock on it (B) before being able to write a value (C). If another thread tries to acquire the lock while it is still in use by thread N, it will be left waiting till the lock is released (D).
After that (not shown in the figure), the waiting thread can acquire the lock, or possibly compete with other threads trying to acquire it.
Listing 14.2 The bfs method
function bfs(graph, start, isGoal)                        #1
  queue ← new Queue()                            #2
  queue.insert(start)                            #3
  distances ← new HashTable()                        #4
  parents ← new HashTable()                            #5
  for v in graph.vertices do                        #6
    distances[v] ← inf
    parents[v] ← null
  distances[start] ← 0                            #7
  while not queue.empty() do                        #8
    v ← queue.dequeue()                            #9
    if isGoal(v) then                            #10
      return (v, parents)
    for e in graph.adjacencyList[v] do                    #11
      u ← e.dest
      if distances[u] == inf then                        #12
        distances[u] ← distances[v] + 1                    #13
        parents[u] ← v
        queue.enqueue(u)
   return (null, parents)                            #14
Classic Computer Science Problems in Python

This is an excerpt from Manning's book Classic Computer Science Problems in Python.

To pick the state on the frontier with the lowest f(n), an A* search uses a priority queue as the data structure for its frontier. A priority queue keeps its elements in an internal order, such that the first element popped out is always the highest-priority element. (In our case, the highest-priority item is the one with the lowest f(n).) Usually this means the internal use of a binary heap, which results in O(lg n) pushes and O(lg n) pops.

Python’s standard library contains heappush() and heappop() functions that will take a list and maintain it as a binary heap. We can implement a priority queue by building a thin wrapper around these standard library functions. Our PriorityQueue class will be similar to our Stack and Queue classes, with the push() and pop() methods modified to use heappush() and heappop().

The first node pushed onto the priority queue contains the root vertex.

while not pq.empty:
    u: int = pq.pop().vertex # explore the next closest vertex
    dist_u: float = distances[u] # should already have seen it


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":9}]]"}
!@%STYLE%@!

We keep running Dijkstra’s algorithm until the priority queue is empty. u is the current vertex we are searching from, and dist_u is the stored distance for getting to u along known routes. Every vertex explored at this stage has already been found, so it must have a known distance.

To run Jarník’s algorithm efficiently, a priority queue is used. Every time a new vertex is added to the minimum spanning tree, all of its outgoing edges that link to vertices outside the tree are added to the priority queue. The lowest-weight edge is always popped off the priority queue, and the algorithm keeps executing until the priority queue is empty. This ensures that the lowest-weight edges are always added to the tree first. Edges that connect to vertices already in the tree are ignored when they are popped.