0% found this document useful (0 votes)
18 views7 pages

Daa Project

Design and Analysis of Algo

Uploaded by

Meg Aera
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)
18 views7 pages

Daa Project

Design and Analysis of Algo

Uploaded by

Meg Aera
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/ 7

COSC90- DESIGN AND ANALYSIS OF ALGORITHMS

Members:

BACUNAWA, JUVIE ANN R.


ORDOÑEZ, JOHN HENRY B.
PONCE, JANLLOYD ANGELO T.
RODELAS, ARRIZA V.
SANTOS, MICHELLE JANE T.
SATSATIN, JOHN DAENIELE DG.

ALGORITHMS AND ITS EXAMPLES

1. Brute Force Algorithm


Brute force algorithms are straightforward approaches to solving problems by trying all
possible solutions until the correct one is found. This method is often simple to implement
but can be inefficient for large problem spaces due to its exhaustive nature.

Brute Force Examples:

• 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.

2. Divide and Conquer Algorithm


Divide and conquer algorithms solve problems by breaking them down into smaller sub-
problems, solving each sub-problem independently, and then combining their solutions to
solve the original problem. This technique often leads to more efficient algorithms than
brute force.

ASCENDING DESCENDING
COSC90- DESIGN AND ANALYSIS OF ALGORITHMS

Divide and Conquer Examples:


• Quick Sort
Divides the array around a pivot element into two sub-arrays, recursively sorts each
sub-array, and combines them.
Scenario: Given the array [3, 5, 2, 9, 4], Quick Sort might choose 4 as the pivot,
partitioning the array into [3, 2] and [5, 9]. It then recursively sorts each sub-array and
combines the results to get [2, 3, 4, 5, 9]. This method is efficient for large arrays due to
its logarithmic depth of recursion.
• Binary Search
Divides the search interval in half and recursively searches in the left or right half
depending on the comparison with the middle element.
Scenario: Given the sorted array [1, 2, 3, 4, 5, 6, 7] and target value 5, Binary Search
would first compare 5 to the middle element 4, then search the right half [5, 6, 7], and
find the target at the first position of the new interval.
• Strassen's Matrix Multiplication
Divides matrices into smaller sub-matrices and uses a recursive approach to compute
the product, reducing the number of multiplicative operations compared to the
standard method.
• Scenario: Given two 4x4 matrices, Strassen's algorithm divides each matrix into four
2x2 sub-matrices, performs multiplication and addition on these sub-matrices
recursively, reducing the total number of scalar multiplications compared to the
conventional method.
COSC90- DESIGN AND ANALYSIS OF ALGORITHMS

• Closest Pair of Points


Divides the set of points into subsets, recursively finds the closest pair in each subset,
and checks for the closest pair that straddles the dividing line.
Scenario: Given a set of points on a plane, the algorithm first sorts the points by their x-
coordinates, then divides the points into two halves, recursively finds the closest pair in
each half, and finally checks the points near the dividing line to find the closest pair
overall.
• Karatsuba Algorithm
Multiplies two large numbers by splitting each number into two halves and using a
recursive approach to reduce the number of multiplicative operations.
Scenario: Given two large numbers 1234 and 5678, Karatsuba's algorithm splits them
into 12, 34 and 56, 78, respectively, performs three multiplications on these smaller
numbers, and combines the results using additions and shifts to get the final product.
This reduces the multiplication complexity from O(n^2) to O(n^log2(3)).

3. Decrease and Conquer Algorithm


Decrease and conquer is a problem-solving technique that involves breaking down a
complex problem into smaller, more manageable parts. This approach allows for easier
analysis and solution of the problem by focusing on one part at a time.
MULTIPLY ADDITION

Decrease and Conquer Examples:


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.

Dynamic programming is widely used in various domains such as operations research,


bioinformatics, finance, and computer graphics to solve complex problems efficiently by
leveraging the power of reusing previously computed results.

BOTTOM-UP APPROACH TOP-DOWN APPROACH

Dynamic Programming Examples:


• Longest Common Subsequence (LCS)
Finds the longest subsequence common to two sequences by building a table of
solutions to subproblems and using these to construct the final solution.
Scenario: Given sequences "ABCD" and "ACBAD", LCS constructs a table showing
the length of the LCS for each pair of prefixes and uses this to find the final LCS
"ACD".
• Knapsack Problem
Finds the maximum value subset of items that can be included in a knapsack of
limited capacity by building a table of the maximum values for each capacity and
subset of items.
COSC90- DESIGN AND ANALYSIS OF ALGORITHMS

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).

You might also like