0% found this document useful (0 votes)
4 views

12 - Chapter 21

Uploaded by

Alien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

12 - Chapter 21

Uploaded by

Alien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

CSCI 4470

Algorithms

Chapter 21

Lecture slides are adapted from the CSCI


4470 course taught by Dr Shelby Funk and
the slides provided by the publisher
SPANNING TREES ARE
GRAPHS
• A dag is a directed acyclic graph.

• A Spanning tree is a connected


acyclic undirected graph.

2
Spanning trees
A connected, undirected, acyclic (without cycles) graph is called a spanning tree, and a set of spann
is called a forest

A graph G is a spanning tree, if there is exactly one path between every pair of
vertices in G
SPANNING TREES

• Tree: an undirected connected graph without cycles


• Observations about undirected graphs
1. Connected undirected graph with n vertices must have at
least n – 1 edges.
2. Connected undirected graph with n vertices and
exactly n – 1 edges cannot contain a cycle
3. A connected undirected graph with n vertices and more
than n – 1 edges must contain at least one cycle
SPANNING TREES
• The DFS spanning tree rooted at
vertex a for the graph
SPANNING TREES
• The BFS spanning tree rooted at
vertex a for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Minimum Spanning
Trees

7
Problem: Laying Telephone
Wire

Central office

8
Wiring: Naïve Approach

Central office

Expensive!

9
Wiring: Better Approach

Central office

Minimize the total length of wire connecting the


customers

10
Minimum Spanning Tree (MST)

A minimum spanning tree is a subgraph of an undirected weighted and


connected graph G, such that


it is a tree (i.e., it is acyclic)

it covers all the vertices V

contains |V| - 1 edges

the total cost associated with tree edges
is the minimum among all possible
spanning trees

not necessarily unique

12
How Can We Generate a
MST?

9 b 9 b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4
5 e 5 e
c c

13
MINIMUM SPANNING
TREES
Given a connected,
undirected graph
G=(V,E) with edge 
T may not be unique
weights w(u,v), find
acyclic subset T ⊆ E

T will have |V|-1
that connects all edges
vertices and minimizes
total weight 
Any subset of E
containing |V|-1
edges that connects
T is called a minimum all vertices is called
spanning tree a spanning tree

T is minimum
because it has
minimum weight
TWO ALGORITHMS

Kruskal’s and Prim’s


Both algorithms are greedy and have same general format

Add safe edges to A one by one until |A| = |V|-1


Given a subset A of E, where A is a subset of some minimum
spanning tree, we say (u,v) is safe for A if A∪{(u,v)} is also a
subset of a minimum spanning tree


We’ll first look at generic properties of MSTs and then discuss
Kruskal’s and Prim’s algorithms
GENERIC-MST(G,W)
1. A = Ø
2. while A does not form a spanning tree
3. find an edge (u,v) that is safe for A
4. A = A ∪ {(u,v)}
5. return A

• Challenge: determining which edges are safe


for A
A LITTLE VOCABULARY
Cut respecting
Cut Crossing edge Light edge
A

Let S be a 
An edge (u,v) 
Let A be a 
An edge is a
subset of V crosses the subset of E light edge

A cut of G cut if one 
A cut (S,V-S) crossing the
(S,V-S) is a endpoint is respects A if cut if it has
partition of V in S and the no edge in A minimum
other crosses (S,V- weight of all
endpoint is S) edges
in V-S 
I.e., both crossing the
endpoints cut
of A are 
Light
either in A edges may
or in V-S not be
unique
FINDING SAFE EDGES
Theorem: If cut (S,V-S) respects A, and (u,v) is a light edge for
the cut, then (u,v) is safe for A
1 b
c 3
h
8 7 4
3
6
7
3 g
d a

6 3 1
3
2 f
e
1 b 3
c
f
3 8 7 4

FINDING SAFE EDGES


6
7
d x 3 y
3 1
6 3
2 v
u

Theorem: If cut (S,V-S) respects A, and (u,v) is a light edge for


the cut, then (u,v) is safe for A

Proof by contradiction


Assume we have a MST T such that A  T and (u,v) 
T

Let P be the unique simple path from u to v in T

Let (x,y) be an edge in P that crosses the cut (S,V-S)

We construct another MST T’ that contains (u,v)
1 b 3
c

REPLACE (X,Y) WITH


f
3 8 7 4
6
7
d x 3 y

(U,V) 6

u
3
2
3
v
1

Claim: Replacing (x,y) with (u,v) will still result in a


spanning tree and the total weight will not increase

Need to show two things

Replacing (x,y) with (u,v)


in T will give a spanning w(T’) ≤ w(T)
tree T’
1 b 3
c
f
8 7 4

T’ IS A SPANNING TREE
3 6
7
d x 3 y
3 1
6 3
2 v
u

Adding (u,v) to Removing (x,y) Let q, r be any


T will create a breaks the two vertices
cycle cycle and let Pqr be
containing 
Let T’ = T\ the path from
(u,v) and (x,y) {(x,y)}∪ {(u,v)} q to r in T

T U {(u,v)} 
Want to show that
contains V edges ∃ a path from q to

∃ a path in T from r in T’
u to v containing
(x,y)
1

2 THINGS TO SHOW
c b 3
f
3 8 7 4
6
7

ABOUT T’
d x 3 y
3 1
6 3
2 v
u

∃ path from w(T) ≥


