0% found this document useful (0 votes)
25 views9 pages

Dsa Part-I

The document discusses various data structures and algorithms, highlighting the advantages of linked lists over arrays, performance measurement of algorithms, and the definition of concepts like adjacency matrix, balance factor, and types of trees. It also explains sorting techniques, types of graphs, and differences between arrays and structures. Additionally, it covers dynamic memory allocation functions and applications of stacks.
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)
25 views9 pages

Dsa Part-I

The document discusses various data structures and algorithms, highlighting the advantages of linked lists over arrays, performance measurement of algorithms, and the definition of concepts like adjacency matrix, balance factor, and types of trees. It also explains sorting techniques, types of graphs, and differences between arrays and structures. Additionally, it covers dynamic memory allocation functions and applications of stacks.
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/ 9

SET NUMBER:-1 [DSA]

Q1]

a) What are the Advantages of Linked List over an Array


1. Dynamic Size: Linked lists can grow and shrink in size as needed, unlike arrays, which
have a fixed size.
2. Efficient Insertions/Deletions: Inserting or deleting nodes in a linked list can be done
in constant time (O(1)), given a pointer to the node, while in an array, it can take O(n)
time in the worst case.
3. No Wasted Memory: Linked lists use memory more efficiently since they allocate
space as needed, whereas arrays may have unused allocated memory.
b) How to Measure Performance of an Algorithm
1. Time Complexity: Assess how the execution time of the algorithm changes with the
input size (e.g., Big O notation).
2. Space Complexity: Evaluate how much memory the algorithm uses relative to the
input size.
3. Empirical Analysis: Run the algorithm with various input sizes and measure actual
execution time and memory usage.
c) What is Adjacency Matrix?
An adjacency matrix is a square matrix used to represent a graph. Each cell at position (i, j)
indicates whether there is an edge between vertex i and vertex j. It’s typically used for
weighted graphs where the value in the matrix cell represents the weight of the edge.
d) What is Pointer to Pointer?
A pointer to pointer is a variable that stores the address of another pointer. It allows for multi-
level indirection, enabling the manipulation of pointers themselves. For example, in C/C++, int
**ptr is a pointer to a pointer to an integer.
e) What is a Complete Binary Tree?
A complete binary tree is a type of binary tree in which all levels, except possibly the last, are
fully filled, and all nodes are as far left as possible. This means every level must be filled from
left to right.
f) What is a Polynomial? How is it Different from Structure?
A polynomial is a mathematical expression consisting of variables (indeterminates) raised to
whole-number exponents and coefficients. For example, P(x)=3x2+2x+1P(x) = 3x^2 + 2x +
1P(x)=3x2+2x+1. A structure, on the other hand, is a composite data type in programming
languages (like C) that groups different data types into a single unit. Polynomials can be
represented as structures in code, but they are fundamentally different concepts.
g) What is a Priority Queue?
A priority queue is an abstract data type similar to a regular queue but with an added feature:
each element has a priority assigned to it. Elements with higher priority are dequeued before
elements with lower priority. If two elements have the same priority, they are typically served
according to their order in the queue.
h) Difference Between Stack & Linked List
1. Structure: A stack is a linear data structure that follows the Last In First Out (LIFO)
principle, while a linked list is a collection of nodes where each node points to the
next.
2. Access: In a stack, you can only access the top element, whereas in a linked list, you
can access any node by traversing from the head.
3. Operations: Stacks have push and pop operations, while linked lists support various
operations like insert, delete, and traverse.

i) What is the Need for the Header


In data structures like linked lists, a header (or header node) is used to simplify operations by
providing a reference point for the list. It can store metadata (like size) and make operations
like insertion or deletion at the beginning easier.
j) What is Balance Factor? How is it Calculated?
The balance factor is used in AVL trees to determine the balance of a node. It is calculated as
the difference between the heights of the left and right subtrees:
Balance Factor=Height of Left Subtree−Height of Right Subtree\text{Balance Factor} =
\text{Height of Left Subtree} - \text{Height of Right
Subtree}Balance Factor=Height of Left Subtree−Height of Right Subtree A balance factor of -1,
0, or 1 indicates the tree is balanced. If the factor is outside this range, the tree may require
rebalancing.

