0% found this document useful (0 votes)
34 views37 pages

Cse Learning Materials1729858797779

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views37 pages

Cse Learning Materials1729858797779

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.

Tech

ADVANCED DATA STRUCTURES & ALGORITHM


ANALYSIS
Objectives:

 Understand the fundamentals of algorithm analysis, including space and time


complexity analysis and asymptotic notations.
 Learn to create AVL Trees and perform insertion and deletion operations.
 Explore the applications of AVL Trees in solving real-world problems efficiently.

Modules:

1. Introduction: Algorithm Analysis


2. Space and Time complexity analysis
3. Asymptotic notations
4. AVL trees – creation, insertion, deletion operations and applications

Outcomes :

After completion of the unit, students will be able to

 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.

MODULE 1: Introduction: Algorithm Analysis:

Algorithm Definition:

 An Algorithm is a finite set of instructions that if followed, accomplishes a


particular task.
Characteristics of an algorithm:

 Input: An algorithm has zero or more but only finite number of inputs.

 Output: At least one quantity must be produced as output.

 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:

Algorithm Max (A, n)


// A is an array of size n.
{
Result :=A[l];
for i :=2 to n do
if A[i] >Result
then
Result:=A[i];
return Result;
}

In this algorithm is named Max, A and n are procedure parameters. Result


and i are local variables.

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

 Example 2: Recursive algorithm for Towers of Hanoi problem


Algorithm TowersOfHanoi(n, x, y, z)
//Move the top n disks from tower x to tower y.
{
if (n >= 1) then
{
TowersOfHanoi((n-1), x, z, y)
write(“move top disk from tower”, x, “to top of tower”, y);
TowersOfHanoi((n-1), z, y, x)
}
}

MODULE 2 : Space and Time complexity analysis:

Performance Analysis
 There are two criteria for judging the performance of an algorithm:

 Computing time

 Storage Requirements

 Performance evaluation can be loosely divided into two major phases:

(1) Priori estimates referred to as performance analysis

(2) Posteriori testing referred to as performance measurement.

 The space complexity of an algorithm is the amount of memory it needs to


run to completion.
 The time complexity of an algorithm is the amount of computer time it needs to
run to completion.

Space Complexity

 The space needed by the algorithm is the sum of the following components:

 A fixed part that is independent of the characteristics (e.g., number, size)


of the inputs and outputs.
 This part typically includes the instruction space (i.e., space for the code),
space for simple variables and fixed-size component variables (also called
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 4
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
aggregate), space for constants, and soon.

 A variable part that consists of the space needed by component variables


whose size is dependent on the particular problem instance being solved,
the space needed by referenced variables (to the extent that this depends on
instance characteristics), and the recursion stack space (insofar as this
space depends on the instance characteristics)
 The space requirement S(P) of any algorithm P may be written as :

S (P) = c + Sp (instance characteristics), where c is a constant.

When analyzing the space complexity of an algorithm, we concentrate on estimating


Sp(instance characteristics).
For any given problem, we need first to determine which instance characteristics
to use to measure the space requirements.
 Example 1:

Algorithm abc computes a+ b+b*c+(a+ b -c)/(a+6)+4.0;

Algorithm abc (a,b,c)

return a + b + b * c+ (a + b - c)/(a+ b) + 4.0;

• The problem instance is characterized by the specific values of a, b, and c.

• 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;

• The problem instances for are characterized by n, the number of elements to be


summed.

• The space needed by n is one word, since it is of type integer.

• The space needed by a is the space needed by variables of type array of


floating point numbers.

• This is at least n words, since a must be large enough to hold the n


elements to be summed.

• So, Ssum(n) ≥ (n +3) (n for a[ ]one each for n, i and s).

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.

[This does not involve any calls to other algorithms]

Interactive statement such as for, while & repeat-until Control part of the statement.

 We can determine the number of steps needed by a program to solve a particular


problem by using one of the two methods as follows:
Method1: Count Method

 We introduce a new variable, count, into the program. This is a global


variable with initial value 0. Statements to increment count by the
appropriate amount are introduced into the program. This is done so that
each time a statement in the original program is executed; count is

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.

• If count is zero to start with, then it will be 2n + 3 on termination.

• So each invocation of Sum executes a total of 2n + 3 steps.

Example 2: (Recursive Algorithm)

Algorithm RSum(a,n)

Count:=count+1; // For the if conditional

if (n<= 0) then

count:=count+1; // For the return

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

return RSum (a, n-1) + a[n];

• 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.

• We see that tRSum(0) = 2. When n >0, count increases by 2 .

tR Sum(n) = 2 if n = 0
2 + tR Sum(n-1) if n > 0

• These recursive formulas are referred to as recurrence relations.

• One way of solving any such recurrence relation is to make repeated


