Questions tagged [prims-algorithm]

Prim's algorithm is a fast algorithm for computing minimum spanning trees.

Prim's algorithm is a greedy algorithm (a problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum) that efficiently finds the minimum spanning tree for the connected weighted undirected graphs. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.

Although this algorithm may be modifed to find minimum spanning forest for unconnected graphs, other methods such as Kruskal's algorithm are recommended. Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on adding new nodes to the spanning tree from the given graph.

Below image shows the working of Prim's algorithm.

enter image description here

Image Reference : Wikipedia

187 questions
210
votes
10 answers

When should I use Kruskal as opposed to Prim (and vice versa)?

I was wondering when one should use Prim's algorithm and when Kruskal's to find the minimum spanning tree? They both have easy logics, same worst cases, and only difference is implementation which might involve a bit different data structures. So…
105
votes
15 answers

Difference between Prim's and Dijkstra's algorithms?

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 the exact difference?
23
votes
2 answers

Why can't Prim's or Kruskal's algorithms be used on a directed graph?

Prim's and Kruskal's algorithms are used to find the minimum spanning tree of a graph that is connected and undirected. Why can't they be used on a graph that is directed?
user1472747
  • 499
  • 3
  • 7
  • 22
20
votes
3 answers

How to implement Prim's algorithm with a Fibonacci heap?

I know Prim's algorithm and I know its implementation but always I skip a part that I want to ask now. It was written that Prim's algorithm implementation with Fibonacci heap is O(E + V log(V)) and my question is: what is a Fibonacci heap in…
18
votes
4 answers

Time Complexity of Prims Algorithm?

I found the time complexity of Prims algorithm everywhere as O((V + E) log V) = E log V. But as we can see the algorithm: It seems like the time complexity is O(V(log V + E log V)). But if its time complexity is O((V + E) log V). Then the nesting…
Sonali
  • 531
  • 3
  • 7
  • 11
16
votes
5 answers

Select random element in an unordered_map

I define an unordered_map like this: std::unordered_map edges; Is there a efficient way to choose a random Edge from the unordered_map edges ?
Joe Cool
  • 199
  • 2
  • 7
12
votes
2 answers

How to update element priorities in a heap for Prim's Algorithm?

I am studying Prim's Algorithm. There is a part within the code the next vertex across the cut will be coming to the set of the vertices belonging to the MST. While doing that, we also have to 'update all vertices in the other set which are…
11
votes
6 answers

Applications of Kruskal and Prim's algorithms

Could anyone please give some applications of the two algorithms, where and which applications they can be used for?
vini
  • 4,198
  • 23
  • 72
  • 152
11
votes
3 answers

Prim's Algorithm Time Complexity

I was looking at the Wikipedia entry for Prim's algorithm and I noticed that its time complexity with an adjacency matrix is O(V^2) and its time complexity with a heap and adjacency list is O(E lg(V)) where E is the number of edges and V is the…
kevmo314
  • 3,748
  • 4
  • 29
  • 42
9
votes
1 answer

Creating a 'hard' maze using Prim's Algorithm

I am using Prim's Algorithm to create a maze. I have successfully done so, but I am now trying to make it 'harder' by changing the way that it selects potential cells to be added to the maze. In my mind, 'hard' lies between two extremes: Extreme #1…
asdfjkl
  • 221
  • 1
  • 8
9
votes
3 answers

Why do Kruskal and Prim MST algorithms have different runtimes for sparse and dense graphs?

I am trying to understand why Prim and Kruskal have different time complexities when it comes to sparse and dense graphs. After using a couple of applets that demonstrate how each works, I am still left a little confused about how the density of…
tommy
  • 373
  • 3
  • 5
  • 11
9
votes
3 answers

Prim's MST: Does the start node matter?

I intuitively feel that if one is using Prim's algorithm to find a graph's minimum spanning tree, it doesn't matter which root node is picked - the resultant MST will have the same weight regardless. Is this correct?
Nick Heiner
  • 108,809
  • 177
  • 454
  • 689
8
votes
2 answers

Complexity of Prims Algorithm using Priority Queue?

I am using an adjacency matrix, priority queue is the data structure. By my calculation, complexity is V^3 log V: While loop: V Checking adjacent Vertices: V Checking the queue if the entry is already present, and updating the same: V log v But, I…
6
votes
2 answers

Prims Algorithm Total Running time!

"Thus, the total time for Prim's algorithm is O(V lg V + E lg V) = O(E lg V), which is asymptotically the same as for our implementation of Kruskal's algorithm." From http://serverbob.3x.ro/IA/DDU0137.html But why is O(V lg V + E lg V) = O(E lg V) …
6
votes
1 answer

Prim's algorithm for MST, Adjacency List Implementation in C

I have this question for my programming class which I have been struggling to complete for the past day ... and I have no real idea what to do. I understand the basic concept of Prim's algorithm: 1. Start at an arbitrary node (the first node will…
user1762676
  • 73
  • 1
  • 5
1
2 3
12 13