Professional Documents
Culture Documents
DSF Combined notes
DSF Combined notes
DSF Combined notes
Data Structure
2
Content (for example)
Part –I
➢ Difference between linear and non-liner data structure. Trees and Binary trees-
basic terminology
➢ representation using linked organization, Binary tree- properties
➢ converting tree to binary tree
Part-II
➢, binary tree traversals recursive and non-recursive: depth first and breadth first.
➢ Binary Search Tree (BST), BST operations: Create , Display
➢BST insertion and Deletion
3
Data Structure Classification
4
Linear and Non Linear Data Structure Difference
5
Linear and Non Linear Data Structure Difference
7
Definition of Tree
• The remaining nodes are partitioned into n>=0 disjoint sets T1, ...,
Tn, where each of these sets is a tree.
• Branch directed away from the node is the OUTDEGREE of the node
• The total number of edges from leaf node to a particular node in the
longest path is called as HEIGHT of that Node.
• A node that has sub trees is the parent of the sub trees.
• The sub trees of the node are the children of the node.
• The ancestors of a node are all the nodes along the path from the
root to the node.
Terminology
Applicable for
Full Binary tree
Perfect Binary tree
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Array representation.
Linked representation.
Array Representation
Number the nodes using the numbering scheme for
a full binary tree. The node that is numbered i is
stored in tree[i].
a1
2 3
b c
4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree
a1
b 3
7
c
15
d
tree[] a - b - - - c - - - - - - - d
0 5 10 15
Class BinaryTreeNode
{
<type of data> Object element;
BinaryTreeNode *leftChild; // left subtree
BinaryTreeNode *rightChild;// right subtree
// constructors and any other methods
// come here
};
Linked Representation Example
root a
b c
d e
f g
Left Child
element h
rightChild
Tree
Sequential Representation
Linked Representation
31
Binary Trees
• Two special kinds of binary trees:
(a) skewed tree, (b) complete binary tree
• The all leaf nodes of these trees are on two adjacent levels
Binary Trees
• Properties of binary trees
• Lemma 5.1 [Maximum number of nodes]:
1. The maximum number of nodes on level i of a binary tree is 2i-1,
i 1.
2. The maximum number of nodes in a binary tree of depth k is
2k-1, k1.
• Lemma 5.2 [Relation between number of leaf nodes and
degree-2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf
nodes and n2 is the number of nodes of degree 2, then n0 = n2
+ 1.
• These lemmas allow us to define full and complete
binary trees
Binary Trees
• Definition:
• A full binary tree of depth k is a binary tree of death k having 2k-1 nodes, k
0.
• A binary tree with n nodes and depth k is complete iff its nodes correspond
to the nodes numbered from 1 to n in the full binary tree of depth k.
• From Lemma 5.1, the
height of a complete
binary tree with n nodes
is log2(n+1)
• Full Binary Tree
• A full binary tree is a binary tree where
every node has exactly 0 or 2 children.
Algorithm to convert General Tree to Binary
B C D
E F G H I J K
Example Tree Conversion General to
Binary
A
C
E
D
G
F I
H
J
K
Converting tree to binary tree
The process of converting the general tree to a binary tree is as follows:
* use the root of the general tree as the root of the binary tree
* determine the first child of the root. This is the leftmost node in the general tree at the next level
* insert this node. The child reference of the parent node refers to this node
* continue finding the first child of each parent node and insert it below the parent node with the child
reference of the parent to this node.
* when no more first children exist in the path just used, move back to the parent of the last node entered and
repeat the above process. In other words, determine the first sibling of the last node entered.
* complete the tree for all nodes. In order to locate where the node fits you must search for the first child at
that level and then follow the sibling references to a nil where the next sibling can be inserted. The
children of any sibling node can be inserted by locating the parent and then inserting the first child.
Then the above process is repeated.
39
Binary
Tree
40
Binary
Tree
41
Binary Tree Traversal
PREorder:
visit the root
Tree Traversal
POSTorder
Traverse left subtree
Traverse right subtree
visit the root
Inorder
Traverse left subtree
visit the root
Traverse right subtree
Pre-Order Traversal
A
B E
C D F
A B C D E F
def preorder(treenode):
if treenode:is Null
Print(tree.getRoot(value))
preorder(tree.getLeftChild())
preorder(tree.getRightChild())
A
B E
C D F
C B D A E F
Post-Order Traversal
A
B E
C D F
def postorder(tree):
if tree:
CDB F E A postorder(tree.getLeftChild())
postorder(tree.getRightChild())
print(tree.getRootVal())
Breadth first Traversal
A
B C
D E F G
A B C D E F G
Recursive Traversal
def preorder(treenode):
if treenode:is Null
print(tree.getRootVal())
preorder(tree.getLeftChild())
preorder(tree.getRightChild())
def postorder(treenode):
if treenode:is Null return
postorder(tree.getLeftChild())
postorder(tree.getRightChild())
print(tree.getRootVal()) def inorder(treenode):
if treenode: is Null return
inorder(tree.getLeftChild())
print(tree.getRootVal())
inorder(tree.getRightChild())
50
Binary Search Trees
• Ordered data stored in Array will have efficient
search algorithms but inefficient insertion and
deletion
17
6 19
Is this Binary Search Tree
17
6 19
3 14
Is this Binary Search Tree
17
19
22
Is this Binary Search Tree
17
22 19
Is this Binary Search Tree
17
6 11
Is this Binary Search Tree
17
6 19
11 15
Is this Binary Search Tree
17
6 19
3 22
Insertion in BST
17
6 19
4 15
Add node - 2
Insertion in BST
17
6 19
4 15
2
Insertion in BST
17
6 19
4 15
Add node - 22
Insertion in BST
17
6 19
4 15
22
Insertion
parent 7
cursor 5 9
4 6 8 10
7
Removing 4replace the
link in the parent with 5 9
null
6 8 10
Removal in BST: Example
Case 2: removing a node with 2 SUBTREES
- replace the node's value with the max value in the left subtree
- delete the max node in the left subtree What other element
Removing 7 can be used as
cursor
cursor replacement?
7 6
5 9 5 9
4 6 8 10 4 8 10
Removal in BST: Example
1. https://www.geeksforgeeks.org/
2. https://www.educative.io/
3. https://www.javatpoint.com/
74
Thank You
75
Presentation Topic Advanced Trees
Advance Data Structures
2
Threaded Binary Tree
• In a linked representation of a binary tree, the number
of null links (null pointers) are actually more than non-
null pointers.
• Consider the following binary tree:
Threaded Binary Tree
• In above binary tree, there are 7 null pointers & actual 5
pointers.
• In all there are 12 pointers.
• We can generalize it that for any binary tree with n nodes
there will be (n+1) null pointers and 2n total pointers.
• The objective here to make effective use of these null
pointers.
• A. J. perils & C. Thornton jointly proposed idea to make
effective use of these null pointers.
• According to this idea we are going to replace all the null
pointers by the appropriate pointer values called threads.
Threaded Binary Tree
• And binary tree with such pointers are called threaded
tree.
• In the memory representation of a threaded binary tree,
it is necessary to distinguish between a normal pointer
and a thread.
Threaded Binary Tree
▪ Therefore we have an alternate node representation for
a threaded binary tree which contains five fields as
show below:
Threaded Binary Tree
• Also one may choose a one-way threading or a
two-way threading.
• Here, our threading will correspond to the in
order traversal of T.
Threaded Binary Tree
One-Way
• Accordingly, in the one way threading of T, a
thread will appear in the right field of a node
and will point to the next node in the in-order
traversal of T.
• See the bellow example of one-way in-order
threading.
Threaded Binary Tree:
One-Way
Dangling Pointers
Double Threaded binary tree with dummy node
Threaded Binary Tree
Two-way Threading with Header node
• Again two-way threading has left pointer of the first
node and right pointer of the last node (in the inorder
traversal of T) will contain the null value when T will
point to the header nodes is called two-way threading
with header node threaded binary tree.
Threaded Binary Tree
• Below figure to explain two-way threading with header node.
Threaded Binary Tree
• Below example of link representation of threading
binary tree.
• In-order traversal of below tree: G,F,B,A,D,C,E
Threaded Binary Tree
Threaded Tree Code
• Example code:
class Node {
Node left, right;
boolean leftThread, rightThread;
}
3 8
1 5 7 11
9 13
Start at leftmost node, print it
3 8
1 5 7 11
9 13
Follow thread to right, print node
1 5 7 11
9 13
Follow thread to right, print node
9 13
Follow thread to right, print node
9 13
Follow link to right, go to
leftmost node and print
Amir Kamil 8/8/02 29
Threaded Tree Traversal
Output
6 1
3
5
3 8 6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
3/3/2022 37
Binary Search Tree
disadvantages
3/3/2022 38
AVL Trees
•We have seen that all operations depend on the depth of the tree.
•We don’t want trees with nodes which have large height This can be
attained if both sub trees of each node have roughly the same height.
•AVL tree is a binary search tree where the height of the two sub trees of a
node differs by at most one Height of a null tree is -1
•For the tree to be balanced the Balance factor of tree should be -1,0,1
•The Balance Factor is calculated as HL - HR
•An AVL tree is another balanced binary search tree. Named after their
inventors, Adelson-Velskii and Landis, they were the first dynamically
balanced trees whose height differ by at most 1
3/3/2022 39
AVL Trees
(Adelson – Velskii – Landis)
AVL tree:
2
1 0 2
6 5
7 1 3
8 0
5 1 1
3/3/2022 7 9 40
AVL Trees
Not AVL tree:
20
16 25
7 18 30
5 17 19 27
3/3/2022 41
AVL Tree Rotations
Single rotations: insert 14, 15, 16, 13, 12, 11, 10
• First insert 14 and 15:
14
15
• Now insert 16.
3/3/2022 42
AVL Tree Rotations
Single rotations:
• Inserting 16 causes AVL violation:
14
15
16
• Need to rotate.
3/3/2022 43
AVL Tree Rotations
3/3/2022
Rotate to left
14
•rr_rotation(avl_node *parent)
{
•avl_node *temp;
15
•Temp = parent->right;
•parent->right = temp->left; 16
•temp->left = parent;
•return temp; • Need to rotate.
}
44
AVL Tree Rotations
Single rotations:
• Rotation type:
14
15
16
3/3/2022 45
AVL Tree Rotations
Single rotations:
• Rotation restores AVL balance:
15
14 16
3/3/2022 46
AVL Tree Rotations
15
13 16
12 14
3/3/2022 49
AVL Tree Rotations
Double rotations: Right of Left
Insert element to Right (part) of Left (subtree)
Rotate to left first and then rotate to right
15 •lr_rotation(avl_node *parent)
{
13 •avl_node *temp;
•temp = parent->left;
•parent->left = rr_rotation
14 (temp);
•return ll_rotation (parent);
}
3/3/2022 50
AVL Tree Rotations
15
14
14
13
13 15
3/3/2022 51
AVL Tree Rotations
Double rotations: Left of Right
Insert element to Left (part) of Right (subtree)
Rotate to Right first and then rotate to left
15
•rl_rotation(avl_node *parent)
{
18
•avl_node *temp;
•temp = parent->right;
•parent->right = ll_rotation (temp); 17
•Return rr_rotation (parent);
}
3/3/2022 52
AVL Tree Rotations
15
18 18
17 15 17
3/3/2022 53
AVL Tree Rotations
Single rotations:
15
1 1
1 3 6
1
2 4
• Now insert 11.
3/3/2022 54
AVL Tree Rotations
Single rotations:
1
5
1 1
1 3 6
1
1 2 4
• 1
AVL violation – need to rotate
3/3/2022 55
AVL Tree Rotations
Single rotations:
• Rotation type: 1
5
1 1
1 3 6
1
1 2 4
1
3/3/2022 56
AVL Tree Rotations
Single rotations:
1
3
1 1
2 5 1
1 1
1 4 6
• Now insert 10.
3/3/2022 57
AVL Tree Rotations
Single rotations:
1
3
1 1
2 5 1
1 1
1 4 6
1
• AVL violation – need to rotate
3/3/2022 0 58
AVL Tree Rotations
Single rotations:
• Rotation type:
1
3
1 1
2 5 1
1 1
1 4 6
1
3/3/2022 0 59
AVL Tree Rotations
Single rotations:
1
3
1 1
1 5
1 1 1 1
0 2 4 6
• AVL balance restored.
3/3/2022 60
AVL Tree Rotations
Double rotations: insert 1, 2, 3, 4, 5, 7, 6, 9, 8
• First insert 1 and 2:
1
3
1 1
1 5
1 1 1 1
0 2 4 6
3/3/2022 61
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
1
3
1 1
1 5 1
1 1 1
0 2 4 6
1
3/3/2022
2 62
AVL Tree Rotations
Double rotations:
• Rotation type:
1
3
1 1
1 5 1
1 1 1
0 2 4 6
1
3/3/2022
2 63
AVL Tree Rotations
Double rotations:
• AVL balance restored:
1
3
1 1
1 5
2 1 1 1
2 4 6
1 1
• Now insert 3. 0
3/3/2022 64
AVL Tree Rotations
Double rotations:
• AVL violation – rotate:
1
3
1 1
1 5
2 1 1 1
2 4 6
1 1
3/3/2022 3 0 65
AVL Tree Rotations
Double rotations:
• Rotation type:
1
3
1 1
1 5
2 1 1 1
2 4 6
1 1
3/3/2022 3 0 66
AVL Tree Rotations
Double rotations:
• AVL balance restored:
1
3
1 1
0 5
2 1 1 1
1 14 6
1 3
• Now insert 4. 2
3/3/2022 67
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
1
3
1 1
0 5
2 1 1 1
1 1 14 6
3
3/3/2022 4 2 68
AVL Tree Rotations
Double rotations:
• Rotation type:
1
3
1 1
0 5
2 1 1 1
1 1 14 6
3
3/3/2022 4 2
69
AVL Tree Rotations
Double rotations:
1
2 1
0
3
1 3 1 1
1 1 5
1 1
4
2 4 6
• Now insert 5.
3/3/2022 70
AVL Tree Rotations
Double rotations:
1
2 1
0
3
1 3 1 1
1 1 5
1 1
4
2 4 6
•
5
AVL violation – rotate.
3/3/2022 71
AVL Tree Rotations
Single rotations:
• Rotation type:
1
2 1
0
3
1 3 1 1
1 1 5
1 1
4
2 4 6
5
3/3/2022 72
AVL Tree Rotations
Single rotations:
• AVL balance restored:
1
2 1
0
3
1 4 1 1
1 1 5
1 1
3 5
2 4 6
• Now insert 7.
3/3/2022 73
AVL Tree Rotations
Single rotations:
• AVL violation – rotate.
1
2 1
0
3
1 4 1 1
1 1 5
1 1
3 5
7 2 4 6
3/3/2022 74
AVL Tree Rotations
Single rotations:
• Rotation type:
1
2 1
0
3
1 4 1 1
1 1 5
1 1
3 5
7 2 4 6
3/3/2022 75
AVL Tree Rotations
Double rotations:
• AVL balance restored.
1
4 1
0
3
2 5 1 1
1 1 5
1 1
1 3 7
2 4 6
• Now insert 6.
3/3/2022 76
AVL Tree Rotations
Double rotations:
• AVL violation - rotate.
1
4 1
0
3
2 5 1 1
1 1 5
1 1
1 3 7
2 4 6
6
3/3/2022 77
AVL Tree Rotations
Double rotations:
• Rotation type:
1
4 1
0
3
2 5 1 1
1 1 5
1 1
1 3 7
2 4 6
6
3/3/2022 78
AVL Tree Rotations
Double rotations:
• AVL balance restored.
1
4 1
0
3
2 6 1 1
1 1 5
1 1
1 3 5 7
2 4 6
• Now insert 9 and 8.
3/3/2022 79
AVL Tree Rotations
Double rotations:
• AVL violation - rotate.
1
4 1
0
3
2 6 1 1
1 1 5
1 1
1 3 5 7
2 4 6
9
3/3/2022
8 80
AVL Tree Rotations
Double rotations:
• Rotation type:
1
4 1
0
3
2 6 1 1
1 1 5
1 1
1 3 5 7
2 4 6
9
3/3/2022
8 81
AVL Tree Rotations
Final tree:
• Tree is almost perfectly balanced
1
4 1
0
3
2 6 1 1
1 1 5
1 1
1 3 5 8
2 4 6
7 9
3/3/2022 82
GRAPHS
Data Structures
1
SYLLABUS
Basic Concepts, Storage representation, Adjacency
matrix, adjacency list, adjacency multi list, inverse
adjacency list. Traversals-depth first and breadth first,
Introduction to Greedy Strategy, Minimum spanning
Tree, Greedy algorithms for computing minimum
spanning tree- Prims and Kruskal Algorithms,
Dikjtra's Single source shortest path, Topological
ordering.
Case study- Data structure used in Webgraph and
Google map.
2
WHAT IS A GRAPH?
edge
Example
B
Vertices E
A D
Edges
C
Undirected Graph with 5 vertices and 7 edges
Maxm number of edges in undirected graph n(n-1)/2
A D
6
DIRECTED VS. UNDIRECTED GRAPHS
7
DIRECTED VS. UNDIRECTED GRAPHS
(CONT.)
When the edges in a graph have a direction, the
graph is called directed (or digraph)
if the graph is
directed, the
order of the
vertices in each
edge is important
9
The degree of a vertex is the number of
edges incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of edges
that have v as the head
e = ( di ) / 2
0
10
GRAPH TERMINOLOGY
11
MORE TERMINOLOGY
simple path: no repeated vertices
a b
bec
c
d e
cycle: simple path, except that the last vertex is the same
as the first vertex
a b
acd
a ac d a
c
12
d e
EVEN MORE TERMINOLOGY
•connected graph: any two vertices are connected by some path
13
GRAPH TERMINOLOGY (CONT.)
What is the number of edges in a
complete directed graph with N
vertices?
N * (N-1)
14
GRAPH TERMINOLOGY (CONT.)
What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
15
GRAPH TERMINOLOGY (CONT.)
Weighted graph: a graph in which each edge
carries a value
16
ABSTRACT DATA TYPE
objects: a nonempty set of vertices and a set of
undirected edges, where each edge is a pair of vertices
18
GRAPH TRAVERSAL
Use the same depth-first and breadth-first traversal
algorithms seen for the binary trees.
Differences between graph and tree traversals:
1) Tree traversal always visit all the nodes in the tree
2 ) Graph traversal visits all the nodes in the graph only
when it is connected. Otherwise it visits only a subset of
the nodes. This subset is call the connected component of
the graph.
Recursive and iterative implementations of the algorithms.
Iterative: Use a stack for the depth-first search (dfs)
Use a queue for the breadth-first search (bfs) 19
GRAPH IMPLEMENTATION
Array-based implementation
A 1D array is used to represent the vertices
A 2D array (adjacency matrix) is used to represent the
edges
20
ARRAY-BASED IMPLEMENTATION
21
GRAPH IMPLEMENTATION (CONT.)
Linked-list implementation
A 1D array is used to represent the vertices
A list is used for each vertex v which
contains the vertices which are adjacent from
v (adjacency list)
22
LINKED-LIST IMPLEMENTATION
23
ADJACENCY MATRIX VS.
ADJACENCY LIST REPRESENTATION
Adjacency matrix
Good for dense graphs
Memory requirements( Consider all edges)
Connectivity between two vertices can be tested quickly
Adjacency list
Good for sparse graphs
Memory requirements(Consider only connected edges)
Vertices adjacent to another vertex can be found quickly
24
GRAPH SEARCHING
25
DEPTH-FIRST-SEARCH (DFS)
What is the idea behind DFS?
Travel as far as you can down a path
Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
DFS can be implemented efficiently using a
stack
26
DEPTH-FIRST-SEARCH (DFS) (CONT.)
NEED CORRECTIONS
Set found to false
stack.Push(startVertex)
DO
stack.Pop(vertex)
IF vertex == endVertex
Set found to true
ELSE
Push all adjacent vertices onto stack
WHILE !stack.IsEmpty() AND !found
IF(!found)
Write "Path does not exist"
27
WALK-THROUGH
Visited Array
F C A
A B
B C
D
H D
E
G E F
G
H
D, C
No nodes adjacent to C; cannot
continue ➔ backtrack, i.e., pop stack 32
53
Can be used to attempt to visit all nodes of a
graph in a systematic manner
Works with directed and undirected graphs
Steps
Breadth-first search starts with given node
Then visits nodes adjacent in some specified order
(e.g., alphabetical)
54
BREADTH-FIRST-SEARCHING (BFS)
(CONT.)
BFScan be implemented efficiently using a
queue IF(!found)
Write "Path does not exist"
Set found to false
queue.Enqueue(startVertex)
DO
queue.Dequeue(vertex)
IF vertex == endVertex
Set found to true
ELSE
Enqueue all adjacent vertices onto queue
WHILE !queue.IsEmpty() AND !found
55
Walk-Through Enqueued Array
F C A
A B Q→
B C
D
H D
E
G E F
G
H
F C A
A B Q→D
B C
D
H D √
E
G E F
G
Nodes visited: H
F C A
A B Q→C→E→F
B C √
D
H D √
E √
G E F √
G
Nodes visited: D H
F C A
A B Q→E→F
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C H
F C A
A B Q→F→G
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C, E H
F C A
A B Q→G
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F H
F C A
A B Q→H
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G H √
F C A √
A B √ Q→A→ B
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H H √
F C A √
A B √ Q→B
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, A H √
F C A √
A B √ Q empty
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, H √
A, B
Dequeue B. Visit B. Enqueue unenqueued nodes
65
adjacent to B.
Walk-Through Enqueued Array
F C A √
A B √ Q empty
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, H √
A, B
Q empty. Algorithm done.
66
DEFINITION
A Minimum Spanning Tree (MST) is a subgraph
of an undirected graph such that the subgraph
spans (includes) all nodes, is connected, is
acyclic, and has minimum total edge weight
67
MINIMUM SPANNING TREES
Prim’s Algorithm
• Focuses on nodes
Kruskal’s Algorithm
• Focuses on edges, rather than nodes
68
Minimum Spanning Tree is concerned with connected
undirected graphs.
69
Example of Minimum Spanning Tree
•a •b •d
•c •e •f
•a •b •d •a •b •d •a •b •d
•c •e •f •c •e •f •c •e •f
• The following figure shows a graph G1 together with its three possible
70
minimum spanning trees.
WHAT IS A MINIMUM-COST SPANNING
TREE
• Minimum-Cost spanning tree is concerned with edge-weighted
connected undirected graphs.
• For an edge-weighted , connected, undirected graph, G, the total
cost of G is the sum of the weights on all its edges.
• A minimum-cost spanning tree for G is a minimum spanning tree
of G that has the least total cost.
71
CONSTRUCTING MINIMUM SPANNING TREE
73
PRIM’S ALGORITHM
74
PRIM’S ALGORITHM
Prim’s algorithm finds a minimum cost spanning tree by selecting
edges from the graph one-by-one as follows:
It starts with a tree, T, consisting of the starting vertex, x.
Then, it adds the shortest edge emanating from x that connects T
to the rest of the graph.
It then moves to the added vertex and repeat the process.
77
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C 3 D
10
H 25 D T 0 −
2
3 E 25 D
G 7
E F 18 D
G 2 D
H
78
2 Select node with
minimum distance
3
10
F C
A 7 3 K dv pv
8 4
18 A
4
9
B D B
10
H 25
C 3 D
2
3 D T 0 −
G 7
E E 25 D
F 18 D
G T 2 D
H
79
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C 3 D
10
H 25 D T 0 −
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
80
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A
8 B
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E 7 G
G 7
E F 18 D
G T 2 D
H 3 G
81
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E 7 G
G 7
E F 3 C
G T 2 D
H 3 G
82
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E 7 G
G 7
E F T 3 C
G T 2 D
H 3 G
83
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
84
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
85
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H 3 G
Table entries unchanged
86
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A 10 F
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
87
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A 4 H
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
88
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A T 4 H
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
89
2 Update distances of
adjacent, unselected nodes
3
10
F C K dv pv
A 7
4
3 A T 4 H
8 B 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
Table entries unchanged
90
2 Select node with
minimum distance
3
10
F C K dv pv
A 7
4
3 A T 4 H
8 B T 4 C
18
4
9
B D C T 3 D
10
H 25 D T 0 −
2
3 E T 2 F
G 7
E F T 3 C
G T 2 D
H T 3 G
91
2 Cost of Minimum
Spanning Tree = dv = 21
3
F C K dv pv
A 4
3 A T 4 H
B T 4 C
4
B D C T 3 D
H D T 0 −
2
3 E T 2 F
G E F T 3 C
G T 2 D
H T 3 G
Done
92
Kruskal’s Algorithm
93
KRUSKAL'S ALGORITHM.
Kruskal’ÿ algorithm also finds the minimum cost spanning tree of
a graph by adding edges one-by-one.
94
Walk-Through
Consider an undirected, weight graph
3
10
F C
A 4
4
3
8
6
5
4
B D
4
H 1
2
3
G 3
E
95
Sort the edges by increasing edge weight
3
10
F C edge dv edge dv
A 4
4
3 (D,E) 1 (B,E) 4
8
6
5 (D,G) 2 (B,F) 4
4
B D
4
H 1
(E,G) 3 (B,H) 4
2
3 (C,D) 3 (A,H) 5
G 3
E
(G,H) 3 (D,F) 6
(C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
96
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
8 4
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
97
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
98
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
99
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
100
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
101
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
102
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
103
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
104
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3
8 4 (D,E) 1 (B,E) 4
6
5
4
B D (D,G) 2 (B,F) 4
4
H 1 (E,G) 3 (B,H) 4
2
3 (C,D) 3 (A,H) 5
G 3
E
(G,H) 3 (D,F) 6
(C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
105
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
106
Select first |V|–1 edges which do not
generate a cycle
3
10
F C edge dv edge dv
A 4 3 (D,E) 1 (B,E) 4
4
8
6 (D,G) 2 (B,F) 4
5
4
B D (E,G) 3 (B,H) 4
4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G 3
E (C,F) 3 (A,B) 8
(B,C) 4 (A,F) 10
107
Select first |V|–1 edges which do not
generate a cycle
edge dv edge dv
3
F C (D,E) 1 (B,E) 4
A 4
3
(D,G) 2 (B,F) 4
5
B D (E,G) 3 (B,H) 4
H 1
(C,D) 3 (A,H) 5
2
3 (G,H) 3 (D,F) 6
G E
(C,F)
(B,C)
3
4
(A,B)
(A,F)
8
10
}
not
Done considered
110
SINGLE-SOURCE SHORTEST-PATH
PROBLEM
111
SINGLE-SOURCE SHORTEST-PATH
PROBLEM (CONT.)
Common algorithms: Dijkstra's algorithm,
Bellman-Ford algorithm
BFS can be used to solve the shortest graph
problem when the graph is weightless or all the
weights are the same
(mark vertices before Enqueue)
112
Dijkstra(L[1…n,1…n]):array[2….n]
Array D[2….n]
For i= 2 to n do D[i]=L[1,i]
For w belongs to C do
D[w]=min(D[w],D[v]+L[v,w])
Return D 113
void dij(int n,int s,int cost[10][10],int dist[])
{ int i,u,count,v,flag[10],min;
for(i=1;i<=n;i++)
{ flag[i]=0; dist[i]=cost[s][i];
} count=2;
while(count<=n)
{ min=99;
for(v=1;v<=n;v++)
if(dist[v]<min && !flag[v])
{ min=dist[v]; u=v; }
flag[u]=1;
count++;
for(v=1;v<=n;v++)
if((dist[u]+cost[u][v]<dist[v]) && !flag[v])
dist[v]=dist[u]+cost[u][v];
} 114
}
SYMBOL TABLE & HASH TABLE
1
Compiler is composed of Analysis and synthesis
Compiler
Analysis synthesis
2
3
ANALYSIS
Semantic Analyzer
4
Lexical analysis is the first phase of a compiler. It takes the
modified source code from language preprocessors that are
written in the form of sentences.
The lexical analyzer breaks these syntaxes into a series of
tokens, by removing any whitespace or comments in the source
code.
The input string of characters of the source program are divided
into sequence of characters that have a logical meaning, and are
known as tokens
In programming language, keywords, constants, identifiers,
strings, numbers, operators and punctuations symbols can be
considered as tokens.
int value = 100; contains the tokens:
6
Lexemes are said to be a sequence of characters (alphanumeric)
in a token.
7
The lexical analyzer also follows rule priority where a
reserved word, e.g., a keyword, of a language is given priority
over user input. That is, if the lexical analyzer finds a lexeme
that matches with any existing reserved word, it should
generate an error.
it involves grouping the tokens of the source program into
grammatical phrases that are used by the compiler to
synthesize output
The semantic analysis phase checks the source program for semantic errors
and gathers type information for the subsequent de-generation phase.
It uses the hierarchical structure determined by the syntax-analysis phase to
identify the operators and operands of expressions and statements.
An important component of semantic analysis is type checking.
8
COMPILER
9
Symbol table is a data structure used by a language translator
such as a compiler or interpreter, where each identifier in a
program's source code is associated with information relating to
its declaration or appearance in the source, such as
its type, scope level and sometimes its location.
10
A common implementation technique is to use
a hash table
11
Symbol table is an important data structure
created and maintained by compilers in order to
store information about the occurrence of various
entities such as variable names, function names,
objects, classes, interfaces, etc.
12
Consider the following program written in C
// Declare an external function
13
Symbol name Type Scope
function
x double
parameter
function
count int
parameter
sum double block local
for-loop
i int
statement
15
OPERATIONS ON SYMBOL TABLE
A symbol table, either linear or hash, should provide the
following operations.
insert()
This operation is more frequently used by analysis phase, i.e.,
the first half of the compiler where tokens are identified and
names are stored in the table. This operation is used to add
information in the symbol table about unique names occurring
in the source code
16
lookup()
lookup() operation is used to search a name in the
symbol table to determine:
if the symbol exists in the table.
17
HASH TABLE
a hash table, or a hash map, is a data structure
that associates keys with values (attributes).
19
We can insert element in hash table using direct
addressing mode
21
m slots
00000
….
B-1
B Buckets
22
WHAT IS A HASH TABLE ?
The simplest kind of hash table is an array of records.
This example has 701 records.
... 23
An array of records
WHAT IS A HASH TABLE ?
The simplest kind of hash table is an array of records.
This example has 701 records.
... 24
An array of records
WHAT IS A HASH TABLE ?
Each record has a special field,
called its key. [4]
In this example, the key is a
Number 506643548
long integer field called
Number.
the person.
...
27
INSERTING A NEW RECORD
In order to insert a new
record, the key must Number 580625685
somehow be converted to an
array index.
The index is called the hash
value of the key.
...
28
INSERTING A NEW RECORD
Typical way create a hash
value: Number 580625685
...
29
INSERTING A NEW RECORD
Typical way to create a hash
value: Number 580625685
...
30
INSERTING A NEW RECORD
The hash value is used for the [3]
location of the new record. Number 580625685
[3]
...
32
COLLISIONS
Here is another new record
to insert, with a hash value Number 701466868
of 2.
My hash
value is
[2].
...
33
COLLISION RESOLUTION POLICIES
Two classes
Open hashing
After collision records are stored outside the table
Linear Probing
Quadratic Probing
Double Hashing
Closed hashing
After collisions records are stored somewhere in other
34
LINEAR PROBING
Linear probing (linear hashing, sequential probing):
f(i) = i
Insert: When there is a collision we just probe the next slot
in the table. If it is unoccupied – we store the key there. If it
is occupied – we continue probing the next slot
One main disadvantage of linear probing is it forms a
cluster such clustering increases time to search for a record
35
Consider an example where we want to insert elements.
1 131
2 22
3 31
4 11
5 121
6 61
7 7
8 8
36
9
10
This is called a collision,
because there is already Number 701466868
another valid record at [2].
When a collision
occurs,
move forward until you
find an empty spot.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322
...
37
This is called a collision,
Number 701466868
because there is already
another valid record at [2].
When a collision
occurs,
move forward until
you
find an empty spot.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322
...
38
This is called a collision, Number 701466868
because there is already
another valid record at [2].
When a collision
occurs,
move forward until
you
find an empty spot.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322
...
39
This is called a collision, because there is already
another valid record at [2].
...
40
QUADRATIC PROBING
Quadratic Probing
Quadratic probing: f(i) = i2
A quadratic function is used to compute the next index in
the table to be probed.
Example:
In linear probing we check the i-th position. If it is occupied,
we check the i+1st position, next we check the i+2nd, etc.
Index Value
• H(K)=K (mod)m
• 4=54%10 0
• 2=72%10 1
• Using prime Number
2 72
• Choose prime number close to 99
• H(3205)=4 4 54
43
INSERTING A NEW RECORD
• Mid Square Method:- The key is Squared and
then hash function is defined as H(K)=l where l is
obtained like
• H(K)=K1+k2+K3……Kn
• H(3205)= 32+05=37
• The record will be placed at 37 loc 44
DOUBLE HASHING
Double-hash probing
A technique which eliminates both primary and secondary
clustering is double-hashing.
The idea is to compute a second hash value of the element to be
inserted.
k1 = H(key % size of table(M) or prime no) We want 0 < k <
capacity.
Using this value, search this sequence of alternative locations:
This second hash function needs to generate integers somewhere in
the range from 1 (not 0) to capacity - 1.
H(K2) hash2(obj) = 1+ (K1% M ’) where Max is prime number
less than or equal to size of table or can be considered as less than
99
45
OPEN HASHING
Eachbucket in the hash table is the head of a
linked list
All
elements that hash to a particular bucket are
placed on that bucket’s linked list
46
OPEN HASHING (CHAINING)
0 k2 k3 ...
1
...
2
From a
set of 3
keys
k1,k2,kn 4
D-1 ...
47
CHAINING
Chaining involves maintaining two table in memory
chaining without replacement
48
SEARCHING FOR A KEY
The data that's attached to a
key can be found fairly Number 701466868
quickly.
...
49
SEARCHING FOR A KEY
Calculate the hash value.
Check that location of the array for the Number 701466868
key.
Start by computing the hash value,
which is 2 in this case. Then check
location 2.
If location 2 has a different key than
the one you are looking for, then move
My hash
forward...
value is
Not
[2].
me.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322
...
50
SEARCHING FOR A KEY
Keep moving forward until you Number 701466868
find the key, or you reach an
empty spot.
My hash
value is
[2].
Not
me.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322
...
51
SEARCHING FOR A KEY
Keep moving forward until you
Number 701466868
find the key, or you reach an
empty spot.
My hash
value is
[2].
Not
me.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322
...
52
SEARCHING FOR A KEY
Keep moving forward until you Number 701466868
find the key, or you reach an
empty spot.
My hash
value is
[2].
Yes!
...
53
SEARCHING FOR A KEY
When the item is found, the Number 701466868
information can be copied to the
necessary location.
if a search reaches an empty
spot? In that case, it can halt
and indicate that the key was
not in the hash table. My hash
value is
[2].
Yes!
...
54
DELETING A RECORD
Records may also be deleted from a hash table.
Please
delete
me.
[0] [1] [2] [3] [4] [5] [ 700]
Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 701466868 Number 155778322
...
55
DELETING A RECORD
Records may also be deleted from a hash table.
But the location must not be left as an ordinary "empty
spot" since that could interfere with searches. .
(Remember that a search can stop when it reaches an empty
spot.)
...
56
DELETING A RECORD
Records may also be deleted from a hash table.
But the location must not be left as an ordinary "empty
spot" since that could interfere with searches.
The location must be marked in some special way so that a
search can tell that the spot used to have something in it.
...
57
o When a empty slot is encountered during insertion, that slot
can be used to store the new record.
58