You are on page 1of 19

Algorithm Design and

Analysis
Lecture 5: Minimum Spanning Tree
Minimum Spanning Tree

• Minimum Spanning Tree of a weighted connected graph is a graph that contains a


subset of edges that connect all the vertices of a graph and has minimum total
weight.
• The resultant spanning Tree is a connected undirected graph that contains no
cycles.
• There is a brute force way that the MST could be found for a connected graph.
• we could look at all of the possible subsets of the edge set until we find the MST.
• this is a very time-consuming process if there are N edges, there would be 2N subsets.
• The brute force method would be of order O(2N).
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm
• To find the MST, we will use what is called a “greedy” algorithm.
• Greedy algorithms work by looking at a subset of the larger problem and making the best decision
based on that information.
• At each step of the process, look at a collection of potential edges to add to the spanning
tree and pick the one with the smallest weight.
• By doing this repeatedly, a spanning tree will grow that has a minimum overall total.
• consider the nodes of the graph to be in one of three categories:
• in the tree,
• on the fringe of the tree,
• and not yet considered.
• We begin by picking one node of the graph and putting that into the spanning tree.
• The choice of an initial node has no impact on our final result
• because our result is an unrooted tree
• unless there are multiple MSTs
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm

• Then, place all of the nodes that are select a starting node
connected to this initial one into the build the initial fringe from nodes connected to the
starting node
fringe category. while there are nodes left do
• Loop through the process of picking choose the edge to the fringe of the smallest
weight
the smallest weighted edge connecting add the associated node to the tree
a tree node with a fringe node, adding update the fringe by:
adding nodes to the fringe connected to
the new node to the tree, and then the new node
updating the nodes in the fringe updating the edges to the fringe so that
category. they are the smallest
end while
• When all the nodes have been added to
the tree, we are done.
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm

• If node A was arbitrarily chosen node A to begin


the process.
• a different choice for the starting node will not change
the result, unless there is more than one MST.
• All of the nodes directly connected to node A
become the starting fringe set.
• The edge with the smallest weight connects nodes
A and B, so B is added to the MST along with the
edge AB.
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm
• When node B is added to the tree, we need to
determine if there are any nodes that need to be added
to the fringe set, and we find that nodes E and G must
be added.
• Because the only tree node they are connected to is
node B, we add those edges to the ones we will
consider next.
• At this time, we also need to check to see if the edges
from node A to nodes C, D, and F are still the shortest
or if there are better edges from node B to these three
nodes.
• In the original graph, there are no direct connections
from node B to nodes C and F, so those will not change.
• But the edge from node B to node D has a smaller
weight than the one from node A, and so the edge BD
now replaces the edge AD.
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm

• Of the five edges to fringe nodes, we see that BE


has the smallest weight, and so it and node E are
added to the tree.
• The edge EG has a smaller weight than the edge
BG, so it is now used.
• Of the four current edges to the fringe, we see that
AC has the smallest weight and so it is added next.
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm

• The addition of node C and edge AC to the


spanning tree did not cause any edges to be
updated.
• We next choose the edge AF, so it and the node
F are added to the tree.
• We also update the links because the edge FD
has a smaller weight than BD and edge FG has a
smaller weight than EG.
• In the resulting fringe, we see that the edge FD
is now the remaining edge with the smallest
weight, so it is added next.
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm

• We now just have one node that has not been


added to the tree.
• When it is added, the process is complete, and
we have determined the MST rooted at node A
Minimum Spanning Tree: The Dijkstra-Prim
Algorithm

select a starting node


build the initial fringe from nodes connected to the starting node
while there are nodes left do
choose the edge to the fringe of the smallest weight
add the associated node to the tree
update the fringe by:
adding nodes to the fringe connected to the new node
updating the edges to the fringe so that they are the smallest
end while
Minimum Spanning Tree: The Kruskal Algorithm

