Sunteți pe pagina 1din 5

Primalgorithm with example: We consider a weighted connected graph G with n vertices.

Prims 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)

Prims 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.

Kruskals algorithm example: Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. 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. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). Kruskal's algorithm is an example of a greedy algorithm. The basic algorithm looks like this: Forest MinimumSpanningTree( Graph g, int n, double **costs ) { Forest T; Queue q; Edge e; T = ConsForest( g ); q = ConsEdgeQueue( g, costs ); for(i=0;i<(n-1);i++) { do { e = ExtractCheapestEdge( q ); } while ( !Cycle( e, T ) ); AddEdge( T, e ); } return T; } The steps are: 1. The forest is constructed - with each node in a separate tree.

2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T. Example Image Description

This is our original graph. The numbers near the arcs indicate their weight. None of the arcs are highlighted.

AD and CE are the shortest arcs, with length 5, and AD has been arbitrarily chosen, so it is highlighted.

CE is now the shortest arc that does not form a cycle, with length 5, so it is highlighted as the second arc.

The next arc, DF with length 6, is highlighted using much the same method.

The next-shortest arcs are AB and BE, both with length 7. AB is chosen arbitrarily, and is highlighted. The arc BD has been highlighted in red, because there already exists a path (in green) between B and D, so it would form a cycle (ABD) if it were chosen.

The process continues to highlight the nextsmallest arc, BE with length 7. Many more arcs are highlighted in red at this stage: BC because it would form the loop BCE, DEbecause it would form the loop DEBA, and FE because it would form FEBAD.

Finally, the process finishes with the arc EG of length 9, and the minimum spanning tree is found

S-ar putea să vă placă și