Difference between Prim's and Dijkstra's algorithms?

前端 未结 15 1800
既然无缘
既然无缘 2020-12-07 06:53

What is the exact difference between Dijkstra\'s and Prim\'s algorithms? I know Prim\'s will give a MST but the tree generated by Dijkstra will also be a MST. Then what is

相关标签:
15条回答
  • Prim and Dijkstra algorithms are almost the same, except for the "relax function".

    Prim:

    MST-PRIM (G, w, r) {
        for each key ∈ G.V
            u.key = ∞
            u.parent = NIL
        r.key = 0
        Q = G.V
    
        while (Q ≠ ø)
            u = Extract-Min(Q)
            for each v ∈ G.Adj[u]
                if (v ∈ Q)
                    alt = w(u,v)    <== relax function, Pay attention here
                    if alt < v.key
                        v.parent = u
                        v.key = alt
    }
    

    Dijkstra:

    Dijkstra (G, w, r) {
        for each key ∈ G.V
            u.key = ∞
            u.parent = NIL
        r.key = 0
        Q = G.V
    
        while (Q ≠ ø)
            u = Extract-Min(Q)
            for each v ∈ G.Adj[u]
                if (v ∈ Q)
                    alt = w(u,v) + u.key  <== relax function, Pay attention here
                    if alt < v.key
                        v.parent = u
                        v.key = alt
    }
    

    The only difference is pointed out by the arrow, which is the relax function.

    • The Prim, which searches for the minimum spanning tree, only cares about the minimum of the total edges cover all the vertices. The relax function is alt = w(u,v)
    • The Dijkstra, which searches for the minimum path length, so it cares about the edge accumulation. The relax function is alt = w(u,v) + u.key
    0 讨论(0)
  • 2020-12-07 07:40

    Dijsktra's algorithm finds the minimum distance from node i to all nodes (you specify i). So in return you get the minimum distance tree from node i.

    Prims algorithm gets you the minimum spaning tree for a given graph. A tree that connects all nodes while the sum of all costs is the minimum possible.

    So with Dijkstra you can go from the selected node to any other with the minimum cost, you don't get this with Prim's

    0 讨论(0)
  • 2020-12-07 07:45

    I was bothered with the same question lately, and I think I might share my understanding...

    I think the key difference between these two algorithms (Dijkstra and Prim) roots in the problem they are designed to solve, namely, shortest path between two nodes and minimal spanning tree (MST). The formal is to find the shortest path between say, node s and t, and a rational requirement is to visit each edge of the graph at most once. However, it does NOT require us to visit all the node. The latter (MST) is to get us visit ALL the node (at most once), and with the same rational requirement of visiting each edge at most once too.

    That being said, Dijkstra allows us to "take shortcut" so long I can get from s to t, without worrying the consequence - once I get to t, I am done! Although there is also a path from s to t in the MST, but this s-t path is created with considerations of all the rest nodes, therefore, this path can be longer than the s-t path found by the Dijstra's algorithm. Below is a quick example with 3 nodes:

                                      2       2  
                              (s) o ----- o ----- o (t)     
                                  |               |
                                  -----------------
                                          3
    

    Let's say each of the top edges has the cost of 2, and the bottom edge has cost of 3, then Dijktra will tell us to the take the bottom path, since we don't care about the middle node. On the other hand, Prim will return us a MST with the top 2 edges, discarding the bottom edge.

    Such difference is also reflected from the subtle difference in the implementations: in Dijkstra's algorithm, one needs to have a book keeping step (for every node) to update the shortest path from s, after absorbing a new node, whereas in Prim's algorithm, there is no such need.

    0 讨论(0)
提交回复
热议问题