1.1 data structure and algorithm lecture 13 minimum spanning trees topics reference: introduction to...

33
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees

Upload: opal-walsh

Post on 27-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

1.1Data Structure and Algorithm

Lecture 13

Minimum Spanning Trees

Topics

Reference: Introduction to Algorithm by Cormen

Chapter 13: Minimum Spanning Trees

1.2Data Structure and Algorithm

Minimum Spanning Tree(MST)

In circuit design, sometimes we need to make short some of the pins of the circuit by connecting them with wire.

To interconnect a set of n pins, we can use an arrangement of n-1 wires, each connecting 2 pins.

Of all arrangements, the one that uses the least amount of wire is usually the most desirable.

We can model this wiring problem with a connected, undirected weighted graph G(V,E).

We will find an acyclic subset T from E that connects all the vertices and whose total weight is minimized.

Since T is acyclic and connects all vertices, it is called a spanning tree.

The spanning tree which has minimum weight is called minimum spanning tree.

1.3Data Structure and Algorithm

Spanning Tree Example

2

5

1

3

4

6

2

1

4

2

1

3 5

1

4

A connected, undirected Graph

Many possible Spanning tree

1.4Data Structure and Algorithm

Minimum Spanning Tree Example

2

5

1

3

4

6

2

1

3

A weighted Graph

Minimum Spanning tree

1.5Data Structure and Algorithm

Some Definition

A cut ( S, V-S ) of an undirected graph G =(V,E) is a partition of V. An edge (u,v) crosses the cut ( S, V-S ) if one of its endpoints is in S

and the other is in V-S A cut respects the set A of edges if no edge in A crosses the cut. An edge is a light edge crossing a cut if its weight is the minimum of

any edge crossing the cut

S

V-S

1.6Data Structure and Algorithm

Generic MST

Generic_MST(G,w) A = 0 //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

1.7Data Structure and Algorithm

Kruskal’s Algorithm

This is a greedy algorithm. A greedy algorithm chooses some local optimum (ie. picking an edge with the least weight in a MST).

Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep adding the shortest (least

cost) edge, while avoiding the creation of cycles, until (n - 1) edges have been added.

NOTE: Sometimes two or more edges may have the same cost. The order in which the edges are chosen, in this case, does not matter. Different MSTs may result, but they will all have the same total cost, which will always be the minimum cost

1.8Data Structure and Algorithm

Kruskal’s Algorithm Example(1)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.9Data Structure and Algorithm

Kruskal’s Algorithm Example(2)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.10Data Structure and Algorithm

Kruskal’s Algorithm Example(3)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.11Data Structure and Algorithm

Kruskal’s Algorithm Example(4)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.12Data Structure and Algorithm

Kruskal’s Algorithm Example(5)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.13Data Structure and Algorithm

Kruskal’s Algorithm Example(6)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.14Data Structure and Algorithm

Kruskal’s Algorithm Example(7)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.15Data Structure and Algorithm

Kruskal’s Algorithm Example(8)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.16Data Structure and Algorithm

Kruskal’s Algorithm Example(9)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.17Data Structure and Algorithm

Kruskal’s Algorithm Example(10)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.18Data Structure and Algorithm

Kruskal’s Algorithm Example(11)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.19Data Structure and Algorithm

Kruskal’s Algorithm Example(12)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.20Data Structure and Algorithm

Kruskal’s Algorithm Example(13)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.21Data Structure and Algorithm

Kruskal’s Algorithm Example(14)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.22Data Structure and Algorithm

Kruskal’s Algorithm

MST_KRUSKAL ( G, w ) A = 0 //empty set for each vertex of V[G] do

Make_Set(v) Sort the edges of E by nondecreasing weight w for each edge (u,v) of E, in order by nondecreasing

weight do if Find_Set(u) <> Find_Set(v) then

A = A U {(u,v)} Union (u,v)

return A

1.23Data Structure and Algorithm

Prim’s Algorithm

This algorithm builds the MST one vertex at a time.  It starts at any vertex in a graph (vertex A, for example), and finds the least cost vertex (vertex B, for example) connected to the start vertex. Now, from either 'A' or 'B', it will find the next least costly vertex connection, without creating a cycle (vertex C, for example). Now, from either 'A', 'B', or 'C', it will find the next least costly vertex connection, without creating a cycle, and so on it goes. Eventually, all the vertices will be connected, without any cycles, and an MST will be the result.

NOTE: Two or more edges may have the same cost, so when there is a choice by two or more vertices that is exactly the same, then one will be chosen, and an MST will still result  

1.24Data Structure and Algorithm

Prim’s Algorithm Example(1)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.25Data Structure and Algorithm

Prim’s Algorithm Example(2)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.26Data Structure and Algorithm

Prim’s Algorithm Example(3)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.27Data Structure and Algorithm

Prim’s Algorithm Example(4)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.28Data Structure and Algorithm

Prim’s Algorithm Example(5)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.29Data Structure and Algorithm

Prim’s Algorithm Example(6)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.30Data Structure and Algorithm

Prim’s Algorithm Example(7)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.31Data Structure and Algorithm

Prim’s Algorithm Example(8)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.32Data Structure and Algorithm

Prim’s Algorithm Example(9)

8

14

10

9

8

4

1

7

2

11

6

4

2

7

1.33Data Structure and Algorithm

Prim’s Algorithm

MST_PRIM (G,w,r) // r = starting node Q = V[G] //priority queue containing vertex for each u of Q do

key[u] = infinite key[r] = 0 pre[r] = NIL //pre = parent while Q is not empty do

u = ExtractMin(Q) for each v of Adj[u] do

if v in Q and w(u,v) <key[v] then pre[v] = u key[v] = w(u,v)