Prim”algorithm with example: We consider a weighted connected graph G with n vertices.

Prim’s algorithm finds a minimum spanning tree of G. procedure Prim(G: weighted connected graph with n vertices) T := a minimum-weight edge for i = 1 to n − 2 begin e := an edge of minimum weight incident to a vertex in T and not forming a circuit in T if added to T T := T with e added end return(T)

Prim’s algorithm will proceed as follows. First we add edge {d, e} of weight 1. Next, we add edge {c, e} of weight 2. Next, we add edge {d, z} of weight 2. Next, we add edge {b, e} of weight 3. And finally, we add edge {a, b} of weight 2. This produces a minimum spanning tree of weight 10. A minimum spanning tree is the following.

The forest is constructed . then it finds a minimum spanning forest (a minimum spanning tree for each connected component). where the total weight of all the edges in the tree is minimized. double **costs ) { Forest T. e ). T = ConsForest( g ). The basic algorithm looks like this: Forest MinimumSpanningTree( Graph g.i++) { do { e = ExtractCheapestEdge( q ). Queue q. Kruskal's algorithm is an example of a greedy algorithm. costs ). . int n. Edge e.with each node in a separate tree. for(i=0.i<(n-1). q = ConsEdgeQueue( g. AddEdge( T. } return T. If the graph is not connected. } while ( !Cycle( e.Kruskal’s algorithm example: Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. T ) ). } The steps are: 1. This means it finds a subset of the edges that forms a tree that includes every vertex.

None of the arcs are highlighted. Every step will have joined two trees in the forest together. Extract the cheapest edge from the queue. there will only be one tree in T. CE is now the shortest arc that does not form a cycle. with length 5. If it forms a cycle. with length 5. so it is highlighted as the second arc. . The edges are placed in a priority queue. Else add it to the forest. so that at the end. reject it. 2. and AD has been arbitrarily chosen.2. so it is highlighted. AD and CE are the shortest arcs. 1. Adding it to the forest will join two trees together. Until we've added n-1 edges. Example Image Description This is our original graph. 3. 3. The numbers near the arcs indicate their weight.

BE with length 7. so it would form a cycle (ABD) if it were chosen. The next-shortest arcs are AB and BE. AB is chosen arbitrarily. The process continues to highlight the nextsmallest arc. Finally. both with length 7. and is highlighted. The arc BD has been highlighted in red. Many more arcs are highlighted in red at this stage: BC because it would form the loop BCE. because there already exists a path (in green) between B and D. DF with length 6. and the minimum spanning tree is found .The next arc. the process finishes with the arc EG of length 9. and FE because it would form FEBAD. is highlighted using much the same method. DEbecause it would form the loop DEBA.

Sign up to vote on this title
UsefulNot useful