substitutions for each occurrence of the function tRSum on the right-hand
side until all such occurrences disappear.
tR Sum(n) = 2 + tR Sum(n-1)
= 2 + 2+ tR Sum(n-2)
= 2(2) + tR Sum(n-2)


= n (2) + tR Sum(0)
= 2n + 2, n≥0

So the step count for RSum is 2n+2.

Method 2: Table method


 The second method to determine the step count of an algorithm is to build
a table in which we list the total number of steps contributed by each
statement.

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.1 Frequency table method for Sum of n natural numbers

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

MODULE 3 : Asymptotic notations:


Asymptotic Notation (O,Ω, θ)

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 (Ω)

3. Big–THETA (Θ) and

Asymptotic Analysis of Algorithms

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.

 It is a way to describe the characteristics of a function in the limit.

 It describes the rate of growth of functions.

 Focus on what‟s important by abstracting away low-order terms and constant factors.

 It is a way to compare “sizes” of functions

Table 1.3 Order of growth

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:

 Big oh(O) notation is used to represent upper bound of algorithm runtime.


 Let f(n) and g(n) are two non-negative functions

 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.

Fig 1.1 Big Oh Notation


Example

If f(n)=3n+2 then prove that f(n) = O(n)


Let f (n) =3n+2, c=4, g (n) =n

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:

 Big Omega(Ω) notation is used to represent lowerbound of algorithm runtime.

 Let f(n) and g(n) are two non-negative functions

 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.

Fig 1.2 Big Omega Notation


Example

f(n)=3n+2 then prove that f(n) = Ω(g(n))

Let f(n) =3n+2, c=3, g(n) =n


if n=1 3n+2 ≥ 3n
3(1) + 2 ≥ 3(1)
5 ≥ 3 (T)
3n+2 ≥ 4n for all n ≥ 1
This is in the form of f (n) ≥ c*g(n) for all n≥ no, where c=3, no=1
Therefore, f (n) = Ω (n).

Big Theta Notation:

 Big Theta(ɵ) notation is used to represent the running time between upper bound
and lower bound.

 Let f(n) and g(n) be two non-negative functions.

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

Fig 1.3 Big Theta Notation

Example:

f(n)=3n+2 then Prove that f(n) = θ (g(n))


Lower bound=3n+2 ≥ 3n for all n ≥ 1
c1=3, g(n) = n, no=1
Upper Bound = 3n+2 ≤ 4n for all n ≥ 2
c2 = 4, g(n) = n, n0 = 2
3(n) ≤ 3n+2 ≤ 4(n) for all n, n ≥ 2
This is in the form of c1*g(n) ≤ f(n) ≤ c2* g(n) for all n ≥ no Where c1=3, c2=4, g(n)=n, no=2

MODULE 4: AVL trees – creation, insertion, deletion operations and


applications:

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.

Properties of AVL Tree:


1) The minimum height of an AVL tree with n elements is O(log n)
2) The maximum height of an AVL tree with n elements is 1.44
3) An n-element AVL tree can be searched in O(height)= O(log n) time.
4) A new element can be inserted into an n-element AVL search tree so that the result
is an n+1 element AVL tree and such an insertion can be done in O(log n) time.
5) An element can be deleted from an n-element AVL search tree so that the result is
an n-1 element AVL tree and such a deletion can be done in O(log n) time.

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:

Fig 1.4 AVL tree Fig 1.5 Not an AVL tree

Operations on an AVL Trees


The basic operations performed on an AVL tree are
1. Inserting a new element
2. Deleting an element
3. Searching an element

Insertion operation on AVL Trees


 In AVL Tree, new node is always inserted as a leaf node. The insertion operation is
performed as follows....
 Step 1: Insert the new element into the tree using Binary Search Tree insertion
logic.
 Step 2: After insertion, check the Balance Factor of every node.
 Step 3: If the Balance Factor of every node is 0 or 1 or -1 then go for next
operation.
 Step 4: If after insertion the balance factor of any of the nodes turns out to be
anything other than 0, +1 or -1, then the tree is said to be unbalanced.
 To balance the tree we perform rotations
 Rotations: Rotations are mechanisms which shift some of the sub trees of the
unbalanced tree either to left or right to obtain a balanced tree.
 There are four rotations and they are classified into two types.

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)

Fig 1.6 AVL Rotations

LL Rotation (Single Right Rotation)


 The new node „x‟ is inserted in the Left sub tree of left child of node A. As a result, the
balance of A becomes +2.
 To restore balance at A Single right rotation need to be applied at A.

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

RR Rotation (Single Left Rotation)


 The new node „x‟ is inserted in the Right sub tree of right child of node A. As a result,
the balance of A becomes -2.
 To restore balance at A Single left rotation need to be applied at A.
General Notation:

Example:

