cmsc 132: object-oriented programming ii · minimum spanning tree ... prim's algorithm...

30
CMSC 132: Object-Oriented Programming II Minimum Spanning Trees 1

Upload: others

Post on 14-Aug-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

CMSC 132: Object-Oriented Programming II

Minimum Spanning Trees

1

Page 2: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Minimum spanning tree • Given: Undirected graph G with positive edge weights (connected). • Definition: A spanning tree of G is a subgraph T that is connected

and acyclic. • Goal: Find a min weight spanning tree.

2

Graph G

Page 3: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Minimum spanning tree • Given: Undirected graph G with positive edge weights (connected). • Definition: A spanning tree of G is a subgraph T that is connected

and acyclic. • Goal: Find a min weight spanning tree.

3

Spanning Tree T: cost = 4+6+8+5+11+9+7 = 50

Page 4: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

MST of random graph

4https://who.rocq.inria.fr/Nicolas.Broutin/gallery.html

Page 5: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Simplifying assumptions • Simplifying assumptions.

• Edge weights are distinct. • Graph is connected.

• Consequence. MST exists and is unique.

5

Page 6: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

MST Algorithms• Greedy Algorithms: Prim’s and Kruskal’s.

• Both Prim’s and Kruskal’s Algorithms work with undirected graphs

• Both work with weighted and unweighted graphs but are more interesting when edges are weighted

• Both are greedy algorithms that produce optimal solutions

6

Page 7: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm

• Minimum-spanning-tree algorithm• Consider edges in ascending order of weight.• Add next edge to tree T unless doing so would create

a cycle. • If the graph is not connected, then it finds a minimum

spanning forest

7

Page 8: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

8

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Consider an undirected, weight graph

Page 9: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

9

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (E,1,D)

Page 10: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

10

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (H,3,G)

Page 11: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

11

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (E,4,B)

Page 12: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

12

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (F,4,B)

Page 13: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

13

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (G,4,E)

Page 14: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

14

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (F,5,C)

Page 15: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

15

8

1

A

HB

F

E

D

C

G4

5

4

6

1315

5

48

14

3

10

Add Edge (B,8,A)

Page 16: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal's algorithm Demo

16

1

A

HB

F

E

D

C

G4

4

5

48

3

Cost: 3+4+1+4+8+4+5 = 29

Page 17: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal’s Algorithm Demo

17

Page 18: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Kruskal’s Algorithm Demo

18

Page 19: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

19

Prim's algorithm• Builds the tree one vertex at a time• Starts from an arbitrary starting vertex• Each step adds the cheapest possible connection from

the tree to another vertex.

Page 20: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G3

2

4

6

315

9

48

14

3

10

Page 21: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (E,1,D)

Page 22: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (D,2,G)

Page 23: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (D,3,C)

Page 24: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (G,3,H)

Page 25: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (E,4,B)

Page 26: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (F,4,B)

Page 27: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

315

9

48

14

3

10

Add Edge (H,5,A)

Page 28: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim's algorithm Demo

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G2

4

34

3

Cost: 5+3+2+3+1+4+4 = 20

Page 29: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim’s Algorithm Demo

29

Page 30: CMSC 132: Object-Oriented Programming II · Minimum spanning tree ... Prim's algorithm •Builds the tree one vertex at a time •Starts from an arbitrary starting vertex ... 2 4

Prim’s Algorithm Demo

30