What is the difference between Dijkstra and Prima algorithm?

Can someone tell me the difference between Dijkstra and Prim's algorithms? I know what each of the algorithms do. But they look the same to me. Dijkstra's algorithm stores the summation of the minimum cost minimums, while the Prim algorithm stores no more than one minimum cost margin. Is that not so?

+20
algorithm shortest-path
Dec 10
source share
5 answers

The Dijsktra algorithm finds the minimum distance from node i to all nodes (you specify i). Therefore, in return you get a tree of the minimum distance from node i.

The Primm algorithm gives you the minimum spanning tree for a given graph . A tree that connects all nodes, while the sum of all costs is minimal.

So, with Dijkstra you can go from the selected node to any other with the minimum cost , you will not get it with Prim

+37
Dec 10 '12 at 4:27
source share
— -

The only difference that I see is that the Prim algorithm contains the minimum cost, while the Dijkstra algorithm stores the total value from the original vertex to the current vertex.

Dijkstra gives you the path from the source node to the destination node, so that the cost is minimal. However, the Prim algorithm gives a minimal spanning tree, so that all nodes are connected and the total cost is minimal.

In simple words:

So, if you want to deploy a train to connect several cities, you should use Prim algo. But if you want to move from one city to another saving as much time as possible, you should use Dijkstra algo.

+20
Dec 10 '12 at 4:27
source share

Both can be implemented using exactly the same general algorithm:

Inputs: G: Graph s: Starting vertex (any for Prim, source for Dijkstra) f: a function that takes vertices u and v, returns a number Generic(G, s, f) Q = Enqueue all V with key = infinity, parent = null s.key = 0 While Q is not empty u = dequeue Q For each v in adj(u) if v is in Q and v.key > f(u,v) v.key = f(u,v) v.parent = u 

For Prim, pass f = w(u, v) and for Dijkstra pass f = u.key + w(u, v) .

Another interesting thing: Generic above can also implement Breadth First Search (BFS), although this will be redundant because an expensive priority queue is not required. To enable the above general algorithm in BFS, go f = u.key + 1 , which will be the same as forcing all weights to 1 to be forced (that is, BFS gives the minimum number of edges needed to go from point A to B).

Intuition

Here is one good way to think of a general algorithm: we start with two buckets A and B. First, put all your vertices in B so that A bucket is empty. Then we move one vertex from B to A. Now we look at all the edges from the vertices in A intersecting the vertices in B. We selected one edge using some criteria from these cross-edges and move the corresponding vertex from B to A. Repeat this process until B becomes empty.

Most likely, to implement this idea, it will maintain a priority queue of edges for vertices in A intersecting with B. Obviously, this would be unpleasant if the graph were not sparse. So the question may be, can we maintain a vertex priority queue? In fact, we can, since our solution, finally, is which vertex to choose from B.

Historical context

Interestingly, the general version of the methodology that underlies both algorithms conceptually grows old until 1930, even when there were no electronic computers.

The story begins with Otakar Borevka, who needs an algorithm for a family friend who is trying to figure out how to connect cities in the country of Moravia (now part of the Czech Republic) with minimal cost of electric lines. He published his algorithm in 1926 in a journal devoted to mathematics, since then Computer Science did not exist. This drew attention to Wojtec Järnik, who thought about improving the Borůvka algorithm and published it in 1930. In fact, he discovered the same algorithm that we now know as the Prim algorithm, which reopened it in 1957.

Regardless of all this, in 1956 Dijkstra needed to write a program to demonstrate the capabilities of a new computer developed by his institute. He thought it would be great if the computer found a connection between the two cities of the Netherlands. He developed the algorithm in 20 minutes. He created a graph of 64 cities with some simplifications (because his computer was 6-bit) and wrote the code for this 1956 computer. However, he did not publish his algorithm, because basically there were no journals for computer science, and he thought that this might not be very important. The following year, he learned about the problem of connecting the terminals of new computers, so that the length of the wires was minimized. He thought about this problem and rediscovered the Jarník / Prim algorithm, which again uses the same technique as the shortest path algorithm that he discovered a year ago. He noted that both of his algorithms were developed without the use of a pen or paper. In 1959, he published both algorithms in a document that is only 2 and a half pages long.

+15
Dec 30 '15 at 5:40
source share

The Dijkstra algorithm is a single-source shortest path problem between node i and j, but the Prim algorithm is a minimal spanning tree problem. This algorithm uses a programming concept called the "greedy algorithm"

If you check this concept, visit

+2
Dec 10
source share

Recently, the same question has bothered me, and I think I can share my understanding ...

I think that the key difference between these two algorithms (Dijkstra and Prim) is rooted in the problem that they are intended to solve, namely, the shortest path between the two nodes and the minimum spanning tree (MST). Formally, find the shortest path between the words: node s and t, and a rational requirement is to visit each edge of the graph no more than once. However, it does NOT require us to visit all node. The latter (MST) should force us to visit ALL nodes (no more than once) and with the same rational requirement to visit each edge no more than once.

Having said that, Dijkstra allows us to “make a shortcut” for so long that I can get from s to t without worrying about the consequences - as soon as I get to t, I am done! Although there is a path from s to t in MST, this path st is created taking into account all other nodes, therefore this path can be longer than the path st found by the Dijstra algorithm. Below is a brief example with three nodes:

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

Suppose each of the upper edges has a value of 2, and the lower edge has a value of 3, then Diictra will tell us that we take the lower path, since we do not care about the middle of the node. Prim, on the other hand, will give us back the MST with two upper edges, discarding the lower edge.

Such a difference also affects the subtle difference in implementations: in the Dijkstra algorithm, you need to have the stage of saving the book (for each node) to update the shortest path from s after absorbing a new node, while the Prim algorithm is not necessary.

0
Oct 19 '16 at 1:39 on
source share



All Articles