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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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 — §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
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.
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).
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:
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.
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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.)
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.
The Traveling Salesman Problem 120
The traveling salesman problem
Motivation: Visit all nodes and return home as cheaply as possible.
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.
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.
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
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
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!
The Traveling Salesman Problem 121
The traveling salesman problem
� It is hard to find an optimum solution.
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.
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).
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
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
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
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
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
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
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
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
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
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.
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
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