hello guys i have 2d char array opt[][] and i have 2 sequence in my arrays like in example
my
`opt[0][0]=A
opt[0][1]=T
opt[0][2]=G
opt[0][3]=A`
and
opt[1][0]=A
opt[2][0]=G
opt[3][0]=C
opt[4][0]=T
i have this output currently
x/y| A T G A -
_______________________
0 A | 0 0 0 0
1 G | 0 0 0 0
2 C | 0 0 0 0
3 T | 0 0 0 0
4 - | 0 0 0 0
my problem is this how can i use dynamic programming
to create this array into this
http://i.stack.imgur.com/ViHc9.png
if its a match 0 penalty
if its a mismatch 1 penalty
if its a gap its 2 penalty
i can compare chars of my array like this
for(int i=0;i<4;i++){
if(opt[0][i]==opt[i+1][0]){
result[0][i] =1;
}
but this is just a simple test i made to see if i can compare and it turned out i can.
how can i go from here to there(to the picture array
I suggest you read these articles.
http://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm
http://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm
The implementation in any language is pretty trivial.
And if you need information about dynamic programming in general,
either Google for it yourself, or check these two links.
http://en.wikipedia.org/wiki/Dynamic_programming
https://www.topcoder.com/tc?d1=tutorials&d2=dynProg&module=Static
Related
I want to split a number into different numbers.(e.g. 123456 -> 123,456 or 12,3456 or 123,45,6...etc). They have to be in the same order as well. So one method I was thinking was putting each digit of the number into an array first. Then I would go through the array and find each combination of them. But I think this method would have a long run-time to find all possible combination especially if the number can be split more than 2 times. Would there be a more efficient method in doing this?
Would there be a more efficient method in doing this?
No.
There will be 2n-1 combinations, e.g. for a 6-digit number, 25 = 32 combinations.
Think of it like this: Each "space" between digits can be either separated or not, true/false, 1/0, i.e. a "bit", so you can determine split combination using a 5-bit number:
1 2 3 4 5 6
^ ^ ^ ^ ^
0 0 0 0 0 -> 123456
0 0 0 0 1 -> 12345,6
0 0 0 1 0 -> 1234,56
. . . .
1 1 1 1 0 -> 1,2,3,4,56
1 1 1 1 1 -> 1,2,3,4,5,6
I use EJML to solve a linear equation system.
EJML uses doubles. My inputs (and expected outputs) are integers. I will omit the .000 from the toString().
My Matrix A looks like this (though it is ~1000x1000):
1 0 0 0 0
1 -1 1 0 0
0 1 -1 1 0
0 0 1 -1 1
0 0 0 0 1
My b is just a vector with a value in the first and the last index, the rest is 0.
{-10 0 0 0 10}'
For size 5x5 I can use EJML just fine, but for my 1000x1000 Matrix I get a Solution contains uncountable numbers-Error.
The result looks like this:
{NaN NaN NaN ... NaN -Infinity -Infinity 1}'
My code looks like this (the matrices are correct, I checked that via Sysout and Debugger):
// Setup A
// Setup b
SimpleMatrix x = A.solve(b);
Now I assume my System somehow behaves badly. Sadly I'm not that much into matrices, so I assume I'm maybe using the wrong methods to solve this particular problem. The other thing I can think of is that double-precision gets in my way.
Is there anything I can do or is EJML simply not the right tool here?
I want to find efficient algorithm based on which subset it is. New condition is to be executed for each subset.
For eg: I have 4 flags ABCD and each subset will have seperate condition. What is the most efficient algorithm to solve the following condition. It can be made easily but I want to find the most efficient algorithm. Is there already an algorithm which solves this kind of problem?
A B C D
0 0 0 0 Subset 1 Execute Condition 1
0 0 0 1 Subset 2 Execute Condition 2
0 0 1 0 Subset 3 Execute Condition 3
0 0 1 1 Subset 4 Execute Condition 4
0 1 0 0 Subset 5 Execute Condition 5
0 1 0 1 Subset 6 Execute Condition 6
0 1 1 0 Subset 7 Execute Condition 7
0 1 1 1 Subset 8 Execute Condition 8
1 0 0 0 Subset 9 Execute Condition 9
1 0 0 1 Subset 10 Execute Condition 10
1 0 1 0 Subset 11 Execute Condition 11
1 0 1 1 Subset 12 Execute Condition 12
1 1 0 0 Subset 13 Execute Condition 13
1 1 0 1 Subset 14 Execute Condition 14
1 1 1 0 Subset 15 Execute Condition 15
1 1 1 1 Subset 16 Execute Condition 16
Bitmasking can be used to generate all subsets. There are four values. Therefore, you have 2^4 subsets. All you have to do is iterate this mask 2^4 times and mask it with each of the four values. In each iteration, the result of masking is a subset of the given values. Here's an idea:
allSubsets = {}
for mask in range(1<<4):
subsets = []
for i in range(0,3):
val = mask & (1<<i)
if(val)
subsets.append(a[i]) # Individual subset. Here assume array a has 4 values. Can be just 1s and 0s as in your case.
allSubsets[mask] = subset #keep appending each generated subset
return allSubsets # Do your operation by iterating on each of these subsets
I'm having a hard time thinking of an appropriate data structure to use to represent an adjacency matrix for an undirected graph.
I want to be able to take the nodes from these graphs and insert them into random positions in arrays, and then "score" the arrays based on how well they've managed to keep the adjacent nodes apart. i.e if node A and node B are connected in my graph, and the array places them next to each other, +1 would be added to the array's score, with the lowest scoring array being the best.
So what would be the best data structure to use to represent a collection of nodes, and the neighbouring nodes of each one in the collection?
If I understand your question which I do not think it really clear.
For an adjacency matrix, I think the best way to go is an Array. You can access each positions in O(1) and since it is an undirected graph, it should be easy to create. see graph below
0 --- 1------5---6
| \ \ | /
| \ \ | /
2 3----4---7
0 1 2 3 4 5 6 7
-----------------
0 | 0 1 1 1 0 0 0 0
1 | 1 0 0 0 1 1 0 0
2 | 1 0 0 0 0 0 0 0
3 | 1 0 0 0 1 0 0 0
4 | 0 1 0 1 0 0 0 1
5 | 0 1 0 0 0 0 1 1
6 | 0 0 0 0 0 1 0 1
7 | 0 0 0 0 1 1 1 0
------------------
You can implement your matrix like so and perform whatever operation you want on it. And all that matters is that if a location is not 0 then the graph is connected and you can just pick the highest value for whatever you are doing.
Below is my attempt at solving the 8 queens problem to print one solution. (place 8 queens on a chessboard such that none of them are attacking each other). However, this solution only places 6 queens. I need another opinion of where I am making a mistake. I'm doing this in more of a BFS style instead of backtracking.
It seems your algorithm is malfunctioning at some point. Upon running it, I found the following issues:
You are constantly setting visited[i][j] to 0 in your for loop in main. This always resets visited to 0 even if a recursion call is made. In fact, when you declare both visited and board they are initiated to arrays full of 0s. So you can get rid of both set statements in there. In addition, because you reset the arrays, your recursive function ends up setting both values to 0 and then finds them again."
For debugging, in the !hasQueen statement, you should output the board[row][col] coordinates, which show you the coordinates that have been found. The final list before it prints out the grid shows that 2,4 and 1,6 are found and set twice.
The actual chessboard that is output ends up with an impossible solution:
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0
0 0 0 X 0 Y 0 0
0 0 0 Y 0 X 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
(sorry I can't get the numbers to format)
Both layout X and layout Y fail the 8 queens rules.
If you run your program with the setting to 0 commented out, you will see that it grinds to a halt after finding 6 locations.