0% found this document useful (0 votes)
3 views10 pages

Dsaa - 2 Marks With Answers

The document covers various data structures and algorithms, detailing concepts such as stacks, queues, binary trees, and sorting techniques. It explains principles like Last In, First Out (LIFO) for stacks, and the differences between data structures like binary trees and binary search trees. Additionally, it discusses advanced topics like dynamic programming, backtracking, and graph algorithms, providing examples and applications for each concept.

Uploaded by

dhivagarms53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Dsaa - 2 Marks With Answers

The document covers various data structures and algorithms, detailing concepts such as stacks, queues, binary trees, and sorting techniques. It explains principles like Last In, First Out (LIFO) for stacks, and the differences between data structures like binary trees and binary search trees. Additionally, it discusses advanced topics like dynamic programming, backtracking, and graph algorithms, providing examples and applications for each concept.

Uploaded by

dhivagarms53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT- I - ABSTRACT DATA TYPES AND LINEAR DATA STRUCTURES

PART A ( 2 Marks)

1. What is the basic principle of a stack data structure?


The stack data structure implements and follows the Last In, First Out (LIFO) principle. It implies
that the element that is inserted last comes out first. The process of inserting an element in the
stack is known as the push operation. Here, the deletion of elements is known as the pop operation.
2. How does a circular queue differ from a linear queue?
In linear queue, insertion is done from the rear end, and deletion is done from the front end. In
circular queue, the insertion and deletion can take place from any end. The memory space
occupied by the linear queue is more than the circular queue.
3. Differentiate Double Ended Queue and Queue.
Queue:
 Elements are inserted at the rear and removed from the front, adhering to the FIFO
principle.
 Think of it like a line where people enter at the back and are served from the front.
 Operations are restricted to insertion at the rear and removal from the front.
Double-Ended Queue (Deque):
 Elements can be added or removed from both the front and the rear.
 This flexibility makes deques more versatile than standard queues, allowing them to
function as both queues and stacks.
 Deques are useful in applications where you need to process data from either end, such as
in text editors with undo/redo functionality.
4. What is an infix expression?
An infix expression is a mathematical expression where the operator is placed between the two
operands.
Eg: "A + B", "(A * B) / C".
5. Convert the following infix expression to postfix notation using a stack: (A + B) * C

6. Differentiate Stack and Queue.


7. What conversion is required for a queue data structure to behave as a circular queue?
To convert a standard queue data structure into a circular queue, the key conversion involves how
the indices of the queue's array are handled. Specifically:
Modulo Operation:
 The primary conversion relies on using the modulo operator (%) when incrementing the
front and rear pointers. This allows the pointers to wrap around to the beginning of the
array when they reach the end.
 Instead of simply incrementing the index, you use:
 index = (index + 1) % array_size
 This ensures that when the index reaches array_size, it resets to 0, effectively creating a
circular behavior.
In essence, by utilizing the modulo operation, the linear array used to implement the queue is
treated as if it were a circular structure.
8. What is the function of a circular queue?
A circular queue is a linear data structure that utilizes a fixed-size array or buffer by connecting
the last element to the first, forming a circular structure, to efficiently manage data in a First-In,
First-Out (FIFO) manner, preventing memory wastage.

Common Applications:
Circular queues are used in various applications, including:
 Operating Systems: For managing processes or tasks in a queue, where the order of
execution needs to be maintained.
 Memory Management: To allocate and deallocate memory blocks in a circular manner.
 Buffering Data Streams: In scenarios where data needs to be processed in a continuous
stream, such as in network communication or audio/video processing.
 Traffic Management: In computer-controlled traffic systems, to switch on traffic lights one
by one repeatedly.
9. How would you use a stack to evaluate the expression ((2 + 3) * 5)?
Infix expression ((2 + 3) * 5)
Its equivalent postfix expression is 23+5*

Value = 25 (Draw stack and explain)


10. Why is it necessary to have a stack in evaluating expressions with nested parentheses?
A stack is necessary for evaluating expressions with nested parentheses because it allows you to
track the order of operations and ensure that parentheses are properly matched, which is crucial for
correctly evaluating the expression.

UNIT- II - NON-LINEAR DATA STRUCTURES


PART A ( 2 Marks)
1. Define a Binary Tree.
Binary Tree is a non-linear and hierarchical data structure where each node has at most two children
referred to as the left child and the right child. The topmost node in a binary tree is called the root,
and the bottom-most nodes are called leaves
2. Explain the difference between preorder and postorder traversal of a binary tree.
In binary tree traversal, preorder visits the root node first, then the left subtree, and finally the right
subtree, while postorder visits the left subtree first, then the right subtree, and finally the root node.
Preorder Traversal:
 Visits the root node before its children.
 Follows the order: Root -> Left Subtree -> Right Subtree.
 Useful for tasks like copying a tree or creating a prefix expression from an expression tree.
Postorder Traversal:
 Visits the root node after visiting its children.
 Follows the order: Left Subtree -> Right Subtree -> Root.
 Useful for tasks like deleting a tree or evaluating a postfix expression.
3. Given the numbers 50, 30, 70, 20, 40, 60, 80, construct a Binary Search Tree (BST).

(Draw step by step)


4. Perform an inorder traversal on the given tree
15
/ \
10 20
/ \ / \
5 12 17 25
Inorder Traversal Steps:
1. Visit the left subtree recursively.
2. Visit the root node.
3. Visit the right subtree recursively.

The inorder traversal of the given binary tree is: 5, 10, 12, 15, 17, 20, 25

5. How would you delete a node from a Red-Black Tree while maintaining its balancing properties?
To delete a node from a Red-Black Tree while maintaining its balancing properties, you perform
standard Binary Search Tree (BST) deletion, followed by a series of recoloring and rotations to
restore the Red-Black properties.
Standard BST Deletion:
Locate the node: Find the node to be deleted using standard BST search.
Handle different cases:
Node with no children: Simply remove the node.
Node with one child: Replace the node with its child.
Node with two children: Replace the node with its in-order successor (or predecessor), then delete
the successor (which will have at most one child).
6. Define AVL tree.
An AVL tree is a self-balancing binary search tree (BST) where, for every node, the difference in
height between its left and right subtrees is at most one, ensuring efficient search, insertion, and
deletion operations with O(log n) time complexity.
7. In the context of Splay Trees, explain the 'Zig-Zig' rotation process during a search operation.
In a Splay Tree, a 'Zig-Zig' rotation (or double rotation) occurs when a node (X) and its parent (Y)
are both left or both right children of their respective parents (Y and Z), and it's used during splaying
to bring X to the root.
Example:
 Imagine X, Y, and Z are all left children.
 First, rotate around Y and Z (a right rotation). Now Y is a child of X, and X is a child of Z.
 Then, rotate around X and Y (another right rotation). Now X is the child of Z.
 This double rotation effectively brings X closer to the root.

8. Explain the difference between a full binary tree and a complete binary tree.
In a full binary tree every node has either 0 or 2 child nodes. In a complete binary tree, all levels are
completely filled except possibly the last level. There is no particular sequence for filling in nodes.
All nodes should be on the left.

9. What is the key difference between a Binary Tree and a Splay Tree in terms of node access?
Splay trees are binary search trees which are self-adjusting. Self-adjusting basically means that
whenever a splay tree is accessed for insertion or deletion of a node, then that node pushes all the
remaining nodes to become root.
10. Differentiate Binary Tree and Binary Search Tree

UNIT- III - DIVIDE AND CONQUER STRATEGY AND GREEDYSTRATEGY


PART A ( 2 Marks)
1. What is the divide and conquer method? Provide two examples of its application.
Divide and Conquer is a problem-solving technique used to solve problems by dividing the main
problem into sub problems, solving them individually and then merging them to find solution to
the original problem.
Examples of its application
1. Quicksort is a sorting algorithm that picks a pivot element and rearranges the array elements so
that all elements smaller than the picked pivot element move to the left side of the pivot, and all
greater elements move to the right side. Finally, the algorithm recursively sorts the subarrays on the
left and right of the pivot element.
2. Merge Sort is also a sorting algorithm. The algorithm divides the array into two halves;
recursively sorts them, and finally merges the two sorted halves.
2. What is the idea behind Strassen's method of matrix multiplication?
Strassen's Matrix Multiplication is the divide and conquer approach to solve the matrix
multiplication problems. The usual matrix multiplication method multiplies each row with each
column to achieve the product matrix.
3. What is merge sort?
Merge sort is the sorting technique that follows the divide and conquers strategy. It is very much
similar to the quick sort algorithm as it also prefers the divide and conquers method to sort the
elements. It is one of the most prevalent and efficient sorting algorithms.

4. Compare the performance of quicksort with other sorting algorithms in terms of time complexity
and efficiency.
Quicksort is generally faster and more efficient than other sorting algorithms, like Bubble Sort and
Merge Sort, but its performance depends on the input data.
Time complexity
Quicksort: On average, Quicksort has a time complexity of O(n log n), but can degrade to O(n^2)
in the worst case.
Merge Sort: Merge Sort always has a time complexity of O(n log n), even in the worst case.

Efficiency
Quicksort: Quicksort is often faster than HeapSort and is preferred for sorting arrays.
Merge Sort: Merge Sort is more reliable in terms of time complexity than Quicksort.
5. Apply the divide and conquer strategy to solve a sorting problem. Demonstrate how it works in
practice.
The divide and conquer strategy for sorting involves splitting an array into smaller subarrays,
sorting the subarrays, and then combining the results.
How it works
 Split the array into two halves
 Recursively sort each half
 Compare the first elements of each half
 Place the smaller element into the output array
 Repeat until one of the halves is empty
6. Explain the concept of graph traversal techniques and list some applications.
Graph traversal techniques are methods to visit all the nodes (vertices) in a graph systematically,
such as Breadth-First Search (BFS) and Depth-First Search (DFS).
Applications include:
 Finding shortest paths
 Detecting cycles
 Searching networks and social graphs
 Solving puzzles and games
 Web crawling and network broadcasting
7. What is meant by a spanning tree? Give an example.
A spanning tree of a connected graph is a subgraph that includes all the vertices of the original
graph, is connected, and contains no cycles (i.e., it’s a tree).
Example:
For a graph with vertices A, B, C, D connected in a square with diagonals, a spanning tree could be
edges AB, BC, CD — connecting all vertices without forming any cycle.
8. Discuss the significance of Huffman coding in data compression? Why do we need it?
Huffman coding is a lossless data compression technique that assigns shorter codes to more
frequent symbols and longer codes to less frequent ones, minimizing the overall data size.
Significance:
 Reduces storage space and transmission time.
 Ensures efficient encoding by exploiting symbol frequency, making it essential for file
compression, multimedia, and communication systems.
9. Use the concept of spanning trees to find a minimum spanning tree in a weighted graph.
To find a Minimum Spanning Tree (MST) in a weighted graph using spanning tree concepts:
1. Select edges with the smallest weights that connect all vertices without forming cycles.
2. Common algorithms:
o Kruskal’s Algorithm: Sort edges by weight and add edges one by one if they don’t
create a cycle.
o Prim’s Algorithm: Start from a vertex and repeatedly add the smallest edge
connecting the tree to a new vertex.
The resulting MST connects all vertices with minimum total edge weight.
10. Compare the advantages of using Huffman coding with the other encoding schemes.
Advantages of Huffman Coding over other encoding schemes:
1. Optimal Prefix Codes: Produces the shortest possible average code length for given symbol
frequencies, minimizing data size.
2. Lossless Compression: Ensures no data loss, unlike some fixed-length or simple encoding
methods, making it highly efficient for text and data compression.

UNIT- IV - DYNAMIC PROGRAMMING AND BACKTRACKING


PART A ( 2 Marks)
1. Define dynamic programming.
Dynamic programming is a technique used to solve optimization problems by breaking them into
smaller, overlapping subproblems and storing solutions to these subproblems to avoid redundant
computations. It's a bottom-up approach where you solve all possible small problems and combine
them to get solutions for bigger problems.
Dynamic programming solves problems by:
1. Breaking down: the problem into smaller overlapping subproblems.
2. Storing: solutions to these subproblems (often in a table or array) to avoid recalculating them.
3. Reusing: these stored solutions to build the final solution to the original problem.

2. Describe the knapsack problem with an example.


