Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Birgit Vogtenhuber Minimum Spanning Trees1
Minimum Spanning Trees
Design & Analysis of Algorithms WS 2019/20
Birgit Vogtenhuber Minimum Spanning Trees2 v
Trees in Graphs
• Given an unweighted connected graph G = (V,E), wecan compute a tree T with all shortest paths from aroot s to the other vertices using breadth first search.
• This does not work if G is a weighted graph!
• Given an unweighted connected graph G = (V,E) withn vertices, every subtree with n vertices has the sametotal edge length n− 1.
• This is not true if G is a weighted graph!
This topic: Trees in weighted graphs with minimum totaledge length (edge weight / edge cost).
Birgit Vogtenhuber Minimum Spanning Trees3 iv
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a set S of points, a minimum spanning tree of S isa tree that connects all points and minimizes the total edgelength among all trees on S.
spanning tree
Example:
Birgit Vogtenhuber Minimum Spanning Trees3 vi
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a set S of points, a minimum spanning tree of S isa tree that connects all points and minimizes the total edgelength among all trees on S.
minimum spanning tree
Example:
Observation:every (Euclidean)minimum spanning treeis a plane tree
Birgit Vogtenhuber Minimum Spanning Trees3 ix
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a set S of points, a minimum spanning tree of S isa tree that connects all points and minimizes the total edgelength among all trees on S.
Example applications:bicycle path network, electrical circuits, telephone network.
Possible problem:Direct connection from A to B not always possible or notproportional to the distance (example: mountain road).
⇒ Consider weighted graphs instead.
Birgit Vogtenhuber Minimum Spanning Trees3 x
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a weighted graph G = (V,E,w), a minimumspanning tree of G is a tree T = (V,E′) with E′ ⊆ Eand with minimal total edge length:
w(T ) =∑
e∈E′ w(e)
is minimized over all trees with vertex set V .
Birgit Vogtenhuber Minimum Spanning Trees3 xi
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a weighted graph G = (V,E,w), a minimumspanning tree of G is a tree T = (V,E′) with E′ ⊆ Eand with minimal total edge length:
1
38
7
4
5
6
92
Example:
weighted graph G
Birgit Vogtenhuber Minimum Spanning Trees3 xii
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a weighted graph G = (V,E,w), a minimumspanning tree of G is a tree T = (V,E′) with E′ ⊆ Eand with minimal total edge length:
minimumspanning tree of G
Example:
1
38
7
4
5
6
92
Birgit Vogtenhuber Minimum Spanning Trees3 xiii
Problem Definition
Basic task:Create connections between n locations with minimal cost.
Definition:Given a weighted graph G = (V,E,w), a minimumspanning tree of G is a tree T = (V,E′) with E′ ⊆ Eand with minimal total edge length:
w(T ) =∑
e∈E′ w(e)
is minimized over all trees with vertex set V .
Create connections between n locations with minimal cost:
Consider the complete weighted graph whose vertices arethe locations.
Birgit Vogtenhuber Minimum Spanning Trees4 ii
How Many Different Trees?
Answers:
• Complete graph Kn: nn−2 different spanning trees!!
• Plane graphs on n vertices: O(5.2852n) spanning trees
• Bounds on the number of different plane spanning treeson n points: Ω(6.75n), O(229.33n)
• Point sets known with Ω(12.52n) plane spanning trees
⇒ We can’t try them all !!
Questions:
• How many different spanning trees for a graph G?
• How many different plane spanning trees for n points?
Birgit Vogtenhuber Minimum Spanning Trees5
Iterative Algorithm Idea
Idea:Build a minimum spanning tree (MST) for a graphG = (V,E,w) by iteratively inserting edges:
E′ = 0while |E′| < n− 1 do
select an edge e ∈ E \ E′ which is ‘good’ for E′
E′ = E′ ∪ eodwrite E′
Edge e ∈ E \E′ is ‘good’ for E′ if E′ ∪ e is a subset of anMST of G (there can be more than one MST of G).
Birgit Vogtenhuber Minimum Spanning Trees6 v
Cuts in Graphs
A cut of a graph G = (V,E) is a partition of V into V1, V2.
V1 V2
external edges
An edge e is called external for the cut (V1, V2) if it hasone endpoint in V1 and one in V2; otherwise e is calledinternal.
Birgit Vogtenhuber Minimum Spanning Trees7 iv
Characterization of Good Edges
Theorem: Let E′ be a subset of edges of an MST ofG = (V,E,w). Let (V1, V2) be a cut of G for which alledges of X are internal. Then the external edge of the cutwith minimum weight is a good edge for E′.
e
V1
V2
set E′: green
external edges: black
good edge for E′: e
Example:w = Eucl. distance
Birgit Vogtenhuber Minimum Spanning Trees7 xii
Characterization of Good Edges
Proof:Assume there is an MST T with E′ and without e.
⇒ e closes a cycle C in T .
The cycle C contains at least one edge f of the tree T thatis external for the cut.
By definition of e, theweight w(f) ≥ w(e).
⇒ T\f ∪ e is anMST of G.
If w(f) > w(e) thenT is not an MST of G.
e
V1
V2
C
f
Birgit Vogtenhuber Minimum Spanning Trees7 xiv
Characterization of Good Edges
Theorem: Let E′ be a subset of edges of an MST ofG = (V,E,w). Let (V1, V2) be a cut of G for which alledges of X are internal. Then the external edge of the cutwith minimum weight is a good edge for E′.
e
V1
V2
C
f
Next:Two different greedyalgorithms that use thistheorem to efficientlycompute an MST
Difference:Choice of the cuts
Birgit Vogtenhuber Minimum Spanning Trees8 ii
Prim’s Algorithm
• Start with an arbitrary vertex s of G and iteratively‘grow’ an MST T from s.
• Iterative step:Choose the ‘cheapest’ edge with exactly one node in T .
• Cut: V1 = vertices of T , V2 = vertices not yet in T .
• For each vertex v 6∈ T we maintain: Priority p(v): weight of the shortest edge from v to a
vertex in T (initially: ∞). Nearest n(v): vertex in T realizing p(v): w(v, n(v)) is
min. among neighbors of v in T (initially no vertex).
• A queue Q contains all vertices not yet in T , organizedby priorities (e.g., in a heap; initially all vertices).
Birgit Vogtenhuber Minimum Spanning Trees9
Prim’s Algorithm
PRIM-MST (G,s)p(s) = 0p(v) =∞ ∀ v ∈ V \sn(s) = nilfor all v ∈ V do INSERT(v,Q) odwhile Q 6= 0 do
u = MIN(Q)write u, n(u)for all neighbors v of u do
if v ∈ Q and p(v) > w(u, v) thenp(v) = w(u, v), update Qn(v) = u
fiod
od
MST isfinishedhere
n(u)
X
u
Birgit Vogtenhuber Minimum Spanning Trees10
Prim’s Algorithm
Run-time-analysis:
n ... number of vertices of Gm ... number of edges of Gd(v) ... Degree of vertex v in G
• Initialization, construction of the heap Q: Θ(n)• n times removing the minimum from Q: O(n log n)• Update priorities for all neighbors of v: O(d(v) · log n)⇒ Alltogether:
O(n + n log n +∑
v∈V d(v) log n) = O(m log n)
Memory requirements:O(m + n) = O(m)
Birgit Vogtenhuber Minimum Spanning Trees11 iv
Prim’s Algorithm
Remarks:
• MST always begins at the start vertex s and growsfrom there as a connected tree.
• Shrinking p(v) causes v to move up the heap.⇒ O(log n) time.
• Test for v ∈ Q in O(1) time when bit vector is used tostore which vertices are already in T .
• Runtime can be changed to O(n2). This is useful fordense graphs (see notes on Dijkstra’s algorithm in thenext chapter).
Birgit Vogtenhuber Minimum Spanning Trees12 i
Kruskal’s Algorithm
• Start with empty edge set E′.
• Sort edge set E of G = (V,E,w) in increasing orderof their weights (edges will be inserted in this order):e1, e2, ...em with w(e1) < w(e2) < ... < w(em).
• Iterative step:
E′ forms a forest F (= set of disjoint subtrees,acyclic) in the MST to be constructed.
Edge e that is added to E′ is the shortest edge inE \ E′ that does not close a cycle in E′.
Birgit Vogtenhuber Minimum Spanning Trees12 ii
Kruskal’s Algorithm
Use a UNION-FIND data structure on V for thecomponents (subtrees) M1,M2, ...Mt of F :
• Label the vertex set of G as v1, v2, ...vn (arbitrary)
• Initially there are n disjoint sets M1,M2, ...Mn
(each with one vertex)
• FIND (v): returns index i if vertex v is in Mi
• UNION (i, j): join sets Mi and Mj : Mi = Mi ∪Mj
• End of the algorithm: one component M1 with allvertices of G.
Birgit Vogtenhuber Minimum Spanning Trees12 iii
Kruskal’s Algorithm
A UNION-FIND data structure can be realized such that:
• Creating a 1-element set needs Θ(1) time.
• f FIND and u UNION operations need O(f + u log u)time in total.
• The total memory requirement of the data structure islinear in the number of initial 1-element sets.
Birgit Vogtenhuber Minimum Spanning Trees13 i
Kruskal’s Algorithm
KRUSKAL-MST(G)sort edges by weight: e1, e2, . . . emfor i = 1 to n do Mi = vi odfor k = 1 to m do
(u, v) = eki =FIND(u)j =FIND(v)if i 6= j then
write ekUNION(i, j)
fiod
Birgit Vogtenhuber Minimum Spanning Trees13 iii
Kruskal’s Algorithm
Runtime analysis:
• Sorting of the edges: O(m logm)
• Initialize UNION-FIND data structure for vertices: Θ(n)
• In total 2m FIND operations and n− 1 UNIONoperations: O(m + n log n)
⇒ Altogether O(m logm) time.
⇒ Sorting of the edges dominates the runtime.
Memory requirements:
Θ(n + m) = Θ(m) in total.
Birgit Vogtenhuber Minimum Spanning Trees14 iii
Concluding Remarks
Remarks:
• Both algorithms also work if edge weights can benegative.
• If the calculation of the MST for a point set in a planewith Euclidean distance function is to be carried out(geometric version), this is possible in Ω(n log n) time.The main observation is that the MST is a subgraph ofthe Delaunay triangulation of the point set, which canbe computed in Ω(n log n) time.
• For both algorithms, animated versions are available(see course webpage).