spanning trees. prims mst algorithm algorithm ( this is also greedy) select an arbitrary vertex to...

34
Spanning Trees

Upload: adam-rees

Post on 26-Mar-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Spanning Trees

Page 2: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s MST Algorithm

Algorithm ( this is also greedy)

Select an arbitrary vertex to start the tree,

while there are fringe vertices:

1)select an edge of minimum weight

between a tree vertex and a fringe

vertex.

2)add the selected edge and the

fringe vertex to the tree.

end.

Page 3: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

6

2

52

6

Page 4: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

6

2

52

6

Example: start with 7Example: start with 7

Page 5: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

6

2

52

6

Page 6: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

6

2

52

6

Page 7: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

6

2

52

6

Page 8: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

2

52

6

Page 9: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

2

52

6

Page 10: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

2

56

Page 11: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

3

2

1

4

4

2

5

3

4

7 8

1

2

56

Page 12: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1

4

4

2

5

3

4

7 8

1

2

6

Page 13: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1

4

4

2

5

3

4

7 8

1

2

6

Page 14: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1 4

2

5

3

4

7 8

1

2

6

Page 15: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1 4

2

5

3

4

7 8

1

2

6

Page 16: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1

2

5

3

4

7 8

1

2

Page 17: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1

2

5

3

4

7 8

1

2

Page 18: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Prim’s AlgorithmMinimal Spanning Tree

1 6

2 4

3 5

2

1

2

3

7 8

1

2

MST weight = 15MST weight = 15

4

Page 19: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

Topological Sorting Algorithm

while (the graph has a node with no successor) do

remove one of those nodes from the graph

and add it to the end of a list

if (the graph is empty) then

the list contains the reverse of some topological order

else

the graph contains a cycle

Page 20: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

BB

CC

DD

EE

FF

GG

HH II

JJ

LL

KK

MM

Page 21: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

BB

CC

DD

EE

FF

GG

HH II

JJ

LL

KK

MM

DD

Page 22: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

BB

CC

EE

FF

GG

HH II

JJ

LL

KK

MM

DD EE

Page 23: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

BB

CC

FF

GG

HH II

JJ

LL

KK

MM

DD EE FF

Page 24: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

BB

CCGG

HH II

JJ

LL

KK

MM

DD EE FF CC

Page 25: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

BB

GG

HH II

JJ

LL

KK

MM

DD EE FF CC BB

Page 26: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

GG

HH II

JJ

LL

KK

MM

DD EE FF CC BB II

Page 27: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

GG

HH

JJ

LL

KK

MM

DD EE FF CC BB II HH

Page 28: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

GG

JJ

LL

KK

MM

DD EE FF CC BB II HH GG

Page 29: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

AA

JJ

LL

KK

MM

DD EE FF CC BB II HH GG AA

Page 30: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

JJ

LL

KK

MM

DD EE FF CC BB II HH GG AA KK

Page 31: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

JJ

LL MM

DD EE FF CC BB II HH GG AA KK MM

Page 32: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

JJ

LL

DD EE FF CC BB II HH GG AA KK MM LL

Page 33: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

JJ

DD EE FF CC BB II HH GG AA KK MM LL JJ

Page 34: Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select

DD EE FF CC BB II HH GG AA KK MM LL JJ

J L M K A G H I B C F E DJ L M K A G H I B C F E D