Rajeev
Rajeev
Big O (O-notation):
Definition: It provides a lower bound on the growth rate of a function. It describes the
best-case scenario or the minimum time complexity.
Definition: It provides both an upper and a lower bound on the growth rate of a
function. It describes the tight bound on the asymptotic behavior.
In postfix notation (RPN), operators come after operands, inherently preserving the correct
order of operations without needing parentheses (e.g., A B + C × always means (A + B)
× C).
Thus, parentheses are essential in infix but unnecessary in postfix, making postfix easier
for computer evaluation (e.g., in stack-based computations).
Good pivot (balanced split): Leads to O(nlogn)O(n \log n)O(nlogn) time, as the
array is evenly divided.
Bad pivot (extreme split): Leads to O(n2)O(n^2)O(n2) time, as the array is poorly
divided (one subarray is nearly empty).
Average pivot (random/median): Typically results in O(nlogn)O(n \log
n)O(nlogn) time, balancing the array on average
1. Open Addressing: Collisions are resolved by probing for an empty slot within the
table itself (e.g., linear or quadratic probing).
2. Chaining: Collisions are handled by storing multiple elements at the same index
using linked lists or other structures.
Optimal encoding: Shorter codes for frequent characters, longer for rare.
Prefix-free codes: Ensures no code is a prefix of another.
Efficient decoding: Traversal from root to leaf to decode characters.
Optimal compression: Builds a tree based on character frequencies.
In a regular graph of degree d and n vertices, each vertex has exactly d edges. To find the
total number of edges:
1. Total degree of the graph: Since each of the n vertices has degree d, the total degree
of the graph is n×d.
2. Number of edges: Since each edge is counted twice (once at each endpoint), the total
number of edges E is
E = (n*d)/2
So, the number of edges in a regular graph of degree d and n vertices is (n*d)/2.
Q(e ).Write the algorithm to obtain the connected components of
the graph?
Initialize components = []
For v = 0 to V-1:
If visited[v] is False:
Initialize component = []
Return components
Add v to component
If visited[u] is False:
4. Example Execution
0 -- 1 3 -- 4
| |2 5
Adjacency List:
0 -> 1, 2
1 -> 0
2 -> 0
3 -> 4, 5
4 -> 3
5 -> 3
Step-by-Step Execution:
Component 1: {0, 1, 2}
Component 2: {3, 4, 5}
SECTION – B
Q 2(a) : Write a Pseudo code that will concatenate two linked
lists. Function should have two parameters, pointers to the
beginning of the lists and the function should link second list at
the end of the first list.
Function concatenateLists(list1, list2):
// list1 is Empty
if list1 is NULL:
return list2
// list2 is Empty
if list1 is NULL:
return list1
current = list1
current = current.next
// Link the last node of the first list to the second list
current.next = list2
return list1
if symbol is an operand:
return postfix
1. Secondary Clustering: Can still suffer from secondary clustering (when two keys
follow the same probe sequence).
2. Table Size Restrictions: Requires the table size to be a prime number for efficient
probing.
3. Deletion Complexity: Deletion remains complex due to breaking probe chains.
Ans .
#include <stdio.h>
#include <stdlib.h>
// Definition for a binary tree node
struct Node {
int data;
struct Node *left, *right;
};
struct Stack {
struct Node* data;
struct Stack* next;
};
push(&stack1, root);
while (!isEmpty(stack1)) {
struct Node* node = pop(&stack1);
push(&stack2, node);
if (node->left)
push(&stack1, node->left);
if (node->right)
push(&stack1, node->right);
}
while (!isEmpty(stack2)) {
struct Node* node = pop(&stack2);
printf("%d ", node->data);
}
}
//main function
int main() {
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
return 0;
}
= 2000+[(10−1)×50+(10−1)]×4
= 2000+[(9×50)+9]×4
= 3836
= 2000+[(10−1)×20+(10−1)]×4
= 2000+[(9×20)+9]×4
= 2756
Each node of the linked list represents a term of the polynomial and consists of:
CopyEdit
struct Node {
int coefficient;
int exponent;
};
css
CopyEdit
Each node contains coefficient & exponent, and they are linked together.
1.)Insertion of Terms
Example: Evaluate 3 * (5 - 3)
Infix: 3 * (5 - 3)
Postfix: 3 5 3 - *
1. 3 3 Push 3
2. 5 5, 3 Push 5
3. 3 3, 5, 3 Push 3
Final result = 6 ✅
A double-ended queue (deque) is a linear data structure that allows insertion and
deletion from both ends (front and rear). It can be implemented using an array or a linked
list.
Array Representation
vbnet
CopyEdit
Index: 0 1 2 3 4 (SIZE = 5)
Deque: | 10 | 20 | 30 | 40 | 50 |
Types of Deque:
Input-restricted deque: Insert at rear only, delete from both ends.
Output-restricted deque: Delete from front only, insert at both ends.
Operations on Deque
1 Insert an element at the front
#include <stdio.h>
#include <stdlib.h>
struct Deque {
int arr[SIZE];
};
dq->front = -1;
dq->rear = -1;
// Insert at front
if (isFull(dq)) {
return;
if (isEmpty(dq)) {
dq->front = dq->rear = 0;
} else {
dq->arr[dq->front] = value;
// Insert at rear
if (isFull(dq)) {
return;
if (isEmpty(dq)) {
dq->front = dq->rear = 0;
} else {
dq->arr[dq->rear] = value;
if (isEmpty(dq)) {
return;
if (dq->front == dq->rear) {
} else {
if (isEmpty(dq)) {
printf("Deque is empty! Cannot delete from rear.\n");
return;
if (dq->front == dq->rear) {
} else {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return;
int i = dq->front;
while (1) {
if (i == dq->rear)
break;
i = (i + 1) % SIZE;
printf("\n");
//main function
int main() {
creationDeque(&dq);
insertRear(&dq, 10);
insertRear(&dq, 20);
insertFront(&dq, 5);
insertFront(&dq, 2);
display(&dq);
deleteFront(&dq);
deleteRear(&dq);
display(&dq);
return 0;
Advantages of Deque :
Applications:
1. CPU scheduling
2. Cache memory management
3. Sliding window problems
4. Undo-redo operations in text editors
Q 5(a) : Write a C program for sorting 100 integer
numbers wring selection sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Main function
int main() {
int arr[SIZE];
generateRandomNumbers(arr, SIZE);
printf("Unsorted Array:\n");
displayArray(arr, SIZE);
selectionSort(arr, SIZE);
return 0;
}
Example Output
Unsorted Array:
1. Algorithm:
1. Find the smallest element.
2. Swap it with the first unsorted element.
3. Repeat until the array is sorted.
2. Time Complexity:
1. Best Case: O(n²)
2. Worst Case: O(n²)
3. Average Case: O(n²)
5. Disadvantages:
✅ Conclusion
Selection Sort is a simple sorting algorithm best suited for small datasets.
It has O(n²) time complexity, making it inefficient for large datasets.
Alternative algorithms like Merge Sort and Quick Sort are better for larger inputs.
ANS . Definition: Binary search is a fast searching algorithm that works on sorted arrays
using a divide and conquer approach.
1. Algorithm Steps:
1. Find midpoint.
2. Compare the key with the mid element.
3. If not found, search left or right half.
4. Repeat until element is found or array is exhausted.
2. Time Complexity:
5. Applications:
✅ Conclusion
Binary Search is one of the fastest searching algorithms with O(log n) time complexity.
It requires sorted data and works efficiently for large datasets.
Best choice for search operations in sorted arrays.
#include <stdio.h>
if (arr[mid] == key)
return mid;
left = mid + 1;
else
right = mid - 1;
printf("\n");
// Main function
int main() {
int arr[] = {5, 12, 19, 23, 32, 45, 56, 78, 91}; // Sorted array
int key;
displayArray(arr, n);
scanf("%d", &key);
if (result != -1)
else
printf("\nElement %d not found in the array\n", key);
return 0;
For a binary tree with nnn internal nodes, it has n-1 external nodes. This is derived from
the fact that in a full binary tree, the number of external nodes is one more than the number
of internal nodes.
Each internal node contributes 1 extra depth to its children, causing a shift in path length
between internal and external nodes.
3. Derivation of E = I + 2n
Let’s consider how the path length changes when moving from internal nodes to external
nodes.
E = I + 2n
This equation holds because every internal node adds 2 units (one per child) to the
external path length in addition to its own depth.
1.
4. Example to Illustrate
/ \
B C
/ \ / \
D E F G
o Depth of A = 0
o Depth of B, C = 1
o I=0+1+1=2
o Depth of D, E, F, G = 2
o E=2+2+2+2=8
Verifying : E = I + 2n
8=2+2X3
=2+6
8= 8
✅ Correct!
Q6(b) : Suppose character a,b,c,d,e,f probabilties
has 0.07 ,0.09 , 0.12 ,0.22 , 0.23 , 0.27 respectively.
Find tne optional huffman code and draw huffman
tree . What is the average code of length?
Compute the Average Code Length
where:
= 2.44
an adjacency matrix.
ANS.
#include <stdio.h>
return 0;
}
Example Run:
Input:
0 1 0 1
0 0 1 0
1 0 0 0
0 1 1 0
Output:
0 1 2
1 2 1
2 2 1
3 1 1
This program efficiently calculates the indegree and outdegree for all vertices in O(V²) time
complexity, where V is the number of vertices.