Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Prim's algorithm
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Time complexity == [[File:MAZE 30x20 Prim.ogv|thumb|Prim's algorithm has many applications, such as in the [[maze generation|generation]] of this maze, which applies Prim's algorithm to a randomly weighted [[grid graph]].]] The time complexity of Prim's algorithm depends on the data structures used for the graph and for ordering the edges by weight, which can be done using a [[priority queue]]. The following table shows the typical choices: {| class="wikitable" ! Minimum edge weight data structure !! Time complexity (total) |- | [[adjacency matrix]], searching || <math>O(|V|^2)</math> |- | [[binary heap]] and [[adjacency list]] || <math>O((|V| + |E|) \log |V|) = O(|E| \log |V|)</math> |- | [[Fibonacci heap]] and [[adjacency list]] || <math>O(|E| + |V| \log |V|)</math> |} A simple implementation of Prim's, using an [[adjacency matrix]] or an [[adjacency list]] graph representation and linearly searching an array of weights to find the minimum weight edge to add, requires [[Big-O notation|O]](<nowiki>|</nowiki>V<nowiki>|</nowiki><sup>2</sup>) running time. However, this running time can be greatly improved by using [[Heap (data structure)|heaps]] to implement finding minimum weight edges in the algorithm's inner loop. A first improved version uses a heap to store all edges of the input graph, ordered by their weight. This leads to an O(|E| log |E|) worst-case running time. But storing vertices instead of edges can improve it still further. The heap should order the vertices by the smallest edge-weight that connects them to any vertex in the partially constructed [[minimum spanning tree]] (MST) (or infinity if no such edge exists). Every time a vertex ''v'' is chosen and added to the MST, a decrease-key operation is performed on all vertices ''w'' outside the partial MST such that ''v'' is connected to ''w'', setting the key to the minimum of its previous value and the edge cost of (''v'',''w''). Using a simple [[binary heap]] data structure, Prim's algorithm can now be shown to run in time [[Big-O notation|O]](<nowiki>|</nowiki>E<nowiki>|</nowiki> log <nowiki>|</nowiki>V<nowiki>|</nowiki>) where <nowiki>|</nowiki>E<nowiki>|</nowiki> is the number of edges and <nowiki>|</nowiki>V<nowiki>|</nowiki> is the number of vertices. Using a more sophisticated [[Fibonacci heap]], this can be brought down to [[Big-O notation|O]](<nowiki>|</nowiki>E<nowiki>|</nowiki> + <nowiki>|</nowiki>V<nowiki>|</nowiki> log <nowiki>|</nowiki>V<nowiki>|</nowiki>), which is [[Asymptotic computational complexity|asymptotically faster]] when the graph is [[Dense graph|dense]] enough that <nowiki>|</nowiki>E<nowiki>|</nowiki> is [[Big-O notation#Family of Bachmann.E2.80.93Landau notations|Ο]](<nowiki>|</nowiki>V<nowiki>|</nowiki>), and [[linear time]] when |E| is at least |V| log |V|. For graphs of even greater density (having at least |V|<sup>''c''</sup> edges for some ''c'' > 1), Prim's algorithm can be made to run in linear time even more simply, by using a [[d-ary heap|''d''-ary heap]] in place of a Fibonacci heap.<ref name="tarjan83p77"/><ref>{{citation | last = Johnson | first = Donald B. | author-link = Donald B. Johnson | date = December 1975 | doi = 10.1016/0020-0190(75)90001-0 | issue = 3 | journal = Information Processing Letters | pages = 53β57 | title = Priority queues with update and finding minimum spanning trees | volume = 4}}.</ref> [[File:Prim's algorithm proof.svg|thumb|150px|Demonstration of proof. In this case, the graph ''Y<sub>1</sub>'' = ''Y'' β ''f'' + ''e'' is already equal to ''Y''. In general, the process may need to be repeated.]]
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Prim's algorithm
(section)
Add topic