SlideShare a Scribd company logo
1 of 24
Download to read offline
Ms.Mary Jacob
Assistant Professor, Department of Computer Science
Kristu Jayanti College(Autonomous), Bangalore
Graph Traversals
Definitions
7 / 1 7 / 2 0 2 1
2
A Tree is a data structure that emulates a hierarchical tree structure with a set of
linked nodes.
It is a data structure accessed beginning at the root node. Each node is either a
leaf or an internal node. An internal node has one or more child nodes and is
called the parent of its child nodes. All children of the same node are siblings.
Formal Definitions
7 / 1 7 / 2 0 2 1
3
A tree is either empty (no nodes), or a root and zero or more sub trees.
Tree Terminologies
 Root: node without parent (A) , node at the top of the tree
 Internal node: node with at least one child (A, B, C, F)
 External node: a kind of leaf node without children (E, I, J, K, G, H, D)
 Ancestors of a node: parent, grandparent, grand-grandparent, etc
 Depth of a node: number of ancestors
 Height of a tree: maximum depth of any node (3)
 Descendant of a node: child, grandchild, grand-grandchild, etc
 Degree of an element: no. of children it has
 Sub tree: tree consisting of a node and its descendants
 Path: traversal from node to node along the edges that results in a sequence
Formal Definitions
7 / 1 7 / 2 0 2 1
4
 Parent: any node, except root has exactly one edge running upward to another node. The node
above it is called parent.
 Child: any node may have one or more lines running downward to other nodes. Nodes below
are children.
 Leaf: a node that has no children
 Sub tree: any node can be considered to be the root of a subtree, which consists of its children
and its children's children and so on.
 Visiting: a node is visited when program control arrives at the node, usually for processing.
 Traversing: to traverse a tree means to visit all the nodes in some specified order.
 Levels: the level of a particular node refers to how many generations the node is from the root.
Root is assumed to be level 0.
Binary Tree
7 / 1 7 / 2 0 2 1
5
The simplest form of tree is a binary tree. A binary tree consists of a node (called
the root node) and left and right sub-trees. Both the sub-trees are themselves
binary trees.
Binary tree is a set of finite nodes which is either empty or consists of one or
more nodes in which each node has at most two disjoint binary sub trees called
left sub tree or right sub tree respectively.
There may be a zero degree node or a one degree node or a two degree node.
Binary Tree
7 / 1 7 / 2 0 2 1
6
Strictly Binary Tree
7 / 1 7 / 2 0 2 1
7
A binary tree is strictly binary tree if every
non-terminal node has two sub trees or you can say
if every non-terminal node consists of non-empty
left sub tree as well as non-empty right sub tree.
Complete Binary Tree
7 / 1 7 / 2 0 2 1
8
Complete binary tree is a strict binary tree with all leaf nodes at the
same level. The number of nodes at level ‘n’ is 2 n-1
Properties of binary tree
7 / 1 7 / 2 0 2 1
9
 The maximum number of nodes at a level I of binary tree is 2I where I>=0.
 If h = height of a binary tree,
maximum number of nodes possible in a binary tree of height h is 2h-1
minimum number of nodes possible in a binary tree of height h is h
 For any non empty tree, number of terminal or leaf nodes is equal to number
of non terminal nodes (internal nodes) +1.
 For any non empty binary tree, if ‘n’ is the number of nodes and ‘e’ is the
number of edges, then n=e+1;
7 / 1 7 / 2 0 2 1
10
Traversals of a Binary Tree
PREORDER TRAVERSAL:
 Process the root R.
 Traverse the left sub tree of R in preorder.
 Traverse the right sub tree of R in preorder.
INORDER TRAVERSAL:
 Traverse the left sub tree of R in inorder.
 Process the root R.
 Traverse the right sub tree of R in inorder.
POSTORDER TRAVERSAL:
 Traverse the left sub tree of R in postorder.
 Traverse the right sub tree of R in postorder.
 Process the root R.
