Given:
A list of symbols of size M
The desired size of combination L
Symbols may occur any number of times in a combination
All permutations of any combination of the symbols must be taken into the account
Example: for a list of symbols (a, b, c), and L=4, all of the combinations (a, a, a, a), (a, b, a, c), (a, c, b, b) and so on are valid. For the lack of a better term, I called this "loose combinations".
The particular ordering of the combinations is not important. Being given the combination index N, the algorithm should return a unique combination from the set of possible combinations that satisfy the conditions. My guess is that the most natural order would be if we consider the combinations as numbers of radix M and length L, so that the normal number order would apply, but that is not strictly necessary to follow.
What is the algorithm to find the Nth combination?
I'm not sure how to find the answer myself, and have been searching if there was an answer for this particular set of conditions elsewhere, but did not find it. All the questions that I find are not interested in combinations with repeating elements like (a, a, b, b) and combinations with rearranged order, like (a, a, b, c) and (a, b, c, a) or (a, c, a, b) are treated as the same combination.
As you figured out already, you are essentially interested in enumerating the numbers of length up to L in base M.
So, a solution might look like this:
Define a bijection {0, …, M-1} -> Symbols, i.e. enumerate your symbols.
For any non-negative integer N < M^L, determine its base M representation.
Easily done by repeated modulo M and rounded down division by M.
Without loss of generality, this has length M by adding leading zeroes as needed.
Use your bijection to convert this list of digits 0 to M-1 to a loose combination of symbols.
So, let's go into detail on this part:
Easily done by repeated modulo M and rounded down division by M.
Pseudocode:
int a[L];
for int i from 0 to L-1 do
a[i] = N % M; // Should go from 0 to M-1
N = N / M; // Rounded down, of course
done
Related
a) Determine whether the following statement forms are logically equivalent:
p -> (q -> r) and (p -> q) -> r
b) Use the logical equivalence established in part (a) to rewrite the following sentence in two different ways. (Assume that n represents a fixed integer.)
If n is prime, then n is odd or n is 2.
Can someone help me with the B one? its really confusing
If n is prime, then n is odd or n is 2.
The question is asking you to rewrite the sentence in two different ways in English
If n is prime and n is not odd, then n is 2.
If n is prime and n is not 2, then n is odd.
The following links do a better job of explaining it:
Logically_Equivalent_Statements
Exercises on Logic of Compound Statements and Valid Arguments
V is a subset of R^3 and consists of vectors a{1,1,0) + b{0,1,1} where
a and b are real numbers.
I am confused as to how to determine if V is closed under addition and scalar multiplication. I understand that the vectors would be closed if their sum and product are within the vector space, but the introduction of the scalars a and b has confused me.
Thanks!
Another way of writing a{1,1,0} would be {a, a, 0}
So your 3D space is all vectors of the form {a, a, 0} and {0, b, b}. Is it possible to add or multiple all vectors from this set and still remain in the set?
Use an example of a = 1 and b = 2 and see what happens when you multiply {a, a, 0} x {0, b, b}.
Say we have factors some list of prime factors such as A, B, C, D multiplied together equal integer n
and want to generate every combination of the factorization non-recursively like
[A, B, C, D]
[(A*B), C, D]
[(A*C),B,D]
[(A*D),C,D]
.... And
[(A*B*C),D]
... ect
until we have generated all possible combinations, duplicate factorings are ok. Main concern is the algorithm speed, because looking to use this for as many prime factors as possible such as 100 prime factors.
Is there a good/efficient algorithm for this, or library, or programing language that will do this. Thanks
Given an EREW-PRAM model, that allows me to use an arbitrary number of processors in parallel without them conflicting nor in read, nor in write access, I need to find the number of paths of length 4, considering that I have an input node-node adjacency matrix A representing a directed graph and that I need to exclude paths that don't use distinct edges (e.g.: (a,b),(b,a),(a,b),(b,a) is not a valid path).
I have a function that uses n^3 processors and calculates the matrix multiplication of two given matrices in time O(logn):
mult-matrix(A, A, n) => B --> gives me the paths of length 2.
mult-matrix(B, B, n) => C --> gives me the paths of length 4, but I think it considers paths that run across the same edges.
I tried subtracting 1 from elements of C that have a node u communicating with a node v in both directions, but I'm not sure it works.
How could I solve the problem considering that I just need to exclude some paths from the resulting matrix C?
Any working solution is appreciated, considering that the number of processors is constrained to n^3 and time must be O(logn) in the worst case. The exercises must be solved using a pseudo-pascal language, but given a working solution, I should be able to write the pseudocode by myself.
I think I found a solution in https://www.perlmonks.org/?node_id=522270
Given an input matrix A, I am able to calculate the adjacency matrix for paths of length 2, 3 and 4 with the provided function.
A2 is the adjacency matrix obtained by multiplying A*A and contains paths of length 2
A3 is obtained by multiplying A2*A and contains paths of length 3
A4 is obtained by multiplying A3*A and contains paths of length 4
In order to exclude the repeated edges, I have to compute the matrix C, obtained by doing an element-wise subtraction among the calculated matrices.
C[i,j] = A4[i,j] - A3[i,j] - A2[i,j] - A[i,j]
C contains the final result.
The following pseudocode solves the problem with an EREW-PRAM using O(n^3) processors and in time O(logn).
procedure paths_length_4(A, n) // Work = O(n^3 logn)
begin
A2 := mult_matrix(A, A, n) // T=O(logn), P=O(n^3)
A3 := mult_matrix(A2, A, n) // T=O(logn), P=O(n^3)
A4 := mult_matrix(A3, A, n) // T=O(logn), P=O(n^3)
for all i,j where 1 ≤ i ≤ n, 1 ≤ j ≤ n pardo // P=O(n^2)
C[i,j] := A4[i,j] - A3[i,j] - A2[i,j] - A[i,j]
end
expand.grid(a,b,c) produces all the combinations of the values in a,b, and c in a matrix - essentially filling the volume of a three-dimensional cube. What I want is a way of getting slices or lines out of that cube (or higher dimensional structure) centred on the cube.
So, given that a,b, c are all odd-length vectors (so they have a centre), and in this case let's say they are of length 5. My hypothetical slice.grid function:
slice.grid(a,b,c,dimension=1)
returns a matrix of the coordinates of points along the three central lines. Almost equivalent to:
rbind(expand.grid(a[3],b,c[3]),
expand.grid(a,b[3],c[3]),
expand.grid(a[3],b[3],c))
almost, because it has the centre point repeated three times. Furthermore:
slice.grid(a,b,c,dimension=2)
should return a matrix equivalent to:
rbind(expand.grid(a,b,c[3]), expand.grid(a,b[3],c), expand.grid(a[3],b,c))
which is the three intersecting axis-aligned planes (with repeated points in the matrix at the intersections).
And then:
slice.grid(a,b,c,dimension=3)
is the same as expand.grid(a,b,c).
This isn't so bad with three parameters, but ideally I'd like to do this with N parameters passed to the function expand.grid(a,b,c,d,e,f,dimension=4) - its unlikely I'd ever want dimension greater than 3 though.
It could be done by doing expand.grid and then extracting those points that are required, but I'm not sure how to build that criterion. And I always have the feeling that this function exists tucked in some package somewhere...
[Edit] Right, I think I have the criterion figured out now - its to do with how many times the central value appears in each row. If its less than or equal to your dimension+1...
But generating the full matrix gets big quickly. It'll do for now.
Assuming a, b and c each have length 3 (and if there are 4 variables then they each have length 4 and so on) try this. It works by using 1:3 in place of each of a, b and c and then counting how many 3's are in each row. If there are four variables then it uses 1:4 and counts how many 4's are in each row, etc. It uses this for the index to select out the appropriate rows from expand.grid(a, b, c) :
slice.expand <- function(..., dimension = 1) {
L <- lapply(list(...), seq_along)
n <- length(L)
ix <- rowSums(do.call(expand.grid, L) == n) >= (n-dimension)
expand.grid(...)[ix, ]
}
# test
a <- b <- c <- LETTERS[1:3]
slice.expand(a, b, c, dimension = 1)
slice.expand(a, b, c, dimension = 2)
slice.expand(a, b, c, dimension = 3)