Complete the code to check if the problem has optimal substructure property.
if (problem_has_optimal_substructure == [1]) { printf("Use DP\n"); } else { printf("Use Greedy\n"); }
If a problem has optimal substructure, it means the best solution can be built from best solutions of subproblems, so dynamic programming (DP) is suitable.
Complete the code to check if the problem has greedy choice property.
if (problem_has_greedy_choice_property == [1]) { printf("Use Greedy\n"); } else { printf("Use DP\n"); }
Greedy choice property means a local optimal choice leads to a global optimal solution, so greedy algorithms work.
Fix the error in the code that decides which algorithm to apply based on properties.
if (has_optimal_substructure && [1]) { printf("Use DP\n"); } else if (has_greedy_choice_property) { printf("Use Greedy\n"); } else { printf("No simple solution\n"); }
DP is used when the problem has optimal substructure but does not have greedy choice property.
Fill both blanks to complete the function that decides algorithm type based on problem properties.
const char* choose_algorithm(bool has_optimal_substructure, bool has_greedy_choice_property) {
if (has_optimal_substructure && [1]) {
return "DP";
} else if ([2]) {
return "Greedy";
} else {
return "No simple solution";
}
}The function returns "DP" if the problem has optimal substructure but not greedy choice property, "Greedy" if it has greedy choice property, else no simple solution.
Fill all three blanks to complete the code that prints the recommended algorithm based on problem properties.
void print_algorithm(bool has_optimal_substructure, bool has_greedy_choice_property) {
if (has_optimal_substructure && [1]) {
printf("Use DP\n");
} else if ([2]) {
printf("Use Greedy\n");
} else if ([3]) {
printf("No simple solution\n");
}
}The code prints "Use DP" if optimal substructure is true and greedy choice property is false, "Use Greedy" if greedy choice property is true, else "No simple solution".