0% found this document useful (0 votes)
21 views13 pages

Dsa Model

Uploaded by

ebarath20061
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)
21 views13 pages

Dsa Model

Uploaded by

ebarath20061
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/ 13

1) How is an algorithm analyzed for predicting efficiency?

• By examining its time complexity (how execution time increases with


input size) and space complexity (how memory usage increases).
• Typically expressed using asymptotic notations like Big O (O), Big Ω
(Ω), and Big Θ (Θ).

2) Construct the recurrence tree for the equation T(n) = aT(n/b) + f(n).
• The recurrence tree has a subproblems, each of size n/b. The cost at each
level of the tree is f(n).
The depth of the tree is log_b(n), and the total cost is O(n^log_b(a)) if f(n) is
polynomial or O(f(n)) if it dominates.

3) Create a routine for operations as push() and pop() in the stack.


• push(): Adds an element to the top of the stack by incrementing the top
index and inserting the element at that position.
• pop(): Removes and returns the top element by decrementing the top
index.
stack[++top] = value; // Push
stack[top--]; // Pop

4) Define Queue. Represent the Queue data structure.


• A queue is a linear data structure that follows the First-In-First-Out
(FIFO) principle, where the first element added is the first one to be
removed.
• Representation in C:
struct Queue {
int items[MAX];
int front, rear;
};
5) How many passes are required to perform the insertion sort for the
listed elements?
(8, 23, 32, 36, 45, 78)
• Since the array is already sorted, Insertion Sort will make 5 passes for
an array of 6 elements (n-1 passes).
• During each pass, the algorithm compares the element with its
predecessor but no swaps are needed.

6) Which Sorting techniques have the time complexity of O(n log n)?
• Quick Sort: Best and average case time complexity is O(n log n).
• Merge Sort: Has a time complexity of O(n log n) for all cases due to its
divide-and-conquer approach.

7) Differentiate DFS and BFS.


• DFS (Depth First Search): Explores as far down a branch as possible
before backtracking. Uses a stack or recursion, ideal for pathfinding and
maze-solving.
• BFS (Breadth First Search): Explores all neighbors at the present depth
before moving on to nodes at the next depth level. Uses a queue, ideal for
finding the shortest path.

8) Prove that the maximum number of edges that a graph with n


vertices is n(n-1)/2.*
• A Graph with n vertices can have an edge between any two distinct
vertices. In an undirected graph, the maximum number of such edges is
given by the combination of 2 vertices from n, i.e., C(n, 2).
• Formula:
Maximum Edges= n(n−1)/2
9) Illustrate when the AVL tree is said to be imbalanced.
• An AVL Tree becomes imbalanced when the heights of two child
subtrees of any node differ by more than 1.
This imbalance can occur due to insertions or deletions, and it is corrected by
performing AVL tree rotations

10) What do you mean by linear probing?


• Linear Probing is a collision resolution technique in open addressing
where, upon collision, the next available slot in the hash table is checked
sequentially.
• It is simple to implement but can lead to clustering, slowing down
insertion and search operations.

11)a) Tower of Hanoi


12a) Queue ADT: Definition and Characteristics
1. Definition:
A Queue is an Abstract Data Type (ADT) that follows the First-In-First-
Out (FIFO) principle. This means that the element added first will be the
first one to be removed, much like a line of people waiting where the first
person in line is the first to be served. Queue is used in various real-life
scenarios and programming applications such as task scheduling,
buffering streams, and managing processes in an operating system.
2. Characteristics:
o FIFO Principle: Only two operations—enqueue and dequeue—
can be performed. Enqueue adds elements to the rear, while
dequeue removes elements from the front.
o Sequential Access: Unlike stacks (LIFO), elements are accessed in
the order they were added.
o Two Endpoints: A queue has a front (where elements are
dequeued) and a rear (where elements are enqueued).
o No Direct Access to Elements: Elements can only be accessed in
the sequence they are added.
Operations on Queue ADT
1. Enqueue (Insert):
o Purpose: Adds an element to the rear of the queue.
o Mechanism: The rear pointer (or index in an array) is updated to
the next position, and the element is stored there.
o Condition: In array-based implementations, if the queue is full,
this operation raises an overflow error.
2. Dequeue (Remove):
o Purpose: Removes the front element from the queue.
o Mechanism: The front pointer (or index in an array) is updated to
the next element, effectively removing the first-in element.
o Condition: If the queue is empty, a dequeue operation raises an
underflow error.
3. Peek (Front):
o Purpose: Allows accessing the front element without removing it.
o Use Case: Useful for checking the next element in line for
processing.
4. IsEmpty:
o Purpose: Checks if the queue is empty by verifying whether the
front pointer is null (linked list) or if front equals rear (array-
based).
o Returns: True if the queue is empty, false otherwise.
5. IsFull (For Array-Based Implementations):
o Purpose: Determines if the queue has reached its maximum
capacity.
o Condition: If using a static array, it prevents the enqueue operation
when the array is full.
o Linked List Queues: In linked list-based queues, there is no need
for an isFull check as they are dynamically resizable based on
available memory.

