The Problem is: Imagine a student has n projects and m hours to do the projects. He has to manage his time to get the maximum points that he can get.
By working 1 hour on each project he can catch different points.
For example by working one hour on project 1, he can get 2 point and if he work two hours on project 1 he can get 2.25 points.
Project 2 is different - by working 1 hour on project 2, he can get 1 point, but by working two hours on it he can catch 2.5 points.
Another Example:
m = 5 and n = 10. means that there are 5 projects and 10 hours time to do the projects.
projectnumber hours to complete 1 hour work 2h work 3h work 4h work
1 3 1.5 2 2.25 _
2 4 0.5 1.75 2 2.25
3 3 2 2.25 2.5 _
4 2 1 2 _ _
5 5 1 2 2.5 3
What I could understand:
After thinking about this, I understood it's like job scheduling and maybe the best algorithm for this problem is a dynamic programming algorithm.
At first you should consider the first hour of projects. and sort it by profit descending.
2 1.5 1 1 0.5
After doing the first hour of project 3, you should add the second hour of project 3 to the list (9 hours remain).
1.5 1 1 0.5 0.25 (0.25 is for second hour of project 3)
And it should continue till the end of the 10 hours time that you have to do the projects.
But I'm sure this algorithm has some problems. One of them is that, maybe the second hour of a project causes you to catch a good point. So you can not just consider the first hour of projects.
Any suggestions?
Your problem seems similar to the 0-1 knapsack problem :-
Total hours m is that knapsack capacity
consider each number of hours and corresponding points as an item weight and value
Maximize the points.
0-1 Knapsack problem as DP solution in pseudo polynomial time.
Formulation of problem for Nth project :-
Knapsack(N,M) = max(Knapsack(N-1,M),Knapsack(N-1,M-1)+Points[N][1]+Knapsack(N-1,M-2)+Points[N][2]......)
Note:- Points[N][k] = points earns by working k hours on project N
Knapsack Problem
Related
I have this graph problem I would like to solve. I am not sure what algorithm to apply to it.
Maybe depth first search, topological sort or something else. Even if I know which one was
necessary I am not sure I would be able to apply it. Maybe someone can point me in the right
direction. If you want to completely solve it, you are also welcome, but you should also explain
it, because that's also important and not just get the code. Thank you in advance.
Problem statement:
In the city of N, under unclear circumstances, the territory of one of the factories turned into
an anomalous zone. All entrances to the territory were blocked, and it was named an industrial zone.
There are N buildings in the industrial zone, some of them connected by roads. On any road, you
can move in both directions.
A novice stalker was given the task to get to the warehouse in the industrial zone. He found
several maps of the industrial zone's territory in the electronic archive.
Since the maps were created by different people, each of them contains information
only about certain roads in the industrial zone. The same road can appear on several maps.
On the way, the stalker can upload one map from the archive to the mobile phone.
When loading a new map, the previous one is not saved in the phone's memory.
The stalker can only move along roads marked on the currently loaded map.
Each card download costs 1 dollar. To
minimize costs, the stalker needs to choose a route that allows them to load maps as few times as possible.
A stalker can download the same map several times, and you will have to pay
for each download. Initially, there is no map in the mobile phone's memory.
You need to write a program that calculates the minimum amount of expenses required
for a stalker to get from the entrance to the industrial zone to the warehouse.
Input data format:
The first line of the input file contains two natural numbers N and K (2 ≤ N ≤ 2000; 1 ≤ K ≤
2000) — the number of buildings in the industrial zone and the number of maps, respectively.
The entrance to the industrial zone is located in
building number 1, and the warehouse is located in building number N.
The following lines contain information about available maps.
First line of the I-th description maps contains the number ri— the number of roads marked on the i-th map.
Then come ri-strings containing two natural numbers a and b each (1 ≤ a, b ≤ N; a ≠ b)
indicating that there is a road connecting buildings a and b on the i-th map.
The total number of roads marked on all maps does not
exceed 300,000 (r1+ r2+ … + rK ≤ 300 000).
Output data format:
In the output file, you need to output one number — the minimum amount of expenses of the stalker.
If you can't get to the warehouse, print the number -1.
Examples or test cases:
Example 1:
Input:
5 3
1
3 4
3
1 2
1 3
2 4
1
4 5
output:
2
Example 2:
Input:
5 3
2
3 2
4 5
1
2 1
2
1 3
5 4
output:
-1
Example 3:
input:
12 4
4
1 6
2 4
7 9
10 12
3
1 4
7 11
3 6
3
2 5
4 11
8 9
5
3 10
10 7
7 2
12 3
5 12
output:
3
I have Job Sequencing Problem with three parameters where every task has time to be completed(in weeks) and a deadline that mush be finished before it.
In other words, any week, Job can be worked on at most. All jobs have a hard deadline, which means they must be completed before the deadline. The task is to arrange the jobs so that a high profit shall be accumulated.
Example
Input:
JobID Time Profit Deadline
1 8 100 13
2 1 100 1
3 1 100 3
4 1 100 2
5 4 100 6
Output
Total profit: 400
Jobs in order: 2 4 3 1
I have been trying to apply greedy algorithm but it only works with two parameters(profit & deadline) but here I have to take time into consideration
The solution can be obtained through dynamic programming. Before diving into the recursion, something to develop the intuition:
(I'm assuming any task will start no earlier than the earliest date in the deadline. That is, for your example, it would be day 1.)
Let D_i denote the time in days it takes to complete Task (or job) T_i. Denote the profits for task T_i as P_is.
On a calendar day C (the hypothetical calendar starting on earliest day of the deadline), you can be doing only one task per the problem definition. It could be one of T_1, T_2, T_3, T_4 or T_5.
If you were doing task T_1 on day C, it means that you started the task on C - D_1 day of the calendar. Similarly, if you were doing task T_2 on day C, then you started on the task on day C - D_2, and so on.
You could be doing any one of the tasks T_1, T_2, T_3, T_4 or T_5 on day C. Therefore, the profit you get by doing the last task in the sequence on day C is one of P_1, P_2, P_3, P_4 or P_5. If you add the profit of Task T_i that you were doing on the last day (i.e. P_i) to the maximum profit until day C - D_i, not including that task, for each 1<= i <= 5, you'll get the set of maximum profits you could possibly make on day C. The highest among these profits is what you're looking for. In the end, the answer would be the solution for day 13. (C = 13)
(I've tweaked your example a slight bit for more generality)
I'll leave it up to you to figure out how to codify this into a programming construct. On a broad level, you'll need two variables for your dynamic programming recursion:
Set of tasks (i.e., modeling T_i)
Calendar Days (i.e., modeling C)
Prioritize the jobs by profit per unit of time, breaking ties for the one that finishes first.
In other words think of it as dollars per hour and work on the job that pays you fastest.
JobID Time Profit Deadline Priority
1 8 100 13 12.5
2 1 100 1 100
3 1 100 3 100
4 1 100 2 100
5 4 100 6 25
And now we look at jobs 2, 4, 3, 5, then 1 in that order. We can do the first three and the last.
I was solving min cost path problem through dynamic approach but suddenly I realised that greedy approach is also working.
I applied greedy like this :
choose the min of bottom, right and diagonal cost and move in the min cost path.
1 2 3
4 8 2
1 5 3
where numbers are cost which will be added to the required cost if we include that point.
path from 1 to 3 is 12 through greedy is 8.
If my approach doesn't follow all examples, the what is that example?
How about a map such as:
1 1 1
2 10 10
1 1 1
Your greedy approach will end up taking 1+1+1+10+1, instead of 1+2+1+1
Greedy algorithms can be 'beaten' by giving them a long meandering path with several small steps:
2 2 e
2 ∞ 0
s 3 0
In this case, going from s to e will require either
The greedy solution: See that 2 is smaller and slog through several three 2's for a total cost of 6
A dynamic solution: See that after the three there is an easy path for a total cost of 3.
Also, I'd take a look at how your two algorithms define length. The optimal path is actually 1 -> 4 -> 2 -> 3 which has a cost of 10. If your dynamic solution isn't returning that, it may indicate that something else is going on.
I got this question as a part of the interview and I am still unable to solve it.
It goes like this
A person has to complete N units of work; the nature of work is the same.
In order to get the hang of the work, he completes only one unit of work in the first day.
He wishes to celebrate the completion of work, so he decides to complete one unit of work in the last day.
Given that he is only allowed to complete x, x+1 or x-1 units of work in a day, where x is the units of work
completed on the previous day.
How many minimum days will he take to complete N units of work?
Sample Input:
6
-1
0
2
3
9
13
Here, line 1 represents the number of input test cases.
Sample Output:
0
0
2
3
5
7
Each number represents the minimum days required for each input in the sample input.
I tried doing it using the coin change approach but was not able to do so.
In 2k days, it's possible to do at most 2T(k) work (where T(k) is the k'th triangular number). In 2k+1 days, it's possible to do at most T(k+1)+T(k) at most work. That's because if there's an even (2k) number of days, the most work is 1+2+3+...+k + k+(k-1)+...3+2+1. Similarly, if there's an odd (2k+1) number of days, the most work is 1+2+3+...+k+(k+1)+k+...+3+2+1.
Given this pattern, it's possible to reduce the amount of work to any value (greater than 1) -- simply reduce the work done on the day with the most work, never picking the start or end day. This never invalidates the rule that the amount of work on one day is never more than 1 difference from an adjacent day.
Call this function F. That is:
F(2k) = 2T(k)
F(2k+1) = T(k)+T(k+1)
Recall that T(k) = k(k+1)/2, so the equations simplify:
F(2k) = k(k+1)
F(2k+1) = k(k+1)/2 + (k+1)(k+2)/2 = (k+1)^2
Armed with these observations, you can solve the original problem by finding the smallest number of days where it's possible to do at least N units of work. That is, the smallest d such that F(d) >= N.
You can, for example, use binary search to find d, or an optimal approach is to solve the equations. The minimal even solution has d/2 * (d/2 + 1) >= N which you can solve as a quadratic equation, and the minimal odd solution has (d+1)^2/4 >= N, which has a solution d = ceil(2sqrt(N)-1). Once you've found the minimal even and odd solutions, then you can pick the smaller of the two.
AS you want to have the minimum amounts of days you can just say yeah x+1, since if you want the minimum amount of days, BUT you have to consider that his last day x should be 1 so you have to break at a given point and go x-1, so now we have to determine the Breakpoint.
The Breakpoint is located in the middle of the days, since you start at 1 and want to end at 1.
For example you have to do 16 units so you distribute your days like:
Work done:
1234321.
7 days worked.
When you can't make an even Breakpoint like above repeat some values
5 = 1211
Samples:
2: 11
3: 111
9: 12321
13: 1234321
If you need to do exactly N units, not more, then you could use dynamic programming on the matrix a[x][y] where x is the amount of work done in the last day, y is the total amount of work, and a[x][y] is the the minimum number of days needed. You could use Dijkstra's algorithm to minimize a[1][N]. Begin with a[1][1]=1.
We have to create an algorithm to fill up a given space of HxW perfectly. We have 125 test configurations with a given tile set that can fill up the field. The code for the tile set and the field is given and we have to write a code that can place the tiles and fill up the field(you can swap them if necessary). Does someone have suggestions on how to create such an algorithm because we are stuck and have no inspiration left.
We created a greedy algorithm that fill's up the largest tiles first and then tries to fit in the small ones but this has only solved 1 tile set and get's stuck with the larger tile sets.
Underneath are 2 given configurations:
width: 12 height: 17 scale: 20
2 times 5x5
1 times 7x3
3 times 5x4
1 times 5x3
1 times 6x2
1 times 3x3
2 times 4x2
1 times 6x1
1 times 3x2
1 times 2x2
1 times 3x1
1 times 2x1
width: 42 height: 39 scale: 10
1 times 15x14
1 times 14x14
1 times 14x8
1 times 11x9
1 times 12x6
1 times 14x5
1 times 11x6
1 times 16x4
1 times 12x5
1 times 10x5
2 times 8x6
2 times 8x5
1 times 9x4
1 times 6x6
1 times 7x5
2 times 6x5
1 times 7x4
1 times 6x4
1 times 10x2
1 times 5x4
3 times 6x3
2 times 7x2
1 times 12x1
2 times 6x2
1 times 11x1
1 times 10x1
1 times 5x2
1 times 8x1
1 times 6x1
2 times 3x2
1 times 5x1
2 times 2x2
3 times 3x1
3 times 2x1
1 times 1x1
Of course, it being an NP-hard problem (NP-complete if you only want to know whether it's possible, but it seems like you're already promised this and anyway you want some configuration) is not purely a bad thing - while it means it's probably not going to be overly efficient, it also suggests lots of angles of attack, so this doesn't have to be approached from scratch.
For example Integer Linear Programming, with a model such as (well this is a pretty basic one)
minimize: 0
subject to:
for all (x,y), sum[all i such that tp[i] covers (x,y)] x[i] = 1
for all tiles k, sum[all i such that tp[i] is tile k] x[i] = 1
Where tp contains all the possible "tile-placements", with a copy of every time for every location it can be in.
The first batch of constraints forces all positions in the grid to be covered by a tile, the second bath of constraints forces all tiles to be used exactly once.
Using this I could solve your 42x39 instance:
More tricks may be necessary for larger instances. Some of the cuts mentioned here apply, but when I solve this with Gurobi it spends most time to find a feasible solution, not so much in the integer phase.