Dynamic Programming to solve Permutation - algorithm

I have four digits, "1", "2", "3", "4".
The input of the program is an integer which can only comprise of the above 4 digits. There is going to be a lot of inputs.
Example of inputs: 1123, 4123, 4444
I need to calculate the number of permutations of a given input that adheres to the following rules:
No two similar digits should be adjacent to each other. Example: 1223 is not allowed but 2123 is allowed.
The start end end digits should not be the same. They are considered as being circularly adjacent. Example: 2132 is not allowed.
If the input is 4 digits of length, your resulting permutation should also be of 4 digits of length.
Could I use any type of memoization to solve this problem? How do i store it in a 2d array? Do give tips thanks!

Since you are only interested in the number of allowed arrangements, most of the inputs lead to identical results.
the order of the digits in the input does not matter
only the frequency distribution of digits is important, i.e. 1123 and 1223 lead to the same answer.
Classifying the inputs according to digit frequencies leads to just 5 different cases for four digit inputs:
class examples
4 4444, 2222, ...
3 1 1211, 2232, ...
2 2 1331, 4422, ...
2 1 1 3413, 1123, ...
1 1 1 1 1234, 4231, ...
Once you have figured out the answer for each case, any new input can be handled very fast.

Related

Simple math task: 2 numbers given, we have to find the third. I just need a formula for it

I need a formula for counting the number of combinations within a given limit of numbers. There must only be 2 numbers given, we have to find the third.
For example, for 2(number of repetitions) and 3(limit number), the result would be 3, because there are 3 combinations for the digits: 1 and 2, 1 and 3, 2 and 3.
For 2 and 4 the result is 6,
For 3 and 5 the result is 10,
For 6 and 7 the result is 7, etc.
The first number has to be smaller than the second.
A formula is needed for figuring out the result, if the first number is A, the second is B, what would C is going to be?
You're describing combination. The formula is going to be C = B! / (A!*(B-A)!) (where ! is the factorial operation). It's also worth noting that the first number can be equal to the second -- there should only be one repetition in that case. By convention 0! == 1 and it is OK where both numbers are equal because C(n, n) = 1 and this means n!/(n! * 0!).
Unfortunately, since factorial grows very quickly (21! is too large for a 64-bit unsigned integer), you probably can't compute this directly. Wikipedia has a few algorithms you can use here.

Find largest multiple for a number set

An array of digits(0-9) of size N is provided as input. A set of numbers(N1,...,Nm) of size m with the numbers separated by space is also as the input. The program has to print the largest number that can be formed using the digits in the array of size N that is divisible by the numbers N1,..,Nm
Example Input/Output1:
INPUT:
160
2 3 5
OUTPUT
60
Explanation
60 is the largest number that can be formed using the digits 1,6,0 which is divisible by 2,3,5
Example Input/Output2:
Input
91028
17 5 9
Output
9180
Boundary Conditions
1<=m<=5
2<=N<=50
Can somebody explain how to approach this problem.
Partial answer:
Try all permutations of all subsets of your digits, probably starting with the largest candidates.
If your factors contain 5 the last digit must be 0or 5
If your factors contain 3 or 6 or 9 the sum of all candidate digits muts be a multiple of 3
If your factors contain 2 or 4 or 6 or 8 the last digit must be even.
And so on.

How do I representation percentage in evolutionary Algorithm?