Traversal - Example
7 / 1 7 / 2 0 2 1
11
INORDER TRAVERSAL: 10, 15, 16, 32, 33, 34, 87
PREORDER TRAVERSAL: 32, 16, 10, 15,34,33,87
POSTORDER TRAVERSAL: 15, 10, 16, 33, 87, 34, 32
7 / 1 7 / 2 0 2 1
12
The inorder and preorder traversals of a binary tree are as follows
Inorder: MOUPTCRES
Preorder: COMPUTERS
Draw the binary tree and write the postorder traversal.
Graph Traversals
7 / 1 7 / 2 0 2 1
13
A graph traversal means visiting the nodes of a graph one after the other in a
systematic manner .Two important graph traversals techniques are
1. Breadth First Search (BFS)
2. Depth First Search(DFS)
BFS (Breadth First Search)
7 / 1 7 / 2 0 2 1
14
In graphs we do not have any special vertex designated as source vertex. So
traversal can start from any arbitrary vertex. So traversal can start from any
arbitrary vertex.
Each vertex in a graph has an associated level which arises from the position of
that vertex in the graph.
If the traversal is starting from the node A, we examine the node A then we visit
all the neighbours of A.
Then we examine all the neighbours of the neighbours of A and so on. In BFS
the vertices are visited in the order of their levels.
At each level, the vertices are visited from left to right.
BFS (Breadth First Search)-Design
7 / 1 7 / 2 0 2 1
15
Since the vertices are visited from level 0, level 1and so on, we can
implement BFS using queue data structure. To begin with insert the source
vertex into the queue, and then repeat the following steps until queue is
empty.
1. Delete a vertex from the queue and the vertex is visited or processed.
2. All the nodes which are adjacent to the deleted vertex but not visited
earlier are inserted into the queue from left to right.
7 / 1 7 / 2 0 2 1
16
ALGORITHM BFS(v)
/* Let v be the starting vertex
visited[i] = false initially for i = 1 to n
for any node i visited [i] = true if i has already been visited else it contains false.
Queue is represented as q */
{
u=v;
visited[v]=true;
repeat
{ for all vertices w adjacent from u do
{ if ( visited [w]==false) then
{ add vertex w to q;
visited [w] =true;
}
}
if q is empty then return ;
delete vertex u from q;
}until (false);
}
BFS (Breadth First Search)
7 / 1 7 / 2 0 2 1
17
BFS (Breadth First Search)
7 / 1 7 / 2 0 2 1
18
DFS (Depth First Search)
7 / 1 7 / 2 0 2 1
19
Traverses a single path of the graph as far as it can go (that is until it visits a
node with no successor or a node of all whose successors have already been
visited). It then resumes the last node on the path just traversed that has
unvisited successors and begins traversing a new path emanating from that
node.
DFS-Design
7 / 1 7 / 2 0 2 1
20
Stack is used to implement DFS.
Whenever a vertex is visited for the first time, that vertex is pushed on to
the stack and the vertex is deleted from the stack when a dead end is
reached and the search resumes from the vertex that is deleted most
recently .
If there are no vertices adjacent to the most recently deleted vertex, the
next node is deleted from the stack and the process is repeated until all the
vertices are reached or till the stack is empty.
7 / 1 7 / 2 0 2 1
21
Algorithm depthfirst(vertex v)
{
visited [v] =true;
for each vertex w adjacent to i do
if (visited[w]==false)
depthfirst(w)
}
ALGORITHM DFS(v)
/* Let G be the given graph with n vertices.
{
for i = 1 to n
visited[i] = false
for i =1 to n
if ( visited [i]==false) then
depthfirst (i);
}
DFS-Example
7 / 1 7 / 2 0 2 1
22
DFS-Solution
7 / 1 7 / 2 0 2 1
23
Applications of DFS and BFS
7 / 1 7 / 2 0 2 1
24
1. To check whether the given graph is connected or not
2. To check whether the given graph is cyclic or not
3. To find the spanning tree
4. To find the vertices that is reachable from given vertex.

More Related Content

What's hot

Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data typesPratik Devmurari
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data TypesRokonuzzaman Rony
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operatorsraksharao
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in CArpana shree
 
Computer data type and Terminologies
Computer data type and Terminologies Computer data type and Terminologies
Computer data type and Terminologies glyvive
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-cteach4uin
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++Neeru Mittal
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'illpa
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonRaajendra M
 
Variables and data types in C++
Variables and data types in C++Variables and data types in C++
Variables and data types in C++Ameer Khan
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++Abdul Hafeez
 

What's hot (19)

Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Vhdl introduction
Vhdl introductionVhdl introduction
Vhdl introduction
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Data types in C
Data types in CData types in C
Data types in C
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Computer data type and Terminologies
Computer data type and Terminologies Computer data type and Terminologies
Computer data type and Terminologies
 
Data Types
Data TypesData Types
Data Types
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Data type
Data typeData type
Data type
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
structures and unions in 'C'
structures and unions in 'C'structures and unions in 'C'
structures and unions in 'C'
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
Variables and data types in C++
Variables and data types in C++Variables and data types in C++
Variables and data types in C++
 
Session02 c intro
Session02 c introSession02 c intro
Session02 c intro
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
 

Similar to Graph Traversals

NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxRajitha Reddy Alugati
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationnakulvarshney371
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search treeMayeesha Samiha
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structruresGaurav Sharma
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptxAbirami A
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithmrahela bham
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptssuser5c874e
 

Similar to Graph Traversals (20)

NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structrures
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Tree
TreeTree
Tree
 
17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Graphs
GraphsGraphs
Graphs
 
Analysis & design of algorithm
Analysis & design of algorithmAnalysis & design of algorithm
Analysis & design of algorithm
 
Trees
TreesTrees
Trees
 
data_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.pptdata_structures_and_applications_-_module-4.ppt
data_structures_and_applications_-_module-4.ppt
 

More from MaryJacob24

Unit 1-Introduction to Data Structures-BCA.pdf
Unit 1-Introduction to Data Structures-BCA.pdfUnit 1-Introduction to Data Structures-BCA.pdf
Unit 1-Introduction to Data Structures-BCA.pdfMaryJacob24
 
Unit 2-Data Modeling.pdf
Unit 2-Data Modeling.pdfUnit 2-Data Modeling.pdf
Unit 2-Data Modeling.pdfMaryJacob24
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
Unit 4- Dynamic Programming.pdf
Unit 4- Dynamic Programming.pdfUnit 4- Dynamic Programming.pdf
Unit 4- Dynamic Programming.pdfMaryJacob24
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxMaryJacob24
 
tree traversals.pdf
tree traversals.pdftree traversals.pdf
tree traversals.pdfMaryJacob24
 
Linked List-Types.pdf
Linked List-Types.pdfLinked List-Types.pdf
Linked List-Types.pdfMaryJacob24
 
Unit 5- Cloud Applications.pdf
Unit 5- Cloud Applications.pdfUnit 5- Cloud Applications.pdf
Unit 5- Cloud Applications.pdfMaryJacob24
 
Simplification of Circuits.pdf
Simplification of Circuits.pdfSimplification of Circuits.pdf
Simplification of Circuits.pdfMaryJacob24
 
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...MaryJacob24
 
Algorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdfAlgorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdfMaryJacob24
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfMaryJacob24
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy MethodMaryJacob24
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy methodMaryJacob24
 

More from MaryJacob24 (15)

Unit 1-Introduction to Data Structures-BCA.pdf
Unit 1-Introduction to Data Structures-BCA.pdfUnit 1-Introduction to Data Structures-BCA.pdf
Unit 1-Introduction to Data Structures-BCA.pdf
 
Unit 2-Data Modeling.pdf
Unit 2-Data Modeling.pdfUnit 2-Data Modeling.pdf
Unit 2-Data Modeling.pdf
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
Unit 4- Dynamic Programming.pdf
Unit 4- Dynamic Programming.pdfUnit 4- Dynamic Programming.pdf
Unit 4- Dynamic Programming.pdf
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptx
 
tree traversals.pdf
tree traversals.pdftree traversals.pdf
tree traversals.pdf
 
Linked List-Types.pdf
Linked List-Types.pdfLinked List-Types.pdf
Linked List-Types.pdf
 
Unit 5- Cloud Applications.pdf
Unit 5- Cloud Applications.pdfUnit 5- Cloud Applications.pdf
Unit 5- Cloud Applications.pdf
 
Simplification of Circuits.pdf
Simplification of Circuits.pdfSimplification of Circuits.pdf
Simplification of Circuits.pdf
 
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
 
Algorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdfAlgorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdf
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
 
quick sort.pdf
quick sort.pdfquick sort.pdf
quick sort.pdf
 
Unit 3 - Greedy Method
Unit 3  - Greedy MethodUnit 3  - Greedy Method
Unit 3 - Greedy Method
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Graph Traversals

  • 1. Ms.Mary Jacob Assistant Professor, Department of Computer Science Kristu Jayanti College(Autonomous), Bangalore Graph Traversals
  • 2. Definitions 7 / 1 7 / 2 0 2 1 2 A Tree is a data structure that emulates a hierarchical tree structure with a set of linked nodes. It is a data structure accessed beginning at the root node. Each node is either a leaf or an internal node. An internal node has one or more child nodes and is called the parent of its child nodes. All children of the same node are siblings.
  • 3. Formal Definitions 7 / 1 7 / 2 0 2 1 3 A tree is either empty (no nodes), or a root and zero or more sub trees. Tree Terminologies  Root: node without parent (A) , node at the top of the tree  Internal node: node with at least one child (A, B, C, F)  External node: a kind of leaf node without children (E, I, J, K, G, H, D)  Ancestors of a node: parent, grandparent, grand-grandparent, etc  Depth of a node: number of ancestors  Height of a tree: maximum depth of any node (3)  Descendant of a node: child, grandchild, grand-grandchild, etc  Degree of an element: no. of children it has  Sub tree: tree consisting of a node and its descendants  Path: traversal from node to node along the edges that results in a sequence
  • 4. Formal Definitions 7 / 1 7 / 2 0 2 1 4  Parent: any node, except root has exactly one edge running upward to another node. The node above it is called parent.  Child: any node may have one or more lines running downward to other nodes. Nodes below are children.  Leaf: a node that has no children  Sub tree: any node can be considered to be the root of a subtree, which consists of its children and its children's children and so on.  Visiting: a node is visited when program control arrives at the node, usually for processing.  Traversing: to traverse a tree means to visit all the nodes in some specified order.  Levels: the level of a particular node refers to how many generations the node is from the root. Root is assumed to be level 0.
  • 5. Binary Tree 7 / 1 7 / 2 0 2 1 5 The simplest form of tree is a binary tree. A binary tree consists of a node (called the root node) and left and right sub-trees. Both the sub-trees are themselves binary trees. Binary tree is a set of finite nodes which is either empty or consists of one or more nodes in which each node has at most two disjoint binary sub trees called left sub tree or right sub tree respectively. There may be a zero degree node or a one degree node or a two degree node.
  • 6. Binary Tree 7 / 1 7 / 2 0 2 1 6
  • 7. Strictly Binary Tree 7 / 1 7 / 2 0 2 1 7 A binary tree is strictly binary tree if every non-terminal node has two sub trees or you can say if every non-terminal node consists of non-empty left sub tree as well as non-empty right sub tree.
  • 8. Complete Binary Tree 7 / 1 7 / 2 0 2 1 8 Complete binary tree is a strict binary tree with all leaf nodes at the same level. The number of nodes at level ‘n’ is 2 n-1
  • 9. Properties of binary tree 7 / 1 7 / 2 0 2 1 9  The maximum number of nodes at a level I of binary tree is 2I where I>=0.  If h = height of a binary tree, maximum number of nodes possible in a binary tree of height h is 2h-1 minimum number of nodes possible in a binary tree of height h is h  For any non empty tree, number of terminal or leaf nodes is equal to number of non terminal nodes (internal nodes) +1.  For any non empty binary tree, if ‘n’ is the number of nodes and ‘e’ is the number of edges, then n=e+1;
  • 10. 7 / 1 7 / 2 0 2 1 10 Traversals of a Binary Tree PREORDER TRAVERSAL:  Process the root R.  Traverse the left sub tree of R in preorder.  Traverse the right sub tree of R in preorder. INORDER TRAVERSAL:  Traverse the left sub tree of R in inorder.  Process the root R.  Traverse the right sub tree of R in inorder. POSTORDER TRAVERSAL:  Traverse the left sub tree of R in postorder.  Traverse the right sub tree of R in postorder.  Process the root R.
  • 11. Traversal - Example 7 / 1 7 / 2 0 2 1 11 INORDER TRAVERSAL: 10, 15, 16, 32, 33, 34, 87 PREORDER TRAVERSAL: 32, 16, 10, 15,34,33,87 POSTORDER TRAVERSAL: 15, 10, 16, 33, 87, 34, 32
  • 12. 7 / 1 7 / 2 0 2 1 12 The inorder and preorder traversals of a binary tree are as follows Inorder: MOUPTCRES Preorder: COMPUTERS Draw the binary tree and write the postorder traversal.
  • 13. Graph Traversals 7 / 1 7 / 2 0 2 1 13 A graph traversal means visiting the nodes of a graph one after the other in a systematic manner .Two important graph traversals techniques are 1. Breadth First Search (BFS) 2. Depth First Search(DFS)
  • 14. BFS (Breadth First Search) 7 / 1 7 / 2 0 2 1 14 In graphs we do not have any special vertex designated as source vertex. So traversal can start from any arbitrary vertex. So traversal can start from any arbitrary vertex. Each vertex in a graph has an associated level which arises from the position of that vertex in the graph. If the traversal is starting from the node A, we examine the node A then we visit all the neighbours of A. Then we examine all the neighbours of the neighbours of A and so on. In BFS the vertices are visited in the order of their levels. At each level, the vertices are visited from left to right.
  • 15. BFS (Breadth First Search)-Design 7 / 1 7 / 2 0 2 1 15 Since the vertices are visited from level 0, level 1and so on, we can implement BFS using queue data structure. To begin with insert the source vertex into the queue, and then repeat the following steps until queue is empty. 1. Delete a vertex from the queue and the vertex is visited or processed. 2. All the nodes which are adjacent to the deleted vertex but not visited earlier are inserted into the queue from left to right.
  • 16. 7 / 1 7 / 2 0 2 1 16 ALGORITHM BFS(v) /* Let v be the starting vertex visited[i] = false initially for i = 1 to n for any node i visited [i] = true if i has already been visited else it contains false. Queue is represented as q */ { u=v; visited[v]=true; repeat { for all vertices w adjacent from u do { if ( visited [w]==false) then { add vertex w to q; visited [w] =true; } } if q is empty then return ; delete vertex u from q; }until (false); }
  • 17. BFS (Breadth First Search) 7 / 1 7 / 2 0 2 1 17
  • 18. BFS (Breadth First Search) 7 / 1 7 / 2 0 2 1 18
  • 19. DFS (Depth First Search) 7 / 1 7 / 2 0 2 1 19 Traverses a single path of the graph as far as it can go (that is until it visits a node with no successor or a node of all whose successors have already been visited). It then resumes the last node on the path just traversed that has unvisited successors and begins traversing a new path emanating from that node.
  • 20. DFS-Design 7 / 1 7 / 2 0 2 1 20 Stack is used to implement DFS. Whenever a vertex is visited for the first time, that vertex is pushed on to the stack and the vertex is deleted from the stack when a dead end is reached and the search resumes from the vertex that is deleted most recently . If there are no vertices adjacent to the most recently deleted vertex, the next node is deleted from the stack and the process is repeated until all the vertices are reached or till the stack is empty.
  • 21. 7 / 1 7 / 2 0 2 1 21 Algorithm depthfirst(vertex v) { visited [v] =true; for each vertex w adjacent to i do if (visited[w]==false) depthfirst(w) } ALGORITHM DFS(v) /* Let G be the given graph with n vertices. { for i = 1 to n visited[i] = false for i =1 to n if ( visited [i]==false) then depthfirst (i); }
  • 22. DFS-Example 7 / 1 7 / 2 0 2 1 22
  • 23. DFS-Solution 7 / 1 7 / 2 0 2 1 23
  • 24. Applications of DFS and BFS 7 / 1 7 / 2 0 2 1 24 1. To check whether the given graph is connected or not 2. To check whether the given graph is cyclic or not 3. To find the spanning tree 4. To find the vertices that is reachable from given vertex.