AOA 2023 Solution
AOA 2023 Solution
AOA 2023 Solution
Sum(a. n)
{ if(n<=0) return 0;
Ans=
The space complexity of an algorithm refers to the amount of memory or space required by the
algorithm to solve a computational problem as a function of the input size. In the case of the given
algorithm for calculating the sum of elements in an array, the space complexity is influenced by the
recursion.
Q.3 Distinguish Greedy Method and Dynamic Programming.
3. Boyer-Moore Algorithm
4. Rabin-Karp Algorithm
5. Aho-Corasick Algorithm
PART B
--------------------------
Q.1 Contrast between 3 asymptotic notations & define the use & objective of
asymptotic notations
Contrast between Asymptotic Notations:
• Example: If an algorithm has a time complexity of O(n^2), it means the running time
grows quadratically or less.
• Use: Used to describe the lower bound or best-case time complexity of an algorithm.
• Example: If an algorithm has a time complexity of Ω(n), it means the running time
grows linearly or faster.
• Definition: Represents both upper and lower bounds on the growth rate of an
algorithm. It provides a tight bound on the rate of growth of a function.
• Use: Used to describe both the upper and lower bounds or average-case time
complexity of an algorithm.
• Example: If an algorithm has a time complexity of Θ(n), it means the running time
grows linearly.
2. Comparison of Algorithms:
• Asymptotic notations focus on the growth rate of algorithms with respect to the
input size. This allows for a more abstract and generalized understanding of
algorithm performance.
4. Simplification of Analysis:
Q.2 Sort the following sequence using Quick Sort Method. Consider first element as
Sure, let's use the Quick Sort method to sort the given sequence with the first element as the pivot.
Here are the steps:
2. Step 1 - Partitioning:
• Rearrange the elements such that all elements less than the pivot are on the left,
and all elements greater than the pivot are on the right.
• Result: 9, 2, 3, 10
So, the given sequence sorted using the Quick Sort method with the first element as the pivot is:
Q.3 Find out the solution generated by the job sequencing. When n = 7 with
following profit &
deadline -
1. Sort jobs by profit in non-increasing order: Sort the jobs based on their profits in non-
increasing order. In this case, the sorted order is (P3, P7, P2, P4, P6, P1, P5).
2. Allocate jobs to time slots within deadlines: Iterate through the sorted jobs and allocate
them to time slots within their deadlines. If a time slot is already occupied, try the previous
time slots until an available slot is found.
Following this process for the given profits and deadlines, the job sequence would be:
• Time slot 1: Job P3 (Profit = 20)
3. Calculate the total profit: The total profit is the sum of profits of the scheduled jobs. In this
case, the total profit is 20 + 30 + 5 + 18 + 6 + 3 + 1 = 83.
So, the solution generated by the job sequencing for the given profits and deadlines is to schedule
jobs in the following order: P3, P7, P2, P4, P6, P1, P5, and the total profit is 83.
Q.4 Solve the TSP problem for the following Cost Matrix -
W X Y Z
W [A 8 13 18]
X [3 A 7 8]
Y [4 11 A 10]
Z [6 6 7 A]
The Traveling Salesman Problem (TSP) involves finding the shortest possible route that visits a set of
cities and returns to the starting city. Given the cost matrix you provided, we'll solve the TSP using a
simple approach, assuming we start at city W.
Cost Matrix:
| W X Y Z ---------------
W | A 8 13 18
X|3A78
Y | 4 11 A 10
Z|667A
Solution:
• Start at city W.
• Choose the city with the minimum cost from the current city. Mark the chosen city
as visited.
Path:
Total Cost:
• 8+8+7+4=278+8+7+4=27
So, the solution to the TSP for the given cost matrix is to follow the path: W -> X -> Z -> Y -> W, with a
total cost of 27. This is a heuristic solution, and finding the optimal solution for the TSP is an NP-hard
problem. There are more sophisticated algorithms (such as dynamic programming or integer linear
programming) to find optimal solutions for larger instances of the TSP.
he Quadratic Assignment Problem (QAP) is a combinatorial optimization problem that deals with
assigning a set of facilities to a set of locations, each with an associated cost, in such a way as to
minimize the total cost. The cost is quadratic, meaning it depends on the distance between the
facilities and the flow of objects between the facilities.
Problem Definition: Given �n facilities and �n locations, the QAP seeks to find the best
permutation of facilities to locations to minimize the total cost, where the cost is determined by the
product of flow between facilities and distance between their assigned locations.
Example: Let's consider a specific example to illustrate the Quadratic Assignment Problem.
• Facility Locations: A, B, C
• Locations: 1, 2, 3
• Flow Matrix (F):
Objective:
Find the assignment of facilities to locations that minimizes the total cost, considering both the flow
and distance matrices.
Solution:
To solve the QAP, one needs to explore different permutations of facility assignments and calculate
the total cost for each permutation. An optimal solution is the permutation that yields the minimum
total cost.
Due to the computational complexity of the QAP, exact solutions are often difficult to find for large
instances. Various heuristic and metaheuristic algorithms, such as simulated annealing, genetic
algorithms, and tabu search, are commonly employed to find good approximations to the optimal
solution.
Note:
The QAP arises in various practical applications, including manufacturing facility layout,
telecommunications network design, and data clustering. It represents a challenging optimization
problem with applications across different domains.
example.
Las Vegas and Monte Carlo algorithms are two types of randomized algorithms,
but they differ in their approach and characteristics. Let's differentiate between
them with examples:
1. Definition:
• Las Vegas algorithms always produce the correct result, but their
running time is randomized.
2. Characteristics:
1. Definition:
2. Characteristics:
Comparison:
• Las Vegas: Always produces a correct result, but the running time is
randomized.
• Monte Carlo: May produce an incorrect result with a certain
probability.
• Example:
• Probability of Error:
In summary, Las Vegas algorithms always produce the correct result, but their
running time is randomized. On the other hand, Monte Carlo algorithms may
produce an incorrect result with a certain probability, and the randomness is in
the output rather than the execution time.
https://www.geeksforgeeks.org/proof-that-hamiltonian-cycle-is-np-complete/
PART C
--------------------------------------
example
Problem Statement:
1. Define Subproblems:
��⋅��+1⋅…⋅��Ai⋅Ai+1⋅…⋅Aj.
��⋅��+1⋅…⋅��Ai⋅Ai+1⋅…⋅Aj.
�[�,�]=min�≤�<�{�[�,�]+�[�+1,�]+��−1⋅
��⋅��}m[i,j]=mini≤k<j{m[i,k]+m[k+1,j]+pi−1⋅pk⋅pj}
chain.
selecting a subset of the items, such that the total weight does not
Problem Statement:
Q.3 (a) Find the pattern ABCBC in the test ACABABCABCBCA using KMP Matcher.
• Compute the Longest Prefix Suffix (LPS) array for the given pattern.
• LPS[i] represents the length of the longest proper prefix of the pattern that is also a
suffix of the pattern, considering the first i characters.
2. Pattern Matching:
• Traverse the text and pattern simultaneously, using the LPS array to skip unnecessary
comparisons.
• If a mismatch occurs, use the information from the LPS array to determine the next
position to compare.
• Pattern: ABCBC
• LPS: [0, 0, 0, 1, 2]
2. Pattern Matching:
Continue the process, and when a mismatch occurs, use the LPS array to determine the next
position.
The Naive String Matching Algorithm, also known as the Brute Force or Simple Matching
Algorithm, is a straightforward approach to pattern matching. It involves comparing each
substring of the text with the pattern until a match is found or the end of the text is reached.
• For each position i in the text, compare the substring starting at i with the pattern.
2. Pattern Matching:
3. Continue Iterating:
Example:
Consider the text "ABCABCABC" and the pattern "ABC." The steps would be as follows:
2. Move to the next position and compare "BCA" with "ABC" (no match).
3. Move to the next position and compare "CAB" with "ABC" (no match).
Discussion:
• Time Complexity:
• In the worst case, the Naive String Matching Algorithm has a time complexity of O((n-
m+1)m), where n is the length of the text and m is the length of the pattern.
• Space Complexity:
• The algorithm has a constant space complexity as it does not require additional data
structures.
While the Naive String Matching Algorithm is simple, it may not be efficient for large texts and
patterns. More advanced algorithms like KMP and Boyer-Moore provide better time
complexities for pattern searching.
Q.4 (a) Define the terms flow networks and flow. Explain the essential properties of
flow.
terms
(i) Residual
Network
with example
1. P (Polynomial Time):
algorithm.
• Example: The subset sum problem – given a set of integers, is there a subset
Relationships:
1. P ⊆ NP: Every problem that can be solved in polynomial time is also in NP.
problems in NP.
algorithm exists for any NP-Complete problem, then it exists for all problems
in NP.
(b) Write algorithm for approximation for vertex cover problem with example
https://www.javatpoint.com/daa-approximation-algorithm-vertex-cover