An optimal solution to the problem

Source: Internet
Author: User

Http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Greedy/greedyIntro.htm

Greedy Introduction

Greedy algorithms is simple and straightforward. They is shortsighted in their approach in the sense this they take decisions on the basis of information at hand without Worrying about the effect these decisions is the future. They is easy-to-invent, easy-to-implement and most of the time quite efficient. Many problems cannot is solved correctly by greedy approach. Greedy algorithms is used to solve optimization problems

Greedy Approachgreedy algorithm works by making the decision, seems most promising at any moment; it never reconsiders this decis Ion, whatever situation may arise later.

As an example consider the problem of "Making".

Coins available is:

  • dollars (cents)
  • Quarters (cents)
  • Dimes (cents)
  • Nickels (5 cents)
  • Pennies (1 cent)

problem Make a change of a given amount using the smallest possible number of coins.

Informal algorithm

    • Start with nothing.
    • At every stage without passing the given amount.
      • Add the largest to the coins already chosen.

Formal algorithm

Make change for n units using the least possible number of coins.

Make-change (n)
C←{25,10, 5, 1}//constant.
Sol←{}; Set Thatwill hold the solution set.
Sum←0 sum Ofitem in solution set
while sum is not = n
x =largest item in set C such that sum + x≤n
IFNo such item then
RETURN "No Solution"
S←S {Value OFX}
Sum←Sum + x
RETURN S

Example Make A to 2.89 (289 cents) here n =2.89 and the solution contains 2 dollars, 3 quarters, 1 dime and 4pennies. The algorithm is greedy because at every stage it chooses thelargest coin without worrying about the consequences. Moreover, itnever changes its mind in the sense of once a coin has been includedin the solution set, it remains there.

Characteristicsand Features of problems solved by greedy algorithms


To construct the solution on an optimal. Algorithm Maintainstwo sets. One contains chosen items and the other contains Rejecteditems.

The greedy algorithm consists of four (4) function.

    1. A function that checks whether chosen set of items provide a solution.
    2. A function that checks the feasibility of a set.
    3. The selection function tells which of the candidates are the most promising.
    4. An objectivefunction, which does not appear explicitly, gives the value of a solution.

Structuregreedy algorithm

    • Initially the set of chosen items is empty i.e.,solution set.
    • At each step
      • Item'll is added in a solution set by usingselection function.
      • IF the set would no longer be feasible
        • Reject items under consideration (and Isnever consider again).
      • ELSE IF set is still feasible then
        • Add the current item.
 Definitions OffeasibilityA feasible Set (ofcandidates) is promising if it can being extended to produce not merely asolution, but an optimal solut Ion to the problem. In particular, Theempty set are always promising why? (Because an optimal solution alwaysexists)

Unlike Dynamic programming, which solves Thesubproblems bottom-up, a greedy strategy usually progresses in Atop-down f Ashion, making one greedy choice after another, reducing Eachproblem to a smaller one.

Greedy-choiceproperty

The "Greedy-choice property" and "Optimalsubstructure" is the ingredients in the problem, lend to a greedystrategy.

Greedy-choiceproperty

It says a globally optimal solution can bearrived at by making a locally optimal choice.

An optimal solution to the problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.