• Where the Dijkstra-Prim algorithm began at a particular node and built the
minimum spanning tree outward, Kruskal’s algorithm concentrates instead on the
edges of the graph.
• In this algorithm, we begin with an empty spanning tree and add edges in order of
increasing weight until all nodes are connected to the graph.
• If we run out of edges before all of the nodes are connected, the original graph
wasn’t connected, and the result we have generated is the MSTs of each of the
connected components of the original graph.
Minimum Spanning Tree: The Kruskal Algorithm

• First, add the edge with the lowest weight, which


is the one between nodes D and F, giving the
partial results.
• The edge with weight 2 is added next between the
nodes A and B, and then the edge with weight
three is added.
Minimum Spanning Tree: The Kruskal Algorithm
• The edges with weights of 4 and 5 are next added
to our result.
• Only node G is still unconnected.
• The next edges to consider are those with a weight
of 6.
• Of the four edges with a weight of 6, two are
discarded because they would form a cycle with
edges already part of the MST.
• The edge between nodes C and F would form a cycle
that includes node A.
• The edge between node B and D would form a cycle that
includes nodes A and F.
Minimum Spanning Tree: The Kruskal Algorithm

• The other two nodes are both good alternatives,


and depending on the one chosen, we get the
MST.
Minimum Spanning Tree: The Kruskal Algorithm

• Our main loop will continue until the edgeCount


variable indicates that we have looked at all of sort the edges in nondecreasing order by weight
the edges initialize partition structure
edgeCount = 1
• The included Count indicates that we have includedCount = 0
added enough edges to create the spanning while edgeCount ≤ E and includedCount ≤ N-1 do
parent1 = FindRoot( edge[edgeCount].start )
tree. parent2 = FindRoot( edge[edgeCount].end )
• if there is N nodes in the graph, a spanning tree if parent1 ≠ parent2 then
add edge[edgeCount] to spanning tree
would have one less edge than nodes.
includedCount = includedCount + 1
• Inside the loop, we first find the parents of the end if
Union( parent1, parent2 )

two nodes that are connected by the next edge edgeCount = edgeCount + 1
we are considering. end while
Minimum Spanning Tree: The Kruskal Algorithm

• If those nodes are in partitions with different


roots, adding an edge between them will not sort the edges in nondecreasing order by weight
create a cycle, so this current edge can be initialize partition structure
edgeCount = 1
added to the MST and those two pieces can be includedCount = 0
joined so that they now have the same root. while edgeCount ≤ E and includedCount ≤ N-1 do

• The complexity of this algorithm will be the parent1 = FindRoot( edge[edgeCount].start )


parent2 = FindRoot( edge[edgeCount].end )
complexity of the sort algorithm used because if parent1 ≠ parent2 then
add edge[edgeCount] to spanning tree
the while loop is linearly related to the number
includedCount = includedCount + 1
of edges. Union( parent1, parent2 )
• This makes the complexity of Kruskal’s MST end if
edgeCount = edgeCount + 1
algorithm O(E lg E). end while
SHORTEST-PATH ALGORITHM

• The shortest-path algorithm will find for two nodes the


series of edges between them that will result in the
smallest total weight.
• It might seem that we could use the minimum spanning
tree algorithm to prune out some of the edges and then
just look for the path between the nodes in the spanning
tree.
• Unfortunately, that will not always produce the shortest
path.
• Remember that the minimum spanning tree algorithm is
trying to find an overall total that is smallest, so it is going
to look for the smallest weights possible.
• For example, think of a graph that is “circular” in shape.
SHORTEST-PATH ALGORITHM

• Consider a graph with six nodes and the weights on all of


the edges are 1, except for the edge from node A to
nodeB, which has a weight of 2.
• The minimum spanning tree algorithm will pick all of the
edges with weight 1, and drop the one edge of weight 2.
• But that means that the path between node A and node B
in the minimum spanning tree must go through all of the
other nodes for a path length of 5.
• Clearly, this is not the shortest path, because you can see
that there is a direct path between node A and node B that
has length 2

You might also like