Considering I have 4 chromosomes (gi, i=1 to 4}) to represent 4 percentages of different things so that the sum of 4 percentages are equal to 100. How Do I represent this efficiently?
I know that it is possible by: g1/(g1+g2+g3+g4). However, This is not efficient. Consider all gi=0.2 or all gi=0.1 will represent 25% in these two cases. It is possible to generate many cases where different genes present same percentage. Is there any other efficient way, where unique set of combination of genes present unique set of percentages.
Thanks in advance.
I think you're confusing genes and chromosomes. A chromosome encodes a candidate solution to your problem. A gene is part of a chromosome.
Under this setting, why would you want that constraint on the chromosomes? it sounds like you want it on the genes of a chromosome.
In order to do this you can do a number of things: have each gene encode an integer in [0, 100]. If the genes do not add to 100 in the end, penalize the fitness of those chromosomes.
Another way, which might make crossover operators more natural to apply, is to have each gene store 100 bits. If x bits are set, that means the gene will encode x%.
Yet another way is to have the entire chromosome encode 100 set bits. Then each gene will hold a value x, which represents an interval. The number of set bits between two split points is the percentage associated to that gene. For example:
1 2 3 4 5 6 7 8 ... 100
1 1 1 1 1 1 1 1 ... 1
| | | | |
g1 g2 g3 g4
This can be done by generating 5 random numbers <= 100, sorting them and taking the differences between them.
One way to assign X units to N possibilities is to store X * (N-1) bits. Every unit is given (N-1) bits and if k of the (N-1) bits are set then the unit is assigned to k.
This is easy to work with as there are no invalid solutions and no penalties/repairs are necessary. This makes fitness evaluation, crossover and mutation easier to implement.
For example, the problem is to assign 5 units (X) to one of 4 (N) possibilities. Each individual is (4-1)x5=15 bits.
The bit string: 010 100 000 011 111 assigns the first 2 units to possibility 1 because both groups have 1 bit set. The third unit which has no bits set is assigned to 0. The fourth unit is assigned to 2 and the fifth to 3.
partition units
0 1
1 2
2 1
3 1

Does radix sort work with numbers who have different number of digits?

I know that radix sort works by comparing the digits of the numbers. My question is, assume we have different numbers with different number of digits. Does radix sort work here? We can simply assume that, for example, if we are comparing two numbers, one with 3 digits and one with 6 digits, the first 3 digits of the smaller number is 0. But how about the implementation? How can we make the program assume that if there are not enough digits, then those digits are zero?
Thank you.
You need to somehow add or simulate those nonexistent digits or sort the numbers in groups, each of which containing only numbers of the same length.
These 3 numbers
9912
999
123
can be transformed into
9912
0999
0123
and sorted using the regular radix sort or they can be sorted as 2 independent groups:
9912
and
999
123
The latter will give you (assuming ascending order)
123
999
and the former stays the same. Then you combine the sorted groups (from shorter numbers to longer numbers):
123
999
9912
That's all.
Assuming you have the number in an integer variable, then you can extract the digits like this (n = 0, 1, 2, ...):
digit = (number / radix ^ n) % radix

Confusion regarding genetic algorithms

My books(Artificial Intelligence A modern approach) says that Genetic algorithms begin with a set of k randomly generated states, called population. Each state is represented as a string over a finite alphabet- most commonly, a string of 0s and 1s. For eg, an 8-queens state must specify the positions of 8 queens, each in a column of 8 squares, and so requires 8 * log(2)8 = 24 bits. Alternatively the state could be represented as 8 digits, each in range from 1 to 8.
[ http://en.wikipedia.org/wiki/Eight_queens_puzzle ]
I don't understand the expression 8 * log(2)8 = 24 bits , why log2 ^ 8? And what are these 24 bits supposed to be for?
If we take first example on the wikipedia page, the solution can be encoded as [2,4,6,8,3,1,7,5] : the first digit gives the row number for the queen in column A, the second for the queen in column B and so on. Now instead of starting the row numbering at 1, we will start at 0. The solution is then encoded with [1,3,5,7,0,6,4]. Any position can be encoded such way.
We have only digits between 0 and 7, if we write them in binary 3 bit (=log2(8)) are enough :
000 -> 0
001 -> 1
...
110 -> 6
111 -> 7
A position can be encoded using 8 times 3 digits, e.g. from [1,3,5,7,2,0,6,4] we get [001,011,101,111,010,000,110,100] or more briefly 001011101111010000110100 : 24 bits.
In the other way, the bitstring 000010001011100101111110 decodes as 000.010.001.011.100.101.111.110 then [0,2,1,3,4,5,7,6] and gives [1,3,2,4,5,8,7] : queen in column A is on row 1, queen in column B is on row 3, etc.
The number of bits needed to store the possible squares (8 possibilities 0-7) is log(2)8. Note that 111 in binary is 7 in decimal. You have to specify the square for 8 columns, so you need 3 bits 8 times

Resources