1)List the characteristics of an algorithm?
2M
Characteristics of an Algorithm:
1. Well-defined: Each step in an algorithm must be precisely stated and unambiguous.
There should be no room for interpretation or guesswork.
2. Input: An algorithm takes input data, which can be of various types (numbers, text,
images, etc.).
3. Output: An algorithm produces output, which is the result of the computation
performed on the input data.
4. Finiteness: An algorithm must terminate after a finite number of steps. It should not
go into an infinite loop.
5. Effectiveness: Each step in the algorithm should be executable, meaning it should be
possible to carry out the step using available resources.
6. Correctness: An algorithm should produce the correct output for all valid inputs. It
should not have errors or bugs that lead to incorrect results.
7. Unambiguity: Each step should have a single interpretation. There should be no
ambiguity or multiple meanings.
8. Language-independent: An algorithm is a concept, not a piece of code. It can be
expressed in various programming languages, but the underlying logic remains the
same.
2)
X() 2M
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j=j+i)
printf(“rec”);
}
Find the time complexity of above pseudo code
To analyze the time complexity, let's count the number of times the "printf" statement is executed.
Outer loop: The outer loop iterates from i = 1 to i = n, making n iterations.
1|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Inner loop: The inner loop iterates from j = 1 to j = n, but the increment is j = j + i. This means the number of iterations in
the inner loop depends on the value of i.
• Case 1: i = 1: The inner loop iterates n times.
• Case 2: i = 2: The inner loop iterates n/2 times.
• Case 3: i = 3: The inner loop iterates n/3 times.
• ...
• Case n: i = n: The inner loop iterates n/n = 1 time.
Total iterations: The total number of iterations for the inner loop can be approximated by the harmonic series: 1 + 1/2 +
1/3 + ... + 1/n. This series is known to be O(log n).
Overall time complexity: Since the outer loop has n iterations and the inner loop has O(log n) iterations, the overall time
complexity is O(n * log n).
Therefore, the time complexity of the given pseudocode is O(n * log n).
3) Define time and space complexity? 2M
What is Time Complexity?
Time complexity useful to measure time taken by algorithm to execute program with input size “n”.
Time complexity is not time to execute program.
The execution time is different for different systems.
Time complexity means units of time to execute a program.
We always check worst case only.
We never check the best time case.
In worst time complexity representation, there is rules.
1. Avoid Constants.
2. Avoid lesser values.
O(1) – constant complexity – takes the same amount of space regardless of the input size.
O(log n) – logarithmic complexity – takes space proportional to the log of the input size.
O(n) – linear complexity – takes space directly proportional to the input size.
What is Space Complexity?
It is useful to measure memory space taken by algorithm to execute program with input size “n”.
Total Space Complexity: Input Space + Auxiliary Space
Input Space: The memory occupied by the input data.
Auxiliary Space: Extra memory used by the algorithm beyond the input data,
like temporary variables,
. data structures, etc.
2|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
4)List the applications of AVL Trees 2M
Applications of AVL Tree
In-memory sets and dictionaries: AVL trees are often used to implement these data structures due to their
efficient search and retrieval capabilities.
Database indexing: They are employed in databases for indexing data to speed up query processing, especially
when frequent lookups and relatively fewer insertions/deletions are involved.
Real-time systems: AVL trees provide predictable performance, making them suitable for applications requiring
consistent response times.
Game development: They can be used to implement game elements like inventory management, character
attributes, or level structures that require efficient search and updates.
Business applications: For maintaining employee records, tracking inventory, or managing customer data, AVL
Show the AVL tree Construction for the given elements 14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20 (5M)
3|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
4|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
5|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
6|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
6) Demonstrate the Deletion of the elements from the given AVL tree 5M
7)Explain the priority queue and how has it been implemented with an example? 5M
What is Priority Queue?
A priority queue is standard template library
that processes only the highest priority element.
It means that the first element of the queue is greatest from the rest of the elements of the queue.
Later all the elements are arranged in non-increasing order,
i.e., you can see each element of the queue has fixed priority.
The queue data structure follows the FIFO strategy,
i.e., first in, first out, whereas the priority queue only checks the priority of the elements for retrieving the elements.
Priority queues are implemented as container adapters.
It is a class that uses an encapsulated object of a specific container class and deliver a specific set of member function
to access the queue elements.
9)Make use of asymptotic notations to find the time complexity with suitable examples. 5M
7|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Asymptotic notation is used to represent running time & space of an algorithm-
how much time & space of an algorithm takes with a given input “n”.
"These notations are important because without expanding the cost of running the algorithm, we can estimate the complexity of
the algorithms.
There are mainly 3 asymptotic notations.
• Omega notation (Best case (or)min time)
• Theta Notation
• Big Oh Notation (Worst case (or)max time)
• Little oh notation.
Omega notation (Best case (or)min time): -
Omega notation in asymptotic analysis gives the best-case time complexity of the given runtime algorithm.
Measure of the lower boundary of the runtime algorithm.
It is generally represented as Ω-Notation.
Mathematically, It is denoted by Big Omege (Ω)
It represents minimum no.of steps.
Ω(g(n)) = { f(n)} then there exist positive constants c
and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
Theta Notation (Average Case Analysis): -
The average time taken by the algorithm on input size “n” is called average execution time.
Represents the upper and the lower bound of the running time of an algorithm, it is used for analysing the average-
case complexity of an algorithm.
It is denoted by Big Theta(θ)
It Represents in average no.of steps.
Θ (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1 * g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0}
Let g and f be the function from the set of natural numbers to itself.
The function f is said to be Θ(g),
if there are constants c1, c2 > 0 and a natural number n0
such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0
Big Oh Notation (Worst case (or)max time): -
Maximum time taken by the program to return the output once the input increases is called worst execution time.
Worst time complexity of the program.
n represents the input for the program
It is denoted by Big Oh(O)
It represents maximum no.of steps.
8|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
And represents the upper bound of running time of an algorithm.
Big O notation is helpful in finding the worst-case time complexity of a particular program with the help of huge input.
O(g(n)) = {f(n): there exits positive constants c and such that 0<=f(n)<=c g(n) for all n0<=n}
0 <= f(n)<= c g(n)
n0<=n
c, n are real numbers.
c>0
n0>=1 {minimum input 1}
then we Can represent f(n)= Og(n)
Time Complexity of swapping algorithm: - Space Complexity swapping algorithm: -
a=10; ----------→1 a-→1
b=20 ----------→1 b-→1
----------→ 0 temp-→1
temp=a; ----------→ 1 s(n)= 1+1+1=3
a=b: ----------→ 1 O(1)
b=temp ------ >1
}
Time Complexity of algorithm f(n)= 0+0+1+1+1+0 =3
Time Complexity of Algorithm addition of Array Numbers: -
Sum(a,b) ----------→0 Space Complexity of algorithm f(n)
{ ----------→0 a------ >n
s=0 ----------→ 1 i ----- > 1
for(i=0; i<n; i++) ----------→ n+1 n ----->1
1 n+1 n s ----- >1
s=s+a[i]; ----------→ n ---------------
} ----------→ 0 n+3
return s; ----------→ 1 ---------------
Space Complexity= O(n)
Time Complexity of algorithm f(n)= 0+0+1+(n+1) +n+0+1 =2n+3
= O(n)
9|V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
#include <stdio.h> ----------→0
int main() ----------→0
{
int arr[] = {1, 2, 3, 4, 5}; ----------→5
int sum = 0; ----------→0
int n = sizeof(arr); ----------→ 1
for(i=0; i<n; i++) ----------→ 5+1
1 5+1 5
{
sum = sum + arr[i]; ----------→ 5
}
printf(" %d" sum); ----------→ 1
return 0; ----------→0
}
Time Complexity of algorithm f(n)= 0+0+1+(5+1) +5+0+1 =13
= O(1)
Add(a,b,n) ---------- > 0 Time: - Space: -
{ ---------- >0 a-- > n x n
for(i=0; i<n; i++) ----- > n+1 f(n)=n+1+n2+n+n2 b-- > n x n
{ ------ > 0 =2n2+2n+1 c-- > n x n
for(j=0; j<n; j++) ----- > n(n+1) f(n)=O(n2) n-- > 1
{ i-- > 1
c[I,j] = a[I,j]+ b[i,j]; ----- > nn j-- > 1
} ------- >0 s(n) =3n2 +3
} ------- >0 = O(n2)
} ------- >0
Time Complexity: - Space Complexity: -
A --------- >n x n
for (i=0; i<n; i++) →n+1 B --------- >n x n
{ C --------- >n x n
n --------- >1
for (j=0; j<n; j++) -----------------→n (n+1) = n2+n i --------- >1
{ j --------- >1
c(i,j) = 0 ; ---------------------------→nxn=n2 k --------- >1
for (k=0; k<n; k++) ---------------→ nxnx (n+1) = n3 + n2 s(n) = 3n2 +4
{ s(n) = O(n2)
c (i,j)= c (i,j)+A(i, k) * (B(k, j) ---→nxnx(n+1) = n3+ n2
}
}
}
Total Time complexity for f(n) = 2n3+ 3n2+2n+1
worst case for matrix multiplication is f(n) = 0 (n3)
10 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
10_a) Apply the rotations on an AVL tree with a suitable example. 5M
AVL tree is a height-balanced binary search tree.
If, the difference between the heights of left and right subtrees of every node in the tree is either -1, 0 or +1,
Then that binary tree is said to be balanced. (or)
In an AVL tree, every node maintains an extra information known as balance factor.
In an AVL tree, balance factor of every node should either -1, 0 or +1.
The AVL tree was introduced in the year 1962 by G.M. Adelson-Velsky and E.M. Landis.
What is Balance Factor?
Balance factor of a node is the difference between the heights of the left and right subtrees of that node.
The balance factor of a node is calculated either height of left subtree - height of right subtree (OR) height of
right subtree - height of left subtree.
In the following explanation, we calculate as follows...
Balance factor = heightOfLeftSubtree - heightOfRightSubtree
Example of AVL Tree
The above tree is a binary search tree and every node is satisfying balance factor condition.
So this tree is said to be an AVL tree.
Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.
AVL Tree Rotations
11 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
In AVL tree, after performing operations like insertion and deletion we need to check the balance factor of every node
in the tree.
If every node satisfies the balance factor condition, then we conclude the operation otherwise we must make it
balanced.
Whenever the tree becomes imbalanced due to any operation, we use rotation operations to make the tree balanced.
Rotation operations are used to make the tree balanced.
Rotation is the process of moving nodes either to left or to right to make the tree balanced.
There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)
In LL Rotation, every node moves one position to left from the current position.
To understand LL Rotation, let us consider the following insertion operation in AVL Tree...
Single Right Rotation (RR Rotation)
In RR Rotation, every node moves one position to right from the current position.
To understand RR Rotation, let us consider the following insertion operation in AVL Tree...
12 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Left Right Rotation (LR Rotation)
The LR Rotation is a sequence of single left rotation followed by a single right rotation.
In LR Rotation, at first, every node moves one position to the left and one position to right from the current position.
To understand LR Rotation, let us consider the following insertion operation in AVL Tree...
Right Left Rotation (RL Rotation)
The RL Rotation is sequence of single right rotation followed by single left rotation.
In RL Rotation, at first every node moves one position to right and one position to left from the current position.
To understand RL Rotation, let us consider the following insertion operation in AVL Tree...
13 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Operations on an AVL Tree
The following operations are performed on AVL tree...
1. Search
2. Insertion
3. Deletion
Search Operation in AVL Tree
In an AVL tree, the search operation is performed with O(log n) time complexity.
The search operation in the AVL tree is similar to the search operation in a Binary search tree.
We use the following steps to search an element in AVL tree...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate the function
Step 4 - If both are not matched, then check whether search element is smaller or larger than that node value.
Step 5 - If search element is smaller, then continue the search process in left subtree.
Step 6 - If search element is larger, then continue the search process in right subtree.
Step 7 - Repeat the same until we find the exact element or until the search element is compared with the leaf
node.
Step 8 - If we reach to the node having the value equal to the search value, then display "Element is found" and
terminate the function.
Step 9 - If we reach to the leaf node and if it is also not matched with the search element, then display "Element is
not found" and terminate the function.
Insertion Operation in AVL Tree
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a 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 the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to be imbalanced. In this
case, perform suitable Rotation to make it balanced and go for next operation.
Example: Construct an AVL Tree by inserting numbers from 1 to 7.
14 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
15 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Deletion Operation in AVL Tree
The deletion operation in AVL Tree is similar to deletion operation in BST.
But after every deletion operation, we need to check with the Balance Factor condition.
If the tree is balanced after deletion go for next operation otherwise perform suitable rotation to make the tree
Balanced.
8)Show the max heap construction for the following sequence of numbers
19,43,37,31,33,4,35,33,45 5M
10_b) Build an algorithm to implement maxheap. 5M
Data Structure: A max heap can be efficiently implemented using an array. The indices of the left and right children of
a node at index i can be calculated as follows:
• Left child: 2i + 1
• Right child: 2i + 2
Operations:
1. Insert(element):
o Add the element to the end of the array.
o Heapify up to maintain the max heap property.
2. DeleteMax():
16 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
o Remove the root element (maximum value).
o Replace the root with the last element in the array.
o Heapify down to maintain the max heap property.
Heapify Functions:
• HeapifyUp(index):
o While the current element is greater than its parent:
▪ Swap the current element with its parent.
▪ Update the index to the parent's index.
• HeapifyDown(index):
o While the current element is less than its largest child:
▪ Find the index of the largest child.
▪ Swap the current element with the largest child.
▪ Update the index to the largest child's index.
11)Develop the insertion algorithm for max heap with an example 10M
A heap is a binary tree-based data structure that follows the heap property.
Heap means large amount of something.
Types of Heaps
There are two main types of heaps:
Min Heap: The root node contains the minimum value, and the values increase as you move down the tree.
Max Heap: The root node contains the maximum value, and the values decrease as you move down the tree.
17 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
In a min-heap smallest value is always at the root.
In a max-heap the largest value always at the root.
Heap Operations
Common heap operations are:
Insert: Adds a new element to the heap while maintaining the heap property.
Extract Max: Removes the maximum or minimum element from the heap and returns it.
Extract Min: Removes the maximum or minimum element from the heap and returns it.
Heapify is the process of creating a heap data structure from a binary tree.
Basic Steps for Heap Sort: -
Step1: -First convert the array into heap data structure.
Step2: -Then one by one delete the root node of the Max-heap and replace it with the last node in the heap.
Step3: -Repeat the following steps until the heap contains only one element
Example: -let’s take an unsorted array and try to sort it using heap sort.
Consider the array: arr[] = {4, 10, 3, 5, 1}.
Step1: -First convert the array into heap data structure.
1. Convert binary tree into max heap:
2. After Construct a tree from that unsorted array and try to convert it into max heap.
3. In MAX heap parent node should always be greater than or equal to the child nodes.
4. Here, in this example parent node 4 is smaller than the child node 10.
5. Then swap them to build a max-heap.
6. Now, 4 as a parent is smaller than the child 5 ,
7. Then swap both of these again and the result will explain below.
Step2: -Then one by one delete the root node of the Max-heap and replace it with the last node in the heap.
Now Delete the maximum element (root node) in each step
And then remaining elements transform it into a max heap.
18 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Delete the root element (10) from the max heap swap it with the last node,
After removing the root element, again heapify (convert it into max heap)
Resulted heap and array should look like this:
Step3: -Repeat the following steps until the heap contains only one element
Repeat the above steps and it will look like the following:
Now remove the root (i.e. 3) again and perform heapify.
Now when the root is removed once again it is sorted. and the sorted array will be like arr[] = {1, 3, 4, 5, 10} .
19 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
Output
Sorted array is
5 6 7 11 12 13
12) Develop the deletion algorithm for max heap with an example 10M
I've already provided a comprehensive response to that query. Here's a summary of the steps involved in the deletion
algorithm for a max heap:
1. Remove the root: Remove the root node, which contains the maximum value.
2. Replace with last: Replace the root with the last element in the heap.
3. Heapify:
o Sift down: Compare the new root with its children. If the new root is smaller than either child, swap it
with the larger child. Repeat this process until the heap property is restored.
Example:
Consider the following max heap:
45
/ \
43 37
/\/\
31 19 35 33
Deletion:
1. Remove the root: Remove the root node (45).
2. Replace with last: Replace the root with the last element (33).
3. Heapify:
20 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s
o Sift down: Compare 33 with its children (43 and 37). 33 is smaller than 43, so swap.
o Now, compare 33 with its new child (37). 33 is smaller than 37, so swap again.
Final Heap:
37
/ \
43 33
/\/\
31 19 35 null
By following these steps, you can effectively delete elements from a max heap while preserving its structure and
properties.
21 | V e n k i ’ s A d v a n c e D a t a S t r u c t u r e & A l g o r i t h m A n a l y s i s