8/17/2019 Graphs Spanning
1/51
James Tam
Minimum Spanning Trees
•In this section of notes you will learn twoalgorithms for creating a connected graphat minimum cost as well as a method forordering a graph
8/17/2019 Graphs Spanning
2/51
James Tam
Minimum Spanning Trees
•Applies to weighted, undirected and connected graph
•Create the minimum number of edges/arcs so that all
nodes/vertices are connected
8/17/2019 Graphs Spanning
3/51
James Tam
Example Of A Problem Dealing With MinimumSpanning Trees
http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=22960&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998
8/17/2019 Graphs Spanning
4/51
James Tam
Example Of A Problem Dealing With MinimumSpanning Trees
http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=22960&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998
8/17/2019 Graphs Spanning
5/51
James Tam
Example Of A Problem Dealing With MinimumSpanning Trees
$$$
http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=22960&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=21230&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998
8/17/2019 Graphs Spanning
6/51
James Tam
Example Of A Problem Dealing With MinimumSpanning Trees
http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=17711&size=big&papass=&sort=1&thecat=998
8/17/2019 Graphs Spanning
7/51James Tam
Algorithms For Determining The MinimumSpanning Tree
•Prims Algorithm
•!rus"als Algorithm
8/17/2019 Graphs Spanning
8/51James Tam
Prim’s Algorithm
primsAlgorithm #$raph g, Tree t, %ode start&
'
Priority(ueue nodes)eft * new Priority(ueue#&+
%ode temp+
%ode neighbor+
for #int i * + i -* g.no%odes #&+ i&
'
g0i1.set2eight * 3+
g0i1.setParent #null&+
nodes)eft.add #g0i1&+
4
start.set2eight #5&+
Pseudo code is based roughly on the algorithm provided by Matthew A. Becker
http://www.andrew.cmu.edu/user/mbecker/
8/17/2019 Graphs Spanning
9/51James Tam
Prim’s Algorithm (2
while #nodes)eft.is6mpty#& ** false&
'
temp * nodes)eft.de7ueue8in2eight%ode #&+
t.add #temp&+
for each node 9neighbor: which is ad;acent to temp and is in nodes)eft
'
if #weight #temp, neighbor& - neighbor.get2eight#&&
'
neighbor.setParent #temp&+
neighbor.set2eight #weight #temp, neighbor&&+ 4
4
4
4Pseudo code based roughly on the algorithm provided by Matthew A. Becker
http://www.andrew.cmu.edu/user/mbecker/
8/17/2019 Graphs Spanning
10/51James Tam
Example! Fin"ing The Minimum Spanning Tree #nA $raph (Prim’s Algorithm
i
a b
c
d
e
f
g
h
4
6
!
"
#
$%
4
&
$
8/17/2019 Graphs Spanning
11/51James Tam
#nitiali%e" &alues For The $raph An" 'ueue
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Graph
Queue
2eight ) ) ) ) ) ) ) )
%ode A * + D E F $ , #
Predecessor
Tree
8/17/2019 Graphs Spanning
12/51James Tam
Mar- .A/ As &isite"0 1p"ate eighbors
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 ) ) ) 4 ) ) 2
%ode A * + D E F $ , #
Predecessor 5 A A A
Tree
a
Graph
8/17/2019 Graphs Spanning
13/51James Tam
Mar- o"e .#/ As &isite" An" #n5lu"eThe E"ge (A0#
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 ) ) ) 4 ) ) <
%ode A * + D E F $ , I
Predecessor 5 A A A
Tree
a
i
Graph
8/17/2019 Graphs Spanning
14/51James Tam
Mar- o"e .F/ As &isiste" An" #n5lu"eThe E"ge (A0F
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 ) ) ) = 2 ) <
%ode A * + D E > $ , I
Predecessor 5 A A F A
Tree
a
i
f
Graph
8/17/2019 Graphs Spanning
15/51James Tam
Mar- o"e .$/ As &isiste" An" #n5lu"eThe E"ge (F0$
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 ) 6 7 4 < ) <
%ode A * + D E F $ , I
Predecessor 5 A $ $ A > A
Tree
a
i
f
g
Graph
8/17/2019 Graphs Spanning
16/51James Tam
Mar- o"e .D/ As &isiste" An" #n5lu"eThe E"ge (D0$
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 4 ? 7 = < 8 <
%ode A * + @ E > $ , I
Predecessor 5 A D $ $ A > D A
Tree
a
i
f
g
d
Graph
8/17/2019 Graphs Spanning
17/51James Tam
Mar- o"e .,/ As &isiste" An" #n5lu"eThe E"ge (D0,
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 4 ? 7 = < <
%ode A * + @ E > $ I
Predecessor 5 A D $ $ A > @ A
Tree
a
i
f
g
d h
Graph
8/17/2019 Graphs Spanning
18/51James Tam
Mar- o"e .+/ As &isiste" An" #n5lu"eThe E"ge (+0D
i
a b
c
d
e
f g
h
4
6
!
"
#$ %
4
&
$
Queue
2eight 5 3 = ? 9 = < <
%ode A * C @ E > $ I
Predecessor 5 A @ $ + A > @ A
Tree
a
i
f
g
d h
c
Graph
8/17/2019 Graphs Spanning
19/51James Tam
Mar- o"e .E/ As &isiste" An" #n5lu"eThe E"ge (+0E
i
a b
c
d
e
f g
h
4
6
!
"
$ %
4
&
$
Queue
2eight 5 3 = ? B = < <
%ode A * C @ 6 > $ I
Predecessor 5 A @ $ C A > @ A
Tree
a
i
f
g
d h
c
e
Graph
8/17/2019 Graphs Spanning
20/51James Tam
Mar- o"e .*/ As &isite" An" #n5lu"eThe E"ge (A0*
i
a b
c
d
e
f g
h
4
6
"
$ %
4
&
$
Tree
a
i
Queue
2eight 5 = ? B = < <
%ode A D C @ 6 > $ I
Predecessor 5 A @ $ C A > @ A
f
g
d h
c
eb
Graph
8/17/2019 Graphs Spanning
21/51James Tam
:rus-al’s Algorithm! Des5ription
•Etart out with a 9forest: of unconnected trees #9eFtract: the
nodes from the graph&
'ode
'ode 'ode
'ode
Becomes
http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998
8/17/2019 Graphs Spanning
22/51James Tam
:rus-al’s Algorithm! Des5ription
•8a"e the connections between the separate trees #add edges&
which have the lowest cost.
•Connecting two separate nodes #two separate trees& merges
them into one tree
http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998http://www.barrysclipart.com/barrysclipart.com/showphoto.php?photo=20761&papass=&sort=1&thecat=998
8/17/2019 Graphs Spanning
23/51James Tam
:rus-al’s Algorithm
public Eet "rus"al #$raph g&
'
Eet combinedEet * new Eet #&+
int edgesAccepted * 5+
Priority(ueue edges)eft * g.sort6dges #&+
6dge used6dge+
%ode source%ode+
%ode destination%ode+
8/17/2019 Graphs Spanning
24/51
James Tam
:rus-al’s Algorithm (2
while #edgesAccepted - #g.get%umber%odes#& G &&
'
used6dge * edges)eft.de7ueue8in #&+
source%ode * used6dge.getEource%ode #&+
destination%ode * used6dge.get@estination%ode #&+
if ##source%ode H* destination%ode&
#notCycle#source%ode,destination%ode,combinedEet& ** true&
'
edgesAccepted+
combinedEet.union #source%ode, destination%ode&+
4
4
return combinedEet+
4
8/17/2019 Graphs Spanning
25/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!Original $raph
(
A
)
*
B
+
,
4
$
&&-
$$
"
% # 4 6
&
8/17/2019 Graphs Spanning
26/51
8/17/2019 Graphs Spanning
27/51
8/17/2019 Graphs Spanning
28/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!First E"ge A""e" (A=D
Priorit; 8
A = *! Weight > 2
+ = D! Weight > 2
D = E! Weight > 2
* = D! Weight > 9
A = +! Weight > 4
D = $! Weight > 4+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
8/17/2019 Graphs Spanning
29/51
8/17/2019 Graphs Spanning
30/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!Thir" E"ge A""e" (A=*
Priorit; G $ 2eight *
A G D 2eight * <
+ = D! Weight > 2
D = E! Weight > 2
* = D! Weight > 9
A = +! Weight > 4
D = $! Weight > 4+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
&
$
8/17/2019 Graphs Spanning
31/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!Fourth E"ge A""e" (+=D
Priorit; G $ 2eight *
A G D 2eight * <
C G @ 2eight * <
D = E! Weight > 2
* = D! Weight > 9
A = +! Weight > 4
D = $! Weight > 4+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
&
$
$
8/17/2019 Graphs Spanning
32/51
8/17/2019 Graphs Spanning
33/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!Don’t 1se E"ge (*=D
Priorit; G $ 2eight *
A G D 2eight * <
C G @ 2eight * <
@ G 6 2eight * <
D G @ 2eight * B
A = +! Weight > 4
D = $! Weight > 4+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
&
$
$
"
S-ip$
8/17/2019 Graphs Spanning
34/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!Don’t 1se E"ge (A=+
Priorit; G $ 2eight *
A G D 2eight * <
C G @ 2eight * <
@ G 6 2eight * <
D G @ 2eight * B
A G C 2eight * =
D = $! Weight > 4+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
&
$
$
"
S-ip$
S-ip
4
E l T Of : - l’ Al i h
8/17/2019 Graphs Spanning
35/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!Sixth E"ge A""e" (D=$
Priorit; G $ 2eight *
A G D 2eight * <
C G @ 2eight * <
@ G 6 2eight * <
D G @ 2eight * B
A G C 2eight * =
@ G $ 2eight * =+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
&
$
$
"
S-ip$
S-ip
4
4
E l T Of : - l’ Al i h S E"
8/17/2019 Graphs Spanning
36/51
James Tam
Example Tra5e Of :rus-al’s Algorithm! Stop0 E"gesA55epte" > o o"es = 8
Priorit; G $ 2eight *
A G D 2eight * <
C G @ 2eight * <
@ G 6 2eight * <
D G @ 2eight * B
A G C 2eight * =
@ G $ 2eight * =
+ ? F! Weight > 6
E ? $! Weight > 3
D ? F! Weight > 7
* ? E! Weight > 8
(
A
)
*
B
+
,
&
&
$
$
"
S-ip$
S-ip
4
4
&
$
"
4
%
6
E l T Of : - l’ Al ith
8/17/2019 Graphs Spanning
37/51
James Tam
Example Tra5e Of :rus-al’s Algorithm!The Final Tree
(
A
)
*
B
+
,
&
&
$
$$
4
8/17/2019 Graphs Spanning
38/51
James Tam
Topologi5al Sorting Of $raphs! @eal ife ExampleB
•Ksed to order information that
could be represented as nodes in adirected acyclic graph.
•If theres a path from a node n to
another node n
8/17/2019 Graphs Spanning
39/51
James Tam
Topologi5al Sorting Of $raphs! @eal ife Example
•Ksed to order information that could be represented in the form
of the nodes in a directed acyclic graph
(P( $"&
(P( $""
(P( $"%
(P( $6% (P( "$%
(P( ""& ,'* "&&
Many se0or
courses
Many se0or
courses
t T l i l S ti + t * D With
8/17/2019 Graphs Spanning
40/51
James Tam
ote! Topologi5al Sorting +annot *e Done With+;5li5al $raphs
•Eorry no vacancies today.,
(P( $"& Admission into the
)epartment&
Computer Science 231 12"3$3&5
Introduction to Computer Science I
Problem solving and programming in a
structured language. )ata representation
program control basic 7ile handling the use o7
simple data structures and their
implementation. Pointers. 8ecursion.Prerequisite: Admission into the )epartment
o7 (omputer cience
Admission requirements into the
epartment of Computer Science
A grade o7 (3 or higher in (P( $"&
& his e9ample is purely 7ictional that was created to illustrate the principles o7 graph theory and should not be taken as an o77icial
description o7 peruisite reuirements 7or the )epartment o7 (omputer cience
8/17/2019 Graphs Spanning
41/51
James Tam
Algorithm For A Topologi5al Sort
public )ist topogicalEort #$raph g&
'
int i+
int no%odes * g.get%umber%odes #&+
)ist ordered%odes * new )ist #&+
%ode temp+
for #i * 5+ i - no%odes+ i&
'
temp * g.get%eFtTopParent #&+
ordered%odes.add #temp&+
g.delete%ode6dges #temp&+
4
4
8/17/2019 Graphs Spanning
42/51
James Tam
Example Of A Topologi5al Sort
(
A
)
*
B
+
,
-
& &
" $
" $
Graph
!ist
@emo e .+/ An" E"ges From $raph An"
8/17/2019 Graphs Spanning
43/51
James Tam
@emoCe .+/ An" E"ges From $raph An"A"" To ist
(
A
)
*
B
+
,
-
& &
" $
" $
Graph
!ist
051 01 0
8/17/2019 Graphs Spanning
44/51
8/17/2019 Graphs Spanning
45/51
James Tam
1p"ate The umbers An" @emoCe .*/
)
*
B
+
,
-
& $
$ $
Graph
!ist
051 01 0
8/17/2019 Graphs Spanning
46/51
James Tam
1p"ate The umbers An" @emoCe .D/
)
*+
,
- &
$ $
Graph
!ist
051 01 0
8/17/2019 Graphs Spanning
47/51
James Tam
1p"ate The umbers An" @emoCe .E/
*+
,
-
& &
Graph
!ist
051 01 0
8/17/2019 Graphs Spanning
48/51
8/17/2019 Graphs Spanning
49/51
8/17/2019 Graphs Spanning
50/51
James Tam
ou Shoul" o :no
•2hat is a minimum spanning tree
•Two algorithms #Prims and !rus"als& for creating minimum
spanning trees
•2hat is a topological sort and the algorithm for this sort.
8/17/2019 Graphs Spanning
51/51
Top Related