0% found this document useful (0 votes)
8 views6 pages

DAA Lab Problem Statements

The document outlines a series of lab experiments for a course on Design and Analysis of Algorithms at SRM Institute of Science and Technology. Each experiment focuses on implementing various algorithms and data structures, including sorting techniques, string matching, and optimization problems, with specific objectives and performance evaluations. The experiments aim to enhance understanding of algorithmic concepts and their practical applications through programming tasks.

Uploaded by

hellobro23231
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)
8 views6 pages

DAA Lab Problem Statements

The document outlines a series of lab experiments for a course on Design and Analysis of Algorithms at SRM Institute of Science and Technology. Each experiment focuses on implementing various algorithms and data structures, including sorting techniques, string matching, and optimization problems, with specific objectives and performance evaluations. The experiments aim to enhance understanding of algorithmic concepts and their practical applications through programming tasks.

Uploaded by

hellobro23231
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

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

SCHOOL OF COMPUTING
21CSC204J-DESIGN AND ANALYSIS OF ALGORITHMS

Problem statements for all lab experiments


List of Lab Experiments
Expt1: Heap and AVL Tree data structures
Exp 2: Insertion sort, Bubble Sort
Expt 3: Use Brute force algorithm technique and perform string matching.
Expt 4: Merge sort, Binary search
Expt 5: Quick Sort
Expt 6: Strassen Matrix multiplication
Expt 7a: Huffman coding,
7b: knapsack using greedy
Expt 8: Longest common subsequence
Expt 9: N queen’s problem
Expt 10: Travelling salesman problem
Expt 11: Randomized Quick sort
Expt 12: String matching algorithms

Expt1 : Heap and AVL Tree data structures


The aim of this lab exercise is to use the two crucial data structures you learnt in the DSA
course -- heaps and AVL trees -- to design an algorithm to sort (arrange in increasing
order) a list of numbers.
Design menu driven C or C++ program to sort a list of numbers using heap and AVL trees
based on the users’ choice.
Choice 1. Insert the elements into a heap, one at a time, starting from an empty heap. Then
you need to perform, in a loop, several deletemax operations (which will also rebuild the
heap automatically) to get the sorted order.
Choice 2. Insert the elements into an AVL tree, one at a time, starting from an empty AVL
tree. Then you need to traverse the AVL tree in a particular order to get the sorted list.
Figure out the order and execute it.
Exp: 2 Insertion sort, Bubble Sort
Design and implement a menu-driven program to sort a given set of integers using
Bubble Sort and Insertion Sort algorithms. n should be atleast 10000, to ensure
meaningful performance evaluation.
The program must provide options to perform Bubble Sort or Insertion Sort based on the
user’s choice and sort the elements in ascending order. After each sorting operation, the
sorted array should be displayed.
For each algorithm, the CPU execution time must be measured using the same input data
and compared with the execution time obtained from the sorting techniques implemented
in the previous experiment, namely Heap-based sorting and AVL tree–based sorting.
This experiment aims to study and compare the performance of basic sorting algorithms
with advanced data-structure–based sorting techniques when handling large datasets.
Note:
 clock() returns the CPU time used by the program
 CLOCKS_PER_SEC gives clock ticks per second
 CPU Time Formula:
CPU Time = (end - start) / CLOCKS_PER_SEC

Expt 3: Use Brute force algorithm technique and perform string matching
Develop a program to perform string matching using the Brute Force algorithm
technique.
The program should:
1. Accept a main string (text) and a pattern string from the user.
2. Apply the Brute Force String Matching algorithm to search for the pattern in the
given text.
3. Display all the starting indices where the pattern is found.
4. Display a suitable message if the pattern is not found.
The objective of this experiment is to understand the working of the Brute Force string
matching technique and to analyze its performance through direct character comparisons.

Exp 4: Merge sort, Binary search


