![]() ![]() Finally, we will see how to implement priority queue. The above example shows, that the most recently put into the queue is removed by get.ĥ. The above example uses a single thread to show how the elements are removed from the queue in the same order in which they are inserted.ģ. # if not empty get the numbers from queueĢ. If you do not, you may want to read some of the references before continuing.ġ. Note : This discussion assumes you already understand the general nature of a queue. ![]() Locking is generally handled for the caller. between creator and consumer threads safely. Queues can be used to pass data or any wide range of information e.g. It would be better to use a collections.namedtuple: HeapEntry = namedtuple('HeapEntry', 'distance node parent')Īnd then you can write: current_node = heappop(h).The queue module provides a first-in, first-out (FIFO), Last-in, First out (LIFO) data structure suitable for multi-threaded programming. The code for getting data out of these entries is hard to understand because the tuple lookup doesn't have any distinctive meaning: curr_nd = hq.heappop(h) The heap entries are data structures of the form: (distance, (node, parent)) This line assigns a variable dst that is not used anywhere: for nd, dst in enumerate(adj_matrix):Īn improvement would be to write: for node, distance in enumerate(adj_matrix):Īnd then you could use distance instead of adj_matrix, saving a couple of indexing operations each time. Instead of representing the set of visited nodes as a dictionary mapping nodes to True, use a set. It would be better to construct it in the way that you use it: # For each node, the shortest distance from src found so far.īest_distance = * len(adj_matrix) But the construction of curr_dist uses AP to get the nodes. The variable lv is used only once, so you could just inline its value at the point of use.īecause the graph is given as an adjacency matrix (and because of the enumerate line), we know that the nodes can be labelled with consecutive integers. Similarly, instead of importing a module and giving it a hard-to-read alias: import heapq as hqĬonsider importing the names you need: from heapq import heappop, heappush There is no danger of running out of vowels, so nothing will go wrong if you write node and distance instead. Names like nd and lv and dst are hard to read. (Or else you have to remember to reset these variables, which is inconvenient and easy to forget.) It also means that the code can't be used from multiple threads. This means that you can only call prims_mst once - on the second call, the global variables have the wrong initial values. What does prims_mst do? What does it return? What arguments should I pass? I think that if you had stopped and written a docstring then you might have spotted the problem in section 1. So the while loop is executed \$O(n^2)\$ times, and on each iteration of the while loop the inner for loop takes \$O(n)\$, giving a runtime of \$O(n^3)\$ overall.īut with an adjacency matrix it is possible to implement Prim's algorithm in \$O(n^2)\$ (see Wikipedia). In particular, the \$n\$th node to be visited can be added to the queue up to \$n-1\$ times (because a new and better route might be discovered via any of the previous \$n-1\$ nodes to be visited). This is becasue a node is added to the queue every time a new route is discovered to that node that is shorter than the previously known best route. The key thing to spot about the code in the post is that a node might get added to the queue more than once. (It could certainly be modified to construct the tree, but you have to actually do it!) 2. But the code in the post does not construct a tree. The code in the post does not implement Prim's algorithm! Prim's algorithm constructs a minimum spanning tree. ![]()
0 Comments
Leave a Reply. |