0% found this document useful (0 votes)
13 views

Spanning Tree

The document provides an overview of spanning trees and minimum spanning trees, explaining their definitions, properties, and the algorithms used to find them, such as Prim's and Kruskal's algorithms. It highlights the applications of these concepts in various fields, including computer networks and civil planning. Additionally, it contrasts Prim's and Kruskal's algorithms, and introduces Dijkstra's algorithm for finding the shortest path in graphs.

Uploaded by

fewibi7074
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Spanning Tree

The document provides an overview of spanning trees and minimum spanning trees, explaining their definitions, properties, and the algorithms used to find them, such as Prim's and Kruskal's algorithms. It highlights the applications of these concepts in various fields, including computer networks and civil planning. Additionally, it contrasts Prim's and Kruskal's algorithms, and introduces Dijkstra's algorithm for finding the shortest path in graphs.

Uploaded by

fewibi7074
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 55

Spanning Tree

Minimum Spanning Tree


Spanning Tree
• An undirected graph is a graph in which the edges do not point in any
direction (ie. the edges are bidirectional).
Spanning Tree
• A connected graph is a graph in which there is always a path from a
vertex to any other vertex.
Spanning Tree

• A spanning tree is a sub-graph of an undirected connected graph, which includes


all the vertices of the graph with a minimum possible number of edges. If a
vertex is missed, then it is not a 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

• Some of the properties of the spanning tree are given as follows -

• There can be more than one spanning tree of a connected graph G.

• A spanning tree does not have any cycles or loop.

• A spanning tree is minimally connected, so removing one edge from

the tree will make the graph disconnected.


Spanning Tree Properties

• A spanning tree is maximally acyclic, so adding one edge to the tree will

create a loop.

• There can be a maximum nn-2 number of spanning trees that can be

created from a complete graph.

• A spanning tree has n-1 edges, where 'n' is the number of nodes.
Spanning Tree Properties

• If the graph is a complete graph, then the spanning tree can be

constructed by removing maximum (e-n+1) edges, where 'e' is the

number of edges and 'n' is the number of vertices.

• So, a spanning tree is a subset of connected graph G, and there is no

spanning tree of a disconnected graph.


Minimum Spanning Tree

• A minimum spanning tree is a spanning tree in which the sum of the

weight of the edges is as minimum as possible.


Minimum Spanning Tree
Example
• Let's understand the above definition with the help of the example
below.
• The initial graph and The possible spanning trees from the given tree
are:
Minimum Spanning Tree
Example The minimum spanning tree from the above
spanning trees is:
Minimum Spanning Tree

• The minimum spanning tree from a graph is found using the

following algorithms:

• Prim's Algorithm

• Kruskal's Algorithm
Spanning Tree Applications

• Computer Network Routing Protocol

• Cluster Analysis

• Civil Network Planning


Minimum Spanning Tree
Applications

• To find paths in the map

• To design networks like telecommunication networks,

water supply networks, and electrical grids.


Prim's Algorithm

• Prim's algorithm is a minimum spanning tree algorithm that takes a graph as


input and

• 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

• The steps for implementing Prim's algorithm are as follows:

• Initialize the minimum spanning tree with a vertex chosen at random.

• Find all the edges that connect the tree to new vertices, find the minimum and
add it to the tree

• Keep repeating step 2 until we get a minimum spanning tree


Example of prim's algorithm
• Suppose, a weighted graph is -
Example of prim's algorithm
• Step 1 - First, we have to choose a vertex from the above graph. Let's
choose B.
Example of prim's algorithm
• Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges
from vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges,
the edge BD has the minimum weight. So, add it to the MST.
Example of prim's algorithm
• Step 3 - Now, again, choose the edge with the minimum weight among all the other edges. In this
case, the edges DE and CD are such edges. Add them to MST and explore the adjacent of C, i.e., E
and A. So, select the edge DE and add it to the MST.
Example of prim's algorithm
• Step 4 - Now, select the edge CD, and add it to the MST.
Example of prim's algorithm
• Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle
to the graph. So, choose the edge CA and add it to the MST.
Example of prim's algorithm
• So, the graph produced in step 5 is the minimum spanning tree of the given graph. The cost of
the MST is given below –
• Cost of MST = 4 + 2 + 1 + 3 = 10 units.
Prim's Algorithm Application

• Laying cables of electrical wiring

• In network designed

• To make protocols in network cycles


Kruskal's Algorithm

• Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as


input and

• 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:

• Sort all the edges from low weight to high

• Take the edge with the lowest weight and add it to the spanning tree. If

adding the edge created a cycle, then reject this edge.

• Keep adding edges until we reach all vertices.


Example of Kruskal's algorithm
Suppose a weighted graph is –
Example of Kruskal's algorithm
• The weight of the edges of the above graph is given in the below
table -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2

• 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

• In order to layout electrical wiring

• In computer network (LAN connection)


Difference Between Prim's and Kruskal's
algorithm

• Two popular algorithms to solve this problem are Prim's and Kruskal's
algorithms.

• While both algorithms aim to find the minimum spanning tree of a


graph,

• 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.

Applications of prim’s algorithm are Travelling Salesman Problem, Network for


Applications of Kruskal algorithm are LAN connection, TV Network etc
roads and Rail tracks connecting all the cities etc.

Prim’s algorithm prefer list data structures. Kruskal’s algorithm prefer heap data structures.
Dijkstra's Algorithm

• It differs from the minimum spanning tree

• Because the shortest distance between two vertices might not include

all the vertices of the graph.


Fundamentals of Dijkstra's
Algorithm
The following are the basic concepts of Dijkstra's Algorithm:

• 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.

• The Algorithm keeps records of the presently acknowledged shortest distance


from each node to the source node, and it updates these values if it finds any
shorter path.
Fundamentals of Dijkstra's
Algorithm
• Once the Algorithm has retrieved the shortest path between the source and

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

nodes, following the shortest possible path to reach each node.


Fundamentals of Dijkstra's
Algorithm
• A graph and source vertex are requirements for Dijkstra's Algorithm. This

Algorithm is established on Greedy Approach and thus finds the locally optimal

choice (local minima in this case) at each step of the Algorithm.

• 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.

• We are using this property so that we do not revisit any node.

• 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 revised when any neighbor of the node is visited.

• 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.

Moreover, the path to the source node is set to zero (0).


Dijkstra's Algorithm with an
Example
• The following is the step that we will follow to implement Dijkstra's Algorithm:

• 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 4: We will then mark the current node X as visited.

• 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.

• First, we will mark all the nodes as unvisited.

• 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

the path from node A to B is 4, and the minimum((0 + 4), INFINITY) is 4.

• 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

now relaxed; therefore, we can move ahead.

• 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

remain 13, and the path to node F will become 14 (8 + 6).

• Now we will visit node D, and only node F will be relaxed. However, the path to node F will remain

unchanged, i.e., 14.

• 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)

• F = 5 + 3 + 6 = 14 (A -> C -> E -> F)


Dijkstra's Algorithm
Applications

• To find the shortest path

• In social networking applications

• In a telephone network

• To find the locations in the map

You might also like