Spanning Trees
Prim’s MST Algorithm
Algorithm ( this is also greedy)
Select an arbitrary vertex to start the tree,
while there are fringe vertices:
1)select an edge of minimum weight
between a tree vertex and a fringe
vertex.
2)add the selected edge and the
fringe vertex to the tree.
end.
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
6
2
52
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
6
2
52
6
Example: start with 7Example: start with 7
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
6
2
52
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
6
2
52
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
6
2
52
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
2
52
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
2
52
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
2
56
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
3
2
1
4
4
2
5
3
4
7 8
1
2
56
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1
4
4
2
5
3
4
7 8
1
2
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1
4
4
2
5
3
4
7 8
1
2
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1 4
2
5
3
4
7 8
1
2
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1 4
2
5
3
4
7 8
1
2
6
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1
2
5
3
4
7 8
1
2
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1
2
5
3
4
7 8
1
2
Prim’s AlgorithmMinimal Spanning Tree
1 6
2 4
3 5
2
1
2
3
7 8
1
2
MST weight = 15MST weight = 15
4
Topological Sorting Algorithm
while (the graph has a node with no successor) do
remove one of those nodes from the graph
and add it to the end of a list
if (the graph is empty) then
the list contains the reverse of some topological order
else
the graph contains a cycle
AA
BB
CC
DD
EE
FF
GG
HH II
JJ
LL
KK
MM
AA
BB
CC
DD
EE
FF
GG
HH II
JJ
LL
KK
MM
DD
AA
BB
CC
EE
FF
GG
HH II
JJ
LL
KK
MM
DD EE
AA
BB
CC
FF
GG
HH II
JJ
LL
KK
MM
DD EE FF
AA
BB
CCGG
HH II
JJ
LL
KK
MM
DD EE FF CC
AA
BB
GG
HH II
JJ
LL
KK
MM
DD EE FF CC BB
AA
GG
HH II
JJ
LL
KK
MM
DD EE FF CC BB II
AA
GG
HH
JJ
LL
KK
MM
DD EE FF CC BB II HH
AA
GG
JJ
LL
KK
MM
DD EE FF CC BB II HH GG
AA
JJ
LL
KK
MM
DD EE FF CC BB II HH GG AA
JJ
LL
KK
MM
DD EE FF CC BB II HH GG AA KK
JJ
LL MM
DD EE FF CC BB II HH GG AA KK MM
JJ
LL
DD EE FF CC BB II HH GG AA KK MM LL
JJ
DD EE FF CC BB II HH GG AA KK MM LL JJ
DD EE FF CC BB II HH GG AA KK MM LL JJ
J L M K A G H I B C F E DJ L M K A G H I B C F E D