Problem: A set of n coins is placed in a row. The coins have positive values which need not be distinct. Find the maximum amount that can be collected given the constraint that no two adjacent coins can be picked up.
Its recursive relation is
F(n) = max{cn + F(n − 2), F(n − 1)} for n > 1,
F(0) = 0, F(1) = c1.
My question is how this recursive relation is developed. Please someone explain this to me.
First, envision a line of coins, with the value of each depicted by the variable ci:
c1 c2 c3 c4 c5 ... cn
If there are no coins, than obviously the max amount that can be made is 0. Likewise, if there is only 1 coin, the max amount is the value of that coin, c1. This accounts for the base case.
For the recursive case of the max value for n coins, start at cn, which is the right-most coin. Since the constraint is that you cannot select adjacent coins, the max value that you can achieve is either the right most coin plus the max achieved from 2 slots to the left (this accounts for the f(n - 2), or the max achieved by selecting the coin immediately to the left (accounting for the f(n - 1) case) and discarding the rightmost coin cn.
Considering the following line of coins again:
c1 c2 c3 c4 c5 c6
The f(6) case would look at c6 + the greatest amount from coins c1 - c4, OR the greatest amount from coins c1 - c5 (and excludes c6).
f(4), likewise, returns c4 + the greatest amount from coins c1 - c2, OR the great amount from coins c1 - c3 (again excluding c4).
f(2) returns c2 + c0 or the greatest amount from c1 (effectively c1) The first equates to c2, since c0 is 0 by the base case, and the second equates to c1 (again by the base case). So f(2) is really just the max of c1 or c2.
Note, too, that the f(n - 2) and f(n - 1) may be the same, since in the n - 1 case it might be beneficial to select the coin to the left (which is the f(n - 2) case). But that is why the first half is not merely f(n - 2), but also adds to it cn
Let's start at the end.
Let's denote the answer to the problem of n coins as F(n)
If you have zero coins, the amount is zero. so F(0) = 0.
If you have a single coin, the amount is that coin's value, so F(1)=c1
Now suppose someone told you the values of F(n-1), F(n-2). How can you use them to find F(n)?
If you have n coins, you have two possible moves:
Pick the nth coin, skip the adjacent one ((n-1)th coin, that's the rule!) and resume solving from there.
Skip the nth coin, and resume solving from the adjacent (n-1)th.
How do you express the notions of 1 and 2 with the tools you have?
If you pick the nth coin, it's value is Cn. Now you have to skip the (n-1)th coin, and continue solving from (n-2) coin. This is Cn + F(n-2).
If you skip the nth coin it contributes 0 to the solution, and now you resume solving from the (n-1)th coin. That's F(n-1).
Which one of either case 1 or case 2 is larger? You don't know. But you can express it as
max(Cn + F(n-2), F(n-1)),
which is saying "I don't which one is larger, but one of them is so return it please".
I hope this helps!
Let F(n) be the maximum amount that can be picked up from the row of n coins.
To derive a recurrence for F(n), we partition all the allowed coin selections into two groups:
those that include the last coin and those without it.
The largest amount we can get from the first group is equal to cn + F(n − 2)—
the value of the nth coin plus the maximum amount we can pick up from the first n − 2 coins.
The maximum amount we can get from the second group is equal to F(n − 1) by the definition of F(n).
Related
I'm rather new to excel and I want to use excel or perhaps another program to subtract on a fixed amount but randomly.
It is like n1 + n2 + n3 = 300
But I want n1 and n2 and n3 to be different numbers hence not division
Examples
150 + 75 + 75 = 300
or
100 + 100 + 100 = 300
or
50 + 100 + 150 = 300
A function to subtract a fixed amount but random subtraction
I'm still quite confused on how to do this on excel, sorry for my bad English and explaination
Please help.
If it is always 3 numbers:
The first number we use:
=RANDBETWEEN(1,A1)
Then the second:
=RANDBETWEEN(1,A1-B1)
Then the third is just the remainder:
=A1-B1-C1
In excel you have your numbers in rows and columns
I would enter 100, 100 , 100 in separate columns of the same row and then use this
formula
=(A1+B1+C1).
Meaning row A1 100, row B1 100, row C1 100. The answer should be 300
if you want to subtract just use the minus sign instead of the plus sign:
formula =(A1-B1-C1).
Try:
=LET(A,RANDBETWEEN(0,300),B,RANDBETWEEN(0,300-A),HSTACK(A,B,300-SUM(A,B)))
Or, if no HSTACK() available:
=LET(A,RANDBETWEEN(0,300),B,RANDBETWEEN(0,300-A),CHOOSE({1,2,3},A,B,300-SUM(A,B)))
I'm just unsure if pure mathematically this is as random as can be. Would it be 'more random' if one would list all possible permutation of 1-300 that would add up to 300 and randomly pick a solution from this list?
One way to approach this if you want two numbers to always add up to the same value, is to modify each value with the same amount, by adding to one value and subtracting from the other
100 = n1 + n2 = (n1+a) + (n1-a)
where a is any random number.
To extend this to three numbers, you can use two artbitrary numbers a and b to do this following
100 = n1 + n2 + n3 = (n1+a) + (n2-a+b) + (n3-b)
The simplified approach to this it to pick completely random n2 and n3 and let n1 pick up the difference
100 = (100-n2-n3) + n2 + n3
To do this in Excel, use the =RANDBETWEEN() function for n2 and n3 and then for n1 just subtract from 100
=100 - SUM(n2,n3)
Is there a excel formula that is able to calculate the highest team start position deducted from the highest team finish position and (if the result is positive) multiplied by 5 to a maximum of 50?
in the above example Team T's highest start position was 13th and there highest finish position was 9th so 4 places difference * 5 = 20 ( added to D10 and 0 in D2 can be either way round so the team score is only calculated once )
Thanks
Use MINIFS():
=MIN(IF(C2=MINIFS(C:C,A:A,A2),MINIFS(B:B,A:A,A2)-MINIFS(C:C,A:A,A2),0)*5,50)
Give this a shot:
IF(ISERROR(MATCH(A11,$A$1:A10,0)),MIN((MINIFS($B$2:$B$21,$A$2:$A$21,A11)-MINIFS($C$2:$C$21,$A$2:$A$21,A11))*5,50),0)
to break this down:
IF(ISERROR(MATCH(A11,$A$1:A10,0))
<== first well check to see if this If I've calculated this team, by trying to match against the team list, up to this point. Match() returns a numeric for a hit (already done ==> return zer0), or #NA (error) for a new (not already found) instance of the team.
MIN( <= take the lessor of the next calculation or 50
(MINIFS($B$2:$B$21,$A$2:$A$21,A11) <= Return the lowest MIN() START (B:B) where all A11's are found in the list A2:A21
- <= and deduct from
MINIFS($C$2:$C$21,$A$2:$A$21,A11)) <=== <= Return the lowest MIN() FINISH (C:C) where all A11's are found in the list A2:A21
*5 <==and multiply result by 5
,50) <== or return 50 from MIN above)
,0) <= from if() above
Hope this works for you.
I am currently studying experimental designs in statistics and I am calculating values pertaining to 2^3 factorial designs.
The question that I have is particularly with the calculations of the "contrasts".
My goal of this question is to learn how to use the table "Coded Factors" and "Total" in order to get the values "Contrast" using the IF THEN function in Excel.
For example, Contrast A is calculated as : x - y . Where
x = sum of the values in the Total, where the Coded Factor A is + .
And y= sum of the values in the Total, where the Coded Factor A is - .
This would be rather simple, but for the interactions it is a bit more complex.
For example, contrast AC is obtained as : x - y . Where
x = sum of the values in the Total, where the product of Coded Factor A and that of C becomes + .
And y = sum of the values in the Total, where the product of Coded Factor A and that of B becomes - .
I would really appreciate your help.
Edited:
Considering the way how IF statements work, I thought that it might be a good idea to convert the + into 1 and - into -1 to make the calculation straight forward.
Convert all +/- to 1/-1. Use some cells as helper..
Put in these formulas :
J2 --> =LEFT(J1)
K2 --> =MID(J1,2,1)
L2 --> =MID(J1,3,1)
Put
J3 --> =IF(J$2="",1,INDEX($B3:$D3,MATCH(J$2,$B$2:$D$2,0)))
and drag to L10. Then
M3 --> =J3*K3*L3*G3
and drag to M10. Lastly,
M1 --> =SUM(M3:M10)
How to use : Input the Factor comb in cell J1 and the result will be in M1.
Idea : separate the factor text > load the multiplier > multiply Total values with multiplier > get sum.
Hope it helps.
I want to calculate the probability that h number of heads will appear in n coin tosses using Excel. For example, the probability of 4 heads appearing in 5 coin tosses. This is the formula:
[n! / h!(n-h)!] * 2^-n
How do I convert this into Excel? What I have is:
=(FACT($A$2)/FACT(B2)*FACT($A$2-B2))*POWER(2,-$A$2)
With A2 representing the number of tosses and B2 the number of heads, but this doesn't seem to work. Well, it works for 4 heads and 5 heads, but that's it. For 0 heads I should be getting 1/32, but instead I get 450. For 1 head I should be getting 5/32, but instead I get 90. I'm really confused. I suspect I'm not multiplying my factorials correctly.
Just this should do:
=(FACT($A$2)/(FACT(B2)*FACT($A$2-B2))*POWER(2,-$A$2))
Your formula just needed brackets in the denominator
=FACT($A$2)/FACT(B2)*FACT($A$2-B2)
doesn't equal
=FACT($A$2)/(FACT(B2)*FACT($A$2-B2))
if it's easier to read
a/b*c = (a*c)/b --> but you want --> a/(b*c)
Based on this paper:
IEEE TRANSACTIONS ON PAITERN ANALYSIS : Computation of Normalized Edit Distance and Applications In this paper Normalized Edit Distance as followed:
Given two strings X and Y over a finite alphabet, the normalized edit
distance between X and Y, d( X , Y ) is defined as the minimum of W( P
) / L ( P )w, here P is an editing path between X and Y , W ( P ) is
the sum of the weights of the elementary edit operations of P, and
L(P) is the number of these operations (length of P).
Can i safely translate the normalized edit distance algorithm explained above as this:
normalized edit distance =
levenshtein(query 1, query 2)/max(length(query 1), length(query 2))
You are probably misunderstanding the metric. There are two issues:
The normalization step is to divide W(P) which is the weight of the edit procedure over L(P), which is the length of the edit procedure, not over the max length of the strings as you did;
Also, the paper showed that (Example 3.1) normalized edit distance cannot be simply computed with levenshtein distance. You probably need to implement their algorithm.
An explanation of Example 3.1 (c):
From aaab to abbb, the paper used the following transformations:
match a with a;
skip a in the first string;
skip a in the first string;
skip b in the second string;
skip b in the second string;
match the final bs.
These are 6 operations which is why L(P) is 6; from the matrix in (a), matching has cost 0, skipping has cost 2, thus we have total cost of 0 + 2 + 2 + 2 + 2 + 0 = 8, which is exactly W(P), and W(P) / L(P) = 1.33. Similar results can be obtained for (b), which I'll left to you as exercise :-)
The 3 in figure 2(a) refers to the cost of changing "a" to "b" or the cost of changing "b" to "a". The columns with lambdas in figure 2(a) mean that it costs 2 in order to insert or delete either an "a" or a "b".
In figure 2(b), W(P) = 6 because the algorithm does the following steps:
keep first a (cost 0)
convert first b to a (cost 3)
convert second b to a (cost 3)
keep last b (cost 0)
The sum of the costs of the steps is W(P). The number of steps is 4 which is L(P).
In figure 2(c), the steps are different:
keep first a (cost 0)
delete first b (cost 2)
delete second b (cost 2)
insert a (cost 2)
insert a (cost 2)
keep last b (cost 0)
In this path there are six steps so the L(P) is 6. The sum of the costs of the steps is 8 so W(P) is 8. Therefore the normalized edit distance is 8/6 = 4/3 which is about 1.33.