q to r in T’ w(T’)
COROLLARY
Assume the following:
G = (V,E) is a C = (VC,EC) is a
connected T is some minimum connected
undirected graph spanning tree and component (i.e., a

function w:E → ℝ.
with weight A ⊆ T. tree) of the forest
GA = (V,A).
Then if (u,v) is a light edge connecting C to some other
component of GA, then (u,v) is safe for A.
C V-C
a 5
u d f
3
b
v
c 7
e
KRUSKAL(G,W)
1. A=Ø
2. for each v in V
3. MAKE-SET(v) // uses the disjoint forest data structure
4. sort the edges of E in nondecreasing order by w
5. for each (u,v) ∈ G.E // consider in sorted order
6. if FIND-SET(u) ≠ FIND-SET(v)
7. A = A ∪ {(u,v)}
8. UNION(u,v)
9. return A
Minimum Spanning Trees
Kruskal’s Algorithm

Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv

A 4
4
3 (D,E) 1 (B,E) 4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1 (C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10

Accepting edge (E,G) would create


a cycle
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4 (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
8 4
6 (D,G) 2  (B,F) 4
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
8 4
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
8 4
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1
(C,D) 3  (A,H) 5
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
Select first |V|–1 edges which
do not generate a cycle
3
10
F C edge dv edge dv

A 4 3 (D,E) 1  (B,E) 4 
8 4
6 (D,G) 2  (B,F) 4 
5
4
B D (E,G) 3  (B,H) 4 
4
H 1
(C,D) 3  (A,H) 5 
2
3 (G,H) 3  (D,F) 6
G 3
E (C,F) 3  (A,B) 8
(B,C) 4  (A,F) 10
KRUSKAL RUNTIME
1. A=Ø
2. for each v in V
3. MAKE-SET(v) // uses the disjoint forest data structure
4. sort the edges of E in nondecreasing order by w
5. for each (u,v) ∈ G.E // consider in sorted order
6. if FIND-SET(u) ≠ FIND-SET(v)
7. A = A ∪ {(u,v)}
8. UNION(u,v)
9. return A
CORRECTNESS
• Kruskal’s algorithm builds the MST based on
the corollary presented earlier.

If C is a connected component of A and


(u,v) is a light edge connecting C to some
other component of GA, then (u,v) is safe
for A.
A=Ø Sorted Edges
for each v in V
(f,g) 1
MAKE-SET(v)
sort the edges of E by w
for each (u,v) ∈ G.E
EXAMPLE (c,b) 1
(e,f) 2
if FIND-SET(u) ≠ FIND- 1
SET(v)
b 3 (b,h) 3
c
A = A U {(u,v)} h (a,g) 3
UNION(u,v) 8 7 4
3 (c,d) 3
6
return A
7 (a,e) 3
3 g
d a (a,f) 3
6 3 1 (a,h) 4
3
(d,e) 6
2 f
e (g,h) 6
(c,e) 7
A
A
AA
A
AA
=
A=
=
==
=
==
{(f,g),(c,b),(e,f),(b,h),(a,g),(c,d),(a,h)}
{(f,g),(c,b),(e,f),(b,h),(a,g),(c,d)}
{(f,g),(c,b),(e,f),(b,h),(a,g)}
{(f,g),(c,b),(e,f),(b,h)}
{(f,g),(c,b),(e,f)}
{(f,g),(c,b)}
{(f,g)}
Ø
(a,b) 7
(a,c) 8
PRIM(G,W,R)
1. //r is root of spanning tree
2. for each u in G.V
3. u.key = ∞
4. u.π = NIL
5. r.key = 0
6. Q = G.V
7. while Q ≠ Ø
8. u = EXTRACT-MIN(Q)
9. for each v ∈ G.adj[u]
10. if v ∈ Q and w(u,v) < v.key
11. v.π = u
12. v.key = w(u,v)
13. Decrease-key(Q, V, w(u,v)) //decrease v’s key
Walk-Through
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
Walk-Through
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
10
F C
A 7
4
3
8
18
4
9
B D
10
H 25
2
3
G 7
E
2

3
F C
A 4
3

4
B D
H
2
3
G E

Cost of Minimum Spanning


Tree =
 dv = 21
PRIM’S RUNTIME
1. //r is root of spanning tree
2. for each u in G.V
3. u.key = ∞
4. u.π = NIL
5. r.key = 0
6. Q = G.V
7. while Q ≠ Ø
8. u = EXTRACT-MIN(Q)
9. for each v ∈ G.adj[u]
10. if v ∈ Q and w(u,v) < v.key
11. v.π = u
12. v.key = w(u,v)
13. Decrease-key(Q, V, w(u,v))
CORRECTNESS
• Prim’s algorithm builds the MST by
constantly having A be a tree of a
subset of the nodes.
– Both algorithms are greedy, they just
select from a different candidate set.
CONCLUSION
• Given a connected graph G with weighted edges, a Minimum
Spanning Tree for G is a connected subgraph of G with |V|-1
edges and minimum total weight
• 2 algorithms
– Kruskal
• O(E lg V)
– Prim
• O(E + V lg V)
• Both algorithms are built on the concept of adding safe edges
to the set
– Kruskal’s algorithm adds edges that may be disconnected
• Once an edge is added it remains in the set
– Prim’s algorithms have edges that always form a tree
• Edges may be removed if they can be replaced with lighter edgesM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy