Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views5 pages

Greedy Algorithms

The document explains the concepts of optimal and feasible solutions in algorithm design, emphasizing the role of greedy algorithms in problem-solving. It details the greedy choice property and optimal substructure, illustrating how greedy algorithms can efficiently tackle various applications like network routing and resource allocation. Additionally, it covers Strassen's matrix multiplication algorithm, highlighting its improved time complexity compared to traditional methods.
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)
6 views5 pages

Greedy Algorithms

The document explains the concepts of optimal and feasible solutions in algorithm design, emphasizing the role of greedy algorithms in problem-solving. It details the greedy choice property and optimal substructure, illustrating how greedy algorithms can efficiently tackle various applications like network routing and resource allocation. Additionally, it covers Strassen's matrix multiplication algorithm, highlighting its improved time complexity compared to traditional methods.
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/ 5

In Design and Analysis of Algorithms (DAA), an optimal solution is a feasible

solution that provides the best possible outcome (either maximum or minimum) for a
given problem, based on a defined objective function. It's the "best of the best"
among all feasible solutions.

:
Feasible vs. Optimal:
 Feasible Solution:
A solution that satisfies all the constraints of the problem. It's a valid solution, but
not necessarily the best one.
 Optimal Solution:
A feasible solution that also optimizes the objective function. This means it either
maximizes or minimizes the function, depending on the problem's goal.
Example:
Imagine a problem where you need to find the shortest path between two points in a
network. A feasible solution would be any path that connects the two
points. However, the optimal solution would be the path with the minimum distance.

What is Greedy Algorithm?

A greedy algorithm is a problem-solving approach that makes the best possible


choice at each step to achieve a solution [choice that seems right at each step,
without thinking about the future].(ie) The idea is to choose the option that looks best
at the moment, without worrying about the overall problem.

(ie)It doesn't worry whether the current best result will bring the overall optimal
result.
This algorithm may not produce the best result for all the problems. It's because it
always goes for the local best choice to produce the global best result.

It focuses on taking the most immediate, obvious solution that looks like it will work
best right now. This approach doesn’t worry about whether the decision will be
perfect in the long run, just that it’s the best option at the moment.

Understanding Greedy Algorithm With Real-life Example

Imagine you are hungry and want to eat something. You walk into a store with
different food options. You have only a few coins with you, and you want to get the
most food for your money.
A greedy approach would be to look at all the food, pick the one that gives you the
most for your coins, buy it, and then repeat with the coins you have left. You’re not
planning ahead for what you might want later, just grabbing the best deal you see
right now.
In some cases, this approach works perfectly and gives you the best possible
outcome. In other cases, it might not give you the best overall result, but it’s a quick
and easy way to solve the problem. Greedy algorithms are like this—they work by
making decisions that seem best in the moment, which can lead to a good solution
quickly and easily.
Components of Greedy Algorithm

1. Greedy Choice Property

This property means that a greedy algorithm always makes the choice that seems
the best at the moment. It selects the option that offers the most immediate benefit
without considering the future consequences.
The idea is that by choosing the best local option at each step, the algorithm will
eventually reach an optimal solution for the entire problem.
2. Optimal Substructure

Optimal substructure refers to the idea that a problem can be broken down into
smaller subproblems, and the optimal solution to the overall problem can be
achieved by solving these subproblems optimally.
In other words, if you solve each small part of the problem in the best possible way,
you can combine these solutions to get the best solution for the whole problem. This
is a key characteristic that allows greedy algorithms to work effectively.
How Greedy Algorithms Work? /

Components of Greedy Algorithm

Greedy algorithms solve problems by following a simple, step-by-step approach:


