51
Spanning Trees — §7.1 116 Minimum-weight spanning trees Motivation: Create a connected network as cheaply as possible. Think: Setting up electrical grid or road network. Some connections are cheaper than others. Only need to minimally connect the vertices.

Spanning Trees — Minimum-weight spanning trees

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 116

Minimum-weight spanning trees

Motivation: Create a connected network as cheaply as possible.

� Think: Setting up electrical grid or road network.

� Some connections are cheaper than others.

� Only need to minimally connect the vertices.

Page 2: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 116

Minimum-weight spanning trees

Motivation: Create a connected network as cheaply as possible.

� Think: Setting up electrical grid or road network.

� Some connections are cheaper than others.

� Only need to minimally connect the vertices.

Definition: A weighted graph consists of a graph G = (V ,E )and weight function w : E → R defined on the edges of G .The weight of a subgraph H of G is the sum of the edges in H.

Example.2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

Page 3: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 116

Minimum-weight spanning trees

Motivation: Create a connected network as cheaply as possible.

� Think: Setting up electrical grid or road network.

� Some connections are cheaper than others.

� Only need to minimally connect the vertices.

Definition: A weighted graph consists of a graph G = (V ,E )and weight function w : E → R defined on the edges of G .The weight of a subgraph H of G is the sum of the edges in H.

Example.2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

Definition: For a graph G , a spanning tree T is a subgraph of Gwhich is a tree and contains every vertex of G .

Goal: For a weighted graph G , find a minimum-weight spanning tree.

Page 4: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 117

Kruskal’s algorithm

Kruskal’s Algorithm finds a minimum-weight spanning tree in aweighted graph.

1 Initialization: Order the edges from lowest to highest weight:

w(e1) ≤ w(e2) ≤ w(e3) ≤ · · · ≤ w(ek).

Page 5: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 117

Kruskal’s algorithm

Kruskal’s Algorithm finds a minimum-weight spanning tree in aweighted graph.

1 Initialization: Order the edges from lowest to highest weight:

w(e1) ≤ w(e2) ≤ w(e3) ≤ · · · ≤ w(ek).

2 Step 1: Define T = {e1} and grow the tree as follows:

Page 6: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 117

Kruskal’s algorithm

Kruskal’s Algorithm finds a minimum-weight spanning tree in aweighted graph.

1 Initialization: Order the edges from lowest to highest weight:

w(e1) ≤ w(e2) ≤ w(e3) ≤ · · · ≤ w(ek).

2 Step 1: Define T = {e1} and grow the tree as follows:

3 Step i : Determine if adding ei to T would create a cycle.

� If not, add ei to the set T .� If so, do nothing.

Page 7: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 117

Kruskal’s algorithm

Kruskal’s Algorithm finds a minimum-weight spanning tree in aweighted graph.

1 Initialization: Order the edges from lowest to highest weight:

w(e1) ≤ w(e2) ≤ w(e3) ≤ · · · ≤ w(ek).

2 Step 1: Define T = {e1} and grow the tree as follows:

3 Step i : Determine if adding ei to T would create a cycle.

� If not, add ei to the set T .� If so, do nothing.

If you have a spanning tree, STOP. You have a m.w.s.t.Otherwise, continue onto step i + 1.

Page 8: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

2

4

72 9

33 5

5

6

3

7

a b c

f

h

d

g

e

Page 9: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

Page 10: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2a b

Page 11: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2a b

Page 12: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2a be2�2

b

h

Page 13: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

a b

h

Page 14: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

a b

he3�3

c

d

Page 15: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3

a b

h

c

d

Page 16: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3

a b

h

c

d

e4�3f

g

Page 17: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e4�3

a b

h

c

df

g

Page 18: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e4�3

a b

h

c

df

ge5�3

gh

Page 19: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e4�3

e5�3

a b

h

c

df

g

Page 20: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e4�3

e5�3

a b

h

c

df

ge6�4

a

f

Page 21: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e4�3

e5�3

a b

h

c

df

g

Page 22: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e4�3

e5�3

a b

h

c

df

ge7�5

d

h

Page 23: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e7�5e4�3

e5�3

a b

h

c

df

g

Page 24: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e7�5e4�3

e5�3

a b

h

c

df

g

e8�5

d

g

Page 25: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e7�5e4�3

e5�3

a b

h

c

df

g

Page 26: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e7�5e4�3

e5�3

a b

h

c

df

g

e9�6 de

Page 27: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 118

Kruskal’s algorithm

Example. Run Kruskal’s algorithm on the following graph:

e1�2

e6�4

e2�2e11�7

e3�3

e12�9

e7�5e8�5

e9�6

e4�3

e5�3

e10�7

a b

h

c

df

g

e

e1�2e2�2

e3�3e7�5

e9�6

e4�3

e5�3

a b

h

c

df

g

e

Page 28: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 119

Notes on Kruskal’s algorithm

� Proof of correctness similar to homework. Must additionallyverify that the spanning tree is indeed minimum-weight.

Page 29: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 119

Notes on Kruskal’s algorithm

� Proof of correctness similar to homework. Must additionallyverify that the spanning tree is indeed minimum-weight.

� Kruskal’s algorithm is an example of a greedy algorithm.(It chooses the cheapest edge at each point.)

Page 30: Spanning Trees — Minimum-weight spanning trees

Spanning Trees — §7.1 119

Notes on Kruskal’s algorithm

� Proof of correctness similar to homework. Must additionallyverify that the spanning tree is indeed minimum-weight.

� Kruskal’s algorithm is an example of a greedy algorithm.(It chooses the cheapest edge at each point.)