Develop a program to implement Divide and Conquer algorithms namely Merge Sort
and Binary Search.
The program should:
1. Accept a set of n integer elements from the user.
2. Sort the given elements using the Merge Sort algorithm based on the Divide and
Conquer technique.
3. After sorting, search for a given key element.
4. Display the sorted array.
5. Display the position of the searched element if found, or an appropriate message if not
found.
The objective of this experiment is to understand the application of the Divide and
Conquer paradigm and to analyze the efficiency of Merge Sort and Binary Search
algorithms

Expt 5: Quick Sort


Develop a program to sort a given set of elements using the Quick Sort algorithm
based on the Divide and Conquer technique.
The program should:
1. Accept n integer elements from the user.
2. Apply the Quick Sort algorithm by selecting a pivot element (The pivot can be
selected in any ways, such as the first element, last element, middle element, or a
random element) and partitioning the array such that elements smaller than the pivot
are placed on the left and greater elements on the right and recursively sort the left
and right subarrays.
3. Display the array elements before and after sorting.
4. CPU execution time must be measured using the same input data and compared with
the execution time obtained from the Merge sorting techniques implemented in the
previous experiment.
The objective of this experiment is to understand the working of the Quick Sort
algorithm and to analyze its performance using the Divide and Conquer approach.

Expt 6: Strassen Matrix multiplication


Design and implement a program to multiply two square matrices using Strassen’s
Matrix Multiplication algorithm based on the Divide and Conquer technique.
The program should:
1. Read two square matrices of order n × n from the user.
2. Apply Strassen’s algorithm to compute the product of the matrices by recursively
dividing them into submatrices.
3. Display the resultant matrix after multiplication.
4. CPU execution time must be measured using the same input data and compared with
the execution time obtained from the conventional Matrix multiplication.
The objective of this experiment is to understand the efficiency of Strassen’s algorithm
and to analyze its performance compared to conventional matrix multiplication.

Expt 7a: Huffman coding


Design and implement a program to generate Huffman codes for a given set of
characters and their frequencies using the Greedy algorithm technique.
The program should:
1. Read the characters and their corresponding frequencies.
2. Construct a Huffman Tree by repeatedly selecting the two nodes with the minimum
frequencies.
3. Assign binary codes to each character based on the constructed Huffman Tree.
4. Display the Huffman codes for all characters.
5. Calculate the total number of bits required to encode the given data.
The objective of this experiment is to understand data compression using Huffman
coding and to analyze the effectiveness of the Greedy approach in minimizing the
average code length.

7b. knapsack using greedy


Design and implement a program to solve the Knapsack problem using the Greedy
approach.
The program should:
1. Read the number of items, along with their weights and profits.
2. Read the capacity of the knapsack.
3. Compute the profit-to-weight ratio for each item.
4. Select items based on the maximum profit-to-weight ratio.
5. Display the selected items and the maximum profit obtained.
The objective of this experiment is to understand the application of the Greedy method
in solving optimization problems such as the Fractional Knapsack problem.
Expt 8: Longest Common Subsequence
Design and implement a program to find the Longest Common Subsequence (LCS)
between two given sequences using the Dynamic Programming technique.
The program should:
1. Accept two input sequences (strings) from the user.
2. Construct a dynamic programming table to compute the length of the LCS.
3. Trace back through the table to obtain the actual longest common subsequence.
4. Display the length of the LCS and the LCS string.
The objective of this experiment is to understand the Dynamic Programming approach
and to analyze its effectiveness in solving the Longest Common Subsequence problem.

Expt 9: N queen’s problem


Design and implement a program to solve the N-Queens problem using the
Backtracking technique.
The program should:
1. Read the number of queens N from the user.
2. Place N queens on an N × N chessboard such that no two queens attack each other.
3. Ensure that no two queens share the same row, column, or diagonal.
4. Generate and display all possible valid solutions (or at least one solution, as
specified).
5. Display a suitable message if no solution exists for the given value of N.
The objective of this experiment is to understand the Backtracking approach and to
analyze its efficiency in solving combinatorial problems.
Expt 10: Travelling salesman problem
Design and implement a program to solve the Travelling Salesman Problem (TSP) for a
given set of cities using Backtracking technique.
The program should:
1. Read the number of cities n and the distance (cost) matrix representing distances
between each pair of cities.
2. Determine the minimum cost tour that starts from a given city, visits each city
exactly once, and returns to the starting city.
3. Display the optimal tour and the minimum travel cost.
4. Display a suitable message if no feasible tour exists.
The objective of this experiment is to understand combinatorial optimization problems
and to analyze algorithmic strategies used to solve the Travelling Salesman Problem.
Expt 11: Randomized Quick sort
Design and implement a program to sort a given list of elements using the Randomized
Quick Sort algorithm.
The program should:
1. Read n elements from the user.
2. Select the pivot element randomly for each partition to reduce the chances of worst-
case performance.
3. Partition the array such that elements less than the pivot are placed to its left and
elements greater than the pivot are placed to its right.
4. Recursively apply Randomized Quick Sort to the left and right subarrays.
5. Display the list of elements before and after sorting.
6. CPU execution time must be measured using the same input data and compared with
the execution time obtained from the Quick sort techniques implemented in the
experiment no. 5.
The objective of this experiment is to understand the concept of randomized
algorithms and to analyze how random pivot selection improves the average
performance of Quick Sort.

Expt 12: String matching algorithms


Design and implement a program to perform string matching using the Rabin–Karp
algorithm.
The program should:
1. Read a text string and a pattern string from the user.
2. Compute the hash value of the pattern and the hash values of all possible substrings
of the text having the same length as the pattern using a rolling hash technique.
3. Compare hash values to identify potential matches and verify them using character-
by-character comparison to avoid false positives.
4. Display all starting index positions where the pattern occurs in the text.
5. Display a suitable message if the pattern is not found.
6. CPU execution time must be measured using the same input data and compared with
the execution time obtained from the experiment no 3.
The objective of this experiment is to understand hashing-based string matching
techniques and to analyze the efficiency of the Rabin–Karp algorithm.

Common questions

Powered by AI

The Brute Force algorithm for string matching operates with a straightforward approach, checking for matches starting from each position in the text, leading to a time complexity of O(n*m) where n is the length of the text and m is the length of the pattern. This makes it inefficient for larger texts but straightforward in application. In contrast, the Rabin–Karp algorithm leverages hashing to achieve average-case complexity of O(n+m) by computing rolling hash values for text substrings and comparing these hashes to the pattern's hash. This efficiently narrows candidate matches before verifying with direct comparison, offering better performance in practice especially in search for multiple patterns or in longer inputs. Rabin–Karp prevents unnecessary comparisons, thereby reducing computation, albeit introducing potential hash collisions requiring verification .

In Huffman coding, the Greedy algorithm constructs an optimal prefix code used for data compression by selecting the sequence of operations that offers the most immediate reduction in the average code length step by step. By iteratively combining the two least frequent nodes (characters and their frequencies) to form a tree structure, it minimizes the total average path length in the binary tree, effectively compressing data without loss by minimizing the total number of bits required to encode the given set of characters. The Greedy choice is globally optimal because it guarantees a minimized code, demonstrating the efficacy of this method in achieving efficient, widespread use in real-world applications where data compression is critical, such as in file formats and networks .

The Greedy approach to the Knapsack problem, particularly applicable to the fractional version, involves sorting items based on the profit-to-weight ratio and selecting items to maximize value until the knapsack's capacity is filled. This approach can quickly yield an efficient solution since it makes locally optimal choices at each step leading to a global optimum in the case of fractional items. However, this method fails with the 0-1 Knapsack variant, where items are indivisible, as it doesn't guarantee an optimal solution—a situation where dynamic programming excels. The limitation lies in scenarios where sufficiently smaller valuable items exist but are overlooked due to the myopic nature of greedy selection, highlighting the need for alternate strategies or more comprehensive algorithms for non-divisible items .

The Divide and Conquer paradigm, integral to both Merge Sort and Binary Search, involves efficiently solving complex problems by breaking them down into simpler sub-problems. In Merge Sort, this approach is evident as the list is recursively divided into two halves until each piece is trivially sorted, and these sorted subarrays are then merged back together in O(n log n) time, leveraging both the division into parts and the linear time merge operation. For Binary Search, the array is iteratively split in half, with each division reducing the problem size logarithmically, leading to a search time of O(log n). In both cases, the Divide and Conquer approach provides profound computational gains by capitalizing on the power of recursive divisions leading to straightforward and scalable solutions .

AVL Tree-based sorting has the advantage of maintaining a balanced structure, resulting in consistently efficient operations for insertion and time complexity of O(n log n) for sorting operations. This typically provides better average-case performance for sorted or nearly sorted data compared to Heap-based sorting, which operates at O(n log n) but may yield larger overhead and more swaps as it relies on a different structural approach not optimized for ordered data. Heap-based sorting excels in scenarios where priority queue operations are required. Whereas, AVL trees maintain order and balance dynamically, leading to potentially faster lookups during repeated operation sequences .

Dynamic programming addresses the Longest Common Subsequence (LCS) problem by efficiently solving overlapping subproblems and storing solutions to these in a table to avoid redundant recomputation. Unlike naive recursive solutions with an exponential time complexity due to repeated recalculations, dynamic programming systematically fills a table in O(n*m) time where n and m are the lengths of the sequences. This memoization ensures that each subproblem, such as comparing prefixes of the sequences, is computed only once. By filling the table bottom-up, dynamic programming not only optimizes the computation time but also facilitates straightforward tracing of the LCS through backtracking, making it far superior for performance in comparison to brute force approaches .

Bubble Sort and Insertion Sort, both simple sorting algorithms, have average and worst-case time complexity of O(n²), making them less efficient for large datasets compared to the more advanced Heap and AVL Tree-based sorting algorithms which have O(n log n) complexity. In practice, Bubble Sort is less efficient than Insertion Sort because it requires more comparisons and swaps. Insertion Sort, while still quadratic in complexity, outperforms Bubble Sort particularly on smaller or partially sorted datasets due to its adaptive nature. When compared with Heap and AVL Tree-based sorting, both Bubble and Insertion Sorts leverage simplicity and ease of implementation but are significantly slower on large datasets, showcasing the benefit of data structure-based algorithms for scalability and performance in complex sorting tasks .

Randomized Quick Sort uses random selection of the pivot element, which helps mitigate the potential worst-case performance typically associated with deterministic Quick Sort, which is O(n²) when poor pivot choices consistently lead to unbalanced partitions (for instance, sorted sequences). By randomly selecting the pivot, Randomized Quick Sort averages the pivot location over a more uniform distribution, which effectively avoids always hitting pathological input orders that degrade performance, thus maintaining the average case time complexity of O(n log n). This addition of randomness tends not only to make partition work balanced in the expected sense over many runs but also offers practical robustness against adversarial input patterns, making it generally more reliable for large, varied datasets in practice .

Strassen's Matrix Multiplication algorithm reduces the number of required multiplications compared to the traditional cubic approach, employing a divide and conquer strategy that theoretically operates in O(n^log7) ≈ O(n^2.81), diverging from the straightforward O(n³) complexity. This provides a computational advantage for large matrices, as the reduction in multiplicative operations can lead to considerable time savings. However, in practical implementations, especially with very small matrices, the overhead of recursive calls in Strassen's method can negate its theoretical benefits. Additionally, Strassen's algorithm is beneficial in memory-bound contexts but requires more handling of matrix dimensions and additional computation of sums and subtractions, making it less efficient for all-around use compared to tailored systems optimized for resource tradeoffs .

The Travelling Salesman Problem (TSP) is formulated as a combinatorial optimization problem where the objective is to find the shortest possible route that visits each city exactly once and returns to the origin city. In the backtracking approach, possible city sequences are constructed incrementally, abandoning ('backtracking') a sequence as soon as it's clear that it's not viable or optimal. This method allows exploration of a solution space and potentially avoids complete enumeration of solutions, yet suffers from exponential growth in the number of permutations, O(n!), making it infeasible for large datasets. Challenges include the need for intelligent pruning strategies to manage exploration and the computational overhead of maintaining and comparing many possible paths, which emphasizes the need for heuristic or approximation methods for practical TSP solutions .

You might also like