Top Banner

of 18

Prim_s_Algo

Apr 06, 2018

Download

Documents

Rajjak Shah
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/3/2019 Prim_s_Algo

    1/18

    PRIMS ALGORITHHM

    At first a peak is chosen in random order ,which for simplicity we accept it as V(1).Thisway two sets of pointers are initialized,the 0={1} andP={2...n}.

    The O set (the O is taken from the Greek word Oristiko which means Terminal),willalways contain the pointers of those peaks which are terminally attached in the Ttree.The

    V(1) peak has already been attached in the Ttree.The Pset(Pis taken form the GreekwordProsorino which meansTemporary) contains the rest of the pointers for the

    peaks,P={1...n}-O which are those pointers who have not been terminally connected with

    a node ofT,that means they are not attached in the tree.

    In every execution of thePrim Algorithm a new peak will be connected to the Ttree,notalways with their numbering order, for example the V(4)peak can be connected to the tree

    before the V(2) peak.The corresponding pointer of the newly connected peak will be

    deleted fromPset and will be inserted to the O set.When all peaks are connected therewill be O={1,...n}andP=0.This of course means the end of the algorithm.

    The new peak every time will be chosen by using greedy method,among all sides ofGwhich connect peaks already inserted in the T(pointers in theO set) tree with the rest ofthe peaks (pointers in thePset),we choose one with minimum cost.If the chosen one is

    e(ij)then i belongs in theO set ,V(i)peak is already in the Ttree, j belongs in thePset,

    and V(j)peak has not been attached in the Ttree yet.We put V(j)in the Ttree,we change

    the Oset by putting thejpointer,and we also change thePset by removing thejpointer.

  • 8/3/2019 Prim_s_Algo

    2/18

    INPUT:n,c[e(ij)],i,j belonging to {1,...,n}.

    OUTPUT:p(j) j=2,...,n (pointer of peaks j father in the T tree).

    PRIMS Algorithm STEPS

    1. :(initializations).

    O={1} (V(1) root of the T tree).P={2,...,n}

    For every j belonging toP:e(j):=c[e(j1)] , p(j)=1

    ( all peaks connected to the root.By definition of the cost function:e (j)=infinitewhen V(j) does not connect to V(1).).

    2. Choose akfor which e(k)

  • 8/3/2019 Prim_s_Algo

    3/18

    5.4.1 Kruskal Algorithm.

    The Kruskal Algorithm starts with aforestwhich consists of n trees.Each and everyone

    tree,consists only by one node and nothing else.In every step of the algorithm,two

    different trees of thisforestare connected to a bigger tree.Therefore ,we keep having lessand bigger trees in ourforestuntil we end up in a tree which is the minimum genetic tree

    (m.g.t.).In every step we choose the side with the least cost,which means that we are still

    under greedy policy.If the chosen side connects nodes which belong in the same tree theside is rejected,and not examined again because it could produce a circle which will

    destroy our tree.Either this side or the next one in order of least cost will connect nodes of

    different trees,and this we insert connecting two small trees into a bigger one.

    5.4.1 Pseudocode For The Kruskal Algorithm.

  • 8/3/2019 Prim_s_Algo

    4/18

    E(1)is the set of the sides of the minimum genetic tree.

    E(2) is the set of the remaining sides.

    Kruskal Algorithm STEPS

    E(1)=0,E(2)=EWhile E(1) contains less then n-1 sides and E(2)=0 do

    From the sides ofE(2) choose one with minimum cost-->e(ij)

    E(2)=E(2)-{e(ij) }

    IfV(i),V(j) do not belong in the same tree then

    o unite the trees ofV(i) and V(j) to one tree.

    end (If)

    end (While)

    End Of Algorithm.

    .

    Prim's algorithm is an algorithm in graph theory that finds a minimum spanning treefora connected weighted graph. This means it finds a subset of the edgesthat forms atree

    that includes every vertex, where the total weight of all the edges in the tree is minimized.

    The algorithm was discovered in 1930 by mathematician Vojtch Jarnkand later

    independently by computer scientistRobert C. Prim in 1957 and rediscovered byEdsgerDijkstra in 1959. Therefore it is sometimes called the DJP algorithm, the Jarnkalgorithm, or the Prim-Jarnk algorithm.

    Contents

    [hide]

    1 Description

    2 Time complexity

    3 Example

    4 Pseudocodeo 4.1 Min-heap

    5 Proof of correctness

    6 References

    7 External links

    http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Vojt%C4%9Bch_Jarn%C3%ADkhttp://en.wikipedia.org/wiki/Vojt%C4%9Bch_Jarn%C3%ADkhttp://en.wikipedia.org/wiki/Computer_scientisthttp://en.wikipedia.org/wiki/Robert_C._Primhttp://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://toggletoc%28%29/http://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Description%23Descriptionhttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Time_complexity%23Time_complexityhttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Example%23Examplehttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Pseudocode%23Pseudocodehttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Min-heap%23Min-heaphttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Proof_of_correctness%23Proof_of_correctnesshttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#References%23Referenceshttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#External_links%23External_linkshttp://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Vojt%C4%9Bch_Jarn%C3%ADkhttp://en.wikipedia.org/wiki/Computer_scientisthttp://en.wikipedia.org/wiki/Robert_C._Primhttp://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://toggletoc%28%29/http://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Description%23Descriptionhttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Time_complexity%23Time_complexityhttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Example%23Examplehttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Pseudocode%23Pseudocodehttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Min-heap%23Min-heaphttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#Proof_of_correctness%23Proof_of_correctnesshttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#References%23Referenceshttp://en.wikipedia.org/wiki/Prim-Jarnik_algorithm#External_links%23External_links
  • 8/3/2019 Prim_s_Algo

    5/18

    [edit] Description

    The algorithm continuously increases the size of a tree starting with a single vertex untilit spans all the vertices.

    Input: A connected weighted graph with vertices V and edges E. Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V, Enew=

    {}

    repeat until Vnew=V:

    o Choose edge (u,v) from E with minimal weight such that u is in Vnew and v

    is not (if there are multiple edges with the same weight, choose arbitrarily)o Add v to Vnew, add (u,v) to Enew

    Output: Vnew and Enew describe the minimal spanning tree

    [edit] Time complexity

    Minimum edge weight data structure Time complexity (total)

    adjacency matrix, searching O(V2)

    binary heap (as in pseudocode below) andadjacencylist

    O((V + E) log(V)) = O(E log(V))

    Fibonacci heap and adjacency list O(E + V log(V))

    A simple implementation using an adjacency matrix graph representation and searchingan array of weights to find the minimum weight edge to add requires O(V2) running time.

    Using a simplebinary heapdata structure and an adjacency list representation, Prim's

    algorithm can be shown to run in time which is O(Elog V) where E is the number ofedges and V is the number of vertices. Using a more sophisticated Fibonacci heap, this

    can be brought down to O(E+ Vlog V), which is significantly faster when the graph is

    dense enough thatEis (Vlog V).

    [edit] Example

    Image Description

    This is our original weighted graph. The numbers near the arcs indicate theirweight.

    http://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=1http://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=2http://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Binary_heaphttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Fibonacci_heaphttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Big-O_notationhttp://en.wikipedia.org/wiki/Binary_heaphttp://en.wikipedia.org/wiki/Binary_heaphttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Big-O_notationhttp://en.wikipedia.org/wiki/Fibonacci_heaphttp://en.wikipedia.org/wiki/Fibonacci_heaphttp://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=3http://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=1http://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=2http://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Binary_heaphttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Fibonacci_heaphttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Big-O_notationhttp://en.wikipedia.org/wiki/Binary_heaphttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Big-O_notationhttp://en.wikipedia.org/wiki/Fibonacci_heaphttp://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=3
  • 8/3/2019 Prim_s_Algo

    6/18

    Vertex D has been arbitrarily chosen as a starting point. Vertices A, B, E and Fare connected to D through a single edge. A is the vertex nearest to D and will bechosen as the second vertex along with the edge AD.

    The next vertex chosen is the vertex nearest to eitherD orA. B is 9 away from D

    and 7 away from A, E is 15, and F is 6. F is the smallest distance away, so wehighlight the vertex F and the arc DF.

    The algorithm carries on as above. Vertex B, which is 7 away from A, is

    highlighted.

    In this case, we can choose between C, E, and G. C is 8 away from B, E is 7

    away from B, and G is 11 away from F. E is nearest, so we highlight the vertex Eand the arc EB.

    Here, the only vertices available are C and G. C is 5 away from E, and G is 9

    away from E. C is chosen, so it is highlighted along with the arc EC.

    Vertex G is the only remaining vertex. It is 11 away from F, and 9 away from E.

    E is nearer, so we highlight it and the arc EG.

    Now all the vertices have been selected and the minimum spanning tree is shown

    in green. In this case, it has weight 39.

    [edit] Pseudocode

    [edit] Min-heap

    Initialization

    inputs: A graph, a function returning edge weights weight-function, and an initialvertex

    initial placement of all vertices in the 'not yet seen' set, set initial vertex to be added to the

    tree, and place all vertices in a min-heap to allow for removal of the min distance from

    the minimum graph.

    for eachvertexingraph set min_distance ofvertexto set parent ofvertextonull

    set minimum_adjacency_list ofvertexto empty list set is_in_Q ofvertexto trueset distance of initial vertex tozeroadd to minimum-heap Q all vertices in graph.

    Algorithm

    In the algorithm description above,

    nearest vertex is Q[0], now latest addition

    http://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=4http://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=5http://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=4http://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=5
  • 8/3/2019 Prim_s_Algo

    7/18

    fringe is v in Q where distance of v < after nearest vertex is removednot seen is v in Q where distance of v = after nearest vertex is removed

    The while loop will fail when remove minimum returns null. The adjacency list is set to

    allow a directional graph to be returned.

    time complexity: V for loop, log(V) for the remove functionwhilelatest_addition = remove minimum in Q set is_in_Q oflatest_addition to false

    add latest_addition to (minimum_adjacency_list of (parent oflatest_addition))

    add (parent of latest_addition) to (minimum_adjacency_list oflatest_addition)

    time complexity: E/V, the average number of verticesfor eachadjacentoflatest_addition

    if (is_in_Q of adjacent) and (weight-function(latest_addition,adjacent) < min_distance of adjacent) set parent ofadjacenttolatest_addition

    set min_distance ofadjacentto weight-function(latest_addition, adjacent)

    time complexity: log(V), the height of the heapupdate adjacent in Q, order by min_distance

    [edit] Proof of correctness

    LetPbe a connected, weightedgraph. At every iteration of Prim's algorithm, an edgemust be found that connects a vertex in a subgraph to a vertex outside the subgraph.

    SincePis connected, there will always be a path to every vertex. The output Yof Prim's

    algorithm is a tree, because the edge and vertex added to Yare connected. Let Y1 be a

    minimum spanning tree of P. IfY1=Ythen Yis a minimum spanning tree. Otherwise, let ebe the first edge added during the construction ofYthat is not in Y1, and Vbe the set of

    vertices connected by the edges added before e. Then one endpoint ofe is in Vand the

    other is not. Since Y1 is a spanning tree ofP, there is a path in Y1 joining the twoendpoints. As one travels along the path, one must encounter an edge fjoining a vertex in

    Vto one that is not in V. Now, at the iteration when e was added to Y,fcould also have

    been added and it would be added instead ofe if its weight was less than e. Sincefwasnot added, we conclude that

    w(f) w(e).

    Let Y2 be the graph obtained by removingfand adding e from Y1. It is easy to show thatY2 is connected, has the same number of edges as Y1, and the total weights of its edges isnot larger than that ofY1, therefore it is also a minimum spanning tree ofPand it contains

    e and all the edges added before it during the construction ofV. Repeat the steps above

    and we will eventually obtain a minimum spanning tree ofPthat is identical to Y. This

    shows Yis a minimum spanning tree.

    http://en.wikipedia.org/wiki/Nullhttp://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=6http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Nullhttp://en.wikipedia.org/w/index.php?title=Prim%27s_algorithm&action=edit&section=6http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Tree_(graph_theory)
  • 8/3/2019 Prim_s_Algo

    8/18

    Kruskal's algorithm is an algorithm in graph theory that finds aminimum spanning treefor a connected weighted graph. This means it finds a subset of the edgesthat forms a

    tree that includes everyvertex, 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(aminimum spanning tree for each connected component). Kruskal's algorithm is anexample of a greedy algorithm.

    It works as follows:

    create a forestF(a set of trees), where each vertex in the graph is a separate tree

    create a set Scontaining all the edges in the graph

    while Sis nonempty

    o remove an edge with minimum weight from S

    o if that edge connects two different trees, then add it to the forest,

    combining two trees into a single treeo otherwise discard that edge

    At the termination of the algorithm, the forest has only one component and forms a

    minimum spanning tree of the graph.

    This algorithm first appeared inProceedings of the American Mathematical Society, pp.

    4850 in 1956, and was written byJoseph Kruskal.

    Other algorithms for this problem include Prim's algorithm, Reverse-Delete algorithm,

    andBorvka's algorithm.

    Contents

    [hide]

    1 Performance

    2 Example

    3 Proof of correctness

    o 3.1 Spanning tree

    o 3.2 Minimality

    4 Pseudocode

    5 References 6 See also

    7 External links

    [edit] Performance

    http://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Connected_component_(graph_theory)http://en.wikipedia.org/wiki/Greedy_algorithmhttp://en.wikipedia.org/wiki/Greedy_algorithmhttp://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Nonemptyhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Proceedings_of_the_American_Mathematical_Societyhttp://en.wikipedia.org/wiki/1956http://en.wikipedia.org/wiki/Joseph_Kruskalhttp://en.wikipedia.org/wiki/Joseph_Kruskalhttp://en.wikipedia.org/wiki/Prim's_algorithmhttp://en.wikipedia.org/wiki/Reverse-Delete_algorithmhttp://en.wikipedia.org/wiki/Bor%C5%AFvka's_algorithmhttp://en.wikipedia.org/wiki/Bor%C5%AFvka's_algorithmhttp://en.wikipedia.org/wiki/Bor%C5%AFvka's_algorithmhttp://toggletoc%28%29/http://en.wikipedia.org/wiki/Kruskal's_algorithm#Performance%23Performancehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Example%23Examplehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Proof_of_correctness%23Proof_of_correctnesshttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Spanning_tree%23Spanning_treehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Minimality%23Minimalityhttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Pseudocode%23Pseudocodehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#References%23Referenceshttp://en.wikipedia.org/wiki/Kruskal's_algorithm#See_also%23See_alsohttp://en.wikipedia.org/wiki/Kruskal's_algorithm#External_links%23External_linkshttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=1http://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Minimum_spanning_treehttp://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Connected_component_(graph_theory)http://en.wikipedia.org/wiki/Greedy_algorithmhttp://en.wikipedia.org/wiki/Tree_(graph_theory)http://en.wikipedia.org/wiki/Nonemptyhttp://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/Proceedings_of_the_American_Mathematical_Societyhttp://en.wikipedia.org/wiki/1956http://en.wikipedia.org/wiki/Joseph_Kruskalhttp://en.wikipedia.org/wiki/Prim's_algorithmhttp://en.wikipedia.org/wiki/Reverse-Delete_algorithmhttp://en.wikipedia.org/wiki/Bor%C5%AFvka's_algorithmhttp://toggletoc%28%29/http://en.wikipedia.org/wiki/Kruskal's_algorithm#Performance%23Performancehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Example%23Examplehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Proof_of_correctness%23Proof_of_correctnesshttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Spanning_tree%23Spanning_treehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Minimality%23Minimalityhttp://en.wikipedia.org/wiki/Kruskal's_algorithm#Pseudocode%23Pseudocodehttp://en.wikipedia.org/wiki/Kruskal's_algorithm#References%23Referenceshttp://en.wikipedia.org/wiki/Kruskal's_algorithm#See_also%23See_alsohttp://en.wikipedia.org/wiki/Kruskal's_algorithm#External_links%23External_linkshttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=1
  • 8/3/2019 Prim_s_Algo

    9/18

    WhereEis the number of edges in the graph and Vis the number of vertices, Kruskal's

    algorithm can be shown to run in O(ElogE) time, or equivalently, O(Elog V) time, allwith simple data structures. These running times are equivalent because:

    Eis at most V2 and logV2 = 2logVis O(log V).

    If we ignore isolated vertices, which will each be their own component of theminimum spanning tree anyway, VE+1, so log Vis O(logE).

    We can achieve this bound as follows: first sort the edges by weight using a comparison

    sort in O(ElogE) time; this allows the step "remove an edge with minimum weight from

    S" to operate in constant time. Next, we use a disjoint-set data structure to keep track ofwhich vertices are in which components. We need to perform O(E) operations, two 'find'

    operations and possibly one union for each edge. Even a simple disjoint-set data structure

    such as disjoint-set forests with union by rank can perform O(E) operations in O(Elog V)

    time. Thus the total time is O(ElogE) = O(Elog V).

    Provided that the edges are either already sorted or can be sorted in linear time (forexample with counting sort orradix sort), the algorithm can use more sophisticated

    disjoint-set data structures to run in O(E(V)) time, where is the extremely slowly-

    growing inverse of the single-valued Ackermann function.

    [edit] Example

    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 apath (in green) between B and D, so it would form a cycle (ABD) if it were chosen.

    The process continues to highlight the next-smallest 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.

    [edit] Proof of correctness

    http://en.wikipedia.org/wiki/Big-O_notationhttp://en.wikipedia.org/wiki/Binary_logarithmhttp://en.wikipedia.org/wiki/Comparison_sorthttp://en.wikipedia.org/wiki/Comparison_sorthttp://en.wikipedia.org/wiki/Disjoint-set_data_structurehttp://en.wikipedia.org/wiki/Counting_sorthttp://en.wikipedia.org/wiki/Radix_sorthttp://en.wikipedia.org/wiki/Ackermann_functionhttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=2http://en.wikipedia.org/wiki/Arbitraryhttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=3http://en.wikipedia.org/wiki/Big-O_notationhttp://en.wikipedia.org/wiki/Binary_logarithmhttp://en.wikipedia.org/wiki/Comparison_sorthttp://en.wikipedia.org/wiki/Comparison_sorthttp://en.wikipedia.org/wiki/Disjoint-set_data_structurehttp://en.wikipedia.org/wiki/Counting_sorthttp://en.wikipedia.org/wiki/Radix_sorthttp://en.wikipedia.org/wiki/Ackermann_functionhttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=2http://en.wikipedia.org/wiki/Arbitraryhttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=3
  • 8/3/2019 Prim_s_Algo

    10/18

    The proof consists of two parts. First, it is proved that the algorithm produces a spanning

    tree. Second, it is proved that the constructed spanning tree is of minimal weight.

    [edit] Spanning tree

    LetPbe a connected, weighted graph and let Ybe the subgraph ofPproduced by thealgorithm. Ycannot have a cycle, since the last edge added to that cycle would have been

    within one subtree and not between two different trees. Ycannot be disconnected, since

    the first encountered edge that joins two components ofYwould have been added by thealgorithm. Thus, Yis a spanning tree ofP.

    [edit] Minimality

    The proof is by reductio ad absurdum. Assume that Yis not a minimal spanning tree andamong all minimum weight spanning trees pickY1 which has the smallest number of

    edges which are not in Y. Consider the edge e which was first to be added by the

    algorithm to Yof those which are not in Y1.

    has a cycle. Being a tree, Ycannot contain all edges of this cycle. Therefore this cyclecontains an edgefwhich is not in Y. The graph is also a spanning tree and therefore its

    weight cannot be less than the weight ofY1 and hence the weight ofe cannot be less than

    the weight off. Y2 By another application of the reductio ad absurdum we shall prove thatthe weight offcannot be less than that ofe. Assume the contrary and remember that the

    edges are considered for addition to Yin the order of non-decreasing weight. Thereforef

    would have been considered in the main loop before e, i.e., it would be tested for theaddition to a subforest (recall that e is the first edge ofYwhich is not in Y1). Butfdoes

    not create a cycle in Y1, therefore it cannot create a cycle in Y3, and it would have been

    added to the growing tree.

    The above implies that the weights ofe andfare equal, and hence Y2 is also a minimalspanning tree. But Y2 has one more edge in common with Ythan Y1, which contradicts to

    the choice ofY1, Q.E.D.

    [edit] Pseudocode

    1 function Kruskal(G)2 for each vertex v in G do3 Define an elementary cluster C(v) {v}.

    4 Initialize a priority queue Q to contain all edges in G, usingthe weights as keys.5 Define a tree T //T will ultimately contain the edges

    of the MST6 // n is total number of vertices7 whileT has fewer than n-1 edges do8 // edge u,v is the minimum weighted route from/to v9 (u,v) Q.removeMin()

    10 // prevent cycles in T. add u,v only if T does not alreadycontain an edge consisting of u and v.

    http://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=4http://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=5http://en.wikipedia.org/wiki/Reductio_ad_absurdumhttp://en.wikipedia.org/wiki/Spanning_tree_(mathematics)#Properties_of_spanning_treeshttp://en.wikipedia.org/wiki/Q.E.Dhttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=6http://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=4http://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=5http://en.wikipedia.org/wiki/Reductio_ad_absurdumhttp://en.wikipedia.org/wiki/Spanning_tree_(mathematics)#Properties_of_spanning_treeshttp://en.wikipedia.org/wiki/Q.E.Dhttp://en.wikipedia.org/w/index.php?title=Kruskal%27s_algorithm&action=edit&section=6
  • 8/3/2019 Prim_s_Algo

    11/18

    11 // Note that the cluster contains more than one vertex only ifan edge containing a pair of12 // the vertices has been added to the tree.13 Let C(v) be the cluster containing v, and let C(u) be thecluster containing u.14 ifC(v) C(u) then15 Add edge (v,u) to T.16 Merge C(v) and C(u) into one cluster, that is, union C(v) andC(u).17 return tree T

  • 8/3/2019 Prim_s_Algo

    12/18

  • 8/3/2019 Prim_s_Algo

    13/18

  • 8/3/2019 Prim_s_Algo

    14/18

    Kruskals Example:

  • 8/3/2019 Prim_s_Algo

    15/18

  • 8/3/2019 Prim_s_Algo

    16/18

  • 8/3/2019 Prim_s_Algo

    17/18

  • 8/3/2019 Prim_s_Algo

    18/18