Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Prim's algorithm

From Wikipedia, the free encyclopedia
Method for finding minimum spanning trees
A demo for Prim's algorithm based on Euclidean distance

Incomputer science,Prim's algorithm is agreedy algorithm that finds aminimum spanning tree for aweightedundirected graph. This means it finds a subset of theedges that forms atree that includes everyvertex, where the total weight of all theedges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex.

The algorithm was developed in 1930 byCzech mathematicianVojtěch Jarník[1] and later rediscovered and republished bycomputer scientistsRobert C. Prim in 1957[2] andEdsger W. Dijkstra in 1959.[3] Therefore, it is also sometimes called theJarník's algorithm,[4]Prim–Jarník algorithm,[5]Prim–Dijkstra algorithm[6]or theDJP algorithm.[7]

Other well-known algorithms for this problem includeKruskal's algorithm andBorůvka's algorithm.[8] These algorithms find the minimum spanning forest in a possibly disconnected graph; in contrast, the most basic form of Prim's algorithm only finds minimum spanning trees in connected graphs. However, running Prim's algorithm separately for eachconnected component of the graph, it can also be used to find the minimum spanning forest.[9] In terms of their asymptotictime complexity, these three algorithms are equally fast forsparse graphs, but slower than other more sophisticated algorithms.[7][6]However, for graphs that are sufficiently dense, Prim's algorithm can be made to run inlinear time, meeting or improving the time bounds for other algorithms.[10]

Prim's algorithm starting at vertex A. In the third step, edges BD and AB both have weight 2, so BD is chosen arbitrarily. After that step, AB is no longer a candidate for addition to the tree because it links two nodes that are already in the tree.

Description

[edit]

The algorithm may informally be described as performing the following steps:

  1. Initialize a tree with a single vertex, chosen arbitrarily from the graph.
  2. 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.
  3. Repeat step 2 (until all vertices are in the tree).

In more detail, it may be implemented following thepseudocode below.

function Prim(vertices, edges)isforeach vertexin verticesdo        cheapestCost[vertex] ← ∞        cheapestEdge[vertex] ← null    explored ← empty set    unexplored ← set containing all vertices    startVertex ← any element of vertices    cheapestCost[startVertex] ← 0while unexploredis not emptydo        // Select vertex in unexplored with minimum cost        currentVertex ← vertex in unexplored with minimum cheapestCost[vertex]        unexplored.remove(currentVertex)        explored.add(currentVertex)foreach edge (currentVertex, neighbor)in edgesdoif neighborin unexploredand weight(currentVertex, neighbor) < cheapestCost[neighbor] THEN                cheapestCost[neighbor] ← weight(currentVertex, neighbor)                cheapestEdge[neighbor] ← (currentVertex, neighbor)    resultEdges ← empty listforeach vertexin verticesdoif cheapestEdge[vertex] ≠ nullTHEN            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 inQ that all have equal weights, and the algorithm will automatically start a new tree inF when it completes a spanning tree of each connected component of the input graph. The algorithm may be modified to start with any particular vertexs by settingC[s] to be a number smaller than the other values ofC (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 setQ is implemented: as a simplelinked list orarray of vertices, or as a more complicatedpriority queue data structure. This choice leads to differences in thetime complexity of the algorithm. In general, a priority queue will be quicker at finding the vertexv with minimum cost, but will entail more expensive updates when the value ofC[w] changes.

Time complexity

[edit]
Prim's algorithm has many applications, such as in thegeneration of this maze, which applies Prim's algorithm to a randomly weightedgrid 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 apriority queue. The following table shows the typical choices:

Minimum edge weight data structureTime complexity (total)
adjacency matrix, searchingO(|V|2){\displaystyle O(|V|^{2})}
binary heap andadjacency listO((|V|+|E|)log|V|)=O(|E|log|V|){\displaystyle O((|V|+|E|)\log |V|)=O(|E|\log |V|)}
Fibonacci heap andadjacency listO(|E|+|V|log|V|){\displaystyle O(|E|+|V|\log |V|)}

