It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a list of projects and each project takes you exactly two days to be completed and has a due date. let P[i].id, P[i].duedate, and p[i].value be the id of the project, the due date of the project, and the value you get if you complete the project on time(on or before due date)
write an algorithm that takes as input array A and returns a schedule of which projects you will do and when, to maximize the value you get.
the output of the algorithm is an array B such that B[i] is the id of the project that you will work on during day i, i>= 1.
no more than one project in a particular date, and you don't get the value of the project unless you complete it by the due date, today is day 0 and you will start working on the projects from day 1 (the due date is an integer), e.g., if the due date of a project is 5, you can choose to work on it on days 3 and 5)
1- write the algorithm.
2- prove that the algorithm is optimal?
3- what is the time complexity for the algorithm?
If all the values be same it's simple, just greedy approach by selecting least possible due date works well.
When the values are different, you can use similar approach but this time by dynamic programming (I'll assume your due dates are discrete).
Create an array of size Max{due date} name it as V, this array holds maximum possible value which can be earned in specific time, and another array for each value in V to save the selected tasks in related V[i], now you have this DP choice:
V[0] = 0, V[1] = max{value_x1, V[i] = Max {V[i-2] + value_xi, V[i-1]}
Here value_xi means maximum value task which has due date equal or smaller than i, Also this task shouldn't be in V[i-2] selection, after that update V[i] selection.
Finally I'll left to you to finish your homework by finding the order of this algorithm and it's correctness, also you can improve memory usage.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to find an algorithm which uses linear space of memory for:
Given two strings x and y over an arbitrary alphabet, determine their longest common sub sequence.
Note that when you're calculating the next row of the table in the dynamic programming solution to solve the LCS problem, you only need the previous row and your current row. Then you can modify the dynamic programming solution to keep track of only the previous row and the current row instead of the m x n table. Every time you reach the end of the current row, you set the previous row to the current row, and start from the beginning of the row again. You do this m times where m is the number of rows in your table. This will use space linear in the number of columns.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have come across a problem about the determination of triangle, it says:
Given a sorted integer array(length n), determinate whether you could
build a triangle by choosing three integers from the array, the
answer is "yes" or "no".
A naive solution is by scanning all the possibilities but it turn out to be O(n^3), seems
it will be C(n, 3) possibilities.
Assuming that the integers represent side lengths and array(0) > 0,
bool IsTriangle(int[] aray, int start) {
if(array.length - start <= 2) return false;
return (array(start+2) < array(start+1) + array(start+0))
|| IsTriangle(array,start+1);
}
This works because the list of integers is sorted; thus the RHS will always be larger using any subsequent elements of the array, and the LHS will be smaller using any previous elements of the array, and thus can satisfy the triangle inequality only if the selected three consecutive elements satisfy it. This is of course O(n) and can easily be converted to a (less elegant but more performant) iterative solution.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm struggling with a dynamic programming problem for a couple of days. It goes like this:
John's working day is divided in N time slots, every slot i having associated a gain G[i] which he can receive is he works in that time slot. If he decides to work in the time interval [i, j] his total reward would be R[i,j]=G[i+1]+...+G[j] as the first slot is for warming up. Everyday he has to work exactly T slots - he can chose a subset of T slots from the available N total slots. He wants to maximize his profit by choosing a set of disjunct intervals [a1,b1], [a2,b2], ...[ak,bk] with 1 <= a1 <= b1 < a2 <= b2 <...< ak <= bk and Sum[i=1, k](bi-ai+1)=T.
Example: N=7, T=5 and the gain vector {3,9,1,1,7,5,4}. The optimal solution is selecting the intervals [1,2] and [4,6] with a total profit of 9+12=21.
DP solution:
int f[i][j][0..1];
let f[i][j][0] denotes the maximal gain for the first i time slots and using j time slots, and the i-th time slot is not used.
let f[i][j][1] denotes the maximal gain for the first i time slots and using j time slots, and the i-th time slot is used.
obviously,f[i][j][k] can determine f[i+1][j][k] or f[i+1][j+1][k]. details below:
f[i+1][j+1][1]=max(f[i+1][j+1][1],f[i][j][0],f[i][j][1]+G[i+1]);
f[i+1][j][0]=max(f[i+1][j][0],f[i][j][0],f[i][j][1]);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to design a circuit which accepts n numbers at the input (infinite input) and calculates the average of these numbers as the output. The numbers for the input can only be of values <0,15>.
I need to implement this circuit in VHDL but I cannot find the proper algorithm since I need it to design the logical schema. I understand that I will definitely need a 4bit adder and some registers to store the values. I tried to understand the problem using moving average principle but it just did not work at all.
For input n+1, with value x, the average will be equal to (average*n+x)/(n+1) --> ... = average + (next - average)/(n+1).
From this observation a simple algorithm can be derived:
Initialize all registers to 0
Get the next input and store it in temp register
Increase count register by 1
Subtract previous average from temp register
Divide the temp register by count
Add temp to average
Go to step 2
Lets see, you'd need as input ports: reset, input[3:0], clock; outputs: average[3:0] and internal registers accumulator[a:0] and count[c:0].
I can't remember the syntax of my VHDL and Verilog just now but...
whenever you get an input you need to add it to the accumulator, increment the count by 1, then set the average to be the accumulator divided by the count.
On reset set the accumulator and count to zero.
If you know the maximum number of values for incrementing is countmax then the accumulator needs to be big enough to hole countmax*15 and count has to have enough bits to hold countmax.
This will also give you a size for the divider.
If countmax is unknown then you need to add an overflow output and set it when the accumulator overflows and un-set it on reset.
Hope that helps.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Say you wanted to find which input causes function x to output value y, and you know the (finite) range of possible inputs.
The input and output are both numbers, and positively correlated.
What would be the best way to optimize that?
I'm currently just looping through all of the possible inputs.
Thanks.
One solution would be a binary search over the possible inputs.
Flow:
find the median input x
get the output from function(x)
if the output is less than the desired y
start over using the smaller half of the possible inputs
else
start over using the larger half of the possible inputs
A binary search algorithm, perhaps?
http://en.wikipedia.org/wiki/Binary_search_algorithm
If the range is finite and small, a precomputed lookup table might be the fastest way
if you have some sets of know "x" data that yield "y" you can divied between training and test sets and use neural networks.