> For the complete documentation index, see [llms.txt](https://ksmcclay.gitbook.io/no-code-computer-science/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ksmcclay.gitbook.io/no-code-computer-science/algorithms/greedy.md).

# Greedy

A Greedy Algorithm chooses the best local option

## What is a Greedy Algorithm? :money\_mouth:&#x20;

A **Greedy** Algorithm always takes the best possible local option available.

![Wall Street Bull](https://3642656209-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLGBprg9M6rsaEbC96_%2F-MMSz8LCkS7pKUvsV9Nl%2F-MMTVMcoNvOxtScFqTLt%2Falec-favale-9lI7KVWXVX0-unsplash.jpg?alt=media\&token=f3692117-2d66-444a-afc2-11f54130521e)

## Greedy is Good? :star\_struck:&#x20;

A Greedy algorithm is very useful when choosing between clear right and wrong choices that do not change throughout the algorithms completion.

* Greedy will work:smile:&#x20;
  * Choosing a career based on total salary
  * Choosing a meal based on the biggest size
  * Choosing a product based on price
* Greedy will **Not** work:face\_vomiting:&#x20;
  * Choosing a career based on happiness
  * Choosing a meal based on taste
  * Choosing a product based on quality

## :robot:**Lets Create A Wall Street Trading Bot**:robot:&#x20;

The wall street trading bot is using a simple **Greedy** algorithm which is: *Buy Low & Sell High*. With this greedy algorithm the trading bot plans to maximize profit. To give the bot a chance against all of the wall street "Bulls". The bot will be given the next five days of stock prices for a specific company.&#x20;

* Stock prices for the next 5 days&#x20;
  * $11, $8, $18, $4, $12
* Trading Algorithm :robot:&#x20;
  * Buy Today&#x20;
    * if the Price Increases Tomorrow
  * Sell Today&#x20;
    * if the Price Decreases Tomorrow
* Sample Trading :clock4:&#x20;
  * Next 5 Days = $11, $8, $18, $4, $12
* Transaction Logs of Bot:robot:&#x20;
  * $11 = Nothing
  * $08 = Buys&#x20;
  * $18 = Sells&#x20;
  * $04 = Buys
  * $12 = Sells
  * Profit = 18-8 + 12-4 = $16

:fox:This strategy is the best possible option for this problem but can break down on different types of problems:fox:&#x20;

## :joystick:**Lets Make a Super Mario Bros Algorithm**:joystick:

The **Greedy** algorithm is trying to beat the Super Mario game as fast as possible. The algorithm must obey the rules we set. Algorithm Rules:

* Mario always runs forward
* Mario always jumps on or over enemies
* Mario always jumps over obstacles

The **greedy** part of the algorithm is always "greedily" moving forward, this was chosen because it seems to be the fastest way to complete the level.

* Good:space\_invader:&#x20;
  * Simple strategy
  * Easy to implement
  * Mario will complete levels fast
* Bad :poop:&#x20;
  * Mario will **Not** find&#x20;
    * Secrets
    * Bonus content
  * Faster solutions are possible with shortcuts
* Ugly:skull\_crossbones:&#x20;
  * Unforeseen circumstance can make the game unbeatable
  * Unique enemy (Bowser)
    * Can **Not** defeat using normal jump attacks
  * Unique level (Swim)&#x20;
    * Can **Not** navigate using normal controls
  * Unique puzzle (collect keys)
    * Can **Not** win without doing something

:moneybag:A **Greedy** Algorithm can be short sighted:moneybag:&#x20;
