SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Spanning Trees
GRAPHS
 Graphs are one of the most Interesting data structures in computer science.
 Graphs and trees are somewhat similar by their structure and in fact tree is derived from the
graph data structure.
 Commonly used graph traversal algorithms are:
2
BFS DFS
In this we visit the nodes level by level,
so it will start with level 0, which is the
root node then next, then the last level.
In this we visit the root node first then its
children until it reaches the end node.
Queue is used to implement BFS.
E.G.
Its BFS will be A,B,C,D,E,F.
Stack is used to implement DFS.
E.G.
Its DFS will be A,B,E,F,C,D
3
Spanning trees
 Suppose you have a connected undirected graph
 Connected: every node is reachable from every other node.
 Undirected: edges do not have an associated direction
 ...thus in the Mathematical field of graph theory, a spanning tree of the graph is
a connected sub-graph in which there are no cycles.
 A Spanning tree for a graph has no cycles but still connects to every house.
 If G is a connected graph with n vertices and m edges, spanning tree of G must
have n-1 edges, and no. of edges deleted from G to get a spanning tree must be
m-(n-1)=m-n+1.
 A Graph may have many spanning trees; for instance the complete graph on
four vertices.
A connected,
undirected graph
Four of the spanning trees of the graph
4
Finding a spanning tree
 To find a spanning tree of a graph,
Pick an initial node and call it part of the spanning tree
do a search from the initial node:
each time you find a node that is not in the spanning tree, add to the
spanning tree both the new node and the edge you followed to get to it.
An undirected graph One possible
result of a BFS
starting from top
One possible
result of a DFS
starting from top
5
Minimizing costs
 Suppose you want to supply a set of houses (say, in a
new subdivision) with:
 electric power
 water
 sewage lines
 telephone lines
 To keep costs down, you could connect these houses
with a spanning tree (of, for example, power lines)
 However, the houses are not all equal distances apart
 To reduce costs even further, you could connect the
houses with a minimum-cost spanning tree
6
Minimum-cost spanning trees
 Suppose you have a connected undirected graph with a weight (or cost)
associated with each edge
 The cost or weight of a spanning tree would be the sum of the costs of its
edges.
 A minimum-cost spanning tree is a spanning tree of a connected undirected
graph that has the lowest cost.
 If there are n vertices in the graph , then each spanning tree has n vertices & n-
1 edges.
A B
E D
F C
16
19
21 11
33 14
18
10
6
5
A connected, undirected graph
A B
E D
F C
16
11
18
6
5
A minimum-cost spanning tree
7
Finding minimum spanning trees
 There are two basic algorithms for finding minimum-cost
spanning trees, and both are greedy algorithms i.e., these
always processes the edge with the least weight or cost.
 Kruskal’s algorithm: Start with no nodes or edges in the
spanning tree, and repeatedly add the cheapest edge that does
not create a cycle
 Here, we consider the spanning tree to consist of edges only
 Prim’s algorithm: Start with any one node in the spanning
tree, and repeatedly add the cheapest edge, and the node it leads
to, for which the node is not already in the spanning tree.
 Here, we consider the spanning tree to consist of both nodes and edges
8
Kruskal’s algorithm
Input :A Connected weighted graph G
Output :A minimal spanning tree T.
T = empty spanning tree;
E = set of edges;
N = number of nodes in graph;
while T has fewer than N - 1 edges {
remove an edge (v, w) of lowest cost from E
if adding (v, w) to T would create a cycle
then discard (v, w)
else add (v, w) to T
}
 Finding an edge of lowest cost can be done just by sorting
the edges
E.G of Krusal’s Algorithm
 Find a minimum spanning tree of the below connected undirected
graph by Krusals algorithm.
 List the edges in non-decreasing order
shown below:
 Therefore the minimum spanning tree that will be
produced will be:
9
10
Prim’s algorithm
Input :A connected weighted Graph G.
Output :A minimum spanning tree T.
T = a spanning tree containing a single node s;
E = set of edges adjacent to s;
while T does not contain all the nodes {
remove an edge (v, w) of lowest cost from E
if w is already in T then discard edge (v, w)
else {
add edge (v, w) and node w to T
add to E the edges adjacent to w
}
}
E.G OF PRIM’S ALGORITHM
 Find a minimum spanning tree by using prim’s algorithm of the below given graph:
11
12
DIFFERENCE
PRIM’S ALGORITHM
 In this, at every step
edges of minimum
weight that are
incident to a vertex
already in the tree and
not forming a cycle is
choosen .
KRUSAL’S ALGOTITHM
 In this, at every step
edges of minimum
weight that are not
necessarily incident to
a vertex already in the
tree and not forming a
cycle is choosen .
13
14
The End

More Related Content

