labb
labb
Fractional Knapsack
1. Problem Statement
Maximize the value of items in a knapsack of a given capacity, where each item can be partially taken
(fractional).
2. Problem Analysis
Input: List of items, each with a weight and value, and knapsack capacity.
Objective: Select items to maximize value without exceeding capacity.
Constraint: Items can be fractionally selected.
3. Algorithm Overview
1. Sort items by value-to-weight ratio.
2. Greedily select items in descending ratio order until capacity is full.
4. Time Complexity / Space Complexity
TC: O(nlogn) (sorting items by ratio).
SC: O(1) (constant extra space).
5. Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Item {
int value, weight;
Item(int v, int w) : value(v), weight(w) {}
double valuePerWeight() const { return (double)value / weight; }
};
1. Problem Statement
Split a tree’s vertices with minimum cuts to satisfy certain properties (e.g., balance constraints or
spanning subtrees).
2. Problem Analysis
Input: Tree structure with vertex properties.
Objective: Minimize cuts while satisfying given vertex properties.
Constraints: The tree should remain connected after splits.
3. Algorithm Overview
1. Use dynamic programming (DP) on trees.
2. For each subtree, calculate minimum cuts needed.
4. Time Complexity / Space Complexity
TC: O(V+E) for traversal.
SC: O(V) for DP storage.
5. Code
#include <iostream>
#include <vector>
using namespace std;
1. Problem Statement
Maximize profit by scheduling jobs with deadlines and profits.
2. Problem Analysis
Input: List of jobs with deadlines and profits.
Objective: Schedule jobs within deadlines to maximize profit.
Constraint: Jobs take unit time and must be done before deadlines.
3. Algorithm Overview
1. Sort jobs by profit.
2. Assign jobs to latest available slot before deadline.
4. Time Complexity / Space Complexity
TC: O(nlogn) (sorting jobs).
SC: O(n) (tracking time slots).
5. Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Job {
int id, deadline, profit;
};
1. Problem Statement
Find the minimum number of coins needed to make a target amount.
2. Problem Analysis
Input: Coin denominations and target amount.
Objective: Minimize coins used.
Constraints: Coins may not perfectly sum to amount.
3. Algorithm Overview
1. Use DP array to track minimum coins needed for each amount.
2. For each coin, update DP array for reachable amounts.
4. Time Complexity / Space Complexity
TC: O(n×m)
SC: O(n)
5. Code
#include <iostream>
#include <vector>
using namespace std;
1. Problem Statement
Assign jobs to machines to minimize overall job completion time or machine idle time.
2. Problem Analysis
Input: Job durations and machines.
Objective: Balance workload across machines.
Constraints: Minimize completion time.
3. Algorithm Overview
1. Sort jobs by duration.
2. Greedily assign jobs to the least loaded machine.
4. Time Complexity / Space Complexity
TC: O(nlogn) (sorting jobs).
SC: O(m) (tracking machine loads).
5. Code
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
1. Problem Statement
Maximize the weight loaded into containers without exceeding capacity.
2. Problem Analysis
Input: List of container capacities and item weights.
Objective: Maximize loaded weight.
Constraints: Weight per container should not exceed capacity.
3. Algorithm Overview
1. Sort items by weight.
2. Greedily fill containers until capacity is reached.
4. Time Complexity / Space Complexity
TC: O(nlogn)
SC: O(1)
5. Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;