SlideShare a Scribd company logo
Data structure note
DATA STRUCTURE
DFS,BFS,Spanning Tree
Syed Mujtaba Gillani BSCS-F17-LC-317
Asad Zulfiqar BSCS-F17-LC-337
Jawwad Haider BSCS-F17-LC-032
What is a Graph?
 Graphs are one of the most interesting data structures in computer
science. Graphs and the trees are somewhat similar by their structure. In
fact, tree is derived from the graph data structure. However there are
two important differences between trees and graphs.
 Unlike trees, in graphs, a node can have many parents.
 The link between the nodes may have values or weights.
 Graphs are good in modeling real world problems like representing
cities which are connected by roads and finding the paths between
cities, modeling air traffic controller system, etc. These kinds of
problems are hard to represent using simple tree structures. The
following example shows a very simple graph.
 In the above graph, A,B,C,D,E,F are called nodes and the connecting lines between
these nodes are called edges. The edges can be directed edges which are shown
by arrows; they can also be weighted edges in which some numbers are assigned
to them. Hence, a graph can be a directed/undirected and weighted/un-weighted
graph. In this article, we will discuss undirected and un-weighted graphs.
Graph Traversal
 The breadth first search (BFS) and the depth first search (DFS) are
the two algorithms used for traversing and searching a node in a
graph. They can also be used to find out whether a node is reachable
from a given node or not.
Breadth First Search (BFS)
 This is a very different approach for traversing the graph nodes. The aim of
BFS algorithm is to traverse the graph as close as possible to the root node.
Queue is used in the implementation of the breadth first search. Let’s see
how BFS traversal works with respect to the following graph:
 If we do the breadth first traversal of the above graph and print the visited node
as the output, it will print the following output. “A B C D E F”. The BFS visits the
nodes level by level, so it will start with level 0 which is the root node, and then
it moves to the next levels which are B, C and D, then the last levels which are E
and F.
 Algorithmic Steps
 Step 1: Push the root node in the Queue.
 Step 2: Loop until the queue is empty.
 Step 3: Remove the node from the Queue.
 Step 4: If the removed node has unvisited child nodes, mark them as visited
and insert the unvisited children in the queue.
 Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion
and uses a queue to remember to get the next vertex to start a search, when a
dead end occurs in any iteration.
 As in the example given above, BFS algorithm traverses from A to B to E to F
first then to C and G lastly to D. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert
it in a queue.
 Rule 2 − If no adjacent vertex is found, remove the first vertex from the
queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
St
ep
Traversal Description
1 Initialize the
queue.
2
We start from visiting S(starting
node), and mark it as visited.
3 We then see an unvisited adjacent
node from S. In this example, we
have three nodes but alphabetically
we choose A, mark it as visited and
enqueue it.
4
Next, the unvisited adjacent node
from S is B. We mark it as visited
and enqueue it.
5
Next, the unvisited adjacent node
from S is C. We mark it as visited
and enqueue it.
6
Now, S is left with no unvisited
adjacent nodes. So, we dequeue
and find A.
7
From A we have D as unvisited
adjacent node. We mark it as visited
and enqueue it.
At this stage, we are left with no
unmarked (unvisited) nodes. But as per
the algorithm we keep on dequeuing in
order to get all unvisited nodes. When
the queue gets emptied, the program is
over.
Depth First Search (DFS)
 The aim of DFS algorithm is to traverse the graph in such a way that it tries
to go far from the root node. Stack is used in the implementation of the
depth first search. Let’s see how depth first search works with respect to
the following graph:
 As stated before, in DFS, nodes are visited by going through the depth of the
tree from the starting node. If we do the depth first traversal of the above
graph and print the visited node, it will be “A B E F C D”. DFS visits the root
node and then its children nodes until it reaches the end node, i.e. E and F
nodes, then moves up to the parent nodes.
 Algorithmic Steps
 Step 1: Push the root node in the Stack.
 Step 2: Loop until stack is empty.
 Step 3: Peek the node of the stack.
 Step 4: If the node has unvisited child nodes, get the unvisited child node,
mark it as traversed and push it on stack.
 Step 5: If the node does not have any unvisited child nodes, pop the node
from the stack.
 Depth First Search (DFS) algorithm traverses a graph in a depthward motion and
uses a stack to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.
 As in the example given above, DFS algorithm traverses from S to A to D to G to E to
B first, then to F and lastly to C. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a
stack.
 Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop
up all the vertices from the stack, which do not have adjacent vertices.)
 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