A simple implementation of Prim's, using anadjacency matrix or anadjacency list graph representation and linearly searching an array of weights to find the minimum weight edge to add, requiresO(|V|2) running time. However, this running time can be greatly improved by usingheaps 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 constructedminimum spanning tree (MST) (or infinity if no such edge exists). Every time a vertexv is chosen and added to the MST, a decrease-key operation is performed on all verticesw outside the partial MST such thatv is connected tow, setting the key to the minimum of its previous value and the edge cost of (v,w).

Using a simplebinary heap data structure, Prim's algorithm can now be shown to run in timeO(|E| log |V|) where |E| is the number of edges and |V| is the number of vertices. Using a more sophisticatedFibonacci heap, this can be brought down toO(|E| + |V| log |V|), which isasymptotically faster when the graph isdense enough that |E| isω(|V|), andlinear time when |E| is at least |V| log |V|. For graphs of even greater density (having at least |V|c edges for somec > 1), Prim's algorithm can be made to run in linear time even more simply, by using ad-ary heap in place of a Fibonacci heap.[10][11]

Demonstration of proof. In this case, the graphY1 =Yf +e is already equal toY. In general, the process may need to be repeated.

Proof of correctness

[edit]

LetP be a connected, weightedgraph. At every iteration of Prim's algorithm, an edge must be found that connects a vertex in a subgraph to a vertex outside the subgraph. SinceP is connected, there will always be a path to every vertex. The outputY of Prim's algorithm is atree, because the edge and vertex added to treeY are connected.

LetY1 be a minimum spanning tree of graph P. IfY1=Y thenY is a minimum spanning tree. Otherwise, lete be the first edge added during the construction of treeY that is not in treeY1, andV be the set of vertices connected by the edges added before edgee. Then one endpoint of edgee is in setV and the other is not. Since treeY1 is a spanning tree of graphP, there is a path in treeY1 joining the two endpoints. As one travels along the path, one must encounter an edgef joining a vertex in setV to one that is not in setV. Now, at the iteration when edgee was added to treeY, edgef could also have been added and it would be added instead of edgee if its weight was less thane, and since edgef was not added, we conclude that

w(f)w(e).{\displaystyle w(f)\geq w(e).}

Let treeY2 be the graph obtained by removing edgef from and adding edgee to treeY1. It is easy to show that treeY2 is connected, has the same number of edges as treeY1, and the total weights of its edges is not larger than that of treeY1, therefore it is also a minimum spanning tree of graphP and it contains edgee and all the edges added before it during the construction of setV. Repeat the steps above and we will eventually obtain a minimum spanning tree of graphP that is identical to treeY. This showsY is a minimum spanning tree. The minimum spanning tree allows for the first subset of the sub-region to be expanded into a larger subsetX, which we assume to be the minimum.

Parallel algorithm

[edit]
The adjacency matrix distributed between multiple processors for parallel Prim's algorithm. In each iteration of the algorithm, every processor updates its part ofC by inspecting the row of the newly inserted vertex in its set of columns in the adjacency matrix. The results are then collected and the next vertex to include in the MST is selected globally.

The main loop of Prim's algorithm is inherently sequential and thus notparallelizable. However, theinner loop, which determines the next edge of minimum weight that does not form a cycle, can be parallelized by dividing the vertices and edges between the available processors.[12] The followingpseudocode demonstrates this.

  1. Assign each processorPi{\displaystyle P_{i}} a setVi{\displaystyle V_{i}} of consecutive vertices of length|V||P|{\displaystyle {\tfrac {|V|}{|P|}}}.
  2. Create C, E, F, and Q as in thesequential algorithm and divide C, E, as well as the graph between all processors such that each processor holds the incoming edges to its set of vertices. LetCi{\displaystyle C_{i}},Ei{\displaystyle E_{i}} denote the parts ofC,E stored on processorPi{\displaystyle P_{i}}.
  3. Repeat the following steps untilQ is empty:
    1. On every processor: find the vertexvi{\displaystyle v_{i}} having the minimum value inCi{\displaystyle C_{i}}[vi{\displaystyle v_{i}}] (local solution).
    2. Min-reduce the local solutions to find the vertexv having the minimum possible value ofC[v] (global solution).
    3. Broadcast the selected node to every processor.
    4. Addv toF and, ifE[v] is not the special flag value, also addE[v] toF.
    5. On every processor: updateCi{\displaystyle C_{i}} andEi{\displaystyle E_{i}} as in the sequential algorithm.
  4. ReturnF

