What is Greedy algorithm?

An algorithm design method that builds a solution by repeatedly picking the choice that looks best at that moment, without revisiting earlier choices. It is fast and simple, but it gives an optimal solution only when the problem has special properties, as with coin systems like Japan's or Kruskal's algorithm for minimum spanning trees.

Japanese name: 貪欲法

Greedy algorithm appears in 2 practice questions across 1 topic, including Data Structures and Algorithms.

Field: Data Structures and Algorithms

Try it: questions about Greedy algorithm

You pay 8 yen with coins of 1 yen, 4 yen and 6 yen. Which correctly relates the number of coins chosen by a greedy algorithm, which uses as many of the largest coin as possible first, to the minimum number of coins?

  1. AThe greedy algorithm also reaches the minimum of 2 coins
  2. BThe greedy algorithm uses 3 coins and misses the minimum of 2
  3. CThe greedy algorithm uses 3 coins, and that is the minimum
Show answer and explanation

Answer: B. The greedy algorithm uses 3 coins and misses the minimum of 2

The greedy algorithm first takes one 6-yen coin and pays the remaining 2 yen with two 1-yen coins, for 3 coins. But two 4-yen coins pay it with just 2. Because a greedy algorithm only accumulates the choice that looks best at each moment, it is not optimal for some coin systems. Dynamic programming, which fills in a table of the minimum number of coins for every amount from 0 to 8 yen in increasing order, always finds the minimum. For coin systems such as Japan's 1, 5, 10, 50, 100 and 500 yen, the greedy algorithm always gives the minimum.

Greedy algorithm in the explanations

Questions that mention Greedy algorithm (2)

  1. You pay 8 yen with coins of 1 yen, 4 yen and 6 yen. Which correctly relates the number of coins chosen by a greedy algorithm, which uses as many of the largest coin as possible first, to the minimum number of coins?
  2. A knapsack with capacity 8 is filled with items chosen from four items with (weight, value) of (2, 3), (3, 4), (4, 5) and (5, 6). There is only one of each item and items cannot be split. What is the maximum total value with a total weight of 8 or less?

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Dynamic programming Binary heap Binary search Binary search tree Breadth-first search Depth-first search Dijkstra's algorithm Hash table Linked list Queue

Back to the glossary