Step Traversal Description
1
Initialize the stack.
2 Mark S as visited and put it onto the
stack. Explore any unvisited adjacent
node from S. We have three nodes and
we can pick any of them. For this
example, we shall take the node in an
alphabetical order.
3 Mark A as visited and put it onto the
stack. Explore any unvisited adjacent
node from A. Both Sand D are
adjacent to A but we are concerned
for unvisited nodes only.
4 Visit D and mark it as visited and put
onto the stack. Here, we
have B and C nodes, which are
adjacent to D and both are unvisited.
However, we shall again choose in an
alphabetical order.
5
We choose B, mark it as visited and
put onto the stack. Here B does not
have any unvisited adjacent node. So,
we pop Bfrom the stack.
6
We check the stack top for return to
the previous node and check if it has
any unvisited nodes. Here, we
find D to be on the top of the stack.
7
Only unvisited adjacent node is
from D is C now. So we visit C, mark it
as visited and put it onto the stack.
As C does not have any unvisited
adjacent node so we keep popping the
stack until we find a node that has an
unvisited adjacent node. In this case,
there's none and we keep popping until
the stack is empty.
Spanning tree:
 A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a
spanning tree does not have cycles and it cannot be disconnected..
 By this definition, we can draw a conclusion that every connected
and undirected Graph G has at least one spanning tree. A
disconnected graph does not have any spanning tree, as it cannot
be spanned to all its vertices.
 We found three spanning trees off one complete graph. A complete
undirected graph can have maximum nn-2 number of spanning trees,
where n is the number of nodes. In the above addressed example, n
is 3, hence 33−2 = 3spanning trees are possible.
General Properties of Spanning Tree
 We now understand that one graph can have more than one
spanning tree. Following are a few properties of the spanning tree
connected to graph G −
 A connected graph G can have more than one spanning tree.
 All possible spanning trees of graph G, have the same number of
edges and vertices.
 The spanning tree does not have any cycle (loops).
 Removing one edge from the spanning tree will make the graph
disconnected, i.e. the spanning tree is minimally connected.
 Adding one edge to the spanning tree will create a circuit or loop, i.e.
the spanning tree is maximally acyclic.
Application of Spanning Tree
 Spanning tree is basically used to find a minimum path to connect
all nodes in a graph. Common application of spanning trees are −
 Civil Network Planning
 Computer Network Routing Protocol
 Cluster Analysis
 Let us understand this through a small example. Consider, city
network as a huge graph and now plans to deploy telephone lines
in such a way that in minimum lines we can connect to all city
nodes. This is where the spanning tree comes into picture.
Minimum Spanning Tree (MST)
 In a weighted graph, a minimum spanning tree is a spanning tree that
has minimum weight than all other spanning trees of the same graph.
In real-world situations, this weight can be measured as distance,
congestion, traffic load or any arbitrary value denoted to the edges.
Minimum Spanning-Tree Algorithm
Kruskal's Algorithm
 Kruskal's algorithm to find the minimum cost spanning tree uses the
greedy approach. This algorithm treats the graph as a forest and
every node it has as an individual tree. A tree connects to another
only and only if, it has the least cost among all available options and
does not violate MST properties.
Prim's Algorithm
 Prim's algorithm to find minimum cost spanning tree (as Kruskal's
algorithm) uses the greedy approach. Prim's algorithm shares a
similarity with the shortest path first algorithms.
 Prim's algorithm, in contrast with Kruskal's algorithm, treats the
nodes as a single tree and keeps on adding new nodes to the
spanning tree from the given graph.

More Related Content

What's hot (15)

PPTX
Breadth First Search (BFS) pada Graph
Achmad Solichin
 
PPT
Plot function in R
Vladimir Bakhrushin
 
PDF
Enhanced Set Theory
Kannan Nambiar
 
DOC
Chapter 2 2 1 1
bolovv
 
DOCX
Mc0082 theory of computer science
smumbahelp
 
PPT
Calc 3.2a
hartcher
 
DOCX
G6 m3-c-lesson 14-s
mlabuski
 
PDF
3.6 notes
Mrs. Hedrick's Class
 
PDF
IRJET- An Extensive Review on Residue Number System for Improving Computer Ar...
IRJET Journal
 
PPTX
Graph theory[1]
sethamit
 
PPT
2.3 and 2.4 Lines
leblance
 
PPT
Naive String Matching Algorithm | Computer Science
Transweb Global Inc
 
PPTX
Maxterms
Ammara Javed
 
PPT
lecture 9
sajinsc
 
Breadth First Search (BFS) pada Graph
Achmad Solichin
 
Plot function in R
Vladimir Bakhrushin
 
Enhanced Set Theory
Kannan Nambiar
 
Chapter 2 2 1 1
bolovv
 
Mc0082 theory of computer science
smumbahelp
 
