Transcript
Page 1: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Spanning TreesLecture 20

CS2110 – Fall 2014

1

Page 2: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Spanning Trees

Definitions Minimum spanning trees 3 greedy algorithms (incl. Kruskal’s &

Prim’s) Concluding comments:

Greedy algorithms Travelling salesman problem

2

Page 3: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Undirected Trees

• An undirected graph is a tree if there is exactly one simple path between any pair of vertices

3

Page 4: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Facts About Trees

• |E| = |V| – 1• connected• no cycles

In fact, any two of these properties imply the third, and imply that the graph is a tree

4

Page 5: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Spanning Trees

A spanning tree of a connected undirected graph (V,E) is a subgraph (V,E') that is a tree

5

Page 6: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Spanning Trees

A spanning tree of a connected undirected graph (V,E) is a subgraph (V,E') that is a tree

• Same set of vertices V

• E' E⊆

• (V,E') is a tree

6

Page 7: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Spanning Trees: Examples

http://mathworld.wolfram.com/SpanningTree.html

7

Page 8: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Finding a Spanning Tree

A subtractive method

• If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?)

• Repeat until no more cycles

• Start with the whole graph – it is connected

8

Page 9: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?)

• Repeat until no more cycles

• Start with the whole graph – it is connected

Finding a Spanning Tree

A subtractive method

9

Page 10: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?)

• Repeat until no more cycles

• Start with the whole graph – it is connected

Finding a Spanning Tree

A subtractive method

10

Page 11: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

An additive method

• If more than one connected component, insert an edge between them – still no cycles (why?)

• Repeat until only one component

• Start with no edges – there are no cycles

Finding a Spanning Tree11

Page 12: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If more than one connected component, insert an edge between them – still no cycles (why?)

• Repeat until only one component

• Start with no edges – there are no cycles

An additive method

Finding a Spanning Tree12

Page 13: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If more than one connected component, insert an edge between them – still no cycles (why?)

• Repeat until only one component

• Start with no edges – there are no cycles

An additive method

Finding a Spanning Tree13

Page 14: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If more than one connected component, insert an edge between them – still no cycles (why?)

• Repeat until only one component

• Start with no edges – there are no cycles

An additive method

Finding a Spanning Tree14

Page 15: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If more than one connected component, insert an edge between them – still no cycles (why?)

• Repeat until only one component

• Start with no edges – there are no cycles

An additive method

Finding a Spanning Tree15

Page 16: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

• If more than one connected component, insert an edge between them – still no cycles (why?)

• Repeat until only one component

• Start with no edges – there are no cycles

An additive method

Finding a Spanning Tree16

Page 17: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Minimum Spanning Trees

• Suppose edges are weighted, and we want a spanning tree of minimum cost (sum of edge weights)

• Some graphs have exactly one minimum spanning tree. Others have multiple trees with the same cost, any of which is a minimum spanning tree

17

Page 18: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Minimum Spanning Trees

• Suppose edges are weighted, and we want a spanning tree of minimum cost (sum of edge weights)

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

• Useful in network routing & other applications

• For example, to stream a video

10

14

16

18

Page 19: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

14

16

19

Page 20: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

14

16

20

Page 21: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

6211

1227

49 51

3

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

14

16

21

Page 22: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

6211

1227

49 51

3

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

14

16

22

Page 23: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

1

2

5

66

22 2824

34

72

64

825

54

6211

1227

49 51

3

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

14

16

23

Page 24: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

4

13

9

6

7

21

15

1

2

5

22 24

825

54

11

12

3

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

14

16

24

Page 25: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

4

13

9

6

7

15

1

2

58

25

54

11

12

3

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

14

16

25

Page 26: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

14

4

9

6

7

1

2

58

25

54

11

12

10

A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it

16

26

Page 27: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

14

16

27

Page 28: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

14

16

28

Page 29: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

14

16

29

Page 30: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

14

16

30

Page 31: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

16

31

Page 32: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

16

32

Page 33: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it

Kruskal's algorithm

33

Page 34: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

34

Page 35: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

35

Page 36: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

36

Page 37: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

37

Page 38: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

38

Page 39: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

39

Page 40: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle

Prim's algorithm (reminiscent of Dijkstra's algorithm)

33

14

4

13

9

632

40

7

16

21

15

100

1

2

5

66

22 2824

34

72

64

825

54

101

6211

1227

49 51

3

10

40

Page 41: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

3 Greedy Algorithms

14

4

9

6

7

1

2

58

25

54

11

12

10

• When edge weights are all distinct, or if there is exactly one minimum spanning tree, the 3 algorithms all find the identical tree

16

41

Page 42: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Prim’s Algorithm

O(m + n log n) for adj list Use a PQ Regular PQ produces time O(n + m log

m) Can improve to O(m + n log n) using a

fancier heap

prim(s) {

D[s] = 0; //start vertex

D[i] = ∞ for all i≠s; while (some vertices are unmarked) {

v = unmarked vertex with smallest D;

mark v;

for (each w adj to v)

D[w] = min(D[w], c(v,w));

}

}

• O(n2) for adj matrix– While-loop is executed n times– For-loop takes O(n) time

42

Page 43: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Application of MST

Maze generation using Prim’s algorithm

43

http://en.wikipedia.org/wiki/File:MAZE_30x20_Prim.ogv

The generation of a maze using Prim's algorithm on a randomly weighted grid graph that is 30x20 in size.

Page 44: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

More complicated maze generation

44

http://www.cgl.uwaterloo.ca/~csk/projects/mazes/

Page 45: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Greedy Algorithms

These are examples of Greedy Algorithms

The Greedy Strategy is an algorithm design technique

Like Divide & Conquer

Greedy algorithms are used to solve optimization problems

The goal is to find the best solution

Works when the problem has the greedy-choice property

A global optimum can be reached by making locally optimum choices

Example: Change Making Problem

Given an amount of money, find the smallest number of coins to make that amount

Solution: Use a Greedy Algorithm Give as many large coins as

you can This greedy strategy produces

the optimum number of coins for the US coin system

Different money system greedy strategy may fail

Example: old UK system

45

Page 46: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Similar Code Structures

while (some vertices are unmarked) {

v = best unmarked vertex

mark v;

for (each w adj to v)

update D[w];

}

• Breadth-first-search (bfs)–best: next in queue–update: D[w] = D[v]+1

• Dijkstra’s algorithm–best: next in priority queue–update: D[w] = min(D[w], D[v]+c(v,w))

• Prim’s algorithm–best: next in priority queue–update: D[w] = min(D[w], c(v,w))

here c(v,w) is the vw edge weight

46

Page 47: SPANNING TREES Lecture 20 CS2110 – Fall 2014 1. Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)

Traveling Salesman Problem Given a list of cities and the distances

between each pair, what is the shortest route that visits each city exactly once and returns to the origin city? The true TSP is very hard (NP complete)… for

this we want the perfect answer in all cases, and can’t revisit.

Most TSP algorithms start with a spanning tree, then “evolve” it into a TSP solution. Wikipedia has a lot of information about packages you can download…

47


Top Related