How does Dijkstra's Shortest Path Algorithm work with Priority Queues? - priority-queue

I have been reading online websites and everybody says that using a priority queue will make it good, but I don't understand what is used as the "priority" here.
At the very beginning, is the first item on the priority queue always the starting point node? If so, when we extract the starting node with distance 0, how do we get its neighbors from the priority queue?

A priority queue Q stores a set of distinct elements. Each element
x has an associated key x.key
When Dijkstra is based on a priority queue. Then we store the vertices In the queue whose distances from the source are yet to be settled, keyed on their current distance from the source.
Take a look at this pdf where the algorithm is based on the abstract data structure called a priority queue, which can be implemented using a binary heap.
http://www.cs.bris.ac.uk/~montanar/teaching/dsa/dijkstra-handout.pdf

Related

Is there a way to predefine a backlog queue in simpy?

I am exploring the use of Simpy to model the queue of elective surgery demand following Covid. Here I want to explore various strategies, such as number of theatres, on the cutting through the existing backlog. Is there any way to predefine a queue length and waiting time distribution in Simpy? I imagine I can create a source of patients to create the waiting list size and hold off serving them until I reach the required queue size and waiting list distribution, but wondering if there are any more elegant solutions.
You can put what ever timestamp you want on your backlog objects and added them directly to the queue at start up, but you will still need to delay your queue processing with a timeout to start it at the right time

Getting minimum element from maximum priority queue

What is the most efficient way to get the minimum element from a maximum priority queue?
Let's say i have created a generic priority queue. Now this queue contains cats and cats has a variable fish, which is the number of fishes the cat has eaten and i want to get the cat that has eaten the fewest fish and give her some more fish, then sort the priority queue again(which means i call swim() to get the fish that has eaten closer to the root). But since the priority queue is a max one( it has to be max , it can't be min) how can i get the cat that has eaten the fewest fishes?
Given a priority queue in which the top element is the largest, the only way to get the smallest item is to remove all of the elements from the queue. The last one you remove is the smallest item. The complexity of that approach is O(n log n). Plus you'll have to save the items you've popped and put them back into the queue after you've removed the last one.
It appears that you've implemented the priority queue as a binary max heap. If that's the case, then the smallest item will be a leaf, which by definition will be in the last n/2 items. So, if you have access to the backing array, you could search the last n/2 items for the smallest item. That's an O(n) operation.
If removing the smallest item is something you need to do on a regular basis, you might consider implementing a min-max heap.
Redefine the ordering property for fish.
If you're working in a language which uses the comparator concept, one way to do this is to write a comparator that reverses the default ordering property for fish. Another alternative is to store the negative of the fish count when creating/pushing a cat. The logic could be built into cat objects, so the user only sees and manipulates positive quantities of fish, but they are internally stored as negative values.

Fast priority queue with incremental updates

I am trying to write a load-balancer in Haskell with leastconn strategy (partly for fun..). I need a priority queue where only the following operations are required to be 'fast':
read minimum key
+1 on minimum key
-1 on any key
If I had an imperative language with pointers, I would probably come with:
Head
|
Priority 0 -> Item <-> Item <-> Item <-> Item
|
Priority 1 -> Item <-> Item
|
Priority 4 -> Item <-> Item <-> Item
Priorities are connected using a doubly linked list, the items for every priority too. Each Item contains a link to the head Priority. This structure would have complexity:
O(1) for read minimum key - Take first from queue under head
O(1) for +1 - remove first item under first priority, insert it on lower level (possibly creating a new level)
O(1) for -1 - provided we have a pointer to the item, we can immediately access the Priority, remove the item from doubly linked list and insert it into a different one
Is there some (functional?) data structure that would behave approximately the same? The number of items would be approximately at most a couple of hundreds.
It sounds to me like the common structure which suits your needs is a Priority Search Queue as described in Hinze (2001). One of the better hackage packages providing such a structure is here: http://hackage.haskell.org/package/psqueues
This is perhaps not tuned exactly for your workflow, but it certainly isn't shabby!

Priority Queue cost?

I wrote a program to calculate the median of a running sequence using two priority queues min and max. By comparing the input with the current median, it is put on a queue depending on its value. By comparing queue sizes the median can is determined.
I have to hypothesize and test the execution time of the program. From testing and plotting the graph for different input sizes I can see the execution time is indeed linear.
This surprises me, the reason being is I thought the bigger the queues the longer it takes to place the correct value at the root ? or is the size of the queue irrelevant to placing the correct value at the root (as it seems in my results).
If someone could elaborate on the cost of priority queue that would be very great.

Reverse a linked list as big as having 7 million nodes efficiently using Threads

I was asked this question to reverse a singly linked list as big as having 7 million nodes by using threads efficiently. Using recursion doesn't look feasible if there are so many nodes so I opted for divide and conquer where in each thread be given a chunk of linked list which gets reversed by just making the node pointer point back to previous node by store a reference to current, future and past node and later adding it with reversed chunks from other threads. But the interviewer insisted that the size of the link list is not know, and you can do it without finding the size in an efficient manner. Well I couldn't figure it out , how would you go about it ?
Such questions I like to implement "top-down":
Assume that you already have a Class that implement Runnable or extends Thread out of which you can create instances and run, each instance receives two parameters: a pointer to a Node in the List and number of Nodes to reverse
Your main traverse all 7 million nodes and "marks" the starting points for your threads, say we have 7 threads, the marked points will be: 1, 1,000,000, 2,000,000,... save the marked nodes in an array or whichever data-structure you like
After you finished "marking the starting points, create the threads and give each one of them its starting point and the counter 1,000,000
After all the threads are done, "glue" each of the marking points to point back to the last node of the previous thread (which should be saved in another "static" ordered data-structure).
Now that we have a plan - all that's left to do is implement a (considerably easy) algorithm that, give the number N and a Node x, it will reverse the next N nodes (including x) in a singly linked list :)

Resources