PPTX
Spanning trees & applications
PPTX
Employee Management System
PPTX
Tree - Data Structure
PPTX
Personality Development
PPTX
Data structure - Graph
PPTX
Big Data Analytics
PDF
Data structure ppt
PPTX
Advancement technology in Robotics.pptx
Spanning trees & applications
Employee Management System
Tree - Data Structure
Personality Development
Data structure - Graph
Big Data Analytics
Data structure ppt
Advancement technology in Robotics.pptx

What's hot (20)

PPT
Minimum spanning tree
PPTX
Tree traversal techniques
PPTX
Prim's algorithm
PPTX
Breadth First Search & Depth First Search
PPTX
Priority Queue in Data Structure
PPTX
Bfs and Dfs
PPTX
Graph in data structure
PPT
Prim's Algorithm on minimum spanning tree
PPTX
Doubly Linked List
PDF
Shortest Path in Graph
PPTX
Presentation on Breadth First Search (BFS)
PDF
All pairs shortest path algorithm
PPTX
B and B+ tree
PPT
PPTX
Shortest path algorithm
PPTX
Prims and kruskal algorithms
PPTX
Graph representation
PPTX
Topological Sorting
PPT
Sum of subsets problem by backtracking 
PPTX
Priority queue in DSA
Minimum spanning tree
Tree traversal techniques
Prim's algorithm
Breadth First Search & Depth First Search
Priority Queue in Data Structure
Bfs and Dfs
Graph in data structure
Prim's Algorithm on minimum spanning tree
Doubly Linked List
Shortest Path in Graph
Presentation on Breadth First Search (BFS)
All pairs shortest path algorithm
B and B+ tree
Shortest path algorithm
Prims and kruskal algorithms
Graph representation
Topological Sorting
Sum of subsets problem by backtracking 
Priority queue in DSA
Ad

Viewers also liked (19)

PPTX
Connected components and shortest path
PPT
B trees and_b__trees
PPT
Divide and conquer
PPT
Greedymethod
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
PPT
Best for b trees
PPT
File organization 1
PPT
FILE STRUCTURE IN DBMS
PPT
B trees dbms
PPT
Chapter 11 - File System Implementation
PPT
Cpu Scheduling Galvin
PPTX
Divide and Conquer - Part 1
PPTX
Computer architecture and organization
PPT
11. Storage and File Structure in DBMS
PPTX
Dynamic Programming
PPT
17. Trees and Graphs
PPTX
Hashing Technique In Data Structures
Connected components and shortest path
B trees and_b__trees
Divide and conquer
Greedymethod
Graph Traversal Algorithms - Depth First Search Traversal
Best for b trees
File organization 1
FILE STRUCTURE IN DBMS
B trees dbms
Chapter 11 - File System Implementation
Cpu Scheduling Galvin
Divide and Conquer - Part 1
Computer architecture and organization
11. Storage and File Structure in DBMS
Dynamic Programming
17. Trees and Graphs
Hashing Technique In Data Structures
Ad

Similar to Spanning trees (20)

PPT
Graph Theory PPT presentation created by Selvam.
PPT
KRUSKALS'S algorithm from chaitra
PPT
KRUSKAL'S algorithm from chaitra
PPTX
PPT
33 spanning-trees
PPTX
Data structure and algorithm
PPTX
logic.pptx
PPT
Weighted graphs
PPTX
DATA STRUCTURES.pptx
PPTX
Data structure
PPTX
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
PPT
Data Structures-Non Linear DataStructures-Graphs
PPTX
UNIT II - Graph Algorithms techniques.pptx
PPTX
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
PPTX
Prim's Algorithm Presentation for prims algorithm (1).pptx
PPTX
Spanningtreesppt
PPT
17 prims-kruskals (1)
PPTX
Minimum spanning tree
PPTX
Minimum spanning tree.pptx data structure programming
PDF
spanningtreesapplications-120903115339-phpapp02 (1).pdf
Graph Theory PPT presentation created by Selvam.
KRUSKALS'S algorithm from chaitra
KRUSKAL'S algorithm from chaitra
33 spanning-trees
Data structure and algorithm
logic.pptx
Weighted graphs
DATA STRUCTURES.pptx
Data structure
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
Data Structures-Non Linear DataStructures-Graphs
UNIT II - Graph Algorithms techniques.pptx
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
Prim's Algorithm Presentation for prims algorithm (1).pptx
Spanningtreesppt
17 prims-kruskals (1)
Minimum spanning tree
Minimum spanning tree.pptx data structure programming
spanningtreesapplications-120903115339-phpapp02 (1).pdf

More from Shareb Ismaeel (9)

