Spanning Tree
Spanning Tree
• The total number of spanning trees with n vertices that can be created from a
complete graph is equal to `n(n-2)
• If we have n=4, the maximum number of possible spanning trees is equal to 4(4-2).
• Thus, 16 spanning trees can be formed from a complete graph with 4 vertices.
Spanning Tree Example
• Let's understand the spanning tree with examples below:
• Let the original graph be:
Spanning Tree Example
• Some of the possible spanning trees that can be created from the
above graph are:
Spanning Tree Properties
• A spanning tree is maximally acyclic, so adding one edge to the tree will
create a loop.
• A spanning tree has n-1 edges, where 'n' is the number of nodes.
Spanning Tree Properties
following algorithms:
• Prim's Algorithm
• Kruskal's Algorithm
Spanning Tree Applications
• Cluster Analysis
• finds the subset of the edges of that graph which form a tree that includes
• every vertex has the minimum sum of weights among all the trees that can
be formed from the graph.
How Prim's algorithm works
• It falls under a class of algorithms called greedy algorithms that find the
local optimum in the hopes of finding a global optimum.
• We start from one vertex and keep adding edges with the lowest weight
until we reach our goal.
How Prim's algorithm works
• Find all the edges that connect the tree to new vertices, find the minimum and
add it to the tree
• In network designed
• finds the subset of the edges of that graph which form a tree that includes
• every vertex has the minimum sum of weights among all the trees that can be
formed from the graph
How Kruskal's algorithm works
• It falls under a class of algorithms called greedy algorithms that find
the local optimum in the hopes of finding a global optimum.
• We start from the edges with the lowest weight and keep adding
edges until we reach our goal.
How Kruskal's algorithm works
• The steps for implementing Kruskal's algorithm are as follows:
• Take the edge with the lowest weight and add it to the spanning tree. If
• Now, sort the edges given above in the ascending order of their
weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Example of Kruskal's algorithm
• Now, let's start constructing the minimum spanning tree.
• Step 1 - First, add the edge AB with weight 1 to the MST.
Example of prim's algorithm
• Step 2 - Add the edge DE with weight 2 to the MST as it is not
creating the cycle.
Example of prim's algorithm
• Step 3 - Add the edge BC with weight 3 to the MST, as it is not
creating any cycle or loop.
Example of prim's algorithm
• Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not
forming the cycle.
Example of prim's algorithm
• Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the cycle, so
discard it.
• Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so discard it.
• Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle, so discard
it.
• So, the final minimum spanning tree obtained from the given weighted graph by using Kruskal's
algorithm is -
Example of Kruskal’s algorithm
• The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.
Kruskal's Algorithm Applications
• Two popular algorithms to solve this problem are Prim's and Kruskal's
algorithms.
• they differ in their approaches and the underlying principles they rely
on.
Difference Between Prim's and Kruskal's
algorithm
Difference Between Prim's and Kruskal's
algorithm
Prim’s Algorithm Kruskal’s Algorithm
It starts to build the Minimum Spanning Tree from the vertex carrying minimum
It starts to build the Minimum Spanning Tree from any vertex in the graph.
weight in the graph.
It traverses one node more than one time to get the minimum distance. It traverses one node only once.
Prim’s algorithm has a time complexity of O(V2), V being the number of vertices Kruskal’s algorithm’s time complexity is O(E log V), V being the number of
and can be improved up to O(E log V) using Fibonacci heaps. vertices
Prim’s algorithm gives connected component as well as it works only on Kruskal’s algorithm can generate forest(disconnected components) at any instant
connected graph. as well as it can work on disconnected components
Prim’s algorithm runs faster in dense graphs. Kruskal’s algorithm runs faster in sparse graphs.
It generates the minimum spanning tree starting from the root vertex. It generates the minimum spanning tree starting from the least weighted edge.
Prim’s algorithm prefer list data structures. Kruskal’s algorithm prefer heap data structures.
Dijkstra's Algorithm
• Because the shortest distance between two vertices might not include
• Dijkstra's Algorithm begins at the node we select (the source node), and it
examines the graph to find the shortest path between that node and all the other
nodes in the graph.
another node, that node is marked as 'visited' and included in the path.
• The procedure continues until all the nodes in the graph have been included in
the path. In this manner, we have a path connecting the source node to all other
Algorithm is established on Greedy Approach and thus finds the locally optimal
• Each Vertex in this Algorithm will have two properties defined for it:
Visited Property
Path Property
Fundamentals of Dijkstra's
Algorithm
Let us understand these properties in brief.
• Visited Property:
• The 'visited' property signifies whether or not the node has been visited.
• A node is marked visited only when the shortest path has been found.
Fundamentals of Dijkstra's
Algorithm
• Path Property:
• The 'path' property stores the value of the current minimum path to the node.
• The current minimum path implies the shortest way we have reached this node till now.
• This property is significant because it will store the final answer for each node.
Fundamentals of Dijkstra's
Algorithm
• Initially, we mark all the vertices, or nodes, unvisited as they have yet to be
visited.
• The path to all the nodes is also set to infinity apart from the source node.
• Step 1: First, we will mark the source node with a current distance of 0 and set the rest of the nodes to
INFINITY.
• Step 2: We will then set the unvisited node with the smallest current distance as the current node, suppose
X.
• Step 3: For each neighbor N of the current node X: We will then add the current distance of X with the
weight of the edge joining X-N. If it is smaller than the current distance of N, set it as the new current
distance of N.
• Step 5: We will repeat the process from 'Step 2' if there is any node unvisited left in the graph.
Dijkstra's Algorithm with an
Example
Let us now understand the implementation of the algorithm with the help of an example:
Dijkstra's Algorithm with an
Example
• We will use the above graph as the input, with node A as the source.
• We will set the path to 0 at node A and INFINITY for all the other nodes.
• We will now mark source node A as visited and access its neighboring nodes.
Note: We have only accessed the neighboring nodes, not visited them.
Dijkstra's Algorithm with an
Example
• We will now update the path to node B by 4 with the help of relaxation because the path to node A is 0 and
• We will also update the path to node C by 5 with the help of relaxation because the path to node A is 0 and
the path from node A to C is 5, and the minimum((0 + 5), INFINITY) is 5. Both the neighbors of node A are
• We will now select the next unvisited node with the least path and visit it. Hence, we will visit node B and
perform relaxation on its unvisited neighbors. After performing relaxation, the path to node C will remain 5,
whereas the path to node E will become 11, and the path to node D will become 13.
Dijkstra's Algorithm with an
Example
• We will now visit node E and perform relaxation on its neighboring nodes B, D, and F. Since only node F is
unvisited, it will be relaxed. Thus, the path to node B will remain as it is, i.e., 4, the path to node D will also
• Now we will visit node D, and only node F will be relaxed. However, the path to node F will remain
• Since only node F is remaining, we will visit it but not perform any relaxation as all its neighboring nodes are
already visited.
• Once all the nodes of the graphs are visited, the program will end.
Dijkstra's Algorithm with an
Example
Hence, the final paths we concluded are:
• A=0
• B = 4 (A -> B)
• C = 5 (A -> C)
• D = 4 + 9 = 13 (A -> B -> D)
• E = 5 + 3 = 8 (A -> C -> E)
• In a telephone network