Representing a carbon molecule in the form of a matrix - java

how to generate all possible combinations of a 14x10 matrix containing only 1's and 0's
Hello. While searching for what i wanted i found the above URL to be something useful, but not exactly helpful to my particular problem.
What i'm looking for is a way to find all possible permutations of the numbers 1 and 0 in an n dimensional square matrix. I realise that the number of such matrices will be very large and the computation time will be immense.
Hence, I wish to impose the following restrictions on the matrix.
1) The matrix should be reduced in size to an upper triangular matrix.
2) Each column must have atleast one '1'
3) The number of 1's in the matrix must be equal to or less than n- the dimension of the square matrix.
4)The total 1's in a particular column must be less than or equal to 4.
the reason i'm looking for such a code is so that i can represent a carbon molecule in the form of a matrix with each column representing one atom and each element representing a bond (1) or no bond (0) between the carbon atoms( one in the column and one in the corresponding row)
for example
X 1 0 0;
X X 1 0;
X X X 1;
X X X X
where the X shows that the matrix is now only an upper triangular matrix to avoid redundancies
the rows separated by ';' and each column entry is separated by a space.
the above example shows one particular combination of 0's and 1's. what i'm looking for is a code that will give all possible combinations.
thank you.

Related

Generating all possible Permutation matrices [duplicate]

This question already has answers here:
All possible permutations of a NxN matrix in Java
(2 answers)
Closed 1 year ago.
Permutation matrices are matrices with exactly one 1 on each line and column.
Example:
(1 0 0)
(0 1 0)
(0 0 1)
I am trying to generate all possible permutations of a n-sized matrix but this seems harder to solve than expected. I tried swapping rows at first, but if you only swap rows of one initial matrix, you can't generate all possibilities. So I tried some recursion, but sadly I can't get my head through recursion. Does somebody have some advice?
I think you could make an algorithm based on recursion, with this design:
public Collection<Matrix> permute(int n)
Base case is n=1: It returns a single matrix {1}.
The rest of cases must iterate from i=0 to n-1. On eeach iteration:
Call to permute(n-1) and iterate the returned list. On each iteration:
Convert the n-1 matrix to a n x n matrix, inserting a row at position 0 and a column at position 0 (filled with 0).
Set an 1 in the (0,0) position.
Move the first column to the i position.
I suggest you could also make a battery of unit tests. Each one should check, at least, that the size of the collection returned by permute(n) has n! elements:
1->1
2->2
3->6
4->24

1d array of unique distances between points

I'm coding an N-body simulator with planets (points). I've got it working but there are still a lot of possible optimizations, one of them is to calculate distances between points once and store them in an array. I could use a 2d array (needed to be converted to 1d because the GPU doesn't support 2d arrays) but there are two problems:
The simulation would be limited to √2147483647 (max integer value) planets = 46340 planets since the size of the array would be n^2 (n is the number of planets)
A lot of the distances would be repeated (distance between 0 and 1 is the same as distance between 1 and 0)
To get the amount of unique connections the formula is: D = (n*(n-1))/2
But now I've got a problem: how to get an index in the distances array given two planet id-s (x,y). The distance index between planet 0 and 1 is 0, 0-2 -> 1, 1-2 -> 2...
To help visualize what this looks like, here is a table showing which index (in the 1D array) corresponds to each pair of planet ids. To use this table, given a pair of planet ids, find the row that corresponds to the larger of the two ids, and the column that corresponds to the smaller of the two ids, and that will give you the cell with the index of where that distance is stored in your 1D array.
So, now we need a formula that is given two planet ids and computes the 1D index number. For simplicity, we will assume that the larger planet id is given first. (If not, then we can simply swap the two planet ids.)
The first job is to take the larger planet id and determine the first index on its row. In other words, we need to convert 1 to 0, 2 to 1, 3 to 3, 4 to 6, 5 to 10, 6 to 15, etc. Luckily, the pattern 0,1,3,6,10,15 is well known - it is the triangular numbers. The usual formula for the Nth triangular number is (n*(n+1))/2 but in this case we actually want the N-1th triangular number, so the formula we want is (n*(n-1))/2.
Once we have the first index of the row, we can just add the smaller planet id to get the final index.
So, our final formula is:
given A (larger planet id) and B (smaller planet id):
the index of dist(A,B) in the 1D array is ((A * (A-1)) / 2) + B

Algorithm to implement the BINGO game

For those who doesn't know a BINGO game, its played as follows
1)You get a BINGO card in which there is a NXN matrix of numbers randomly printed.Numbers are unique.The max number printed can be greater than N^2. e.g. if you have 5x5 matrix then the max number can be 75 as well.But the range of numbers is pre-decided say 1 to M.
2)A person speaks out numbers randomly in the range 1 to M.
3)If the number is on your card you cross the number.
4)The process of crossing numbers is repeated.When you have crossed a full row or a full column or the two diagonals,then you get your first bingo
The game is still continued as the total BINGOs possible are N+N+2 for N rows,N columns and 2 diagonals.
Now I want to create an algorithm for it.The user will input random numbers and the algorithm will hear them and cross its numbers in the matrix(already provided).As soon as it gets BINGO it declares it.What is the best possible approach
I tried it as maintaining a 2-D matrix for the card
When a number is announced, i search it in O(NxN) time.When I find it ,I make it as 0.
After making it as 0, I search whether it the corresponding row & column has now all zeroes.If it was on the diagonal , I also search for the diagonal.It takes O(3N) time.
Any better approach?
You can form a map for each number that would map to a pair (row, column).
if ( myMap[number] exists ) {
then increment rowCount[ myMap[number].row ];
then increment columnCount[ myMap[number].column ];
}
if ( rowCount[myMap[number].row] == N ) { bingo! }
if ( columnCount[myMap[number].column] == N ) { bingo! }
myMap.erase( number );
Similarly for diagonals.
Use an array to store the numbers on the card and keep it sorted. Upon number being called, search the number using Binary Search (O(logN) time). This should be a quick approach.
Create a class Coordinate that holds x and y position in bingo card.
NxN array of booleans initialized to false to keep track of what gets crossed off on bingo card.
N^2 time to iterate through bingo card and add each number to hash table using the number as the key and a new Coordinate as the value.
n time to iterate through all the numbers that will be called out, retrieve the Coordinate from the hash table, and update the status of the boolean array. In case of duplicate numbers called, you must retrieve and update boolean array until the hash table does not contain the key.
4N time to check each direction on boolean array for first bingo
N^2 + n*4N total runtime

