data structures and advanced programming · properties introduction 4 a connected graph can have...

50
CS062 DATA STRUCTURES AND ADVANCED PROGRAMMING 42: Minimum Spanning Trees Alexandra Papoutsaki LECTURES Mark Kampe LABS Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition GRAPHS

Upload: others

Post on 08-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

CS062 DATA STRUCTURES AND ADVANCED PROGRAMMING

42: Minimum Spanning Trees

Alexandra PapoutsakiLECTURES

Mark KampeLABS

Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition

GRAPHS

Page 2: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TODAY’S LECTURE IN A NUTSHELL

Lecture 42: Minimum Spanning Trees

▸ Introduction

▸ Kruskal’s Algorithm

▸ Prim’s Algorithm

�2

Some slides adopted from Algorithms 4th Edition or COS226

Page 3: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

Spanning Trees

�3INTRODUCTION

▸ Given an edge weighted graph � (not digraph!), a spanning tree of � is a subgraph � that is:

▸ A tree: connected and acyclic.

▸ Spanning: includes all of the vertices of � .

G G T

G

https://www.tutorialspoint.com/data_structures_algorithms/spanning_tree.htm

Page 4: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

Properties

�4INTRODUCTION

▸ A connected graph � can have more than one spanning tree.

▸ All possible spanning trees of � , have the same number of vertices and edges.

▸ A spanning tree has � edges.

▸ A spanning tree by definition cannot have any cycle.

▸ Adding one edge to the spanning tree would create a cycle (i.e. spanning trees are maximally acyclic).

▸ Removing one edge from the spanning tree would make the graph disconnected (i.e. spanning trees are minimally connected).

G

G

|V | − 1

https://www.tutorialspoint.com/data_structures_algorithms/spanning_tree.htm

Page 5: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

Minimum spanning tree problem

�5INTRODUCTION

▸ Given a connected edge-weighted undirected graph find a spanning tree of minimum weight.

Page 6: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

Minimum spanning applications

�6INTRODUCTION

▸ Network design

▸ Cluster analysis

▸ Cancer imaging

▸ Cosmology

▸ Weather data interpretation

▸ Many others

▸ https://www.ics.uci.edu/~eppstein/gina/mst.html

▸ https://personal.utdallas.edu/~besp/teaching/mst-applications.pdf

Page 7: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TODAY’S LECTURE IN A NUTSHELL

Lecture 42: Minimum Spanning Trees

▸ Introduction

▸ Kruskal’s Algorithm

▸ Prim’s Algorithm

�7

Page 8: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

KRUSKAL’S ALGORITHM

Kruskal’s algorithm

▸ Sort edges in ascending order of weight.

▸ Starting from the one with the smallest weight, add it to the MST � unless doing so would create a cycle.

▸ Uses a data structure called Union-Find (Chapter 1.5 in book).

▸ Running time of � in worst case.

T

|E | log |V |

�8

Page 9: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �9

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

http://algs4.cs.princeton.edu

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

KRUSKAL'S ALGORITHM DEMO

Page 10: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �10

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

2

Kruskal's algorithm demo

5

4

7

13

0

2

6

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

6-0 0.58

6-4 0.93

graph edgessorted by weight

an edge-weighted graph

Page 11: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �11

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

3

Kruskal's algorithm demo

0-7 0.16

5

4

7

13

0

2

6

does not create a cycle

in MST

Page 12: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �12

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

4

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

5

4

7

13

0

2

6

in MST

does notcreate a cycle

Page 13: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �13

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

5

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

5

4

7

13

0

2

6

in MST

does not create a cycle

Page 14: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �14

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

6

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.265

4

7

13

0

2

6

in MST

does not create a cycle

Page 15: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �15

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

7

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.285

4

7

13

0

2

6

in MST

does not create a cycle

Page 16: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �16

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

8

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

5

4

7

13

0

2

6

creates a cycle

not in MST

Page 17: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �17

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

9

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

5

4

7

13

0

2

6

creates a cycle

not in MST

Page 18: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �18

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

10

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

5

4

7

13

0

2

6

creates a cycle

not in MST

Page 19: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �19

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

11

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

5

4

7

13

0

2

6

in MST

does not create a cycle

Page 20: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �20

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

12

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

5

4

7

13

0

2

6

creates a cycle

not inMST

Page 21: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �21

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

13

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

5

4

7

13

0

2

6

creates a cycle

not inMST

Page 22: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �22

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

14

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

5

4

7

13

0

2

6

creates a cycle not in MST

Page 23: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �23

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

15

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

5

4

7

13

0

2

6

in MSTdoes not create a cycle

Page 24: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �24

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

16

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

5

4

7

13

0

2

6

creates a cycle

not in MST

Page 25: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �25

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

17

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

6-0 0.58

5

4

7

13

0

2

6

creates a cycle

not in MST

Page 26: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �26

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

18

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

6-0 0.58

6-4 0.93

5

4

7

13

0

2

6

creates a cycle

not in MST

Page 27: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �27

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

19

Kruskal's algorithm demo

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

6-0 0.58

6-4 0.93

5

4

7

13

0

2

6

a minimum spanning tree

