I want to add several items to make an array of sum in this order.
Example,
sum = 01 +12 + 23 + 34+ 45
(start with element row 0 column 1, last column number is index for next element)
some more example:
sum = 01 +13 + 34 + 45+ 52
sum = 01 +14 + 45 + 52+ 23
sum = 01 +15 + 52 + 23+ 34
sum = 02 +23 + 34 + 45+ 51
sum = 02 +24 + 45 + 51+ 13 and so on.
Each sum should take maximum number (maximum row or column, an item column is row for next item and each column is taken only one times)
as number of items in the matrix. If row and column is same we don't add it or if the cell value is -1.
So you have a matrix, say M and you want to find the sum Mab + Mbc + Mcd + ... for some indices a=0, b=1, c, d, ...
How are these indices determined. Are they chosen to maximize the sum? How long is this sum?
And what do you mean by "generalize"? Do you just want an algorithm?
Thanks for reply. I knew it is hard. Generilize mean I want to have algorithm that can be used in code. Or if some body has code if I can share it.
say we have total row = column = f
sum = Mab + Mbc + Mcd + Mde + Mef
sum = Mbc + Mcd + Mde + Mef + Mfa
sum = Mbd + Mdf + Mfa + Mab + Mbc
If there are factorial (f-1) types of sum. I need the sum in an array.
When I find the sum, once a column index has been used, it can't be used if you see the pattern.
If I use row index a then column index can be any index between 0 to f
row index can be between 0 to f without repeating the column index.
columns = {2, 3, 4, 5}
for each i < 4! {
choices = i'th permutation of columns
row = 0
col = 1
sum = matrix[row][col]
for each j < 4 {
row = col
col = choices[i]
sum[i] += matrix[row][col] if it is not -1
}
}
Oh Yes it is easy with algorithm. It díd not come to my mind to use it.
Next permutation is giving me what I need,
Thanks for pointing that. I am going to put it in my application in Qt, if it is supported there then I am done if not do you know can I get the algorithm code for next permutation?
Hi,
Is there any C++ code that I can use for the sort and next_permutation methods. I tested my theory in Windows with these and worked great but if I tried to use it in Qt I get errors
error: 'sort' was not declared in this scope
error: 'next_permutation' was not declared in this scope
Can I get the implementation of these so I can include it in my project?