Linked List Implementation of Queue


1. Structure of Linked List-Based Queue:
o Nodes: Each node in the linked list has two fields:
▪ Data: Holds the element value.
▪ Next Pointer: Points to the next node in the queue.
o Front and Rear Pointers: To manage queue operations efficiently,
we maintain two pointers:
▪ Front: Points to the node where elements are dequeued.
▪ Rear: Points to the node where elements are enqueued.
2. Advantages of Linked List-Based Queue:
o Dynamic Size: Unlike array-based queues, linked lists don’t have a
fixed size and can grow or shrink as needed.
o No Overflow (limited only by memory): As long as memory is
available, new elements can be added without overflow, unlike
array-based implementations where overflow occurs when the
array reaches its capacity.
o Efficient Memory Utilization: Only memory for current elements
is used, as memory for nodes is allocated dynamically.
3. Operations in Linked List Queue:
o Enqueue Operation:
▪ Steps:
▪ Create a new node with the desired data.
▪ If the queue is empty (front and rear are null), set both
the front and rear pointers to this new node.
▪ If the queue is not empty, link the current rear node to
this new node, and then update the rear pointer to this
new node.
▪ Complexity: O(1), as only pointer adjustments are needed.
o Dequeue Operation:
▪ Steps:
▪ Check if the queue is empty. If it is, return an
underflow message.
▪ Retrieve the data from the front node.
▪ Move the front pointer to the next node and delete the
old front node.
▪ If the front becomes null after deletion, set the rear to
null as well (indicating an empty queue).
▪ Complexity: O(1), as it only involves pointer updates.
o Peek Operation:
▪ Purpose: Returns the data from the front node without
removing it, allowing inspection of the front element.
▪ Condition: Only valid if the queue is not empty.
o IsEmpty Operation:
▪ Purpose: Checks if the front pointer is null, indicating an
empty queue.
4. Complexity Analysis:
o Time Complexity:
▪ Enqueue and Dequeue operations are O(1) because they
involve only pointer updates, regardless of the number of
elements.
▪ Peek and IsEmpty are also O(1) since they involve direct
pointer access.
o Space Complexity: The space complexity is O(n), where n is the
number of elements in the queue. Each element requires space for
the data and a pointer to the next element.
13) a) Binary search
https://www.geeksforgeeks.org/binary-search/
https://www.w3schools.com/dsa/dsa_algo_binarysearch.php

14) b)
(i)
https://www.tutorialspoint.com/data_structures_algorithms/tree_traversal.htm

(ii) A Binary Tree Data Structure is a hierarchical data structure in which each
node has at most two children, referred to as the left child and the right child. It
is commonly used in computer science for efficient storage and retrieval of data,
with various operations such as insertion, deletion, and traversal.

Complete Binary Tree


A Binary Tree is a Complete Binary Tree if all the levels are completely filled
except possibly the last level and the last level has all keys as left as possible.
A complete binary tree is just like a full binary tree, but with two major
differences:
• Every level except the last level must be completely filled.
• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a complete binary
tree doesn’t have to be a full binary tree.

Full Binary Tree


A Binary Tree is a full binary tree if every node has 0 or 2 children. The
following are examples of a full binary tree. We can also say a full binary tree is
a binary tree in which all nodes except leaf nodes have two children.
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children. It is also known as a proper
binary tree.
15 b) Linear probing and quadratic probing

You might also like