SlideShare a Scribd company logo
1 of 14
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

What's hot (20)

Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Graph data structure and algorithms
Graph data structure and algorithmsGraph data structure and algorithms
Graph data structure and algorithms
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Heaps
HeapsHeaps
Heaps
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
And or graph
And or graphAnd or graph
And or graph
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 

Viewers also liked

Connected components and shortest path
Connected components and shortest pathConnected components and shortest path
Connected components and shortest pathKaushik Koneru
 
B trees and_b__trees
B trees and_b__treesB trees and_b__trees
B trees and_b__treesmeghu123
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Best for b trees
Best for b treesBest for b trees
Best for b treesDineshRaaja
 
File organization 1
File organization 1File organization 1
File organization 1Rupali Rana
 
FILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMSFILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMSAbhishek Dutta
 
Chapter 11 - File System Implementation
Chapter 11 - File System ImplementationChapter 11 - File System Implementation
Chapter 11 - File System ImplementationWayne Jones Jnr
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Computer architecture and organization
Computer architecture and organizationComputer architecture and organization
Computer architecture and organizationTushar B Kute
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMSkoolkampus
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programmingparamalways
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 

Viewers also liked (20)

Connected components and shortest path
Connected components and shortest pathConnected components and shortest path
Connected components and shortest path
 
B trees and_b__trees
B trees and_b__treesB trees and_b__trees
B trees and_b__trees
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
File organization 1
File organization 1File organization 1
File organization 1
 
FILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMSFILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMS
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
Chapter 11 - File System Implementation
Chapter 11 - File System ImplementationChapter 11 - File System Implementation
Chapter 11 - File System Implementation
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Virtual memory ppt
Virtual memory pptVirtual memory ppt
Virtual memory ppt
 
Computer architecture and organization
Computer architecture and organizationComputer architecture and organization
Computer architecture and organization
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Lec1
Lec1Lec1
Lec1
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 

Similar to Spanning trees

KRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitraKRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitraguest1f4fb3
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraKRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraguest1f4fb3
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...KUSHDHIRRA2111026030
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)Madhu Bala
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning treeSTEFFY D
 
spanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdfspanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdfYasirAli74993
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithmshreeuva
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfzefergaming
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 

Similar to Spanning trees (20)

KRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitraKRUSKALS'S algorithm from chaitra
KRUSKALS'S algorithm from chaitra
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraKRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitra
 
Graphs
GraphsGraphs
Graphs
 
33 spanning-trees
33 spanning-trees33 spanning-trees
33 spanning-trees
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Data structure
Data structureData structure
Data structure
 
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
17 prims-kruskals (1)
17 prims-kruskals (1)17 prims-kruskals (1)
17 prims-kruskals (1)
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
spanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdfspanningtreesapplications-120903115339-phpapp02 (1).pdf
spanningtreesapplications-120903115339-phpapp02 (1).pdf
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
prim's and kruskal's algorithm
prim's and kruskal's algorithmprim's and kruskal's algorithm
prim's and kruskal's algorithm
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Lecture3
Lecture3Lecture3
Lecture3
 

More from Shareb Ismaeel

More from Shareb Ismaeel (9)

Cybercrimes
CybercrimesCybercrimes
Cybercrimes
 
Unions
UnionsUnions
Unions
 
Multiprocessor structures
Multiprocessor structuresMultiprocessor structures
Multiprocessor structures
 
Installation testing
Installation testingInstallation testing
Installation testing
 
Genome data management
Genome data managementGenome data management
Genome data management
 
E mail systems
E mail systemsE mail systems
E mail systems
 
E commerce
E commerceE commerce
E commerce
 
Disk structure
Disk structureDisk structure
Disk structure
 
.Netframework
.Netframework.Netframework
.Netframework
 

Recently uploaded

Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsmeharikiros2
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 

Recently uploaded (20)

Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.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