You are on page 1of 20

Greedy Method

Reported by: NHOEL C. RESOS

The Greedy Method

The greedy method is a general algorithm
design paradigm, built on the following elements:
configurations: different choices, collections, or values
to find
objective function: a score assigned to configurations,
which we want to either maximize or minimize

It works best when applied to problems with the

greedy-choice property:
a globally-optimal solution can always be found by a
series of local improvements from a starting

The Greedy Method

What is a greedy algorithm?

Greedy algorithm: an algorithm always
makes the choice that looks best at the
Human beings use greedy algorithms a
How to maximize your final grade of this
How to become a rich man?
How does a casher minimize the number of
coins to make a change?

What is a greedy algorithm?

To guarantee that a greedy algorithm is
2 things have to be proved:
Greedy-choice property: we can
assemble a globally optimal solution by
making locally greedy(optimal) choices.
i.e. The greedy choice is always part of certain
optimal solution

Optimal substructure: an optimal solution

to the problem contains within it optimal
solutions to subproblems.
i.e. global optimal solution is constructed from
local optimal solutions

What is Greedy Algorithm?

In the hard words: Agreedy algorithmis
analgorithmthat follows the
problemsolving heuristics of making the
locally optimal choice at each stage with
the hope of finding a global optimum.

Simplify: Choose the best choice that

reachable at current state

To construct the solution in an optimal way. Algorithm
Maintains two sets,
-One contains chosen items and
-The other contains rejected items.
Greedy algorithms make good local choices in the hope that
They result in,
-An optimal solution.
-Feasible solutions.

The greedy algorithm consists of four (4) function.
A function that checks whether chosen set of items provide a
A function that checks the feasibility of a set.
The selection function tells which of the items is the most
An objective function, which does not appear explicitly, gives
the value of a solution.

An optimization problem:
Given a problem instance, a set of constraints and an
objective function.
Find a feasible solution for the given instance for which the
objective function has an optimal value.
Either maximum or minimum depending on the problem being
solved.A feasible solution that does this is called optimal

A feasible solution satisfies the problems constraints

The constraints specify the limitations on the required

Sample Usage of Greedy

For better explanation we use old
simple problem: Travelling Salesman

The Problem is how to travel from city A
and visit all city on the map, then back to
city A again.

The rules: you only visit each city once and

you cant pass through any traversed path.

Find the shortest path from city A(start) to
any other city.

Because the nearest city is B, so we go to B

From B, we find any other city but

A(because A has been visited) that has
nearest path. So we choose C:

Keep tuning on

From C, we look to nearest city again,

but dont look for A and B, because
both has been visited. So we choose D.

Soon end

At this node(D), we cant go to any city,

because all neighbor of D has been
visited. We go back to first city(A).

And that was how to solve TSP problem.

Sample Usage of Greedy

Theknapsack problemorrucksack problemis a problem incombinatorial optimization: Given a set of items,

each with a weight and a value, determine the number of each item to include in a collection so that the total
weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the
problem faced by someone who is constrained by a fixed-sizeknapsackand must fill it with the most valuable
The problem often arises inresource allocationwhere there are financial constraints and is studied in fields such
ascombinatorics,computer science,complexity theory,cryptography,applied mathematics, and
daily fantasy sports.
The knapsack problem has been studied for more than a century, with early works dating as far back as 1897.[1]
The name "knapsack problem" dates back to the early works of mathematicianTobias Dantzig(18841956),[2]
and refers to the commonplace problem of packing your most valuable items without overloading your luggage.

George Dantzig proposed a greedy approximation algorithm to solve the unbounded knapsack problem.[19] His version
sorts the items in decreasing order of value per unit of weight,
. It then proceeds to insert them into the sack,
starting with as many copies as possible of the first kind of item until there is no longer space in the sack for more.
Provided that there is an unlimited supply of each kind of item, if
is the maximum value of items that fit into the
sack, then the greedy algorithm is guaranteed to achieve at least a value of
. However, for the bounded
problem, where the supply of each kind of item is limited, the algorithm may be far from optimal.

Knapsack problems appear in real-world decision-making processes in a wide variety of fields,

such as finding the least wasteful way to cut raw materials,[4]selection ofinvestmentsand
portfolios,[5]selection of assets forasset-backed securitization,[6]and generating keys for
theMerkleHellman[7]and otherknapsack cryptosystems.

Advantage of Greedy
Greedy is easy to be implemented.
Just search the best choice from the
current state that reachable (has
any paths or any connections).
In simple case, greedy often give you
the best solution.

Drawback of Greedy
In large and complex case, greedy
doesnt always give you the best
solution, because its just search and
take the best choice that you can
reach from the current state.
It takes longer time than any other
algorithms for big case of problem


They are easier to implement,
they require much less computing resources,
they are much faster to execute.
Greedy algorithms are used to solve optimization problems
Their only disadvantage being that they not always reach the
global optimum solution;
on the other hand, even when the global optimum solution is
not reached, most of the times the reached sub-optimal
solution is a very good solution.