Daa Project
Daa Project
Members:
• String Matching
Checks each possible position of the pattern in the text to see if the pattern
matches the substring of the text at that position.
Scenario: Given the text "hello" and the pattern "ll", the brute force approach would
check substrings "he", "el", "ll", and "lo" to find the match. It evaluates all positions
in the text to find a match, which can be computationally expensive for large texts.
• Travelling Salesman Problem
COSC90- DESIGN AND ANALYSIS OF ALGORITHMS
Evaluates all possible permutations of the cities to determine the shortest possible
route that visits each city exactly once and returns to the origin city.
Scenario: Given cities A, B, C, and D, the brute force approach would consider all
permutations of routes like A-B-C-D-A, A-C-B-D-A, A-D-B-C-A, etc., to find the
shortest route. This method is impractical for a large number of cities due to
factorial growth in permutations.
• Sorting (Bubble Sort)
Compares all pairs of adjacent elements in the list and swaps them if they are in the
wrong order, repeating this process until the list is sorted.
Scenario: Given the array [3, 5, 2, 9, 4], Bubble Sort would repeatedly compare and
swap adjacent elements (e.g., 3 and 5, 5 and 2, etc.) until the array is sorted into [2,
3, 4, 5, 9]. This process can be inefficient for large arrays due to the number of
comparisons and swaps.
• Finding Pairs
Finds pairs in an array that sum to a given value by checking every possible pair of
elements.
Scenario: Given the array [3, 5, 2, 9, 4] and target sum 7, the brute force approach
would check pairs (3, 5), (3, 2), (3, 9), (3, 4), (5, 2), etc., to find pairs that sum to 7.
This can be computationally expensive for large arrays.
ASCENDING DESCENDING
COSC90- DESIGN AND ANALYSIS OF ALGORITHMS
• Binary Search
Reduces the search space by half in each step by comparing the target value with
the middle element of the current interval.
Scenario: Given a sorted array [1, 2, 3, 4, 5, 6, 7] and target value 5, Binary Search
compares 5 with the middle element 4, then continues searching in the right half [5,
6, 7], finding the target in the reduced search space.
• Euclidean Algorithm
Finds the greatest common divisor (GCD) of two numbers by repeatedly applying
the remainder operation until the remainder is zero.
Scenario: To find the GCD of 48 and 18, the Euclidean Algorithm computes 48 mod
18 = 12, then 18 mod 12 = 6, and finally 12 mod 6 = 0. The GCD is 6.
• Topological Sorting
Removes nodes with no incoming edges from the graph one at a time and
recursively applies this to the remaining subgraph.
Scenario: Given a directed graph of tasks with dependencies, Topological Sorting
would repeatedly remove tasks that have no dependencies and add them to the
sorted order, ensuring that all dependencies are resolved.
• Russian Peasant Multiplication
Multiplies two numbers by halving one number and doubling the other, summing
the values when the halved number is odd.
Scenario: To multiply 18 by 25, Russian Peasant Multiplication repeatedly halves 18
and doubles 25: (9, 50), (4, 100), (2, 200), (1, 400). Summing the relevant products
(50 and 400) gives the final product 450.
• Counting Sort
Counts the occurrences of each distinct element in the array and uses this
information to place each element at the correct position in the output array.
Scenario: Given the array [3, 5, 2, 9, 4], Counting Sort counts occurrences: 2 occurs
once, 3 occurs once, 4 occurs once, 5 occurs once, 9 occurs once. It then
constructs the sorted array [2, 3, 4, 5, 9] based on these counts.
COSC90- DESIGN AND ANALYSIS OF ALGORITHMS
4. Dynamic Programming
Dynamic programming is a method of optimization that solves complex problems by
dividing them into simpler sub-problems, caching the results to prevent unnecessary
recalculations. This approach is especially beneficial for problems characterized by
overlapping sub-problems and an optimal substructure.
Scenario: Given items with weights and values, and a knapsack capacity of 50, the
algorithm constructs a table of the maximum values for each capacity up to 50,
ultimately determining the optimal set of items to maximize value without
exceeding capacity.
• Matrix Chain Multiplication
Finds the optimal way to parenthesize a chain of matrices to minimize the number
of scalar multiplications by solving subproblems of multiplying smaller subchains
and using these solutions to build up to the final solution.
Scenario: Given matrices A1 (10x20), A2 (20x30), and A3 (30x40), the algorithm
determines the optimal order of multiplication (e.g., (A1(A2A3)) or ((A1A2)A3)) to
minimize the total number of multiplications.
• Edit Distance
Measures the minimum number of operations (insertions, deletions, substitutions)
required to convert one string into another by constructing a table of edit distances
for substrings and using these to find the edit distance for the full strings.
Scenario: To convert "kitten" to "sitting", the algorithm constructs a table showing
the minimum operations for each prefix pair, ultimately finding the edit distance to
be 3 (substitute 'k' with 's', substitute 'e' with 'i', and insert 'g').
• Coin Change Problem
Finds the minimum number of coins needed to make a specific amount from a given
set of denominations by building a table of solutions to subproblems of smaller
amounts.
Scenario: Given denominations [1, 3, 4] and a target amount of 6, the algorithm
constructs a table showing the minimum number of coins needed for each amount
up to 6, determining that the minimum coins needed for 6 is 2 (using coins 3 and 3).