Java permutations of offsets

Had a question regarding generating a list of 10-digit phone numbers on a PhonePad, given a set of possible moves and a starting number.
The PhonePad:
1 2 3
4 5 6
7 8 9
* 0 #
Possible moves:
The same number of moves a Queen in chess can make (so north, south, east, west, north-east, north-west, south-east, south-west... n-spaces per each orientation)
Starting number: 5
So far I have implemented the PhonePad as a 2-dimensional char array, implemented the possible moves a Queen can make in a HashMap (using offsets of x and y), and I can make the Queen move one square using one of the possible moves.
My next step is to figure out an algorithm that would give me all 10-digit permutations (phone numbers), using the possible moves in my HasMap. Repetition of a number is allowed. * and # are not allowed in the list of phone numbers returned.
I would imagine starting out with
- 5555555555, 5555555551, 5555555552... and so on up to 0,
- 5555555515, 5555555155, 5555551555.. 5155555555.. and with the numbers 2 upto 0
- 5555555151, 5555551515, 5555515155.. 5151555555.. and with numbers 2 upto 0
... and so on for a two digit combination
Any suggestions on a systematic approach generating 10-digit combinations? Even a pseudocode algorithm is appreciated! Let me know if further clarification is required.
Thanks in advance! :)
In more detail, the simplest approach would be a recursive method, roughly like:
It accepts a prefix string initially empty, a current digit (initially '5'), and a number of digits to generate (initially 10).
If the number of digits is 1, it will simply output the prefix concatenated with the current digit.
If the number of digits is greater than 1, then it will make a list of all possible next digits and call itself recursively with (prefix + (current digit), next digit, (number of digits)-1 ) as the arguments.
Other approaches, and refinements to this one, are possible of course. The "output" action could be writing to a file, adding to a field in the current class or object, or adding to a local variable collection (List or Set) that will be returned as a result. In that last case, the (ndigits>1) logic would have to combine results from multiple recursive calls to get a single return value.

Multiple sequence alignment for general strings java

I'm working in hadoop and I must align n strings in java and i want an algorithm which compute general strings (no bioinformatics, genome, etc) in Java. Es.
ASFHASFHASDSAAPJEIHRA <-- seq1
AAPSOFHASFDSOISISN--A <-- seq2
AWP-JWRAIADSDIA--N--A <-- seq3
AOPSJD-A-JDSSDSOQOSSJ <-- seq4
100000000011000000000 <-- score
There's someone can help me for a name, library or something?
You could write your own dynamic programming algorithm, but the complexity is: O(N^k) if N is the sequence length and k the number of sequences. Suppose you have k=2 sequences:
The you have a 2D grid where each point in your grid corresponds to a pair of characters. So position (1,1) corresponds to word1[1] and word2[1]. Horizontal and vertical edges in this grid correspond to insertions and deletions, while diagonal ones are matches or mismatches. For each you have to device a penalty. In your example a match = +1 while the other possibilities are +0. When you arrive at the bottom right of the grid you have the optimal alignment score.

Categories