Difference between Prim and Dijkstra graph algorithm
-
I'm reading graph algorithms from Cormen book. Below is pseudocode from that book Prim algorithm for MST MST-PRIM (G, w, r) for each u in G.V u.key = infinity u.p = NIL r.key = 0 Q = G.V while Q neq null u = EXTRACT-MIN(Q) for each v in G.Adj[u] if (v in Q) and (w(u,v) < v.key) v.p = u v.key = w(u,v) Dijkstra algorithm to find single source shortest path. INITIALIZE-SINGLE-SOURCE (G,s) for each vertex v in G.V v.d = infinity v.par = NIL s.d = 0 DIJKSTRA (G, w, s) INITIALIZE-SINGLE-SOURCE(G,s) S = NULL Q = G.V while Q neq null u = EXTRACT-MIN(Q) S = S U {u} for each vertex v in G.Adj[u] RELAX(u,v,w) My question is, why we are checking if vertex belongs to Q (v in Q), i.e. that vertex doesn't belong to tree, whereas in Dijkstra algorithm we are not checking for that. Any reason, why?
-
Answer:
The algorithms called https://en.wikipedia.org/wiki/Prim%27s_algorithm and https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm solve different problems in the first place. 'Prim' finds a minimum spanning tree of an undirected graph, while 'Disjkstra' solves the single-source shortest path problem for directed graphs with nonnegative edge weights.
venkysmarty at Stack Overflow Visit the source
Other answers
In both algorithms queue Q contains all vertices that are not 'done' yet, i.e. white and gray according to common terminology (see http://www.cs.cornell.edu/courses/cs2112/2014fa/lectures/lecture.html?id=ssp). In Dijkstra's algorithm, the black vertex cannot be relaxed, because if it could, that would mean that its distance was not correct beforehand (contradicts with property of black nodes). So there is no difference whether you check v in Q or not. In Prim's algorithm, it is possible to find an edge of small weight, that leads to already black vertex. That's why if we do not check v in Q, then the value in vertex v can change indeed. Normally, it does not matter, because we never read min-weight value for black vertices. However, your pseudocode is using https://en.wikipedia.org/wiki/Binary_heap data structure. In this case each modification of vertex values must be accompanied with DecreaseKey. Clearly, it is not valid to call DecreaseKey for black vertices, because they are not in heap. That's why I suppose author decided to check for v in Q explicitly. Speaking generally, the codes for Dijkstra's and Prim's algorithms are usually absolutely same, except for a minor difference: Prim's algorithm checks w(u, v) for being less than D(v) in RELAX. Dijkstra's algorithm checks D(u) + w(u, v) for being less D(v) in RELAX.
stgatilov
Related Q & A:
- How to find contour lines for Appel's Hidden Line Removal Algorithm?Best solution by Computer Science
- How does Lowe compute the "repeatability" of his SIFT Algorithm?Best solution by Stack Overflow
- How to Code Encryption Algorithm in Java?Best solution by code2learn.com
- How to make a dijkstra longest path?Best solution by stackoverflow.com
- What is an Ant colony optimization algorithm?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.