PPTX
Cybercrimes
PPT
PPTX
Multiprocessor structures
PPTX
Installation testing
PPT
Genome data management
PPT
E mail systems
PPTX
E commerce
PPT
Disk structure
PPT
.Netframework
Cybercrimes
Multiprocessor structures
Installation testing
Genome data management
E mail systems
E commerce
Disk structure
.Netframework

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
web development for engineering and engineering
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
composite construction of structures.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
web development for engineering and engineering
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Operating System & Kernel Study Guide-1 - converted.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Internet of Things (IOT) - A guide to understanding
bas. eng. economics group 4 presentation 1.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mechanical Engineering MATERIALS Selection
composite construction of structures.pdf

Spanning trees

  • 2. GRAPHS  Graphs are one of the most Interesting data structures in computer science.  Graphs and trees are somewhat similar by their structure and in fact tree is derived from the graph data structure.  Commonly used graph traversal algorithms are: 2 BFS DFS In this we visit the nodes level by level, so it will start with level 0, which is the root node then next, then the last level. In this we visit the root node first then its children until it reaches the end node. Queue is used to implement BFS. E.G. Its BFS will be A,B,C,D,E,F. Stack is used to implement DFS. E.G. Its DFS will be A,B,E,F,C,D
  • 3. 3 Spanning trees  Suppose you have a connected undirected graph  Connected: every node is reachable from every other node.  Undirected: edges do not have an associated direction  ...thus in the Mathematical field of graph theory, a spanning tree of the graph is a connected sub-graph in which there are no cycles.  A Spanning tree for a graph has no cycles but still connects to every house.  If G is a connected graph with n vertices and m edges, spanning tree of G must have n-1 edges, and no. of edges deleted from G to get a spanning tree must be m-(n-1)=m-n+1.  A Graph may have many spanning trees; for instance the complete graph on four vertices. A connected, undirected graph Four of the spanning trees of the graph
  • 4. 4 Finding a spanning tree  To find a spanning tree of a graph, Pick an initial node and call it part of the spanning tree do a search from the initial node: each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it. An undirected graph One possible result of a BFS starting from top One possible result of a DFS starting from top
  • 5. 5 Minimizing costs  Suppose you want to supply a set of houses (say, in a new subdivision) with:  electric power  water  sewage lines  telephone lines  To keep costs down, you could connect these houses with a spanning tree (of, for example, power lines)  However, the houses are not all equal distances apart  To reduce costs even further, you could connect the houses with a minimum-cost spanning tree
  • 6. 6 Minimum-cost spanning trees  Suppose you have a connected undirected graph with a weight (or cost) associated with each edge  The cost or weight of a spanning tree would be the sum of the costs of its edges.  A minimum-cost spanning tree is a spanning tree of a connected undirected graph that has the lowest cost.  If there are n vertices in the graph , then each spanning tree has n vertices & n- 1 edges. A B E D F C 16 19 21 11 33 14 18 10 6 5 A connected, undirected graph A B E D F C 16 11 18 6 5 A minimum-cost spanning tree
  • 7. 7 Finding minimum spanning trees  There are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms i.e., these always processes the edge with the least weight or cost.  Kruskal’s algorithm: Start with no nodes or edges in the spanning tree, and repeatedly add the cheapest edge that does not create a cycle  Here, we consider the spanning tree to consist of edges only  Prim’s algorithm: Start with any one node in the spanning tree, and repeatedly add the cheapest edge, and the node it leads to, for which the node is not already in the spanning tree.  Here, we consider the spanning tree to consist of both nodes and edges
  • 8. 8 Kruskal’s algorithm Input :A Connected weighted graph G Output :A minimal spanning tree T. T = empty spanning tree; E = set of edges; N = number of nodes in graph; while T has fewer than N - 1 edges { remove an edge (v, w) of lowest cost from E if adding (v, w) to T would create a cycle then discard (v, w) else add (v, w) to T }  Finding an edge of lowest cost can be done just by sorting the edges
  • 9. E.G of Krusal’s Algorithm  Find a minimum spanning tree of the below connected undirected graph by Krusals algorithm.  List the edges in non-decreasing order shown below:  Therefore the minimum spanning tree that will be produced will be: 9
  • 10. 10 Prim’s algorithm Input :A connected weighted Graph G. Output :A minimum spanning tree T. T = a spanning tree containing a single node s; E = set of edges adjacent to s; while T does not contain all the nodes { remove an edge (v, w) of lowest cost from E if w is already in T then discard edge (v, w) else { add edge (v, w) and node w to T add to E the edges adjacent to w } }
  • 11. E.G OF PRIM’S ALGORITHM  Find a minimum spanning tree by using prim’s algorithm of the below given graph: 11
  • 12. 12
  • 13. DIFFERENCE PRIM’S ALGORITHM  In this, at every step edges of minimum weight that are incident to a vertex already in the tree and not forming a cycle is choosen . KRUSAL’S ALGOTITHM  In this, at every step edges of minimum weight that are not necessarily incident to a vertex already in the tree and not forming a cycle is choosen . 13