Calc 3.2a
hartcher
 
G6 m3-c-lesson 14-s
mlabuski
 
IRJET- An Extensive Review on Residue Number System for Improving Computer Ar...
IRJET Journal
 
Graph theory[1]
sethamit
 
2.3 and 2.4 Lines
leblance
 
Naive String Matching Algorithm | Computer Science
Transweb Global Inc
 
Maxterms
Ammara Javed
 
lecture 9
sajinsc
 

Similar to Data structure note (20)

PPTX
Data structure
lalithambiga kamaraj
 
PPTX
logic.pptx
KENNEDY GITHAIGA
 
PPTX
DATA STRUCTURES.pptx
KENNEDY GITHAIGA
 
PPT
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
 
PPTX
Graphs
KomalPaliwal3
 
PPSX
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
PPTX
Data structure and algorithm
sakthibalabalamuruga
 
PPTX
Depth first traversal(data structure algorithms)
bhuvaneshwariA5
 
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
VISWANATHAN R V
 
PPT
Graph Theory PPT presentation created by Selvam.
selfcinima
 
PPTX
Depth First Searching Algorithm (DFS).pptx
NicoleAngelaGilaRamo
 
PPTX
Depth First Searching Algorithm (DFS).pptx
NicoleAngelaGilaRamo
 
PDF
graph representation.pdf
amitbhachne
 
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
kncetaruna
 
PPTX
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
RIYABEPARI
 
PDF
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
PPTX
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
Data structure
lalithambiga kamaraj
 
logic.pptx
KENNEDY GITHAIGA
 
DATA STRUCTURES.pptx
KENNEDY GITHAIGA
 
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
 
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Data structure and algorithm
sakthibalabalamuruga
 
Depth first traversal(data structure algorithms)
bhuvaneshwariA5
 
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
VISWANATHAN R V
 
Graph Theory PPT presentation created by Selvam.
selfcinima
 
Depth First Searching Algorithm (DFS).pptx
NicoleAngelaGilaRamo
 
Depth First Searching Algorithm (DFS).pptx
NicoleAngelaGilaRamo
 
graph representation.pdf
amitbhachne
 
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
kncetaruna
 
Riya Bepari_34700122020_Artificial Intelligence_PEC-IT501B.pptx
RIYABEPARI
 
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Graph data structures for ppt for understanding.pptx
ramkumar649780
 
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
sahadevbkbiet2023
 
Data structure Graph PPT ( BFS & DFS ) NOTES
sahadevbkbiet2023
 
Ad

Recently uploaded (20)

PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Ad