Page 28: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

KRUSKAL’S ALGORITHM

Practice Time

�28

Page 29: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

KRUSKAL’S ALGORITHM

Answer

�29

Page 30: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TODAY’S LECTURE IN A NUTSHELL

Lecture 42: Minimum Spanning Trees

▸ Introduction

▸ Kruskal’s Algorithm

▸ Prim’s Algorithm

�30

Page 31: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

PRIM’S ALGORITHM

Prim’s algorithm

▸ Start with a random vertex (here, 0) and greedily grow tree � .

▸ Add to � the min weight edge with exactly one endpoint in � .

▸ Repeat until � edges.

▸ Two versions, lazy and eager. We will see lazy, here…

▸ Uses min-priority queue.

▸ Running time of � in worst case, as well.

T

T T

|V | − 1

|E | log |V |

�31

Page 32: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �32

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

3

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 0.162-3 0.171-7 0.190-2 0.265-7 0.281-3 0.291-5 0.322-7 0.344-5 0.351-2 0.364-7 0.370-4 0.386-2 0.403-6 0.526-0 0.586-4 0.93

an edge-weighted graph

Page 33: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �33

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

4

Prim's algorithm demo

5

4

7

13

0

2

6

Page 34: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �34

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

5

Prim's algorithm demo

0

0-7 0.160-2 0.260-4 0.386-0 0.58

edges with exactlyone endpoint in T(sorted by weight)

5

4

7

13

2

6

in MST

min weight edge withexactly one endpoint in T

Page 35: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �35

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

6

Prim's algorithm demo

5

4

7

13

0

2

6

0-7

MST edges

Page 36: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �36

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

7

Prim's algorithm demo

5

4

7

13

0

2

6

1-7 0.190-2 0.265-7 0.282-7 0.344-7 0.370-4 0.386-0 0.58

in MST

edges with exactlyone endpoint in T(sorted by weight)

0-7

MST edges

min weight edge withexactly one endpoint in T

Page 37: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �37

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

8

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 1-7

MST edges

Page 38: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �38

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

9

Prim's algorithm demo

5

4

7

13

0

2

6

0-2 0.265-7 0.281-3 0.291-5 0.322-7 0.341-2 0.364-7 0.370-4 0.386-0 0.58

edges with exactlyone endpoint in T(sorted by weight)

in MST

0-7 1-7

MST edges

min weight edge withexactly one endpoint in T

Page 39: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �39

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

10

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 1-7 0-2

MST edges

Page 40: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �40

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

11

Prim's algorithm demo

5

4

7

13

0

2

6

2-3 0.175-7 0.281-3 0.291-5 0.324-7 0.370-4 0.386-2 0.406-0 0.58

edges with exactlyone endpoint in T(sorted by weight)

in MST

0-7 1-7 0-2

MST edges

min weight edge withexactly one endpoint in T

Page 41: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �41

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

12

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 1-7 0-2 2-3

MST edges

Page 42: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �42

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

13

Prim's algorithm demo

5

4

7

13

0

2

6

5-7 0.281-5 0.324-7 0.370-4 0.386-2 0.403-6 0.526-0 0.58

edges with exactlyone endpoint in T(sorted by weight)

in MST

0-7 1-7 0-2 2-3

MST edges

min weight edge withexactly one endpoint in T

Page 43: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �43

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

14

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 1-7 0-2 2-3 5-7

MST edges

Page 44: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �44

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

15

Prim's algorithm demo

5

4

7

13

0

2

6

4-5 0.354-7 0.370-4 0.386-2 0.403-6 0.526-0 0.58

edges with exactlyone endpoint in T(sorted by weight)

in MST

0-7 1-7 0-2 2-3 5-7

MST edges

min weight edge withexactly one endpoint in T

Page 45: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �45

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

16

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 1-7 0-2 2-3 5-7 4-5

MST edges

Page 46: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �46

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

17

Prim's algorithm demo

5

4

7

13

0

2

6

6-2 0.403-6 0.526-0 0.586-4 0.93

edges with exactlyone endpoint in T(sorted by weight)

in MST

0-7 1-7 0-2 2-3 5-7 4-5

MST edges

min weight edge withexactly one endpoint in T

Page 47: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

TEXT �47

・Start with vertex 0 and greedily grow tree T.

・Add to T the min weight edge with exactly one endpoint in T.

・Repeat until V - 1 edges.

18

Prim's algorithm demo

5

4

7

13

0

2

6

0-7 1-7 0-2 2-3 5-7 4-5 6-2

MST edges

Page 48: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

PRIM’S ALGORITHM

Practice Time

�48

Page 49: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

PRIM’S ALGORITHM

Answer…

�49

Page 50: DATA STRUCTURES AND ADVANCED PROGRAMMING · Properties INTRODUCTION 4 A connected graph can have more than one spanning tree. All possible spanning trees of , have the same number

ASSIGNED READINGS AND PRACTICE PROBLEMS

Readings:

▸ Textbook: Chapter 4.3 (Pages 604-629)

▸ Website:

▸ https://algs4.cs.princeton.edu/43mst/

�50

Practice Problems:

https://visualgo.net/en/mst