Q2]
a) What is Height-Balanced Tree? Explain RR and RL rotations with an example
A height-balanced tree, often referred to as an AVL tree, is a binary tree in which the height
difference between the left and right subtrees of any node is at most one. This property
ensures that the tree remains balanced, leading to efficient search, insertion, and deletion
operations.
Rotations:
• RR Rotation (Right-Right Rotation): This occurs when a node is inserted into the right
subtree of the right child of a node, causing a right-heavy imbalance.
Example:
css
Copy code
A
\
B
\
C
After inserting C, the tree becomes unbalanced. A single left rotation at A will balance it:
css
Copy code
B
/\
A C
• RL Rotation (Right-Left Rotation): This occurs when a node is inserted into the left
subtree of the right child of a node, causing a right-heavy imbalance.
Example:
css
Copy code
A
\
B
/
C
After inserting C, the tree is unbalanced. We perform a right rotation on B, followed by a left
rotation on A:
css
Copy code
C
/\
A B
b) What is a Linked List? Explain its types in detail
A linked list is a linear data structure where elements, called nodes, are stored in separate
memory locations. Each node contains data and a reference (or pointer) to the next node in
the sequence.
Types of Linked Lists:
1. Singly Linked List:
o Each node has a single pointer to the next node.
o Operations like insertion and deletion can be done in O(1) time at the head.
o Traversal is one-way.
Structure:
c
Copy code
struct Node {
int data;
struct Node* next;
};
2. Doubly Linked List:
o Each node contains two pointers: one to the next node and one to the previous
node.
o Allows traversal in both directions and makes deletion easier (if we have a
reference to a node).
Structure:
c
Copy code
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
3. Circular Linked List:
o The last node points back to the first node, forming a circle.
o Can be singly or doubly linked.
o Useful for applications where the end of the list should link back to the
beginning.

c) Explain Different Types of Asymptotic Notation in detail


Asymptotic notation is used to describe the performance or complexity of an algorithm in
terms of time or space as the input size grows.
1. Big O Notation (O):
o Describes the upper bound of the time complexity.
o Provides a worst-case scenario.
o Example: O(n) means that in the worst case, the time grows linearly with the
input size.
2. Omega Notation (Ω):
o Describes the lower bound of the time complexity.
o Provides the best-case scenario.
o Example: Ω(n) indicates that the time will at least grow linearly with the input
size.
3. Theta Notation (Θ):
o Describes the exact bound of the time complexity.
o It is used when the upper and lower bounds are the same.
o Example: Θ(n) means the time complexity grows linearly in both best and
worst cases.
d) Insertion Sort Technique
Insertion sort is a simple sorting algorithm that builds a sorted array (or list) one element at a
time. It’s efficient for small data sets and works similarly to sorting playing cards.
Steps:
1. Start with the second element (the first element is considered sorted).
2. Compare it with elements in the sorted portion (left side) and insert it in the correct
position.
3. Repeat until the entire list is sorted.
Example: Given the array: [5, 2, 9, 1, 5]
• Start with the second element 2. Compare with 5 and insert it before 5.
o Array: [2, 5, 9, 1, 5]
• Next is 9. It’s already in the correct place.
o Array: [2, 5, 9, 1, 5]
• Next is 1. Compare with 9, 5, and 2 and insert it at the start.
o Array: [1, 2, 5, 9, 5]
• Finally, 5. Insert it after the first 5.
o Array: [1, 2, 5, 5, 9]

e) Differentiate Array and Structure

Feature Array Structure

A collection of elements of the A composite data type that groups different


Definition
same type. data types.

Fixed size (must be defined at Dynamic size (can contain varying types and
Size
declaration). sizes).

Elements accessed using an Members accessed using dot (.) operator or


Access
index. arrow (->) for pointers.

Memory Contiguous memory


Non-contiguous memory allocation.
Allocation allocation.

Types of
Must be of the same data type. Can contain different data types.
Elements

SET NUMBER:-2 [DSA]


Q1]

a) How to Measure Performance of an Algorithm


Performance can be measured in terms of time complexity (how the runtime increases with
input size) and space complexity (how the memory requirement increases). Commonly, Big
O notation is used to express these complexities, providing an upper bound on the growth rate
of the algorithm's running time or space requirement.

b) What is polynomial? How is it different from structure


A polynomial is a mathematical expression involving variables raised to non-negative integer
powers, such as P(x)=anxn+an−1xn−1+…+a1x+a0P(x) = a_nx^n + a_{n-1}x^{n-1} + \ldots +
a_1x + a_0P(x)=anxn+an−1xn−1+…+a1x+a0. In contrast, a structure (in programming) is a
composite data type that groups variables under a single name, allowing different data types
to be combined, like in C using the struct keyword.

c) What is Balance Factor? How is it calculated


