-
Notifications
You must be signed in to change notification settings - Fork 28
Minimum Spanning Trees
Jinho D. Choi edited this page Oct 24, 2016
·
18 revisions
-
Create a package
cs323.graph
. -
Create a package
cs323.graph.span
. -
Download the
SpanningTree
class and put it underspan
. -
Download the
MSTAlgorithm
interface and put it underspan
.
- Create classes
MSTPrim
,MSTKruskal
, andMSTEdmonds
underspan
. - Inherit the
MSTAlgorithm
interface. - Override the
getMinimumSpanningTree
method in each class by implementing Prim's, Kruskal's, and Chu–Liu-Edmonds' algorithms, respectively. -
@param graph
a graph containing at least one spanning tree. -
@return
a minimum spanning tree.
- Pick a vertex.
- Put the picked vertex to a
set
and add all its incoming edges to apriority queue
using their weights. - Remove the minimum edge in the
queue
and check if theset
contains the source of this edge. - If the set contains the source, add the edge to a
tree
. - If all edges in the
tree
form a spanning tree, return thetree
. - Otherwise, go to #2 by picking the source of the edge.
- Go to #3 unless the
queue
is empty.
- Create a
forest
in which eachset
consists of each vertex in the graph. - Create a
priority queue
containing all edges in the graph. - Remove the minimum edge in the
queue
and check if the source and the target belong to the sameset
in theforest
. - If the source and the target do not belong to the same
set
, add the edge to atree
and merge the source and the target sets. - If all edges in the
tree
form a spanning tree, return thetree
. - Go to #3 unless the
queue
is empty.
- Initially, every vertex is considered a tree.
- For each tree, keep 1 incoming edge with the minimum weight.
- If there is no cycle, go to #5.
- If there is a cycle, merge trees with the cycle into one and update scores for all incoming edges to this tree, and goto #2.
- For each vertex in the tree, add the weight of its outgoing edge to its incoming edges not in the tree. Break all cycles by removing edges that cause multiple parents.
- Give a graph where Prim's and Kruskal's algorithms generate different kinds of minimum spanning trees. Explain how these trees are generated. If you cannot find an example, explain why these algorithms always generate the same minimum spanning tree given any graph.
- What is the worst-case complexity of Chu–Liu-Edmonds’ Algorithm when
V
is the number of vertices andE
is the number of edges in a graph? Explain how you come up with this complexity.
Copyright © 2014-2017 Emory University - All Rights Reserved.