The knapsack problem is an optimization problem where you select items to maximize their total
value while respecting a weight constraint. Imagine you have a knapsack with a limited weight
capacity and a set of items, each with a weight and value. The goal is to find the combination of
items that yields the highest total value without exceeding the knapsack's weight limit.
Example:
Let's say you have a knapsack with a maximum weight capacity of 5 kg. You have the following
items available:
Item 1: Weight = 2 kg, Value = 6
Item 2: Weight = 3 kg, Value = 10
Item 3: Weight = 1 kg, Value = 4
The optimal solution is to include Items 1 and 3 (2 + 1 = 3 kg) which gives a total value of 10 (6 +
4). If you include Item 2, you'd go over the weight limit.

3. Explain how exhaustive search is used in problem-solving.


Exhaustive search, also known as brute-force search, systematically checks all possible solutions to
a problem. It's a simple approach but computationally expensive as it explores every option. In
problem-solving, exhaustive search guarantees a solution if one exists, but it can be impractical for
large or complex problems due to its time complexity.
4. Define the Travelling Salesman Problem.
The Traveling Salesman Problem (TSP) is a mathematical optimization problem where a
salesperson needs to find the shortest route that visits a set of cities exactly once and returns to the
starting city. The goal is to minimize the total distance travelled.
5. Analyze the role of state space search in algorithmic problem-solving.
State space search is a core technique in algorithmic problem-solving, particularly in AI, where
it allows for systematically exploring possible solutions by representing problems as states and
transitions. This method enables algorithms to efficiently navigate towards a desired goal state by
generating and evaluating different states.
6. What is backtracking?
Backtracking is a problem-solving technique used in programming where we try to build a solution
step by step. If at any point we realize that the current path won't lead to a valid solution, we go back
(backtrack) and try a different approach.
7. Highlighting the key differences of Floyd's algorithm and Warshall’s algorithm and their uses in
graph theory.
Floyd's Algorithm (All-Pairs Shortest Paths):
 Purpose: Finds the shortest path between every pair of vertices in a weighted graph (directed or
undirected).
 Handles: Negative edge weights.
 Cannot Handle: Negative cycles (as they can lead to infinitely short paths).
 Time Complexity: O(V^3) where V is the number of vertices.
Warshall's Algorithm (Transitive Closure):
 Purpose: Determines whether there is a path between every pair of vertices in a graph.
 Handles: Only binary paths (path exists or not), does not consider edge weights.
 Time Complexity: O(V^3).

8. Write a concise explanation of the N-Queens problem and its applications.


The N-Queens problem is a classic combinatorial puzzle that involves placing N queens on an
N×N chessboard such that no two queens threaten each other (i.e., no two queens share the same
row, column, or diagonal).
 Applications:
It is used in algorithm design, constraint satisfaction problems, backtracking
techniques, and has relevance in parallel computing, AI, and optimization problems.

9. Define a Hamiltonian circuit and calculate the number of such circuits in a graph with five vertices.
A Hamiltonian circuit is a closed loop in a graph that visits each vertex exactly once and returns
to the starting vertex.
In a complete graph with nnn vertices, the number of Hamiltonian circuits is:
(n−1)!2\frac{(n-1)!}{2}2(n−1)!
For 5 vertices:
(5−1)!2=4!2=242=12\frac{(5-1)!}{2} = \frac{4!}{2} = \frac{24}{2} = 122(5−1)!=24!=224=12
Answer: A Hamiltonian circuit visits each vertex once and returns to the start; there are 12 such
circuits in a complete graph with 5 vertices.
10. Compare Breadth-First Search (BFS) and Depth-First Search (DFS)
 BFS (Breadth-First Search):
 Traverses level by level, starting from the root node and exploring all adjacent nodes
before moving to their neighbors.
 Uses a queue to keep track of nodes to be visited, ensuring that nodes at the same level are
explored before moving to the next level.
 Guarantees finding the shortest path in an unweighted graph.
 Suitable for finding shortest paths in graphs where edge weights are uniform.
DFS (Depth-First Search):
 Explores a graph by going as deep as possible along each branch before backtracking to
explore other branches.
 Uses a stack or recursion to keep track of nodes to be visited.
 Does not guarantee finding the shortest path.
 More efficient for exploring graphs where the solution path is not necessarily the shortest.

UNIT- V - BRANCH-AND-BOUND,NP PROBLEMS AND APPROXIMATION ALGORITHMS


