By the end of this lab session, you should be able to:
Here are the activities for which you will upload solutions to xSITe or Gradescope:
Select the best answer for each question. Some questions may have more than one correct option. In those cases, select all that apply.
Which of the following conditions is MOST specific to proving that a greedy algorithm—rather than dynamic programming—will always produce an optimal solution?
Which of the following problems can be correctly solved using a greedy algorithm? There may be more than one correct option.
A greedy algorithm produces a suboptimal solution for a particular problem instance. Which conclusion is the MOST accurate?
Why is the Fractional Knapsack problem solvable using a greedy algorithm, while the 0/1 Knapsack problem is not?
Which of the following are valid advantages or limitations of greedy algorithms? There may be more than one correct option.
You are given an undirected graph represented by n vertices and m edges. Write a program to determine the minimum number of colors required to color the graph such that no two adjacent vertices have the same color.
Map Coloring & Geographic Boundaries. Coloring countries on a map so that no two neighboring countries share the same color. This is essentially the same problem, just visualized geographically.
0 1
0 2
1 2
1 3
2 4
3 4
Vertex Color
0 0
1 1
2 2
3 0
4 1
Minimum number of colors required: 3
This problem is NP-complete, meaning that finding an exact solution efficiently for large graphs is computationally infeasible.
However, heuristic approaches like the greedy coloring algorithm can provide approximate solutions. The greedy algorithm doesn’t always yield the optimal (minimum) number of colors. Its effectiveness heavily depends on the vertex ordering.
Ref TextBook Section 34-3 for more graph coloring problems.

You are a backpacker and you have a knapsack with a weight capacity of W. You are given n items, where each item has a weight and a value. Unlike the 0/1 knapsack problem, you are allowed to take fractions of items (i.e., you can take a part of an item, not necessarily the entire item).
Your goal is to maximize the total value of the items in the knapsack without exceeding the weight limit.
Maximizing value under a capacity constraint, especially in resource management. - Cargo Loading & Shipping. E.g. Airlines, shipping companies, or truck logistics need to decide which goods to load given limited weight or volume capacity.
Calculate Value-to-Weight Ratio: For each item, compute the ratio of its value to its weight (value/weight).
Sort Items by Ratio: Sort all items in descending order based on their value-to-weight ratio.

Initialize the total value to 0 and the remaining capacity of the knapsack to W.
Iterate through the sorted list of items:
If the item's weight is less than or equal to the remaining capacity, add the entire item's value to the total value and decrease the remaining capacity accordingly.
If the item's weight exceeds the remaining capacity, take the fraction of the item that fits, add the corresponding fraction of its value to the total value, and fill the knapsack to its capacity.
Terminate When Full: Stop the process once the knapsack reaches its weight capacity or all items have been considered.
Week 12: Greedy Algorithm (Lab)