Lab 5: Sorting of backpack problems and time-bound jobs, and time-limit of backpacks
I. Experiment Name: Sorting of backpack problems and time-bound jobs
Ii. Tutorial purpose: to master the ideas and general processes of greedy algorithms for solving problems and learn to use greedy algorithms to solve practical problems.
Iii. experiment content
Lab problems and program running results:
Part 1 backpack Problems
1. Analyze programs 6-1 on page 1 and draw a flowchart.
2. Analyze the Knapsack class and private variables, which respectively indicate the following meanings:
3. Analyze the greedyKnapsack program and draw a flowchart of the algorithm.
4. When the backpack is:
M = 20:
The output of the solution array is:
5. Improve the greedyKnapsack program so that the output solution array is the same as the original sequence. When the backpack value is:
W |
8 |
2 |
25 |
7 |
4 |
7 |
4 |
P |
10 |
5 |
11 |
15 |
3 |
13 |
1 |
When M = 45, the output of the solution array is:
Part 2 time-bound job Sorting Algorithm
Time Limit |
2 |
1 |
2 |
4 |
3 |
Value |
100 |
10 |
15 |
27 |
9 |
The final result of the greedy algorithm is:
Iv. Experiment summary and experiences: