Professional Documents
Culture Documents
DATA STRUCTURES
MODULE IV
Jacob P Cherian
Asst.Professor
Dept.of CSE, Saintgits College of Engineering
1
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
CONTENTS
Introduction to Tree Data Structure
Tree Operations
Binary Tree Representations
Tree Traversals
Binary Search Trees
Binary Search Tree Operations
Introduction to Graph Data Structure
Representation of Graphs
Graph Traversal- BFS & DFS
Application of Graphs 2
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Tree- Definition
ii) remaining nodes are partitioned into n(n>0) disjoint sets T1,T2...Tn,
where each Ti(i=1,2...n) are called subtrees of the root.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Trees- Basic Idea
The first node of the tree is called the root. If this root node is
connected by another node, the root is then a parent node and the
connected node is a child.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example of a General Tree
A ROOT NODE
B C
D E F
G H I
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Terminology- Parent & Child
A PARENT
CHILDREN
B C
D E F
G H I
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Terminology- Parent & Child
A
B C
D E F
PARENT
G H I CHILDREN
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Trees- Basic Idea
All Tree nodes are connected by links called edges. It’s an important
part of trees, because it’s manages the relationship between nodes.
Leaves are the last nodes on a tree. They are nodes without children.
Like real trees, we have the root, branches, and finally the leaves.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example of a General Tree
A
B C
D E F
LEAF NODES
G H I
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example of a General Tree
A
SIBLINGS
B C
D E F
Nodes which have the same
parent are called siblings
G H I
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Terminology- Levels
A LEVEL 0
B C LEVEL 1
D E F LEVEL 2
G H I LEVEL 3
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Terminology- Height of a Tree
A
Height = 3
G H I
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Binary Trees-Basic Idea
A binary tree is a tree data structure in which each node has at most
two children, which are referred to as the left child and the right child.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Binary Tree- Example
A
Each node has either 0,
1 or 2 Children
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Size of a Binary Tree
A
Size = 8 The total number of nodes in a
binary tree is known as the
size of a binary tree
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
External Nodes & Internal Nodes
A
External node is a node
with at least one child
B C
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
External Nodes & Internal Nodes
A
External node is a node
with at least one child
B C
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Left Sub-tree & Right Sub-tree
A
Left Subtree of
A
B C
Right Subtree of
A
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Left Subtree & Right Subtree
A
B C
Right Subtree of
D E F Node C
Left Subtree of
Node C
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Maximum no:of nodes at any Level of a Binary
Tree
A
Maximum no:of nodes
possible at any level, (say
i)of a binary tree 2i
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Full Binary Tree
A full Binary tree is a special type of binary tree in which every parent
node has either two or no children
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Full Binary Tree- Example
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Complete Binary Tree
A complete binary tree is a binary tree in which all the levels are
completely filled except possibly the lowest one, which is filled from the
left. All the leaf elements must lean towards the left.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Complete Binary Tree- Example
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Identify- Binary Tree(BT), Complete BT or Full
BT
a) b)
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Identify- Binary Tree(BT), Complete BT or Full BT
c) d)
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Identify- Binary Tree(BT), Complete BT or Full BT
e) f)
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Tree Traversal
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Tree Traversal
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc)
which have only one logical way to traverse them, trees can be
traversed in different ways.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
D
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DB
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBA
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBA
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBA
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBA
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAG
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAGE
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAGE
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAGEH
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAGEHC
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAGEHC
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Inorder Traversal LNR
A
B C
D E F
Inorder Traversal
DBAGEHCF
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
A
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
AB
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
ABD
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
ABDC
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
ABDCE
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
ABDCEG
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
ABDCEGH
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Preorder Traversal NLR
A
B C
D E F
Preorder Traversal
ABDCEGHF
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
D
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DB
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DB
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DB
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DB
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBG
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBG
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBGH
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBGHE
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBGHE
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBGHEF
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBGHEFC
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Postorder Traversal LRN
A
B C
D E F
Postorder Traversal
DBGHEFCA
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Traversal Summary
A Inorder Traversal
DBAGEHCF
B C
Preorder Traversal
ABDCEGHF
D E F
Postorder Traversal
DBGHEFCA
G H
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Problem- Find Inorder, Preorder, Postorder
Traversal
B C
D E F
G I J
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Problem- Find Inorder, Preorder, Postorder
Traversal
45
23 13
9
67
89 6 7
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Find the Binary Tree
Given:
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Find the Binary Tree
Given:
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
GATE Question- CS 2013
The postorder traversal of a binary tree is 8, 9, 6, 7, 4, 5, 2, 3, 1. The
inorder traversal of the same tree is 8, 6, 9, 4, 7, 2, 5, 1, 3. The height
of a tree is the length of the longest path from the root to any leaf. The
height of the binary tree above is ________ .
(A) 2
(B) 3
(C) 4
(D) 5
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Binary Search Trees
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Binary Search Trees(BST)- Basic Idea
All nodes of left subtree are less than the root node
All nodes of right subtree are more than the root node
Both subtrees of each node are also BSTs i.e. they have the above two
properties
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
BST-Example
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Is this BT a BST???
55
23 69
88
17 45
9 21 63 91
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Is this BT a BST???
55
23 69
88
17 45
9 21 63 91
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
12
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
12 97
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
12 97
22
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
12 97
22
35
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
12 97
9 22
35
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw a BST by inserting the elements in the order
75,12,97,22,35,9,103
75
12 97
9 22 103
35
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
GATE Question- CS 2013
The preorder traversal sequence of a binary search tree is 30, 20, 10,
15, 25, 23, 39, 35, 42. Which one of the following is the postorder
traversal sequence of the same tree?
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Question
39,56,23,19,45,59,78,82,80,91,99
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Properties of Binary Trees
Maximum Height
Hmax = N-1
Minimum Height
Hmin = Floor(log2 N )
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Properties of Binary Trees
Maximum Nodes
* Height of a BT is the length of the longest path from root to the leaf
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
GATE Question
Which of the following height is not possible for a binary tree with 50
nodes?
(A) 4
(B) 5
(C) 6
(D) None
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Balance Factor
If we define the height of the left subtree as H L and the height of the
B= HL-HR
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Balanced Binary Trees
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Balanced Binary Trees
B C
D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Is the BT Balanced?
B C
E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Representation of a
Binary Tree Node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Representing a Tree Node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Representing a Binary Tree
root
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Node Structure corresponding to the BT?
55
23 69
88
17 45
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Node Structure corresponding to the BT
root
55
23 69
17 45 88
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Searching for a Key
In Binary Search Tree
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Searching for a key
45
Search for the key 66
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Searching for a key
66 > 45, Traverse RST
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Searching for a key
45
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Searching for a key
45
23 56
66 < 69, Traverse
LST
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Searching for a key
45
23 56
69
17
9 21 66 78
Element
Found
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
In Binary Search Tree
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Three Cases:
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 1: Deleting a Leaf Node
45
Delete the Node 21
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 1: Deleting a Leaf Node Traverse to the Node
45 using Key Value
Delete the Node 21
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 1: Deleting a Leaf Node Set corresponding link
45 of Parent to NULL
Delete the Node 21
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
45
23 56
69
17
9 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 2: Delete the Node
45
17
23 56
69
17
9 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 2: Delete the Node Traverse to the Node
45 using Key Value
17
23 56
69
17
9 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 2: Delete the Node Link the child directly to
45 the parent of the Node
17
23 56
69
17
9 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 3: Delete the Node
45
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Find Inorder Predecessor
Case 3: Delete the Node
45 or Inorder Successor
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Inorder Predecessor is
Case 3: Delete the Node
45 the largest node in the
45
LST of the node to be
deleted
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Inorder Predecessor is
Case 3: Delete the Node
45 the largest node in the
45
LST of the node to be
deleted
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Replace node to be
Case 3: Delete the Node
deleted with the Inorder
45
23 Predecessor
17 56
69
9 21
66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Case 3: Delete the Node
45
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Inorder Successor is the
Case 3: Delete the Node smallest node in the RST
45 of the node to be deleted
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Inorder Successor is the
Case 3: Delete the Node smallest node in the RST
45 of the node to be deleted
45
23 56
69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Deleting a Node
Replace node to be
Case 3: Delete the Node
56 deleted with the Inorder
45
Successor
23 69
17
66 78
9 21
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Summary
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Question
Delete the Node
56 45
23 56
50 69
17
9 21 66 78
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Sequential Representation of a BT
records.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Sequential Representation
B C
F
D E
A B C D E - F - - - - - -
0 1 2 3 4 5 6 7 8 9 10 11 12
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Sequential Representation
For node B
A
Left Child at (2*1)+1
B C
F
D E
A B C D E - F - - - - - -
0 1 2 3 4 5 6 7 8 9 10 11 12
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Sequential Representation
For node B
A
Right Child at (2*1)+2
B C
F
D E
A B C D E - F - - - - - -
0 1 2 3 4 5 6 7 8 9 10 11 12
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Sequential Representation
For node F
A
Parent at (6-1)/2
B C
F
D E
A B C D E - F - - - - - -
0 1 2 3 4 5 6 7 8 9 10 11 12
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Draw the BT corresponding to the Sequential
Representation
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Problems
A binary tree has 10 nodes. The preorder and inorder traversals of the
tree are shown below. Draw the tree. Preorder: JCBADEFIGH
Inorder: ABCEDFJGIH
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Problems
A binary tree has eight nodes. The postorder and inorder traversals of
the tree are given below. Draw the tree. Postorder: FECHGDBA
Inorder: FCEABHDG
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Problems
A binary tree has seven nodes. The preorder and postorder traversals of
the tree are given below. Can you draw the tree? If not, explain.
Preorder: GFDABEC
Postorder: ABDCEFG
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Practice Problems
23,10,5,1,8,14,30,25,34,32,43
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Introduction to
Graph Data Structure
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graphs- Definition
A graph is a pair G = (V, E), where V is a set whose elements are called
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graph Data Structure- Basics
A B
C
D E
G = (V , E) F
V = { A,B,C,D,E,F}
E= {(A,B),(A,C),(B,E),(E,D),(D,F),(C,F)}
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graph Data Structure- Basics
A B
E
D C
F
EDGES or LINKS
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graph Data Structure- Basics
A B
E
D C
F
NODES or VERTICES
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Order & Size of a Graph
Order = | V | A B
C
D E
F
Size = | E |
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Real World Examples
Social Networks as a Graph
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Types of Graphs
Weighted Graph Complete Graph
Regular Graph
Directed Graph
Undirected Graph
Simple Graph Disconnected Graph
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Weighted Graph
9
A B
7 2 7
E
D C
4 5
3
8
F
A weighted graph is a graph in G
which each edge is given a
numerical weight.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Un-weighted Graph
A B
E
D C
F
G
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Undirected Graph
A B
E
D C
F
A graph in which all the edges G
are bi-directional
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Directed Graph
A B
E
D C
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Simple Graph
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Simple Graph - Example
A B
A B
D C
D C
C
C
Simple Graph
Not a Simple Graph
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Disconnected Graph
A B
H
E
D
C
F
G
--- Isolated Nodes
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Degree of a Vertex
A B
C
D E
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Degree of a Vertex
A B
In-degree
Out-degree
E
D C
F
G
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Regular Graph
A regular graph is a graph where each vertex has the same number of
neighbors; i.e. every vertex has the same degree
A B
D C
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Path
A path of length ‘n’ from node ‘u’ to node ‘v’ is defined as sequence of
n+1 nodes.
P(u,v)=(v0,v1,v2,v3…….vn)
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Complete Graph
Complete graphs are graphs that have an edge between every single
vertex in the graph.
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Complete Graph (K3)
A B
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Complete Graph (K4)
A B
C D
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
C
D
E
F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
E
C
F
G
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
C
D
E
F
--- Adjacent Vertices of B
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
C
D
E
F
--- Adjacent Vertices of D
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
C
D
E
F
--- Adjacent Vertices of F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
E
C
F
G
--- Adjacent Vertices of F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacent Vertices
A B
E
C
F
G
--- Adjacent Vertices of E
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graph Representation
Reference: https://medium.com/basecs/from-theory-to-practice-representing-graphs-cfd782c5be38#
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graph Representation
Adjacency Matrix
Edge List
Adjacency List
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacency Matrix
1
Definition from Wikipedia
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacency Matrix
0 1 2 3 4 5
0 0 1 1 0 0 0
0 1
1 1 0 0 1 1 0
2 2 1 0 0 0 1 0
3
3 0 1 0 0 0 1
4 4 0 1 1 0 0 1
5
5 0 0 0 1 1 0
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Edge List
An edge list is a list (or array) of all the edges in a graph. Edge lists are
one of the most simplest ways to represent a graph.
Reference: https://medium.com/basecs/from-theory-to-practice-representing-graphs-cfd782c5be38#
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Edge List- Example
[
[0,2],
0 1 Index
0 0 2 [0,1],
2 1 0 1 [1,3],
3
2 1 3 [1,4],
3 1 4 [2,4],
4
5
4 2 4 [3,5],
5 3 5 [4,5]
Can be implemented
using a struct array
6 4 5 ]
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacency Lists
Reference: https://medium.com/basecs/from-theory-to-practice-representing-graphs-cfd782c5be38#
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Adjacency Lists
0 1
0 1
1 2
2
1 1
1 2
2 1
1
2
3 2 0
0 4
4
3
1
1 5
5
4 4
5 1
1 2
2 5
5
5
3
3 4
4
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Graph Traversals
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Depth First Traversal
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
DFS-Example
A B
C
D Stack
E
F
Unvisited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Start with a random node, mark it visited , push it onto the stack,
output the node
A B
C Stack
D
E
F A
Unvisited node
Output A
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Select one of the un-explored nodes of A
A B
C Stack
D
E
F A
Unvisited node
Output A
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark C as visited , push it onto the stack, output the node
A B
C Stack
D
C
E
F A
Unvisited node
Output A C
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Select one of the un-explored nodes of C
A B
C Stack
D
C
E
F A
Unvisited node
Output A C
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark E as visited , push it onto the stack, output the node
A B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Select one of the un-explored nodes of E
A B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark B as visited , push it onto the stack, output the node
A B
B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node B, pop the stack
A B
B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node B, pop the stack
A B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Find an unexplored node from E
A B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark F as visited , push it onto the stack, output the node
A B
F
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B F
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Find an unexplored node from F
A B
F
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B F
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark D as visited , push it onto the stack, output the node
A B
F
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node D, pop the stack
A B
F
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node F, pop the stack
A B
F
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node E, pop the stack
A B
C Stack
D
E
C
E
F A
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node C, pop the stack
A B
C Stack
D
C
E
F A
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
No un-explored nodes available from node A, pop the stack
A B
C Stack
D
E
F A
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Stack is Empty ! Output the DFS Traversal
A B
C Stack
D
E
F
Unvisited node
Output A C E B F D
Visited node
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Point to Remember
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Types of Edges in DFS
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Types of Edges in DFS
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
E F
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
A
E F
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 0
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
B
C
E F
1 2
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
B
C
E F
1 2 3 0
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 4 2 3 0
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 4 2 3 0
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 4 2 3 0 5
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
F
C
E F
1 4 2 3 0 5 6
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 4 2 3 0 5 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
D
C
E F
1 4 2 3 0 8 5 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 4 2 3 0 8 9 5 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
C
E F
1 4 2 3 0 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding DFS Edges A B
C D Stack
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Finding DFS Edges
C D
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
C
Drawing the DFS Tree
A
E
B
F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Finding DFS Edges
C D
What about Edges
FC , FB , CB ?
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Finding DFS Edges
Consider the Edge
C D CB
B appears after C
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Finding DFS Edges
Consider the Edge
C D CB
B appears after C
E F
There is a path from
C to B
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
C D
E F CB is a forward edge
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
C D
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
C D
C appears before F
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
C D
C appears before F
E F
There is a path from
F to C
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
Back Edges
C D
FC is a Back Edge
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
Back Edges
C D
E F
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
A B
Tree Edges
Forward Edges
Finding DFS Edges
Back Edges
C D
Cross Edges
No path from F to B
E
in DFS Tree
F
FB is a Cross Edge
1 4 2 3 0 11 8 9 5 10 6 7
A B C D E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Marked Edges for the DFS Tree
A B
Tree Edges
Forward Edges
Back Edges
C D
Cross Edges
E F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Breadth First Traversal
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
BFS Traversal
A B G
C
F
7 6 5 4 3 2 1 0
D
E
Queue
Unvisited node
Visited node
Output
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Start BFS with a random node
A B G
C
F
7 6 5 4 3 2 1 0
D
E
Queue
Unvisited node
Visited node
Output
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Starting with C, Mark C as Visited, Display C, Add to Queue
A B G
C
F
7 6 5 4 3 2 1 0
C
D
E
Queue
Unvisited node
Visited node
Output C
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Add all adjacent, non-visited vertices of C to the Queue
(which are not already in the Queue).Remove C from the Queue
A B G
C
F
7 6 5 4 3 2 1 0
D A C
D
E
Queue
Unvisited node
Visited node
Output C
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark A as visited,Display A. Add all adjacent, non-visited vertices (which are not
already in the Queue) of A to the Queue & Remove A
A B G
C
F
7 6 5 4 3 2 1 0
E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark D as visited,Display D.Add all adjacent, non-visited vertices (which are not
already in the Queue) of D to the Queue & Remove D
A B G
C
F
7 6 5 4 3 2 1 0
E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A D
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark B as visited,Display B.Add all adjacent, non-visited vertices (which are not
already in the Queue) of B to the Queue & Remove B
A B G
C
F
7 6 5 4 3 2 1 0
G E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A D B
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark E as visited,Display E.Add all adjacent, non-visited vertices (which are not
already in the Queue) of E to the Queue & Remove E
A B G
C
F
7 6 5 4 3 2 1 0
F G E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A D B E
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark G as visited,Display G.Add all adjacent, non-visited vertices (which are not
already in the Queue) of G to the Queue & Remove G
A B G
C
F
7 6 5 4 3 2 1 0
F G E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A D B E G
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark F as visited,Display F.Add all adjacent, non-visited vertices (which are not
already in the Queue) of F to the Queue & Remove F
A B G
C
F
7 6 5 4 3 2 1 0
F G E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A D B E G F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Mark F as visited,Display F.Add all adjacent, non-visited vertices (which are not
already in the Queue) of F to the Queue & Remove F
A B G QUEUE IS EMPTY
F G E B D A C
D
E
Queue
Unvisited node
Visited node
Output C A D B E G F
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
G E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
G E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
G E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
C G E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
C G E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A B
C D E
F G
7 6 5 4 3 2 1 0
C G E D F B A
Unvisited node
Visited node
Queue
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Finding BFS Tree
A
F B
G D E
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Application of Graphs
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Application of Graphs
Social Graphs
Web Document Graphs
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example for a Knowledge Graph
Source : https://medium.com/@brkyataman/knowledge-graph-and-youtube-29d259fd3dc1
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example for a Social Graph
Source : https://towardsdatascience.com/how-to-visualize-social-network-with-graph-theory-4b2dc0c8a99f
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example for a Contact Tracing Graph
Source : https://dzone.com/articles/contact-tracing-apps-for-proximity-alerts
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example for a Neural Network Graph
Source : https://dzone.com/articles/contact-tracing-apps-for-proximity-alerts
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example for a Web Document Graph
Source : https://www.groundai.com/project/a-graph-structured-dataset-for-wikipedia-research/1
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering
Example for a Road Network Graph
Source : https://medium.com/@ghalstein97/exploring-graphing-algorithms-in-computer-science-b67c653a5e21
Downloaded
Jacob P Cherian, Assistant fromofKtunotes.in
Professor, Department CSE, Saintgits College of Engineering