Docsity

Minimum Scanning Trees - Lecture Slides | CS 4310, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS4310 spring08 lecture notes on MCST by
Mahmoud Said 1
1
Minimum Spanning Trees
•Definition of MST
•Generic MST algorithm
•Kruskal's algorithm
•Prim's algorithm
2
Definition of MST
• Let G=(V,E) be a connected, undirected graph.
• For each edge (u,v) in E, we have a weight w(u,v) specifying
the cost (length of edge) to connect u and v.
• We wish to find a (acyclic) subset T of Ethat connects all of
the vertices in Vand whose total weight is minimized.
• Since the total weight is minimized, the subset T must be
acyclic (no circuit).
• Thus, Tis a tree. We call it a spanning tree.
• The problem of determining the tree T is called the
minimum-spanning-tree problem.
3
Application of MST: an example
• In the design of electronic circuitry, it is often
necessary to make a set of pins electrically
equivalent by wiring them together.
• To interconnect n pins, we can use n-1 wires, each
connecting two pins.
• We want to minimize the total length of the wires.
• Minimum Spanning Trees can be used to model
this problem.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Minimum Scanning Trees - Lecture Slides | CS 4310 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS4310 spring08 lecture notes on MCST by

1

Minimum Spanning Trees

•Definition of MST

•Generic MST algorithm

•Kruskal's algorithm

•Prim's algorithm

2

Definition of MST

  • Let G=(V,E) be a connected, undirected graph.
  • For each edge (u,v) in E , we have a weight w(u,v) specifying the cost (length of edge) to connect u and v.
  • We wish to find a (acyclic) subset T of E that connects all of the vertices in V and whose total weight is minimized.
  • Since the total weight is minimized, the subset T must be acyclic (no circuit).
  • Thus, T is a tree. We call it a spanning tree.
  • The problem of determining the tree T is called the minimum-spanning-tree problem.

3

Application of MST: an example

• In the design of electronic circuitry, it is often

necessary to make a set of pins electrically

equivalent by wiring them together.

• To interconnect n pins, we can use n-1 wires, each

connecting two pins.

• We want to minimize the total length of the wires.

• Minimum Spanning Trees can be used to model

this problem.

CS4310 spring08 lecture notes on MCST by

4

Electronic Circuits:

5

Electronic Circuits:

6

Here is an example of a connected graph and its minimum spanning tree:

a

b

h

c d

e

g f

i

Notice that the tree is not unique: replacing (b,c) with (a,h) yields another spanning tree with the same minimum weight.

CS4310 spring08 lecture notes on MCST by

10

Theorem 1

  • 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 minimum spanning tree for G.

  • Let (S,V-S) be any cut of G such that for

any edge (u, v) in A, {u, v} āŠ† S or {u, v} āŠ†

(V-S).

  • Let (u,v) be a light edge crossing (S,V-S).
  • Then, edge (u,v) is safe for A. 1 2 1

1 1

1 1 1

11

Corollary.

  • 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 minimum spanning tree for G.
  • Let C be a connected component (tree) in the forest GA=(V,A).
  • Let (u,v) be a light edge (shortest among all edges connecting C with others components) connecting C to some other component in GA.
  • Then, edge (u,v) is safe for A. (For Kruskal’s algorithm)

12

The algorithms of Kruskal and Prim

  • The two algorithms are elaborations of the generic

algorithm.

  • They each use a specific rule to determine a safe

edge in line 3 of GENERIC_MST.

  • In Kruskal's algorithm,
    • The set A is a forest.
    • The safe edge added to A is always a least-weight edge in the graph that connects two distinct components.
  • In Prim's algorithm,
    • The set A forms a single tree.
    • The safe edge added to A is always a least-weight edge connecting the tree to a vertex not in the tree.

CS4310 spring08 lecture notes on MCST by

13

Kruskal's algorithm(basic part)

1 (Sort the edges in an increasing order)

2 A:={}

3 while E is not empty do {

3 take an edge (u, v) that is shortest in E

and delete it from E

4 if u and v are in different components then

add (u, v) to A

Note: each time a shortest edge in E is considered.

14

Kruskal's algorithm (Fun Part, not required)

MST_KRUSKAL(G,w)

1 A:={}

2 for each vertex v in V[G]

3 do MAKE_SET(v)

4 sort the edges of E by nondecreasing weight w

5 for each edge (u,v) in E, in order by

nondecreasing weight

6 do if FIND_SET(u) != FIND_SET(v)

7 then A:=A∪{(u,v)}

8 UNION(u,v)

9 return A

(Disjoint set is discussed in Chapter 21, Page 498)

15

Disjoint-Set (Chapter 21, Page 498)

  • Keep a collection of sets S 1 , S 2 , .., Sk,
    • Each Si is a set, e,g, S 1 ={v 1 , v 2 , v 8 }.
  • Three operations
    • Make-Set(x)-creates a new set whose only member is x.
    • Union(x, y) –unites the sets that contain x and y, say, Sx and Sy, into a new set that is the union of the two sets.
    • Find-Set(x)-returns a pointer to the representative of the set containing x.
    • Each operation takes O(log n) time.

CS4310 spring08 lecture notes on MCST by

19

a

b

h

c d

e

g f

i

a

b

h

c d

e

g f

i

20

a

b

h

c d

e

g f

i

a

b

h

c d

e

g f

i

21

a

b

h

c d

e

g f

i

a

b

h

c d

e

g f

i

CS4310 spring08 lecture notes on MCST by

22

a

b

h

c d

e

g f

i

a

b

h

c d

e

g f

i

23

Prim's algorithm(basic part)

MST_PRIM(G,w,r)

  1. A={}

2. S:={r} (r is an arbitrary node in V)

3. Q=V-{r};

4. while Q is not empty do {

5 take an edge (u, v) such that (1) u ∈∈∈∈ S and v ∈∈∈∈ Q (v āˆ‰āˆ‰āˆ‰āˆ‰ S ) and

(u, v) is the shortest edge satisfying (1)

6 add (u, v) to A, add v to S and delete v from Q

24

Prim's algorithm(Fun part, not required)

MST_PRIM(G,w,r) 1 for each u in Q do 2 key[u]:=āˆž 3 parent[u]:=NIL 4 key[r]:= 5 Q←V[Q] 6 while Q!={} do 7 u:=EXTRACT_MIN(Q) 8 for each v in Adj[u] do 9 if v in Q and w(u,v)<key[v] 10 then parent[v]:=u 11 key[v]:=w(u,v)

CS4310 spring08 lecture notes on MCST by

28

a

b

h

c d

e

g f

i

a

b

h

c d

e

g f

i

29

a

b

h

c d

e

g f

i

a

b

h

c d

e

g f

i

30

a

b

h

c d

e

g f

i

Bottleneck spanning tree: A spanning tree of G whose largest edge weight is minimum over all spanning trees of G. The value of the bottleneck spanning tree is the weight of the maximum-weight edge in T. Theorem: A minimum spanning tree is also a bottleneck spanning tree. (Challenge problem)