Data structure note

  • 2. DATA STRUCTURE DFS,BFS,Spanning Tree Syed Mujtaba Gillani BSCS-F17-LC-317 Asad Zulfiqar BSCS-F17-LC-337 Jawwad Haider BSCS-F17-LC-032
  • 3. What is a Graph?  Graphs are one of the most interesting data structures in computer science. Graphs and the trees are somewhat similar by their structure. In fact, tree is derived from the graph data structure. However there are two important differences between trees and graphs.  Unlike trees, in graphs, a node can have many parents.  The link between the nodes may have values or weights.  Graphs are good in modeling real world problems like representing cities which are connected by roads and finding the paths between cities, modeling air traffic controller system, etc. These kinds of problems are hard to represent using simple tree structures. The following example shows a very simple graph.
  • 4.  In the above graph, A,B,C,D,E,F are called nodes and the connecting lines between these nodes are called edges. The edges can be directed edges which are shown by arrows; they can also be weighted edges in which some numbers are assigned to them. Hence, a graph can be a directed/undirected and weighted/un-weighted graph. In this article, we will discuss undirected and un-weighted graphs.
  • 5. Graph Traversal  The breadth first search (BFS) and the depth first search (DFS) are the two algorithms used for traversing and searching a node in a graph. They can also be used to find out whether a node is reachable from a given node or not.
  • 6. Breadth First Search (BFS)  This is a very different approach for traversing the graph nodes. The aim of BFS algorithm is to traverse the graph as close as possible to the root node. Queue is used in the implementation of the breadth first search. Let’s see how BFS traversal works with respect to the following graph:
  • 7.  If we do the breadth first traversal of the above graph and print the visited node as the output, it will print the following output. “A B C D E F”. The BFS visits the nodes level by level, so it will start with level 0 which is the root node, and then it moves to the next levels which are B, C and D, then the last levels which are E and F.  Algorithmic Steps  Step 1: Push the root node in the Queue.  Step 2: Loop until the queue is empty.  Step 3: Remove the node from the Queue.  Step 4: If the removed node has unvisited child nodes, mark them as visited and insert the unvisited children in the queue.  Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration.
  • 8.  As in the example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D. It employs the following rules.  Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.  Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.  Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
  • 9. St ep Traversal Description 1 Initialize the queue. 2 We start from visiting S(starting node), and mark it as visited.
  • 10. 3 We then see an unvisited adjacent node from S. In this example, we have three nodes but alphabetically we choose A, mark it as visited and enqueue it. 4 Next, the unvisited adjacent node from S is B. We mark it as visited and enqueue it.
  • 11. 5 Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it. 6 Now, S is left with no unvisited adjacent nodes. So, we dequeue and find A.
  • 12. 7 From A we have D as unvisited adjacent node. We mark it as visited and enqueue it. At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the program is over.
  • 13. Depth First Search (DFS)  The aim of DFS algorithm is to traverse the graph in such a way that it tries to go far from the root node. Stack is used in the implementation of the depth first search. Let’s see how depth first search works with respect to the following graph:
  • 14.  As stated before, in DFS, nodes are visited by going through the depth of the tree from the starting node. If we do the depth first traversal of the above graph and print the visited node, it will be “A B E F C D”. DFS visits the root node and then its children nodes until it reaches the end node, i.e. E and F nodes, then moves up to the parent nodes.  Algorithmic Steps  Step 1: Push the root node in the Stack.  Step 2: Loop until stack is empty.  Step 3: Peek the node of the stack.  Step 4: If the node has unvisited child nodes, get the unvisited child node, mark it as traversed and push it on stack.  Step 5: If the node does not have any unvisited child nodes, pop the node from the stack.
  • 15.  Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.  As in the example given above, DFS algorithm traverses from S to A to D to G to E to B first, then to F and lastly to C. It employs the following rules.  Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.  Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.)  Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
  • 16. Step Traversal Description 1 Initialize the stack. 2 Mark S as visited and put it onto the stack. Explore any unvisited adjacent node from S. We have three nodes and we can pick any of them. For this example, we shall take the node in an alphabetical order.
  • 17. 3 Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both Sand D are adjacent to A but we are concerned for unvisited nodes only. 4 Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes, which are adjacent to D and both are unvisited. However, we shall again choose in an alphabetical order.
  • 18. 5 We choose B, mark it as visited and put onto the stack. Here B does not have any unvisited adjacent node. So, we pop Bfrom the stack. 6 We check the stack top for return to the previous node and check if it has any unvisited nodes. Here, we find D to be on the top of the stack.
  • 19. 7 Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited and put it onto the stack. As C does not have any unvisited adjacent node so we keep popping the stack until we find a node that has an unvisited adjacent node. In this case, there's none and we keep popping until the stack is empty.
  • 20. Spanning tree:  A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges. Hence, a spanning tree does not have cycles and it cannot be disconnected..  By this definition, we can draw a conclusion that every connected and undirected Graph G has at least one spanning tree. A disconnected graph does not have any spanning tree, as it cannot be spanned to all its vertices.
  • 21.  We found three spanning trees off one complete graph. A complete undirected graph can have maximum nn-2 number of spanning trees, where n is the number of nodes. In the above addressed example, n is 3, hence 33−2 = 3spanning trees are possible.
  • 22. General Properties of Spanning Tree  We now understand that one graph can have more than one spanning tree. Following are a few properties of the spanning tree connected to graph G −  A connected graph G can have more than one spanning tree.  All possible spanning trees of graph G, have the same number of edges and vertices.  The spanning tree does not have any cycle (loops).  Removing one edge from the spanning tree will make the graph disconnected, i.e. the spanning tree is minimally connected.  Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree is maximally acyclic.
  • 23. Application of Spanning Tree  Spanning tree is basically used to find a minimum path to connect all nodes in a graph. Common application of spanning trees are −  Civil Network Planning  Computer Network Routing Protocol  Cluster Analysis  Let us understand this through a small example. Consider, city network as a huge graph and now plans to deploy telephone lines in such a way that in minimum lines we can connect to all city nodes. This is where the spanning tree comes into picture.
  • 24. Minimum Spanning Tree (MST)  In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight than all other spanning trees of the same graph. In real-world situations, this weight can be measured as distance, congestion, traffic load or any arbitrary value denoted to the edges. Minimum Spanning-Tree Algorithm Kruskal's Algorithm  Kruskal's algorithm to find the minimum cost spanning tree uses the greedy approach. This algorithm treats the graph as a forest and every node it has as an individual tree. A tree connects to another only and only if, it has the least cost among all available options and does not violate MST properties.
  • 25. Prim's Algorithm  Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses the greedy approach. Prim's algorithm shares a similarity with the shortest path first algorithms.  Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on adding new nodes to the spanning tree from the given graph.