Dynamic Programming-Find the maximum possible sum in a 2D matrix - dynamic-programming

Find the maximum possible sum in a 2D matrix such that if element[i][j] is selected then the next element cannot be selected again from the same ith row or jth column.
example 3*3 matrix
6.0 7.0 6.0
4.5 6.0 6.75
6.0 5.0 9.0
Here maximum sum is 21 because 6.0(1st row)+6.0(2nd row)+9.0(3rd row)=21
We cannot select 7.0(1st row)+6.75(2nd row)+9.0(3rd row) because 6.75 and 9.0 lie in the same column.
I understand its a dynamic programming problem. What would be an algorithm for this?

Given the fact that you cannot re-use a column or row, your sums are going to be diagonal lines. Here is my first stab at an algorithm (in pseudocode):
our largest known sum is 0.
for(0 to columncount-1)
sum element[0][column]'s diagonal (i.e. 6.0 + 6.0 + 9.0, then 7.0 + 6.75, then 6.0)
if it's greater than our largest sum, it becomes the largest sum and we track those elements.
for(1 to rowcount-1)
sum element[row][0]'s diagonal (i.e. 4.5 + 5.0, then 6.0)
if it's greater than our largest sum, it becomes the largest sum and we track those elements.
Then you need to repeat going the other way (i.e. right-left, then top-bottom).
I don't know if that makes sense, but that algorithm will give you the largest combination, for any rectangular matrix.

If I understand correctly, this is essentially an assignment problem which can be solved by the Hungarian algorithm. You just need to convert the maximum goal to a minimum goal by either subtract all elements from the global maximum or negate them.

Related

Find the minimum value of a max frequency range

For a project, I'm trying to find the longest period where a number is higher than 10.000.000.
I was looking to find two things
number of continuous periods with a number higher than 10mil (e.g. 9 in this example)
the minimum value closest to 10 million in that biggest continuous period (e.g. in this example 9 periods)
I did the first one via the following formule
=MAX(FREQUENCY(IF(A28:AA28>=$A$1;COLUMN(A28:AA28));IF(A28:AA28<$A$1;COLUMN(A8:AA28))))
=> this returned 9 as the longest continuous period with values higher than 10 million
I cannot find a way to extract the minimum value from the longest continuous period, specifically how to get the range from the longest continuous period with values higher than 10mil.
A
B
C
D
E
F
G
H
I
J
K
L
M
N
10.000.000
18.000.000
6.000.000
15.000.000
11.000.000
15.000.000
15.000.000
15.000.000
15.000.000
19.000.000
15.000.000
15.000.000
9.000.000
7.000.000
In this example I would have the find the value 11.000.000 as this is the min value from the longest continuous frequency above 10mil.
Does anyone have an idea how to solve this?
Much appreciated!
Assuming B1 contains the result from your current formula, e.g. 9, for Office 365:
=MIN(INDEX(A28:AA28,SEQUENCE(B1,,FIND(REPT(1,B1),CONCAT(N(A28:AA28>=A1))))))
#Jos Woolley's answer is perfect, but I wondered for my own satisfaction if I could get the minimum using the frequency array? The answer is 'yes', but the formula is much longer and less elegant. I believe the number of cells in the range a28:aa28 preceding the longest run can be calculated by counting the number of elements in the frequency array before the maximum and adding the sum of those elements so I ended up with this:
=LET(range,A28:AA28,
seq,SEQUENCE(1,COLUMNS(range)),
freq,FREQUENCY(IF(range>=$A$1,seq),IF(range<$A$1,seq)),
matchPos,MATCH(B1,freq,0),
start,IF(matchPos=1,1,matchPos+SUM(INDEX(freq,SEQUENCE(matchPos-1)))),
end,start+B1-1,
MIN(INDEX(range,start):INDEX(range,end)))

Return three largest values from distribution in one formula

Suppose I have the following datapoints. I would like to extract the cumulative percentage distribution of this set of the three largest values.
So first step would be to transform to 100% distribution and secondly summarise the three largest values of the new distribution.
Data
0.00
1.35
11.05
24.85
37.85
15.40
6.95
1.65
0.25
I can calculate the individual percentage point with a simple datapoint / sum of datapoints per row and use =LARGE 1,2,3 on the new column to sum up the values. However, the challenge is to make all calculations in a single cell and just return just the final value.
In this case, the target value would be: 0.2494 + 0.3804 + 0.1548 = 0.7849 or 78.48%
Thanks for the help
Wrap a LARGE in SUMPRODUCT:
=SUMPRODUCT(LARGE(A2:A10,{1,2,3}))/SUM(A2:A10)

