탐욕적인 알고리즘은 결정을 해야 할 때마다 그 순간에 가장 좋다고 생각되는 것을 해답으로 선택함 으로써 최종적인 해답에 도달한다.
❗️ 하지만, 그 순간의 선택이 그 당시( local )에는 최적이다. 최선이라고 생각했던 답들을 모아서 최종적인 ( gobal) 해답을 만들었다고 해서, 그 해답이 궁긍적으로 최적이라는 보장은 없다!
→ 최적의 해답인지 검증 필요 !!
그리디 알고리즘에 대해 알아보기 위한 예시 알고리즘이다.
동전의 개수가 최소가 되도록 거스름 돈을 주는 문제
의사 코드
While (there are more coins and the instance is not solved)
Grab the largest remaining coin; // selection procedure
If (adding the coin makes the change exceed the amount owed) // feasibility check
reject the coin;
else
add the coin to the change;
If (the total value of the change equals // solution check
the amount owed)
the instance is solved;
}
while 반복문If (adding the coin makes the change exceed the amount owed)if (the total value of the change equals the amount owed)