Data structure note

Muhammad  Nawaz
Muhammad NawazInstructor em Student
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.
1 de 25

Recomendados

Topological Sort and BFSTopological Sort and BFS
Topological Sort and BFSArchanaMani2
80 visualizações18 slides
Daa chpater 12Daa chpater 12
Daa chpater 12B.Kirron Reddi
22 visualizações16 slides
Data Representation of StringsData Representation of Strings
Data Representation of StringsProf Ansari
2.2K visualizações3 slides
3.6 notes3.6 notes
3.6 notesMrs. Hedrick's Class
156 visualizações8 slides
Systems of InequalitiesSystems of Inequalities
Systems of InequalitiesBitsy Griffin
270 visualizações10 slides

Mais conteúdo relacionado

Mais procurados

Plot function in RPlot function in R
Plot function in RVladimir Bakhrushin
1.3K visualizações27 slides
Enhanced Set TheoryEnhanced Set Theory
Enhanced Set TheoryKannan Nambiar
134 visualizações17 slides
Chapter 2 2 1 1Chapter 2 2 1 1
Chapter 2 2 1 1bolovv
951 visualizações5 slides
Mc0082  theory of computer scienceMc0082  theory of computer science
Mc0082 theory of computer sciencesmumbahelp
36 visualizações3 slides
Linear~1Linear~1
Linear~1Carla Maxinne Santos
200 visualizações6 slides

Mais procurados(15)

Breadth First Search (BFS) pada GraphBreadth First Search (BFS) pada Graph
Breadth First Search (BFS) pada Graph
Achmad Solichin292 visualizações
Plot function in RPlot function in R
Plot function in R
Vladimir Bakhrushin1.3K visualizações
Enhanced Set TheoryEnhanced Set Theory
Enhanced Set Theory
Kannan Nambiar134 visualizações
Chapter 2 2 1 1Chapter 2 2 1 1
Chapter 2 2 1 1
bolovv951 visualizações
Mc0082  theory of computer scienceMc0082  theory of computer science
Mc0082 theory of computer science
smumbahelp36 visualizações
Linear~1Linear~1
Linear~1
Carla Maxinne Santos200 visualizações
Calc 3.2aCalc 3.2a
Calc 3.2a
hartcher350 visualizações
G6 m3-c-lesson 14-sG6 m3-c-lesson 14-s
G6 m3-c-lesson 14-s
mlabuski290 visualizações
3.6 notes3.6 notes
3.6 notes
Mrs. Hedrick's Class108 visualizações
Graph theory[1]Graph theory[1]
Graph theory[1]
sethamit945 visualizações
2.3 and 2.4 Lines2.3 and 2.4 Lines
2.3 and 2.4 Lines
leblance1.3K visualizações
Naive String Matching Algorithm | Computer ScienceNaive String Matching Algorithm | Computer Science
Naive String Matching Algorithm | Computer Science
Transweb Global Inc1.7K visualizações
MaxtermsMaxterms
Maxterms
Ammara Javed1.8K visualizações
lecture 9lecture 9
lecture 9
sajinsc539 visualizações

Similar a Data structure note

Data structureData structure
Data structurelalithambiga kamaraj
92 visualizações12 slides
logic.pptxlogic.pptx
logic.pptxKENNEDY GITHAIGA
4 visualizações58 slides
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptxKENNEDY GITHAIGA
7 visualizações59 slides
GraphsGraphs
GraphsKomalPaliwal3
66 visualizações44 slides

Similar a Data structure note(20)

Data structureData structure
Data structure
lalithambiga kamaraj92 visualizações
logic.pptxlogic.pptx
logic.pptx
KENNEDY GITHAIGA4 visualizações
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
KENNEDY GITHAIGA7 visualizações
GraphsGraphs
Graphs
KomalPaliwal366 visualizações
Data structure and algorithmData structure and algorithm
Data structure and algorithm
sakthibalabalamuruga71 visualizações
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
bhuvaneshwariA585 visualizações
graph representation.pdfgraph representation.pdf
graph representation.pdf
amitbhachne47 visualizações
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
Kongunadu College of Engineering and Technology9 visualizações
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham77 visualizações
Graph in data structuresGraph in data structures
Graph in data structures
AhsanRazaKolachi2 visualizações
Y11 m02  networksY11 m02  networks
Y11 m02 networks
Xu Wei131 visualizações
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
infanciaj64 visualizações
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
king77987931 visualizações
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
rahela bham184 visualizações
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
SwarndeviKm29 visualizações
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
alisha230390620 visualizações
Spanning treesSpanning trees
Spanning trees
Shareb Ismaeel9.3K visualizações
Graph TheoryGraph Theory
Graph Theory
Shivam Singh1.3K visualizações

Último(20)

ME_URBAN_WAR.pptME_URBAN_WAR.ppt
ME_URBAN_WAR.ppt
Norvell (Tex) DeAtkine117 visualizações
SIMPLE PRESENT TENSE_new.pptxSIMPLE PRESENT TENSE_new.pptx
SIMPLE PRESENT TENSE_new.pptx
nisrinamadani2135 visualizações
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open Innovation
Michal Hron68 visualizações
Material del tarjetero LEES Travesías.docxMaterial del tarjetero LEES Travesías.docx
Material del tarjetero LEES Travesías.docx
Norberto Millán Muñoz48 visualizações
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
Tariq KHAN57 visualizações
Hands-On Workshop Chat GPT (Intro-B4-Practical).pdfHands-On Workshop Chat GPT (Intro-B4-Practical).pdf
Hands-On Workshop Chat GPT (Intro-B4-Practical).pdf
noorishamirsyad228 visualizações
STYP infopack.pdfSTYP infopack.pdf
STYP infopack.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego125 visualizações
Azure DevOps Pipeline setup for Mule APIs #36Azure DevOps Pipeline setup for Mule APIs #36
Azure DevOps Pipeline setup for Mule APIs #36
MysoreMuleSoftMeetup66 visualizações
ICANNICANN
ICANN
RajaulKarim2053 visualizações
1930s Stoke Newington - Rediscovering a Decade of Profound Change1930s Stoke Newington - Rediscovering a Decade of Profound Change
1930s Stoke Newington - Rediscovering a Decade of Profound Change
History of Stoke Newington147 visualizações
NS3 Unit 2 Life processes of animals.pptxNS3 Unit 2 Life processes of animals.pptx
NS3 Unit 2 Life processes of animals.pptx
manuelaromero201368 visualizações
ICS3211_lecture_week72023.pdfICS3211_lecture_week72023.pdf
ICS3211_lecture_week72023.pdf
Vanessa Camilleri175 visualizações
GSoC 2024GSoC 2024
GSoC 2024
DeveloperStudentClub1041 visualizações
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
Tariq KHAN57 visualizações
Streaming Quiz 2023.pdfStreaming Quiz 2023.pdf
Streaming Quiz 2023.pdf
Quiz Club NITW77 visualizações
ICDE OERAC_CostaRica2023_OER_AI_Final7Nov2023.pptx.pdfICDE OERAC_CostaRica2023_OER_AI_Final7Nov2023.pptx.pdf
ICDE OERAC_CostaRica2023_OER_AI_Final7Nov2023.pptx.pdf
Ebba Ossiannilsson41 visualizações
Universe revised.pdfUniverse revised.pdf
Universe revised.pdf
DrHafizKosar79 visualizações
2022 CAPE Merit List 2023 2022 CAPE Merit List 2023
2022 CAPE Merit List 2023
Caribbean Examinations Council2.3K visualizações

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.