� Greedy algorithms don’t always work.

Page 31: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 120

The traveling salesman problem

Motivation: Visit all nodes and return home as cheaply as possible.

Page 32: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 120

The traveling salesman problem

Motivation: Visit all nodes and return home as cheaply as possible.

� Least cost trip flying between five major cities.

� Optimal routes for delivering mail, collecting garbage.

� Finding a trip to all buildings on campus, return.

Page 33: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 120

The traveling salesman problem

Motivation: Visit all nodes and return home as cheaply as possible.

� Least cost trip flying between five major cities.

� Optimal routes for delivering mail, collecting garbage.

� Finding a trip to all buildings on campus, return.

Goal: Find a minimum-weight Hamiltonian cycle in a weighted graph.

Page 34: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 120

The traveling salesman problem

Motivation: Visit all nodes and return home as cheaply as possible.

� Least cost trip flying between five major cities.� Optimal routes for delivering mail, collecting garbage.� Finding a trip to all buildings on campus, return.

Goal: Find a minimum-weight Hamiltonian cycle in a weighted graph.

1

2 3

1 3

2

1

2 1

100

a

b

c d

e

Page 35: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 120

The traveling salesman problem

Motivation: Visit all nodes and return home as cheaply as possible.

� Least cost trip flying between five major cities.� Optimal routes for delivering mail, collecting garbage.� Finding a trip to all buildings on campus, return.

Goal: Find a minimum-weight Hamiltonian cycle in a weighted graph.

1

2 3

1 3

2

1

2 1

100

a

b

c d

e

Page 36: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 120

The traveling salesman problem

Motivation: Visit all nodes and return home as cheaply as possible.

� Least cost trip flying between five major cities.� Optimal routes for delivering mail, collecting garbage.� Finding a trip to all buildings on campus, return.

Goal: Find a minimum-weight Hamiltonian cycle in a weighted graph.

1

2 3

1 3

2

1

2 1

100

a

b

c d

e

We can not use a greedy algorithm to find this TSP tour!

Page 37: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 121

The traveling salesman problem

� It is hard to find an optimum solution.

Page 38: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 121

The traveling salesman problem

� It is hard to find an optimum solution.

� Goal: Create an easy-to-find pretty good solution.

Page 39: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 121

The traveling salesman problem

� It is hard to find an optimum solution.

� Goal: Create an easy-to-find pretty good solution.

Theorem. When the edge weights satisfy the triangle inequality,the tree shortcut algorithm finds a tour that costs at most twicethe optimum tour.

Recall. The triangle inequality says that if x , y , and z arevertices, then wt(xy) + wt(yz) ≤ wt(xz).

Page 40: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 121

The traveling salesman problem

� It is hard to find an optimum solution.

� Goal: Create an easy-to-find pretty good solution.

Theorem. When the edge weights satisfy the triangle inequality,the tree shortcut algorithm finds a tour that costs at most twicethe optimum tour.

Recall. The triangle inequality says that if x , y , and z arevertices, then wt(xy) + wt(yz) ≤ wt(xz).

Example: Euclidean distances

Non-example: Airfares0.47

1.

0.75

x

y

z

Page 41: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

0.94

0.67

0.67

0.94

2.

2.

0.67

0.94

0.67

2.

0.94

2.

0.67

0.94

0.67

2.

0.94

2.

0.67

0.67

0.94

2.

2.

0.94

000

001

010

011

100

101

110

111

Page 42: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

0.94

0.67

0.67

0.94

2.

2.

0.67

0.94

0.67

2.

0.94

2.

0.67

0.94

0.67

2.

0.94

2.

0.67

0.67

0.94

2.

2.

0.94

000

001

010

011

100

101

110

111

Page 43: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

000

001

010

011

100

101

110

111

Page 44: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

000

001

010

011

100

101

110

111

11.5425

Page 45: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

000

001

010

011

100

101

110

111

10.9902

Page 46: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

000

001

010

011

100

101

110

111

10.4379

Page 47: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

000

001

010

011

100

101

110

111

9.88562

Page 48: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 122

Finding a good TSP-tour

The Tree Shortcut Algorithm to find a good TSP-tour

1 Find a minimum-weight spanning tree (Use Kruskal’s Algorithm)2 Walk in a circuit around the edges of the tree.3 Take shortcuts to find a tour.

000

001

010

011

100

101

110

111

9.27025

Page 49: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 123

Proof of theorem

Theorem. When the edge weights satisfy the triangle inequality,the tree shortcut algorithm finds a tour that costs at most twicethe optimum tour.

Page 50: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 123

Proof of theorem

Theorem. When the edge weights satisfy the triangle inequality,the tree shortcut algorithm finds a tour that costs at most twicethe optimum tour.

Proof. Define:

� TSPA: TSP tour from shortcutting spanning tree

� CIRCA: Circuit constructed by doubling spanning tree

� MST : Minimum-weight spanning tree

� TSP∗: Minimum-weight TSP tour

Page 51: Spanning Trees — Minimum-weight spanning trees

The Traveling Salesman Problem 123

Proof of theorem

Theorem. When the edge weights satisfy the triangle inequality,the tree shortcut algorithm finds a tour that costs at most twicethe optimum tour.

Proof. Define:

� TSPA: TSP tour from shortcutting spanning tree

� CIRCA: Circuit constructed by doubling spanning tree

� MST : Minimum-weight spanning tree

� TSP∗: Minimum-weight TSP tour

Then,

wt(TSPA) ≤ wt(CIRCA) = 2wt(MST ) ≤ 2wt(TSP∗).