LR Rotation (Left Right Rotation)


 Imbalance occurred at A due to the insertion of node x in the right sub tree of left child
of node A.
 LR rotation involves sequence of two rotations
(i) Single Left rotation/RR rotation
(ii) Single Right rotation/LL rotation

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:

RL Rotation (Right Left Rotation)


 Imbalance occurred at A due to the insertion of node x in the Left sub tree of right child
of node A.
 RL rotation involves sequence of two rotations
(i) Single Right rotation/LL rotation
(ii) Single Left rotation/RR rotation
General Notation:

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:

Example for Construction of an AVL Tree


Construct AVL tree for the list by successive insertion: 5, 6, 8, 3, 2, 4, 7
i. Insert 5 into empty tree

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

Deletion operation On AVL Trees


The sequences of steps to be followed in deletion are:
1. Initially, the AVL tree is searched to find the node to be deleted
2. If the search is successful, delete the node. The following are the 3 possibilities for the
node „x‟ that is to be deleted
(i) x is a leaf – In this case the leaf node is discarded
(ii) x has exactly one non empty sub tree – If x has no parent, the root of its sub tree
becomes the new search tree root. If x has a parent P, then we change the pointer from
parent p so that it points to x‟s only child
(iii) x has two non empty sub trees – x is replaced with either the largest element in the
left sub tree or the smallest element in its right sub tree.
Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 20
R -23 ADVANCED DATA STRUCTURES & ALGORITHM ANALYSIS UNIT- I II B.Tech
3. After deletion of node, check the balance factor of each node
4. Rebalance the tree if the tree is unbalanced. For this AVL tree deletion rotations are used.
 On deletion of a node x from the AVL tree. Let A be the closest ancestor node on the
path from x to the root node, with a balance factor of +2 or -2. To restores balance at
node A, we classify the type of imbalance as follows:
 L – type imbalance:
o The imbalance is of type L if the deletion took place from A‟s left sub tree.
o The balance factor of A = -2
o A has a right sub tree with root B
o L-type imbalance is sub classified into types L0, L1 and L-1 depending on the
balance factor of B
L0 Rotation
 L0 imbalance occurs if the deletion takes place from the left sub tree of A and balance
factor of B is 0
 L0 rotation is Single Left rotation, that is applied at node A
General Notation:

Example:

Before deletion After deleting 26 After L0 rotation

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:

Before deletion After deleting 16 After L1 rotation


L-1 Rotation
 L-1 imbalance occurs if the deletion takes place from the left sub tree of A and balance
factor of B is -1
 L-1 rotation is Single Left rotation, that is applied at node A

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:

Before deletion After deleting 16 After L-1 rotation

 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.

Applications of AVL Trees

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 Mark: Objective Questions

MODULE 1: Introduction: Algorithm Analysis:

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

3. An algorithm with a time complexity of O(1) is known as a(n) algorithm.


Answer: constant-time

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

5. Which of the following notations is used to represent the upper bound of an


algorithm's running time?
a) Theta (Θ)
b) Omega (Ω)
c) Big O (O)
d) Little o (o)

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)

2. The space complexity of an algorithm is defined as:


a) The amount of time an algorithm takes to complete
b) The total number of instructions in the algorithm
c) The amount of memory an algorithm needs to run to completion
d) The complexity of nested loops in an algorithm
Answer: c) The amount of memory an algorithm needs to run to completion

3. What is the worst-case time complexity of a linear search algorithm?


a) O(log n)
b) O(n)
c) O(n log n)
d) O(1)
Answer: b) O(n)

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)

6. The space complexity of the merge sort algorithm is .


Answer: O(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)

MODULE 3 : Asymptotic notations

1. Which of the following notations represents the upper bound of an algorithm's


running time?
a) Θ (Theta)
b) Ω (Omega)
c) O (Big O)
d) o (Little o)
Answer: c) O (Big O)

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)

4. The notation is used to describe the upper bound of an algorithm's running


time.
Answer: O (Big O)

5. When an algorithm's running time is within constant factors of a function, we use


the notation .
Answer: Θ (Theta)

6. The notation is used to describe the lower bound of an algorithm's running


time.
Answer: Ω (Omega)

7. If an algorithm's running time grows exactly as fast as a given function, it is


denoted by .
Answer: Θ (Theta)

8. In asymptotic analysis, the notation indicates that a function grows at least as


fast as another function.
Answer: Ω (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

1. What does AVL in AVL trees stand for?

a) Algorithmic Value Log


b) Adelson-Velsky and Landis
c) Asymptotic Value Level
d) Advanced Value Log
Answer: b) Adelson-Velsky and Landis

2. What is the height balance property of an AVL tree?

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

4. What is the time complexity for insertion in an AVL tree?

a) O(n)
b) O(log n)
c) O(n log n)
d) O(1)
Answer: b) O(log n)

