DS Unit 1 - Brief Solution of Important Questions
DS Unit 1 - Brief Solution of Important Questions
Session: 2022-23
Answer:
Asymptotic Notations:
• Asymptotic Notations are mathematical tools used to analyse the performance of algorithms
by understanding how their efficiency changes as the input size grows.
• These notations provide a concise way to express the behaviour of an algorithm’s time or space
complexity as the input size approaches infinity.
• Rather than comparing algorithms directly, asymptotic analysis focuses on understanding the
relative growth rates of algorithms’ complexities.
• By using asymptotic notations, such as Big O, Big Omega, and Big Theta, we can categorize
algorithms based on their worst-case, best-case, or average-case time or space complexities,
providing valuable insights into their efficiency.
Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it gives
the worst-case complexity of an algorithm.
If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive constant C
and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
It returns the highest possible output value (big-O) for a given input. The execution time serves as
an upper bound on the algorithm’s time complexity.
Mathematical Representation of Big-O Notation:
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
Example:
f(n)=2n2+n
2n2+n<= C g(n)
Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides
the best-case complexity of an algorithm.
The execution time serves as a lower bound on the algorithm’s time complexity.
It is defined as the condition that allows an algorithm to complete statement execution in the
shortest amount of time.
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 is a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
Example:
f(n)=2n2+n
2n2+n>= C g(n)
So we can write as f(n)= Ω(n2) but for all n>=0 and C=2
3. Theta Notation (Θ-Notation):
Theta notation encloses the function from above and below. Since it 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.
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
Theta notation
Θ (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}
The execution time serves as both a lower and upper bound on the algorithm’s time complexity.
It exists as both, most, and least boundaries for a given input value.
Example:
f(n)=2n2+n
So we can write as f(n)= Θ (n2) but for all n>=0 and C1=2 and C2=3
Answer
o For each iteration of the outer loop (value of i), the inner loop runs from j = 1 to j = i.
o Hence, the number of iterations of the inner loop depends on the value of i:
...
Time Complexity:
o Ignoring constant factors and lower-order terms, the time complexity is: O(n2)
Final Answer:
Answer
A trade-off is a situation where one thing increases and another thing decreases. It is a way to
solve a problem in:
The best Algorithm is that which helps to solve a problem that requires less space in memory
and takes less time to generate the output. But in general, it is not always possible to achieve
both conditions at the same time.
Linear search checks each element in the array sequentially until the target is found or the end of the
array is reached.
Characteristics:
Binary search requires a sorted array. It repeatedly divides the array into halves and checks the
middle element to determine whether to search in the left or right half.
Characteristics:
2. Space Complexity:
3. Preprocessing Cost: Sorting the array takes O(nlogn) which adds an upfront cost if the array
is unsorted.
Time-Space Trade-off
1. Linear Search:
2. Binary Search:
o Time: Faster (O(logn)
o Space: Requires extra space and/or time for sorting if the array isn't already sorted.
o Sorting the array can add O(nlogn) preprocessing time and may require O(n)
additional space for temporary storage.
4. Derive the formula to calculate the memory location of an element in Row Major Order and
Column Major Order. Session: 2020-21
Answer
Row Major Address Calculation:
Answer
Row Major:
B = Base address,
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero),
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution:
Address of A[2][1] = 200 + 4 * ((2 – 0) * 3 + (1 – 0))
= 200 + 4 * (6 + 1))
= 200+28
Address of A[I][J] = 228
Function CREATE_LIST(data[]):
new_node.data ← value
new_node.next ← NULL
head ← new_node
Else:
temp ← head
temp ← temp.next
End While
temp.next ← new_node
End If
End For
End Function
1. At Beginning
new_node.data ← data
new_node.next ← head
head ← new_node
End Function
2. In the end
Function INSERT_END(data):
new_node= (struct Node*)malloc(sizeof(struct Node))
new_node.data ← data
new_node.next ← NULL
head ← new_node
Else:
temp ← head
temp ← temp.next
End While
temp.next ← new_node
End If
End Function
3. At a position
If pos = 1:
new_node.data ← data
new_node.next ← head
head ← new_node
Else:
new_node.data ← data
temp ← head
count ← 1
temp ← temp.next
count ← count + 1
End While
If temp = NULL:
Else:
new_node.next ← temp.next
temp.next ← new_node
End If
End If
End Function
1. From Beginning
Function DELETE_BEGINNING():
If head = NULL:
Else:
temp ← head
head ← head.next
Free temp
End If
End Function
2. From End
Function DELETE_END():
If head = NULL:
Print "List is empty"
Else If head.next = NULL: // Single node in the list
Free head
head ← NULL
Else:
temp ← head
While temp.next.next ≠ NULL:
temp ← temp.next
End While
Free temp.next
temp.next ← NULL
End If
End Function
If head = NULL:
Else If pos = 1:
Call DELETE_BEGINNING()
Else:
temp ← head
count ← 1
temp ← temp.next
count ← count + 1
End While
Else:
to_delete ← temp.next
temp.next ← to_delete.next
Free to_delete
End If
End If
End Function
7. Represent the polynomial 4x3+3x2+ 5 using a linked list. Session: 2019-20 ,2022-23, 2023-24
Answer
To represent a polynomial 4x3+3x2+ 5 using a linked list, each node will store:
1. The coefficient of the term.
2. The exponent of the term.
3. A pointer to the next node (to link the terms together).
Representation
• Each term of the polynomial is a node in the linked list.
• The terms are stored in decreasing order of their exponents.
Node Structure
Each node contains:
plaintext
Copy code
struct Node {
int coefficient;
int exponent;
Node* next;
};
Answer
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, meaning
the last element added to the stack is the first one to be removed.
The Abstract Data Type (ADT) defines the behaviour of a stack in terms of its operations
without specifying the implementation details.