Minimum Spanning Trees and Kruskal - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Minimum Spanning Trees and Kruskal

Description:

Minimum Spanning Trees and Kruskal s Algorithm CLRS 23 Minimum Spanning Trees (MST) A common problem in communications networks and circuit design: connecting a set ... – PowerPoint PPT presentation

Number of Views:423
Avg rating:3.0/5.0
Slides: 21
Provided by: sju74
Category:

less

Transcript and Presenter's Notes

Title: Minimum Spanning Trees and Kruskal


1
Minimum Spanning Trees and Kruskals
AlgorithmCLRS 23
2
Minimum Spanning Trees (MST)
  • A common problem in communications networks and
    circuit design connecting a set of nodes by a
    network of minimum total length
  • Represent the problem as an undirected graph
    where edge weights denote wire lengths

8
7
9
4
4
14
11
6
7
10
8
1
2
3
Formally,
  • Given a connected undirected graph G(V,E), a
    spanning tree is an acyclic subset of edges T
    that connects all the vertices together.
  • Assuming each edge (u,v) of G has a numeric
    weight (or cost) w(u,v), the cost of a spanning
    tree T is the sum of the weights of the edges in
    T.
  • A minimum spanning tree (MST) is a spanning tree
    of minimum weight.

4
Minimum Spanning Trees (MST)
  • Never pays to have cycles
  • Resulting connection graph is connected,
    undirected and acyclic, hence a free tree
  • Not unique

5
Generic MST Algorithm
  • Two main algorithms for computing MSTs
  • Kruskals
  • Prims
  • Both greedy algorithms
  • The greedy strategy captured by a generic
    aproach
  • Grow the MST one edge at a time
  • which edge to select?

6
Generic MST Algorithms
  • Maintain a set of edges A
  • Loop invariant A is a subset of some MST
  • Safe edge (u,v) A U (u,v) is also a subset of
    an MST
  • Generic-MST(G, w)
  • A empty set
  • while A does not form a spanning tree do
  • find an edge (u,v) that is safe for A
  • A A U (u,v)
  • return A

7
How to recognize safe edges?
  • Definitions first
  • Cut Let S be a subset of vertices V. A cut (S,
    V-S) is a partition of vertices into two disjoint
    sets S and V-S.

7
8
9
4
2
4
14
11
S
6
7
10
8
V-S
1
2
8
  • We say that an edge crosses the cut (S, V-S) if
    one of its endpoints is in S and the other is in
    (V-S)

7
8
9
4
2
4
14
11
S
6
7
10
8
V-S
1
2
9
  • We say that a cut respects a set A of edges if no
    edge in A crosses the cut

7
8
9
4
2
4
14
11
S
6
7
10
8
V-S
1
2
Total weight 37
10
  • An edge is a light edge crossing a cut if its
    weight is the minimum of any edge crossing the
    cut.

7
8
9
4
2
4
14
11
S
6
7
10
8
V-S
1
2
Total weight 37
11
How to recognize safe edges?
  • Theorem Let G(V,E) be a connected undirected
    graph with a real valued weight function w
    defined on E. Let A be a subset of E that is
    included in some MST for G. Let (S, V-S) be any
    cut of G that respects A and let (u,v) be a light
    edge crossing (S, V-S). Then, edge (u,v) is safe
    for A.

12
Proof
  • Suppose that no MST contains (u,v).
  • Let T be an MST of G.

6
8
4
T
13
  • Add (u,v) to T, creating a cycle.
  • There must be at least one more edge on this
    cycle that crosses the cut (x,y)

6
8
4
14
  • The edge (x,y) is not in A, since the cut
    respects A
  • Remove (x,y) and restore the spanning tree T
  • w(T) w(T) w(u,v) w(x,y) lt w(T)
  • Conradicts the assumption that T is an MST.

x
v
6
4
T
u
y
15
Kruskals Algorithm
  • Add edges to A in increasing order of weights.
  • If the edge being considered introduces a cycle,
    skip it.
  • Otherwise add to A.
  • Notice that the edges in A make a forest, and
    they keep getting merged together.

16
Correctness
  • Say (u, v) is the edge is going to be added next,
    and it does not introduce a cycle in A.
  • Let A denote the tree of A that contains vertex
    u. Consider the cut (A, V-A).
  • Every edge crossing the cut is not in A, so this
    cut respects A and (u,v) is the light edge
    crossing it.
  • Thus, (u,v) is safe..

17
How to efficiently detect if adding (u,v) creates
a cycle in A
  • Can be done by a data structure called Disjoint
    Set Union-Find
  • Supports three operations
  • CreateSet(u) create a set containing a single
    item u
  • FindSet(u) return the set that contains item u
  • Union(u,v) merge the set containing u with the
    set containing v
  • Suffices to know takes O(n log n m) time to
    carry out any sequence of m union-find operations
    on a set of size n.

18
  • Kruskal(G(V,E), w)
  • A empty set
  • for each (u in V)
  • CreateSet(u) //create a set for each vertex
  • Sort E in increasing order by weight
  • for each ((u,v) from the sorted list E)
  • if (FindSet(u) ! FindSet(v))
  • // u and v are in different trees
  • Add(u,v) to A
  • Union(u,v)
  • return A

T(E log E) for sorting the edges O(V log V E)
for a sequence of E union find operations Total
O(E log E) since (E gt V-1)
19
Prims Algorithm
  • Edges in A always form a tree (partial MST)
  • Start from a vertex r and grow until the tree
    spans all vertices
  • Let S denote the set of vertices which are on
    this partial MST
  • A is the set of edges connecting the vertices in
    S
  • At each stage the light edge crossing (S, V-S) is
    added to A

S
V-S
7
8
9
4
2
r
4
14
11
7
6
10
8
2
1
20
Prims Algorithm
  • Prim(G(V,E),w,r)
  • for each (u in V)
  • keyu infinity
  • predu NIL
  • keyr 0
  • PQ V //add all vertices to Priority Queue PQ
    based on keys
  • while (PQ is not empty) do
  • u PQ.Extract-Min()
  • for each (v in Adju)
  • if (v is in PQ and w(u,v) lt keyv) then
  • predv u
  • keyvw(u,v)
  • PQ.DecreaseKey(v, keyv) //reorganizes the
    PQ
Write a Comment
User Comments (0)
About PowerShow.com