The balance factor is used in AVL trees to maintain balance. It is calculated as the height of
the left subtree minus the height of the right subtree for a given node:
Balance Factor=Height(left subtree)−Height(right subtree)\text{Balance Factor} =
\text{Height(left subtree)} - \text{Height(right
subtree)}Balance Factor=Height(left subtree)−Height(right subtree) A balance factor of -1, 0,
or 1 indicates the tree is balanced.
d) What are Abstract Data Types (ADTs)
ADTs are theoretical concepts that define data types purely in terms of their behavior
(operations) rather than their implementation. Examples include stacks, queues, and lists.
ADTs specify what operations can be performed and their effects, but not how those
operations are implemented.

e) What is Ancestor of Node


An ancestor of a node in a tree is any predecessor node on the path from the root to that
node. This includes the node's parent, grandparent, and so on, up to the root.
f) Types of Graph
Common types of graphs include:
1. Directed Graph (digraph): edges have a direction.
2. Undirected Graph: edges do not have a direction.
3. Weighted Graph: edges have weights or costs.
4. Unweighted Graph: edges are unweighted.
5. Cyclic Graph: contains cycles.
6. Acyclic Graph: does not contain cycles.
7. Complete Graph: every pair of vertices is connected.
g) Array vs Structure
• Array: A collection of elements of the same type, accessed by indices. Arrays are fixed
in size.
• Structure: A composite data type that can contain variables of different types,
allowing for more complex data organization.
h) What is Space and Time Complexity
• Time Complexity: A measure of the amount of time an algorithm takes to complete as
a function of the length of the input (often expressed in Big O notation).
• Space Complexity: A measure of the amount of memory space required by an
algorithm in relation to the input size.
i) Pointer to Pointer
A pointer to a pointer is a variable that stores the address of another pointer. It allows for
multi-level indirection, useful in scenarios like dynamic memory allocation or creating data
structures such as linked lists.

j) What is Spanning Tree


A spanning tree of a graph is a subgraph that includes all the vertices and is a tree (connected
and acyclic). It has exactly V−1V - 1V−1 edges, where VVV is the number of vertices in the
graph. A minimum spanning tree has the smallest possible sum of edge weights.

Q2]

a) Explain Insertion Sort Technique with an example


Insertion Sort is a simple sorting algorithm that builds a sorted array (or list) one item at a
time. It works by comparing each new element with the already sorted elements and inserting
it into the correct position.
Example: Consider the array: [5, 2, 4, 6, 1, 3]

1.
[2, 5, 4, 6, 1, 3].

2. [2, 4, 5, 6, 1, 3].
3. [2, 4, 5, 6, 1, 3].

4. [1, 2, 4, 5, 6, 3].

5. [1, 2, 3, 4, 5, 6].
Time Complexity: O(n^2) in the worst case; O(n) in the best case.

b) What is Circular Queue? How it is different from Static Queue


A circular queue is a linear data structure that connects the last position back to the first
position, effectively making the queue circular. This allows for efficient use of space since it
can overwrite old data when space is needed.
Differences from Static Queue:
• Static Queue: Has a fixed size; when it reaches capacity, it cannot accept new
elements until some are dequeued. It may also suffer from the "wasted space"
problem if elements are dequeued from the front.
• Circular Queue: Also has a fixed size but wraps around when it reaches the end,
efficiently using available space.

c) What is Stack? What are the various applications of stack


A stack is a linear data structure that follows the Last In First Out (LIFO) principle. Elements
can only be added or removed from the top of the stack.
Applications of Stack:
1. Function call management (call stack).
2. Expression evaluation and parsing (e.g., converting infix to postfix).
3. Backtracking algorithms (e.g., maze solving).
4. Undo mechanisms in software applications.
Operations Performed on Stack:
• Push: Add an element to the top.
• Pop: Remove the top element.
• Peek/Top: View the top element without removing it.
• isEmpty: Check if the stack is empty.
e) Dynamic Memory Allocation Functions
In C/C++, dynamic memory allocation is handled through several standard library functions:
1. malloc(size_t size): Allocates a block of memory of specified size. It returns a pointer
to the allocated memory. Memory is not initialized.
2. calloc(size_t num, size_t size): Allocates memory for an array of num elements, each
of size bytes. Memory is initialized to zero.
3. *realloc(void ptr, size_t size)**: Resizes a previously allocated memory block. If the
new size is larger, it may allocate a new block and copy the old data.
4. *free(void ptr)**: Deallocates the memory previously allocated by malloc, calloc, or
realloc, freeing up space.
Dynamic memory allocation is crucial for creating flexible and efficient data structures in
programs.

You might also like