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

PPTX
Topological Sort and BFS
PDF
Daa chpater 12
PDF
Data Representation of Strings
PDF
PPT
Systems of Inequalities
PPTX
Ap review session continuity, differentiability and major theorems
PPTX
Depth First Search (DFS) pada Graph
PPT
Topological Sort and BFS
Daa chpater 12
Data Representation of Strings
Systems of Inequalities
Ap review session continuity, differentiability and major theorems
Depth First Search (DFS) pada Graph

What's hot (15)

PPTX
Breadth First Search (BFS) pada Graph
PPT
Plot function in R
PDF
Enhanced Set Theory
DOC
Chapter 2 2 1 1
DOCX
Mc0082 theory of computer science
PPT
Calc 3.2a
DOCX
G6 m3-c-lesson 14-s
PDF
PDF
IRJET- An Extensive Review on Residue Number System for Improving Computer Ar...
PPTX
Graph theory[1]
PPT
2.3 and 2.4 Lines
PPT
Naive String Matching Algorithm | Computer Science
PPTX
Maxterms
PPT
lecture 9
Breadth First Search (BFS) pada Graph
Plot function in R
Enhanced Set Theory
Chapter 2 2 1 1
Mc0082 theory of computer science
Calc 3.2a
G6 m3-c-lesson 14-s
IRJET- An Extensive Review on Residue Number System for Improving Computer Ar...
Graph theory[1]
2.3 and 2.4 Lines
Naive String Matching Algorithm | Computer Science
Maxterms
lecture 9
Ad

Similar to Data structure note (20)

PDF
Graph Traversals
PPT
Data Structures-Non Linear DataStructures-Graphs
PPTX
PPT
Unit VI - Graphs.ppt
PPTX
Graphs in Data Structure
PPTX
Algorithms and data Chapter 3 V Graph.pptx
PPTX
Data structure
PPTX
breadth first search
PPT
8-Graph.ppt
PPTX
Breath first Search and Depth first search
PPTX
Depth first traversal(data structure algorithms)
PPTX
Data structure and algorithm
PPTX
BFS (Breadth First Search) Tree Traversal
PPTX
Graph data structures for ppt for understanding.pptx
PPT
2.5 bfs & dfs 02
PDF
U1 L5 DAA.pdf
PDF
graphtraversals.pdf
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
Graph Traversals
Data Structures-Non Linear DataStructures-Graphs
Unit VI - Graphs.ppt
Graphs in Data Structure
Algorithms and data Chapter 3 V Graph.pptx
Data structure
breadth first search
8-Graph.ppt
Breath first Search and Depth first search
Depth first traversal(data structure algorithms)
Data structure and algorithm
BFS (Breadth First Search) Tree Traversal
Graph data structures for ppt for understanding.pptx
2.5 bfs & dfs 02
U1 L5 DAA.pdf
graphtraversals.pdf
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
Ad

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Pre independence Education in Inndia.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
01-Introduction-to-Information-Management.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPH.pptx obstetrics and gynecology in nursing
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Pre independence Education in Inndia.pdf
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
102 student loan defaulters named and shamed – Is someone you know on the list?
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Supply Chain Operations Speaking Notes -ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
01-Introduction-to-Information-Management.pdf

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.