PART A ( 2 Marks)
1. Define the Branch and Bound method.
Branch and Bound is an algorithmic method used to solve optimization problems, especially in
combinatorial search spaces. It systematically explores branches of a solution tree, eliminating
(bounding) suboptimal branches using upper and lower bounds, thereby reducing the search
space.
Key features:
 Branch: divide the problem into smaller subproblems.
 Bound: calculate a bound on the best possible solution in each subproblem.
 Prune: discard branches that cannot yield better solutions than already found.
Used in problems like the Travelling Salesman Problem (TSP) and Knapsack problem.
2. Identify the key steps involved in a Brute Force Algorithm.
Key steps in a Brute Force Algorithm:
1. Generate all possible solutions to the problem without any optimization.
2. Evaluate each solution against the problem’s criteria.
3. Select the best solution based on the evaluation.
This method guarantees a correct result but is often inefficient for large problem sizes due to its
exhaustive nature.

3. What is Travelling Salesman Problem?


The Traveling Salesman Problem (TSP) is a mathematical optimization problem where a salesman
needs to find the shortest route that visits a set of cities exactly once and then returns to the starting
city. It's an NP-hard problem, meaning finding the optimal solution for large datasets is
computationally expensive. The goal is to minimize the total distance travelled.
4. Explain the Knapsack Problem with an example
The Knapsack Problem is an optimization problem where you aim to maximize the total value of
items placed in a knapsack without exceeding its weight capacity.
Example:
You have a knapsack with a capacity of 50 kg and three items:
Item Weight (kg) Value ($)
A 10 60
B 20 100
C 30 120
Goal: Choose items to maximize value without exceeding 50 kg.
Solution: Items B and C (total weight = 50 kg, value = $220) give the maximum value.
5. List the different types of Approximation Algorithms used in problem-solving.
Types of Approximation Algorithms used in problem-solving include:
1. Greedy Approximation Algorithms – Build a solution step-by-step by choosing the best
local option.
2. Local Search Algorithms – Start with an initial solution and improve it through small
changes.
These are commonly applied to NP-hard problems like Knapsack, Vertex Cover, and Travelling
Salesman Problem.
6. Discuss the real-world applications of Assignment Problem.
Real-world applications of the Assignment Problem include:
1. Job Scheduling: Assigning workers to tasks to minimize total completion time or cost.
2. Resource Allocation: Matching resources (like machines or vehicles) to jobs efficiently.
3. Workforce Management: Assigning employees to shifts or projects based on skills and
availability.
4. Task Assignment in Robotics: Allocating robots to tasks to optimize performance.
These applications help in optimizing costs, time, and resource utilization in industries and
operations.
7. Compare NP-Hard and NP-Complete problems, highlighting their key differences and examples.
NP-Complete problems are decision problems that are both in NP (verifiable in polynomial time)
and as hard as any problem in NP; if one NP-Complete problem is solved in polynomial time, all NP
problems can be.
NP-Hard problems are at least as hard as NP-Complete problems but are not necessarily decision
problems or in NP (may not be verifiable in polynomial time).
Key differences:
 NP-Complete problems are both in NP and NP-Hard.
 NP-Hard problems may not be in NP (e.g., optimization problems).
Examples:
 NP-Complete: 3-SAT, Subset Sum
 NP-Hard: Travelling Salesman Problem (optimization version), Halting Problem
(undecidable).
8. Define P and NP classes.
P (Polynomial time): Class of decision problems that can be solved efficiently by a deterministic
Turing machine in polynomial time.
NP (Nondeterministic Polynomial time): Class of decision problems for which a given solution
can be verified efficiently (in polynomial time) by a deterministic Turing machine.
9. Discuss the role of Polynomial Reduction in computational problems.
Polynomial Reduction is a technique used to transform one problem into another in polynomial
time. It helps to:
 Show problem relationships, especially to prove that a problem is at least as hard as
another (used in NP-Completeness proofs).
 Transfer solutions or hardness: If problem A reduces to problem B, solving B efficiently
means A can also be solved efficiently.
This is key in classifying problems and understanding computational complexity.
10. Evaluate the importance of Approximation Algorithms in solving complex problems efficiently.
Approximation Algorithms are important because they provide near-optimal solutions to
complex, NP-hard problems within reasonable time, where exact solutions are computationally
infeasible.
They enable practical problem-solving in fields like logistics, scheduling, and network design by
balancing solution quality and computational efficiency.

You might also like