5. Which rotation is performed to balance an AVL tree when an insertion causes an

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

6. Which rotation is performed to balance an AVL tree when an insertion causes an


imbalance in the right-right (RR) case?

a) Left rotation
b) Right rotation
c) Left-right rotation
d) Right-left rotation
Answer: a) Left rotation

7. In an AVL tree, what is the time complexity for searching an element?

a) O(n)
b) O(log n)
c) (n log n)
d) O(1)
Answer: b) O(log n)

8. An AVL tree is a type of binary search tree.


Answer: balanced

9. An insertion in an AVL tree requires rotations in the worst case to maintain


balance.
Answer: two

10. In an AVL tree, the time complexity for deletion is .


Answer: 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:

1. Define Big O notation. [BL – 1]


2. List three common asymptotic notations used in algorithm analysis. [BL – 1]
3. Explain the difference between worst-case and average-case time complexity. [BL – 2]
4. Describe the significance of space complexity in algorithm analysis. [BL – 2]
5. Determine the time complexity of a linear search algorithm in an unsorted array. [BL – 3]
6. Compare and contrast Big O and Big Theta notations. [BL – 4]

MODULE 2: Space and Time complexity analysis

1. Define space complexity in the context of algorithm analysis. [BL – 1]


2. List two examples of algorithms with logarithmic time complexity. [BL – 1]
3. Explain how the worst-case time complexity of an algorithm is determined. [BL – 2]
4. Calculate the space complexity of a recursive algorithm using a stack. [BL – 3]
5. Compare the time complexities of linear search and binary search. [BL – 4]

MODULE 3: Asymptotic notations


1. Define Big O notation. [BL – 1]
2. List the four main asymptotic notations used in algorithm analysis. [BL – 1]
3. Explain the meaning of Big Omega (Ω) notation. [BL – 2]
4. Describe how Big Theta (Θ) notation differs from Big O (O) notation. [BL – 2]

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

1. Define an AVL tree. [BL – 1]


2. List the types of rotations used to maintain balance in AVL trees. [BL – 1]
3. Explain the balance factor of a node in an AVL tree. [BL – 2]
4. Describe the process of insertion in an AVL tree and how it affects tree balance. [BL - 2]
5. Given an AVL tree, identify which rotation is needed if an imbalance occurs in the
left-right (LR) case. [BL – 3]
6. Apply an AVL tree rotation to balance a tree after inserting a node that causes an
imbalance. [BL – 3]

Part-C
5 Marks : Descriptive Questions

MODULE 1: Introduction: Algorithm Analysis:

1. Define algorithm. Explain the characteristics of Algorithm. [BL – 1]


2. Write an algorithm for matrix addition and matrix multiplication. [BL – 2]
3. Write an algorithm for finding sum of N natural number using recursion and
non recursion. [BL – 2]

MODULE 2: Space and Time complexity analysis

1. Explain about performance analysis of an algorithm [BL – 2]


2. Explain how to compute the time complexity of a nested loop. Provide a detailed
example of a nested loop structure and demonstrate the steps to derive its time
complexity, including the analysis of inner and outer loops. [BL – 2]
3. Discuss the difference between time complexity and space complexity. [BL – 2]
4. Write an algorithm for matrix addition and matrix multiplication and find its time
complexity and space complexity. [BL – 2]
5. Write an algorithm for sum of N natural number using recursion and non recursion
and find its time complexity and space complexity. [BL – 2]
6. Write an algorithm for Fibonacci series and find its time complexity. [BL – 2]
7. Write an algorithm for finding factorial of a given number and find its time
complexity. [BL – 2]

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

MODULE 3: Asymptotic notations

1. Explain about asymptotic notations with examples. [BL – 2]

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]

MODULE 4 : AVL trees – creation, insertion, deletion operations and


applications

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

D. Self organization list

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

A. both O(N) and Ω(N)

B. O(N) but not Ω(N)

C. Ω(N) but not O(N)

D. neither O(N) nor Ω(N)

10. Consider the following C function. GATE CSE 2015


int fun1 (int n) {
int i, j, k, p, q = 0;
for (i = 1; i < n; ++i)
{
p = 0;
for (j = n; j > 1; j = j/2)
++p;
for (k = 1; k < p; k = k * 2)
++q;
}
return q;
}
Which one of the following most closely approximates the return value of the function fun1?
A. n3

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

f1, f 2, f3 and f4? GATE CSE 2021


f1 = 2n f2 = n3/2
f3(n) = nlog2n
f4 (n) = n log2n
A. f3, f2, f4, f1

B. f3, f2, f1, f4

C. f2, f3, f1, f4

D. f2, f3, f4, f1

Seshadri Rao Gudlavalleru Engineering College Dept. of CSE AS&A 2024-2025 Page 38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy