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:
You are driving a car from city A to city B. The distance between the two cities is D miles. Your car can travel up to M miles on a full tank of gas. Along the way, there are N gas stations at distances S1,S2,…,SN miles from city A (with S1 < S2 < ⋯ < SN < D) You start with a full tank of gas.
Your task is to determine the minimum number of stops you need to make to reach city B. If it is not possible to reach city B, return −1.
You can find the template files on GitHub repository. Complete the partial gas_station.cpp file. The make command will compile the project.
To receive credit for your work, upload your completed gas_station.cpp file to the Gradescope assignment titled [“Gas Stops”]
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.
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
You can find the template files on GitHub repository. Complete the partial graph_coloring.cpp file. The make command will compile the project.
To receive credit for your work, upload your completed graph_coloring.cpp file to the Gradescope assignment titled [“Color Graph”]
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.
You can find the template files on GitHub repository. Complete the partial knapsack.cpp file. The make command will compile the project.
To receive credit for your work, upload your completed knapsack.cpp file to the Gradescope assignment titled [“Knapsack”]
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)