Excel Finding the highest decimal value per integer

I am trying to create a formula in Excel that returns the highest decimal value for every integer in a list.
For example, my sheet may have the values [10, 10.1,10.4, 11.3] and I need it to return both 10.4 and 11.3 since 10.4 is larger than 10 and 10.1, and 11.3 is the largest decimal value for 11.
I need this so that a pivot table can filter out the values as IDs and only return the highest value, but I can't get anything working to the point that providing my existing 'code' wouldn't help whatsoever.
Thanks for the help
If your list is sorted, you could use the Advanced Filter to generate a list of the highest decimal value for each integer.
Given:
Criteria Formulas:
A2: =A6<>INT(A6)
B2: =OFFSET(A6,1,0)=INT(OFFSET(A6,1,0))
Criteria Dialog
Note that you can choose to have the results copied elsewhere
Results
Solve as follows:
value bracket decimal
10 =int(a2) =a2-b2
10.4 =int(a3) =a3-b3
10.1 =int(a4) =a4-b4
11 =int(a5) =a5-b5
11.3 =int(a6) =a6-b6
or values:
value bracket decimal
10 10 0
10.4 10 0.4
10.1 10 0.1
11 11 0
11.3 11 0.3
Now pivot by:
bracket (columns/rows)
and in the value section add the "original value", aggregate by "max".
Pivot table and then group.
pivot table
change PivotTable Fields values to Max of values
group
result

origin value and after sum after ROUND() is different

I have a table with "ordered amount", "percentages", and "total sum". Note that "ordered amount" and "total sum" should be the same.
column F= sum of C:F
*rows 4,6,8,10 used =round()
As shown in the table(image), for some numbers, value in F is not the same as A when it should be. (e.g. A6(105) and F7(104), -1 after rounding).
Is there anyway to avoid this?
Thank you very much.
If the sum of the decimal points adds up to > 5, your final rounded number will round up. If they add up to < 5, the final number will round down.
Consider this example:
10.5 + 5 = 15.5, which rounds to 16.
However
10 + 5 = 15
This is what is happening in your table.
In order to get the sum of rounded numbers to add up to the same as the sum of the numbers, in your example, you will need to NOT round, but rather subtract, one of the values. I would suggest altering the largest value, as it would seem to have the least effect on the percentages, but that is a choice you can make.
To do that, with your data in row 8, for example you could do the following.
A9: =IF(B$8=MAX($B$8:$E$8),SUM($B$8:$E$8)-SUM(ROUND($B$8:$E$8,0))+ROUND(B$8,0),ROUND(B$8,0))
entered as an array formula with ctrl+shift+enter and fill right to E9.
This would give a sum of 344 which is the same as F8

Excel - allocate weight based on text

I have an risk control assessment where some controls are key and hold greater weight than non key controls.
Key vaule (1-4)
Y 4
Y 3
N 2
N 2
I want the keys with a "Y" to be summed at a weight of 70% and the non-keys with an "N" to be summed at a weight of 30%.
If we add the column we get 11. However, I want the 7 (4+3) to be multiplied by 70% and the 4 (2+2) be multiplied by 30%.
There may be 4 rows or 40. There generally are only 1 or 2 key controls ("Y"), but, if there are 40 rows or controls, there may be up to 5 "Y"s.
Any thoughts?
A simple way to do what I think you want would be to create a third column that had formulas like this one: =IF(A1="Y",B1*0.7,B1*0.3). Then, you could use the SUM function to add up all of the results. See the cells with formulas below.
Key Value Weighted Value
Y 1 =IF(A2="Y",B2*0.7,B2*0.3)
N 2 =IF(A3="Y",B3*0.7,B3*0.3)
N 3 =IF(A4="Y",B4*0.7,B4*0.3)
Y 4 =IF(A5="Y",B5*0.7,B5*0.3)
=SUM(C2:C5)
Here would be the result...
Key Value Weighted Value
Y 1 0.7
N 2 0.6
N 3 0.9
Y 4 2.8
5
As you can seen from #TimWilliams' comment, there is some uncertainty about your requirement but if it is weighting factors then the following formula might suit:
=IF(A2="Y",C$1*SUM(B:B)*B2/SUMIF(A:A,"=Y",B:B)/SUM(B:B),(1-C$1)*SUM(B:B)*B2/SUMIF(A:A,"=n",B:B)/SUM(B:B))
copied down to suit and assuming a layout as shown:

Resources