the minimal spanning tree algorithm

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QingyingLiu/article/details/81986435

Minimal spanning tree

What is spanning tree?

Given a connected and undirected graph, a spanning tree of that graph is a
subgraph that is a tree and connects all the vertices together. A single
graph can have many different spanning trees. A minimum spanning tree (MST)
or minimum weight spanning tree for a weighted, connected and undirected graph
is a spanning tree with weight less than or equal to the weight of every other
spanning tree. The weight of a spanning tree is the sum of weights given to
each edge of the spanning tree.

Properties of spanning tree

  • A minimum spanning tree has (V – 1) edges where V is the number of vertices in
    the given graph.
  • spanning treee is acyclic.

Kruskal algorithm

steps:

  1. Sort all the edges in non-decreasing order of their weight.
  2. pick the smallest edges.Check if if forms a cycle with the spanning tree
    formed so far. If cycle is not formed, include this edge. Else, discard it.
  3. Repeat step#2 until there are (|V|-1) edges in the spanning tree.

greedy choice and its demonstration

The algorithm is Greedy Algorithm.The greedy choice is to pick the smallest
weight edge that does not cause a cycle in the MST constructed so far.

now let’s demonstrate it.

lemma:The minimal spanning tree must include the minimal edge of the graph.

proof:
wo suppose that there is a minimal spanning tree T that does not exist the
minimal edge(u,v) of graph, and one of T’s edges is denoted by (u,x).
Now we remove the edge(u,x) but add edge(u,v), formming a new spanning tree T’.
Then W(T)-W(T’)=edge(u,x).w-edge(u,v).w>0 , contradicting to our suppose that
T is minimal spanning tree.Therefore,the lemma that the minimal spanning must
include the minimal edge of graph is corret.

optimal substructure and its demonstration

lemma: we suppose G=G’+edge(u,v),and T is the minimal spanning tree of G.We
can state that T’=T-edge(u,v) is also a minimal spanning tree of G’.

proof:
we suppose T’ is not minimal spanning tree of G’ but T” is, and T”’=edge(u,v)
+T”. So W(T”’)=W(T”)+edge(u,v) < W(T’)+edge(u,v)=W(T), which means T is not
minimal spanning tree, contradicting to our suppose.Therefore, our lemma is
proven.

prim algorithm

step

  • Start by picking any vertix to be the root of the tree.
  • While the tree does not contain all the vertices in the graph,
    find shortest edge leaving the tree and add it to the tree.

more details

step 0: Choose any element r;set S={r} and A={}(empty set).

step 1:find a lightest edge such that one endpoint is in S and the other is
in V\S.Add this edge to A and its endpoint to S.

step 3:if V\S={}(empty),then stop and output the minimum spanning tree(S,A).
Otherwise go to step 1.

crucial questions about prim algorithm

  1. How does the algorithm update S efficiently?
    Anwser: Color the vertices. Initially all are white. Change the color to black
    when the vertex is moved to S. Use color[v] to store color.

  2. How does the algorithm find the lightest edge and update A efficiently?
    Anwser: Using a priority queue to find the lightest edge. We suppose that:

    • u is a vertex in V/S.
    • key[u] is the weight of lightest edge from u to any vertex in S
    • pred[u] is the endpoint of this edge in S. The array is used to build the MST
      tree.

Here is the pseudocode:

pseudocode

the greedy choice of prim algorithm

lemma: always add the lightest edge to the tree until all verteces are included
into the tree.

proof:
we suppose T is the minmum spanning tree not including the lightest edge(u,x)
but including edge (u,y). We substitute the edge(u,v) with edge(u,x),formming a
new spanning tree T’.So,

W(T)-W(T’) = w(edge(u,y)) - w(edge(u,x)) > 0,contradicting to our suppose.
So,our lemma is corret.

the optimal substructure of prim algorithm

the same as the Kruskal’s.

Reference

  1. https://www.cse.ust.hk/~dekai/271/notes/L07/L07.pdf
  2. introduction to algorithm

猜你喜欢

转载自blog.csdn.net/QingyingLiu/article/details/81986435