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!
== Description == The algorithm may informally be described as performing the following steps: {{ordered list | Initialize a tree with a single vertex, chosen arbitrarily from the graph. | Grow the tree by one edge: Of the edges that connect the tree to vertices not yet in the tree, find the minimum-weight edge, and transfer it to the tree. | Repeat step 2 (until all vertices are in the tree). }} In more detail, it may be implemented following the [[pseudocode]] below. '''function''' Prim(vertices, edges) '''is''' '''for''' '''each''' vertex '''in''' vertices '''do''' cheapestCost[vertex] β β cheapestEdge[vertex] β null explored β empty set unexplored β set containing all vertices startVertex β any element of vertices cheapestCost[startVertex] β 0 '''while''' unexplored '''is''' not empty '''do''' // Select vertex in unexplored with minimum cost currentVertex β vertex in unexplored with minimum cheapestCost[vertex] unexplored.remove(currentVertex) explored.add(currentVertex) '''for''' '''each''' edge (currentVertex, neighbor) '''in''' edges '''do''' '''if''' neighbor '''in''' unexplored '''and''' weight(currentVertex, neighbor) < cheapestCost[neighbor] THEN cheapestCost[neighbor] β weight(currentVertex, neighbor) cheapestEdge[neighbor] β (currentVertex, neighbor) resultEdges β empty list '''for''' '''each''' vertex '''in''' vertices '''do''' '''if''' cheapestEdge[vertex] β null '''THEN''' resultEdges.append(cheapestEdge[vertex]) '''return''' resultEdges As described above, the starting vertex for the algorithm will be chosen arbitrarily, because the first iteration of the main loop of the algorithm will have a set of vertices in ''Q'' that all have equal weights, and the algorithm will automatically start a new tree in ''F'' when it completes a spanning tree of each connected component of the input graph. The algorithm may be modified to start with any particular vertex ''s'' by setting ''C''[''s''] to be a number smaller than the other values of ''C'' (for instance, zero), and it may be modified to only find a single spanning tree rather than an entire spanning forest (matching more closely the informal description) by stopping whenever it encounters another vertex flagged as having no associated edge. Different variations of the algorithm differ from each other in how the set ''Q'' is implemented: as a simple [[linked list]] or [[Array data structure|array]] of vertices, or as a more complicated [[priority queue]] data structure. This choice leads to differences in the [[time complexity]] of the algorithm. In general, a priority queue will be quicker at finding the vertex ''v'' with minimum cost, but will entail more expensive updates when the value of ''C''[''w''] changes.
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