Greedy algorithms are an approach to solving certain kinds of optimization problems. Greedy algorithms are similar to dynamic programming algorithms in that the solutions are both efficient and optimal if the problem exhibits some particular sort of substructure. A greedy algorithm builds a solution by going one step at a time through the feasible solutions, applying a heuristic to determine the best choice. A heuristic applies an insight to solving the problem, such as always choose the largest, smallest, etc.
In general, greedy algorithms have five components:
Greedy algorithms produce good solutions on some mathematical problems, but not on others.
Greedy algorithms should be applied to problems exhibiting these two properties:
Note:The traveling salesman problem doesn't have this property, and therefore the greedy algorithm solution isn't right for it.
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.
Download PDFChange making C program using a greedy algorithm. Making money/coin change using the USD coin set {25,10,5,1}
Get the code here:
https://github.com/randerson112358/C-Programs/blob/master/greedy.c
Here we will determine the minimum number of coins to give while making change using the greedy algorithm. The coins in the U.S. currency uses the set of coin values {1,5,10,25}, and the U.S. uses the greedy algorithm which is optimal to give the least amount of coins as change. It is optimal because
Problems with greedy algorithm (when the greedy algorithm isn't optimal):
Let's use the greedy algorithm to give us the least amount of coins for 36 cents. We will use the coin set {1, 5, 10, 20}.
An example of making change with the least amount of coins:
Problem: Given a set of coins {1,5,10,25,50} use a greedy algorithm to give the minimum amount of coins as change.
Download PDF/** This programs uses the greedy algorithm to give the least amount of change back. Written in the C programming language By: (randerson112358) */ # include <stdio.h> int main(void) { int changeOwed; int check; char invisibleChar; int count = 0; int numQ=0, numD=0, numN=0, numP=0; /***Run this loop until the user inputs a number and it is greater than or equal to 0***/ do{ printf("How much change is owed (in cents)?\n"); check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise //Get's rid of any extra invisible characters if the input is a char/String do{ scanf("%c",&invisibleChar); }while(invisibleChar != '\n'); }while(check == 0 || !(changeOwed >=0 )); int c = changeOwed;// The variable c was only used to shorten my typing //Continue to do this loop until while(c > 0){ //Use as many quarters needed while(c >= 25){ count ++; numQ++; c = c - 25; } //Use as many dimes needed while(c >= 10){ count ++; numD++; c = c - 10; } //Use as many nickels needed while(c >= 5){ count ++; numN++; c = c - 5; } //Use as many pennies needed while(c >= 1){ count ++; numP++; c = c - 1; } } printf("Quarters: %d, Dimes: %d, Nickels: %d, Pennies: %d\nNumber of coins used= %d\n\n", numQ, numD, numN, numP, count); system("pause"); //Comment this out if you are not using windows operating system }