This algorithm can generally be implemented on distributed machines[12] as well as on shared memory machines.[13] The running time isO(|V|2|P|)+O(|V|log|P|){\displaystyle O({\tfrac {|V|^{2}}{|P|}})+O(|V|\log |P|)}, assuming that thereduce andbroadcast operations can be performed inO(log|P|){\displaystyle O(\log |P|)}.[12] A variant of Prim's algorithm for shared memory machines, in which Prim's sequential algorithm is being run in parallel, starting from different vertices, has also been explored.[14] It should, however, be noted that more sophisticated algorithms exist to solve thedistributed minimum spanning tree problem in a more efficient manner.

See also

[edit]

References

[edit]
  1. ^Jarník, V. (1930), "O jistém problému minimálním" [About a certain minimal problem],Práce Moravské Přírodovědecké Společnosti (in Czech),6 (4):57–63,hdl:10338.dmlcz/500726.
  2. ^Prim, R. C. (November 1957),"Shortest connection networks And some generalizations",Bell System Technical Journal,36 (6):1389–1401,Bibcode:1957BSTJ...36.1389P,doi:10.1002/j.1538-7305.1957.tb01515.x.
  3. ^Dijkstra, E. W. (December 1959),"A note on two problems in connexion with graphs"(PDF),Numerische Mathematik,1 (1):269–271,CiteSeerX 10.1.1.165.7577,doi:10.1007/BF01386390,S2CID 123284777.
  4. ^Sedgewick, Robert; Wayne, Kevin Daniel (2011),Algorithms (4th ed.), Addison-Wesley, p. 628,ISBN 978-0-321-57351-3.
  5. ^Rosen, Kenneth (2011),Discrete Mathematics and Its Applications (7th ed.), McGraw-Hill Science, p. 798.
  6. ^abCheriton, David;Tarjan, Robert Endre (1976), "Finding minimum spanning trees",SIAM Journal on Computing,5 (4):724–742,doi:10.1137/0205051,MR 0446458.
  7. ^abPettie, Seth;Ramachandran, Vijaya (January 2002),"An optimal minimum spanning tree algorithm"(PDF),Journal of the ACM,49 (1):16–34,CiteSeerX 10.1.1.110.7670,doi:10.1145/505241.505243,MR 2148431,S2CID 5362916.
  8. ^Tarjan, Robert Endre (1983), "Chapter 6. Minimum spanning trees. 6.2. Three classical algorithms",Data Structures and Network Algorithms, CBMS-NSF Regional Conference Series in Applied Mathematics, vol. 44,Society for Industrial and Applied Mathematics, pp. 72–77.
  9. ^Kepner, Jeremy; Gilbert, John (2011),Graph Algorithms in the Language of Linear Algebra, Software, Environments, and Tools, vol. 22,Society for Industrial and Applied Mathematics, p. 55,ISBN 9780898719901.
  10. ^abTarjan (1983), p. 77.
  11. ^Johnson, Donald B. (December 1975), "Priority queues with update and finding minimum spanning trees",Information Processing Letters,4 (3):53–57,doi:10.1016/0020-0190(75)90001-0.
  12. ^abcGrama, Ananth; Gupta, Anshul; Karypis, George; Kumar, Vipin (2003),Introduction to Parallel Computing, Addison-Wesley, pp. 444–446,ISBN 978-0201648652
  13. ^Quinn, Michael J.; Deo, Narsingh (1984), "Parallel graph algorithms",ACM Computing Surveys,16 (3):319–348,doi:10.1145/2514.2515,S2CID 6833839
  14. ^Setia, Rohit (2009),"A new parallel algorithm for minimum spanning tree problem"(PDF),Proc. International Conference on High Performance Computing (HiPC)

External links

[edit]
Graph andtree traversal algorithms
Search
Shortest path
Minimum spanning tree
Retrieved from "https://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&oldid=1321323864"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp