Cse Learning Materials1729858797779
Cse Learning Materials1729858797779
Tech
Modules:
Outcomes :
Analyze algorithms in terms of space and time complexity using asymptotic notations.
Create AVL Trees and perform insertion and deletion operations on them.
Apply AVL Trees to solve practical, real-world problems efficiently.
Algorithm Definition:
Input: An algorithm has zero or more but only finite number of inputs.
Definiteness: Each instruction must be clear and ambiguous. It must be clear what
should be done.
Seshadri Rao Gudlavalleru Engineering College Dept of CSE AS&A 2024-2025 Page 2
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
For example consider the instruction: add 6 or 7 to x. It is not clear about
which one of the two possibilities should be done.
Finiteness: If we trace out the instructions of an algorithm, then for all cases, the
algorithm terminates after a finite number of steps.
An algorithm must terminate after a finite number of operations, but the time for
termination should be reasonably short.
For example, an algorithm could be devised that decides whether any given
position in a game of chess is a winning position. An algorithm works by
examining all possible positions and counter moves, but this takes billions
of years to make a decision, even with more modern computers. So we say
that algorithm is not finite.
Effectiveness: Every instruction must be very basic so that it can be carried out by
a person using only pencil and paper.
Each operation must be feasible and effective and can be done in a finite
amount of time.
For example, performing integer arithmetic is effective but performing real
arithmetic is not effective as it involves long decimal expansions.
Example 1: Algorithm that finds and returns the maximum of n given numbers:
Seshadri Rao Gudlavalleru Engineering College Dept of CSE AS&A 2024-2025 Page 3
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Performance Analysis
There are two criteria for judging the performance of an algorithm:
Computing time
Storage Requirements
Space Complexity
The space needed by the algorithm is the sum of the following components:
• Making the assumption that one word is adequate to store the values of
each of a, b, c, and the result, we see that the space needed by abc is
independent of the instance characteristics.
• Sp (instance characteristics) = 0.
Example 2:
Algorithm Sum compute sum of a [1] to a[n] iteratively, where the a[i]'s are real
numbers
Algorithm Sum (a,n)
{
s :=0.0;
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 5
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
for i :=to n do
s :=s+ a[i];
returns;
Time Complexity
The time T(p) taken by a program P is the sum of the compile time and the run
time(execution time)
The compile time does not depend on the instance characteristics. Also we may assume
that a compiled program will be run several times without recompilation .This rum time is
denoted by tp (instance characteristics).
The number of steps any problem statement is assigned depends on the kind of statement.
For example,
comments 0 steps.
An assignment statement is 1 step.
Interactive statement such as for, while & repeat-until Control part of the statement.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 6
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
incremented by the step count of that statement.
Example1: By including a global variable Count (Iterative Algorithm)
Algorithm sum(a,n)
{
s=0.0;
count=count+1;
for i= 1 to n do
{
count=count+1;
s = s + a[i];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
For the for loop, the value of count will increase by a total of 2n.
Algorithm RSum(a,n)
if (n<= 0) then
return 0.0;
else
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 7
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
count:=count+1; // For the addition, function invocation and return
• When analyzing a recursive program for its step count, we often obtain a
recursive formula for the step count.
• Let tRSum(n) be the increase in the value of count when the Algorithm
terminates.
tR Sum(n) = 2 if n = 0
2 + tR Sum(n-1) if n > 0
= n (2) + tR Sum(0)
= 2n + 2, n≥0
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 8
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
This figure is often arrived at by first determining the number of steps per
execution(s/e) of the statement and the total number of times (i.e.,
frequency) each statement is executed.
The s/e of a statement is the amount by which the count changes as a result
of the execution of that statement.
By combining these two quantities, the total contribution of each statement
is obtained.
By adding the contributions of all statements, the step count for the entire
algorithm is obtained
Example 3: The step count of an algorithm can be determined by using the
table method as follows. Constructing Frequency Table
Table 1.2 Frequency table method for Sum of n natural numbers using recursion
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 9
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
The following notations are commonly use notations in performance analysis and used to
characterize the complexity of an algorithm:
1. Big–OH (O)
2. Big–OMEGA (Ω)
Our approach is based on the asymptotic complexity measure. This means that we don‟t try to
count the exact number of steps of a program, but how that number grows with the size of the
input to the program. That gives us a measure that will work for different operating systems,
compilers and CPUs. The asymptotic complexity is written using big-O notation.
Focus on what‟s important by abstracting away low-order terms and constant factors.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 10
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Big Oh Notation:
Definition: The function f(n) = O(g(n)) if and only if there exists positive constants c
and n0 such that f(n)≤c*g(n) for all n , n ≥ n0.
if n=1 3n+2≤ 4n
3(1)+2 ≤ 4(1)
3+2≤4
5≤4 (F)
if n=2 3n+2≤4n
3(2)+2≤4(2)
8≤8 (T)
3n+2≤4n for all n ≥2
This is in the form of f(n) ≤ c*g(n) for all n ≥ no, where c=4, no =2
Therefore, f(n) = O(n).
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 11
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Big Omega Notation:
Definition: The function f(n) = Ω(g(n)) if and only if there exists positive constants c
and n0 such that f(n) ≥ c*g(n) for all n , n ≥ n0.
Big Theta(ɵ) notation is used to represent the running time between upper bound
and lower bound.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 12
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Definition: The function f(n) = θ(g(n)) if and only if there exists positive constants c1 ,
c2 and n0 such that c1*g(n) ≤ f(n)≤c2* g(n) for all n, n≥n0
Example:
AVL TREES
BALANCED TREES (OR) HEIGHT BALANCED TREES
Trees whose height in the worst case turns out to be O(log n) are called balanced trees.
Example: AVL trees,2-3 trees, Red Black Trees, Splay trees.
We can guarantee O(log n) performance for the search ,insert and delete operations of a
search tree by ensuring that the search tree height is always O(log n).
Since trees are balanced by working with their height, they are also known as height
balanced trees.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 13
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Definition: A height balanced tree T is a binary tree that may be empty. If non empty,
then
1. Its left and right sub trees must also be height balanced trees
2. The height of left and right sub trees differ by at most 1 i.e |HL-HR|<=1
Examples of Balanced trees: AVL trees, Splay trees, Red Black trees, B trees and B+
trees.
AVL TREES
An AVL tree is a binary search tree in which difference between the heights of the left
and right sub trees will be either -1, 0 or 1.
Therefore an AVL tree is a balanced/height balanced binary search tree.
Definition: An empty binary tree is an AVL tree. If T is a non empty binary tree with
TL and TR are its left and right sub trees, then T is an AVL tree if and only if
1) TL and TR are AVL trees and
2) |HL-HR|<=1 where HL and HR are the height of left sub trees(TL) and right sub tree
(TR) respectively of T.
Balance factor (bf) is associated with every node is an AVL tree which may be either 0
or +1 or -1.
Balance factor: The balance factor bf(u) of a node u is defined as the height of the left
sub tree of u minus the height of the right sub tree of u.
bf(u) = ( h(uL) - h(uR) ) where h(uL) and h(uR) are the height of the left and right sub
trees of the node u respectively.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 14
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Examples:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 15
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
LL ROTATION
(Single Right Rotation)
SINGLE ROTATION
RR ROTATION
(Single Left Rotation)
LR ROTATION
(Left Right Rotation)
DOUBLE ROTATION
RL ROTATION
(Right Left Rotation)
General Notation:
Example:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 16
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Example:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 17
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
General Notation:
Example:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 18
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Example:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 19
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Example:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 21
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
L1 Rotation
L1 imbalance occurs if the deletion takes place from the left sub tree of A and balance
factor of B is 1
L1 rotation is RL rotation, which involves 2 rotations:
i. Single Right
ii. Single Left
General Notation:
Example:
General Notation:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 22
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Example:
R – type Imbalance:
o The imbalance is of type R if the deletion took place from A‟s Right sub tree.
o The balance factor of A = 2
o A has a left sub tree with root B
o R-type imbalance is sub classified into types R0, R1 and R-1 depending on the
balance factor of B
R0 Rotation
R0 imbalance occurs if the deletion takes place from the Right sub tree of A and balance
factor of B is 0
R0 rotation is Single Right rotation, that is applied at node A
General Notation:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 23
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Example:
R1 Rotation
R1 imbalance occurs if the deletion takes place from the Right sub tree of A and balance
factor of B is 1
R1 rotation is Single Right rotation, that is applied at node A
General Notation:
Example:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 24
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
R-1 Rotation
R-1 imbalance occurs if the deletion takes place from the Right sub tree of A and balance
factor of B is -1
R-1 rotation is LR rotation, which involves 2 rotations:
i. Single Left
ii. Single Right
General Notation:
Example:
Searching an element
Searching operation on AVL search tree is same as Binary Search Tree.
1. Databases and File Systems: AVL trees are used in databases and file systems to maintain
ordered data efficiently. They help in fast lookup, insertion, and deletion operations,
ensuring that the data structure remains balanced for optimal performance.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 25
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
2. Memory Management: In memory management systems, AVL trees are used to keep
track of free memory blocks. The balancing property of AVL trees ensures that the
allocation and deallocation of memory can be performed efficiently.
3. Routing Algorithms: In network routing algorithms, AVL trees help manage routing
tables and maintain the shortest path to different nodes in a network.
4. Compiler Optimisation: AVL trees are used in compiler design for various optimisations,
such as constant folding, code motion, and dead code elimination. They help in
maintaining the structure of the code in an optimised manner.
5. Multimedia Applications: In multimedia systems, AVL trees can be used for indexing
and searching multimedia files like images, videos, and audio files. The balanced nature of
AVL trees ensures quick access to these files.
6. Scheduling Systems: AVL trees are used in scheduling systems to manage tasks or
processes based on priority. They ensure that the highest priority task is always executed
first and new tasks are inserted efficiently.
7. Geometric Applications: In computational geometry, AVL trees are used to manage and
query geometric objects, such as points, lines, and polygons. They help in solving
problems like finding the nearest neighbour or detecting intersections.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 26
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Assignment cum Tutorial questions
Part-A
1. In asymptotic analysis, the notation is used to denote the lower bound of an algorithm's
running time.
Answer: Ω (Omega)
2. When analyzing the performance of an algorithm, the case scenario considers the input
that causes the algorithm to perform the most iterations.
Answer: worst
4. Which of the following is the correct order of growth for functions from fastest to
slowest?
a) O(n), O(log n), O(n^2), O(1)
b) O(1), O(log n), O(n), O(n^2)
c) O(n^2), O(n), O(log n), O(1)
d) O(log n), O(1), O(n^2), O(n)
Answer: b
Answer: c
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 27
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
MODULE 2 : Space and Time complexity analysis :
1. Which of the following represents the time complexity for accessing an element in an
array?
a) O(n)
b) O(log n)
c) O(1)
d) O(n log n)
Answer: c) O(1)
4. Which of the following data structures generally has the best space complexity?
a) Linked List
b) Array
c) Stack
d) Hash Table
Answer: b) Array
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 28
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
5. Which complexity class represents an algorithm whose running time doubles with each
additional input element?
a) O(n)
b) O(n^2)
c) O(log n)
d) O(2^n)
Answer: d) O(2^n)
7. In Big-O notation, the notation is used to represent the average case time complexity.
Answer: O(n)
Answer: O(log n)
8. An algorithm with a time complexity of O(n^3) is also known as a(n) algorithm.
Answer: cubic
9. The additional memory used by an algorithm, excluding the memory for input data, is
referred to as the algorithm's space complexity.
Answer: auxiliary
10. The worst-case time complexity for deleting an element from a linked list is .
Answer: O(n)
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 29
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
2. Which notation is used to describe the average case time complexity
a) O (Big O)
b) Θ (Theta)
c) Ω (Omega)
d) o (Little o)
Answer: b) Θ (Theta)
3. The notation that represents the lower bound of an algorithm's running time is:
a) O (Big O)
b) Θ (Theta)
c) Ω (Omega)
d) o (Little o)
Answer: c) Ω (Omega)
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 30
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
MODULE 4 : AVL trees – creation, insertion, deletion operations and
applications
a) The height of the left and right subtrees of every node can differ by at most 1.
b) The height of the left and right subtrees of every node can differ by at most 2.
c) The height of the left subtree is always greater than the right subtree.
d) The height of the right subtree is always greater than the left subtree.
Answer: a) The height of the left and right subtrees of every node can differ by
at most 1.
3. Which of the following operations may cause an AVL tree to become unbalanced?
a) Creation
b) Insertion
c) Searching
d) Deletion
Answer: b) Insertion and d) Deletion
a) O(n)
b) O(log n)
c) O(n log n)
d) O(1)
Answer: b) O(log n)
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 31
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
imbalance in the left-left (LL) case?
a) Left rotation
b) Right rotation
c) Left-right rotation
d) Right-left rotation
Answer: b) Right rotation
a) Left rotation
b) Right rotation
c) Left-right rotation
d) Right-left rotation
Answer: a) Left rotation
a) O(n)
b) O(log n)
c) (n log n)
d) O(1)
Answer: b) O(log n)
14. An AVL tree ensures that the height of the tree remains with respect to the
number of nodes.
Answer: logarithmic
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 32
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
15. The notation is used to describe the average case time complexity for search,
insertion, and deletion operations in an AVL tree.
Answer: O(log n)
Part-B
2 Marks : Short Answer Questions
MODULE 1: Introduction: Algorithm Analysis:
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 33
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
MODULE 4: AVL trees – creation, insertion, deletion operations and
applications
Part-C
5 Marks : Descriptive Questions
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 34
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
2. Describe Big Omega (Ω) notation and its role in understanding the lower bound of an
algorithm‟s time complexity. Provide examples of algorithms where Ω notation is useful
for analyzing the best-case performance. [BL – 3]
3. Compare and contrast Big O, Big Theta, and Big Omega notations. [BL – 3]
1. Differentiate between AVL tree and a Binary Search Tree? Does AVL tree offers
better performance than a Binary Search Tree? Give reasons [BL – 3]
2. Define Rotation. Explain different rotations that are applied during insertion operation
on an AVL tree with examples. [BL – 2]
3. What is an AVL tree? Explain the need for rotation of AVL trees. Construct an AVL
Tree for the list 8, 9, 11, 6, 5, 7, 10 by using successive insertions. Illustrate the steps
clearly. [BL – 3]
4. Illustrate with examples different rotations that are applied during deletion operation
on an AVL tree. [BL – 2]
5. Show the AVL tree that results after each of the integer keys 9, 27, 50, 15, 2, 21,
and 36 are inserted, in that order, into an initially empty AVL tree. Clearly show
the tree that results after each insertion, and make clear any rotations that must be
performed. [BL – 3]
6. Insert the following sequence of elements into an AVL tree, starting with an empty
tree :15, 20, 24, 10, 13, 7, 30, 36, 25 [BL – 3]
*********
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 35
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
Part-D
Competitive Examination Questions
1. The minimum height of an AVL tree with n nodes is ISRO CSE 2020
A. Ceil (log2(n+1))
B. 1.44 log2 n
C. Floor (log2 (n+1))
D. 1.64 log2 n
2. In a balanced binary search tree with n elements, what is the worst case time complexity of reporting
all elements in range[a,b]? Assume that the reported elements is k. GATE CSE 2020
A. Θ (log n)
B. Θ (lon n +k)
C. Θ (k log n)
D. Θ (n log k)
2
3. What is the worst case time complexity of inserting n elements into an AVL-tree with n elements
initially? GATE CSE 2020
A. Θ (n4)
B. Θ (n2)
C. Θ (n2 log n)
D. Θ (n3)
4. Access time of the symbolic table will be logarithmic if it is implemented by ISRO CSE 2016
A. Linearlist
B. Search tree
C. Hash table
5. The number of rotations required to insert a sequence of elements 9,6,5,8,7,10 into an empty AVL
tree is? ISRO CSE 2013
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 36
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
A. 0
B. 1
C. 2
D. 3
6. The worst case running time to search for an element in a balanced binary search tree with n2n
elements is GATE CSE 2012
A. Θ (n log n)
B. Θ (n2n)
C. Θ (n)
D. Θ ( log n)
7. What is the maximum height of any AVL-tree with 7 nodes? Assume that the height of a tree with a
single node is 0. GATE CSE 2009
A. 2
B. 4
C. 5
D. 3
8. Let T(n) be the recurrence relation defined as follows: GATE CSE 2024
T(0)=1
T(1)=2, and
T(n)=5T(n−1)−6T(n−2) for n≥2
Which one of the following statements is TRUE?
A. T(n)= Θ (n2n)
B. T(n)= Θ (2n)
C. T(n)= Θ (3n)
D. T(n)= Θ (n3n)
9. Given an integer array of size N, we want to check if the array is sorted (in either ascending or
descending order). An algorithm solves this problem by making a single pass through the array and
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 37
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
comparing each element of the array only with its adjacent elements. The worst-case time complexity
of this algorithm is GATE CSE 2024
B. n (log n)2
C. n log n
D. n log (log n)
11. Which of the given options provides the increasing order of asymptotic Complexity of functions
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 38