1. Identify the Problem: The first step is to understand the problem and determine
that it can be solved using a greedy approach. This usually involves recognizing that
the problem can be broken down into a series of decisions.
2. Make the Greedy Choice / Greedy Choice Property: At each step of the
problem, the algorithm makes the best choice that seems optimal at the moment.
This choice is made based on the greedy choice property, which means selecting
the option that offers the most immediate benefit.
3. Solve the Subproblems / Optimal Substructure: After making a greedy choice,
the problem is reduced to a smaller subproblem. The algorithm then repeats the
process—making another greedy choice for the smaller problem.
4. Combine the Solutions: The algorithm continues making greedy choices and
solving subproblems until the entire problem is solved. The solution to the overall
problem is simply the combination of all the greedy choices made at each step.
In other words, if you solve each small part of the problem in the best possible way,
you can combine these solutions to get the best solution for the whole problem. This
is a key characteristic that allows greedy algorithms to work effectively

General Method of Greedy Algorithm:


Algorithm Greedy(a,n) // a[l:n]
contains the n inputs.
{
Solution =0; //
Initializethe solution.
for i = 1 to n do
{
x = Select(a);
if Feasible(solution, x) then
solution:= solution + x //
solution:= Union (solution,x)
}
Return solution;
}

Applications of Greedy Algorithms

 Network Routing: Greedy algorithms like Dijkstra’s algorithm are used to find
the shortest path in network routing, optimizing data packet delivery across the
internet.
 Task Scheduling: Used in job sequencing problems to maximize profit or
minimize deadlines by selecting jobs in order of priority.
 Resource Allocation: Applied in scenarios like the fractional knapsack
problem to allocate resources efficiently based on value-to-weight ratios.
 Data Compression: Huffman coding uses a greedy approach to compress
data efficiently by encoding frequently used characters with shorter codes.
 Coin Change: A greedy approach is often used to provide the minimum
number of coins for a given amount of money, particularly when the
denominations are standard (like in most currencies).
 Greedy Coloring: Applied in graph theory for problems like graph coloring,
where the goal is to minimize the number of colors needed to color a graph
while ensuring no two adjacent vertices share the same color.
 Traveling Salesman Problem (TSP) Heuristic: Greedy heuristics are used in
approximating solutions to the TSP by selecting the nearest unvisited city at
each step.

The 0/1 Knapsack Problem

Rules:

 Every item has a weight and value.


 knapsack has a weight limit.
 Choose items you want to bring in the knapsack.
 Cannot take half of an item, either take an item or not.

Goal:

 Maximize the total value of the items in the knapsack.


Strassen's matrix multiplication algorithm is a recursive, divide-and-conquer
algorithm for multiplying matrices that offers a better asymptotic time complexity
than the standard matrix multiplication algorithm for large matrices.

 Calculate Seven Products (P1 to P7): Instead of the usual eight


multiplications required in standard block matrix multiplication, Strassen's
algorithm calculates seven specific products using combinations of additions
and subtractions of the sub-matrices.

Use formula
P1 = A11 * (B12 - B22)
P2 = (A11 + A12) * B22
P3 = (A21 + A22) * B11
P4 = A22 * (B21 - B11)
P5 = (A11 + A22) * (B11 + B22)
P6 = (A12 - A22) * (B21 + B22)
P7 = (A11 - A21) * (B11 + B12)

These products are computed recursively using Strassen's algorithm itself until the
sub-matrices are small enough to be multiplied directly (e.g., 1×1 matrices).

 Combine: Use the seven products to compute the four sub-matrices of the
resulting product matrix C.

Use formula
C11 = P5 + P4 - P2 + P6
C12 = P1 + P2
C21 = P3 + P4
C22 = P1 + P5 - P3 - P7
Construct Result: Combine these four sub-matrices to form the final product matrix
C.
C = | C11 C12 |
| C21 C22 |

Time Complexity:
The standard matrix multiplication algorithm has a time complexity of O(N^3)
Strassen's algorithm, by reducing the number of recursive multiplications from 8 to
7, achieves a time complexity of O(Nlog27)which is approximately O(N^2.81). This
makes it asymptotically faster for large matrices, although the overhead of additions
and subtractions can make it less efficient for smaller matrices compared to the
naive algorithm.

You might also like