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?
Related
I have an array like that :
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 0
0 1 1 1 1 0 0 1 0 1 0
0 1 1 0 1 0 0 0 0 1 1
0 0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0
And I want to group elements which are '1'.
So you see I have a classical dfs by using a stack. The question is, If I have a matrix like the above, what is the time complexity of this algorithm where n is the number of matrix elements. (Row*column).If it worse than O(N) (since I must traverse whole 2D array) which approach will help me to improve this algorithm?
Example of an O(n * log n) algorithm
(where n is the number of matrix elements)
The idea of the algorithm is as follows.
Initialize a treeset of unhandled elements U by adding all matrix elements into it
While U isn't yet empty, take any u from U and check it for its value
If u = '0' then just remove it, i.e. U := U \ {u}
If u = '1' then start exploration DFS(u, U)
Where procedure DFS(u, U) uses the matrix to explore the '1' neighbours of u.
However, here comes the kicker, DFS(u, U) also removes every discovered element from U.
It is rather easy to understand and prove that this algorithm indeed always finishes in O(n * log n). Deleting an element from a treeset has worst-case complexity O(log n). Each DFS(u, U) run can visit at most |U| elements, and each element visited through any means is removed from U as the execution progresses. The algorithm terminates when U becomes empty.
Short summary
It is possible to produce an O(n^2) algorithm for example by running DFS on each element regardless of your previously attained knowledge.
Using any mechanism ensuring that you don't run DFS on an already discovered group/island is likely to produce a superior algorithm.
Sorry that I can't analyze your own algorithm directly, but this may help you do it on your own.
The lower bound of the algorithm that you need to devise should be O(m+n) - I am considering the number of rows and columns as m and n respectively. This is because you need to traverse the whole 2D array anyway. If you are solving this using two for loops, this will take O(m+n) as well. For each element in your matrix, you will compare with other 4 adjacent elements and hence, the overall checking size is <= 4mn.
I think there is no better way to solve this in less than O(m+n) time.
Please note that in your question, if you are considering m+n = N then the complexity that I am referring to is O(N).
This was a highschool-degree coding contest question a while back. The basic idea was to recreate a painting of black and white with only rectangular XOR operations, or that's what they called it.
The problem
Let us assume we have this painting we are trying to recreate (represented as a binary matrix, 0 being black and 1 being white):
1 0 0
1 1 1
1 0 1
One way of recreating the painting would be the following operations:
(0, 0) (2, 2)
(1, 0) (2, 0)
(1, 2) (1, 2)
The operations are in the form of (xStart, yStart) (xEnd, yEnd)
Thus the above operations would do the following, if we start from an all-black canvas:
beginning:
0 0 0
0 0 0
0 0 0
after (0, 0) (2, 2) :
1 1 1
1 1 1
1 1 1
after (1, 0) (2, 0) :
1 0 0
1 1 1
1 1 1
after (1, 2) (1, 2) :
1 0 0
1 1 1
1 0 1
Technicalities about the assignment:
Winner has the least operations.
One operation should be in the form of (xStart, yStart) (xEnd, yEnd).
There are no time or space restraints.
In the assignment, the painting we were trying to recreate was 200x200 in size, and generated with 2000 random XOR operations.
My own ideas
I have come up with a couple of ways to do this. I'll list them here in order of worse to best.
XOR all the pixels:
We can recreate the painting by simply writing 1 to a blank canvas where a 1 resides in the painting we are trying to recreate. This solution is the simplest and most obvious of all. The number of operations needed is basically the amount of white pixels in the painting.
XOR all horizontally adjacent whites:
This is a substantial improvement from the first solution, but still very simple and obvious. In this method we simply XOR all horizontally adjacent whites. In this way, for example the operations
(0, 0) (0, 0)
(1, 0) (1, 0)
(2, 0) (2, 0)
would be diminished to (0, 0) (2, 0).
XOR rectangles:
This I think is a clear follow-up of the previous method, which we can see as XORing rectangles with height of 1 - Now we just add a second dimension to the rectangles, further improving our results. I determined the XORable area by getting the rectangle with the most whites. The improvement is still good.
XOR the biggest difference:
This is a slight change from the above methods, and a bit more brute-force. In this method I find the rectangle with the biggest difference to the painting, and XOR it. For example, if we have the painting
1 0 1
0 1 1
0 1 0
and an all-black canvas, the biggest difference would be in the rectangle (0, 0) (2, 1), which has a difference of 2. I calculate the difference by getting all the non-same colors of the painting, which is 4 in the above situation, and then subtracting the amount of same colors from it, which in the above situation is 2. So different_colors - same_colors = difference.
In the above painting and a blank canvas, there are many rectangles that produce the same difference. One other is (1, 0) (2, 2).
This method gave the smallest improvement from the previous one with large paintings, but an improvement nonetheless. Interstingly, this method sometimes came up with a worse solution than the previous one with small paintings (can't remember how small, though).
Any code I had for the described methods above have been long since lost. Can you come up with breath-taking solutions? Is there a magical approach from outer space? I find this question (post) pretty interesting and would like to see if anyone can come up with anything.
About the tags
I have tagged this with both Java and C++, not because this question concerns particularly those languages, but because I can easily understand any code written in those languages, and with languages with similar syntax.
I think, to complete this task, we require to find the coordinates of maximum size sub-matrix containing only zeros.
I can explain that algorithm but I think following link has the best explanation :
Maximum size sum-matrix with all 1s in binary matrix
Here solution is for all 1s, we can modify it for all 0s.
Then all we need to do is find the coordinate from maximum value and we can do operation.
I'll update if I can think of some better method.
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.
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
In the yester Code Jam Qualification round http://code.google.com/codejam/contest/dashboard?c=433101#s=a&a=0 , there was a problem called Snapper Chain. From the contest analysis I came to know the problem requires bit twiddling stuff like extracting the rightmost N bits of an integer and checking if they all are 1. I saw a contestant's(Eireksten) code which performed the said operation like below:
(((K&(1<<N)-1))==(1<<N)-1)
I couldn't understand how this works. What is the use of -1 there in the comparison?. If somebody can explain this, it would be very much useful for us rookies. Also, Any tips on identifying this sort of problems would be much appreciated. I used a naive algorithm to solve this problem and ended up solving only the smaller data set.(It took heck of a time to compile the larger data set which is required to be submitted within 8 minutes.). Thanks in advance.
Let's use N=3 as an example. In binary, 1<<3 == 0b1000. So 1<<3 - 1 == 0b111.
In general, 1<<N - 1 creates a number with N ones in binary form.
Let R = 1<<N-1. Then the expression becomes (K&R) == R. The K&R will extract the last N bits, for example:
101001010
& 111
————————————
000000010
(Recall that the bitwise-AND will return 1 in a digit, if and only if both operands have a 1 in that digit.)
The equality holds if and only if the last N bits are all 1. Thus the expression checks if K ends with N ones.
For example: N=3, K=101010
1. (1<<N) = 001000 (shift function)
2. (1<<N)-1 = 000111 (http://en.wikipedia.org/wiki/Two's_complement)
3. K&(1<<N)-1 = 0000010 (Bitmask)
4. K&(1<<N)-1 == (1<<N)-1) = (0000010 == 000111) = FALSE
I was working through the Snapper Chain problem and came here looking for an explanation on how the bit twiddling algorithm I came across in the solutions worked. I found some good info but it still took me a good while to figure it out for myself, being a bitwise noob.
Here's my attempt at explaining the algorithm and how to come up with it. If we enumerate all the possible power and ON/OFF states for each snapper in a chain, we see a pattern. Given the test case N=3, K=7 (3 snappers, 7 snaps), we show the power and ON/OFF states for each snapper for every kth snap:
1 2 3
0b:1 1 1.1 1.0 0.0 -> ON for n=1
0b:10 2 1.0 0.1 0.0
0b:11 3 1.1 1.1 1.0 -> ON for n=1, n=2
0b:100 4 1.0 0.0 1.0
0b:101 5 1.1 1.0 1.0 -> ON for n=1
0b:110 6 1.0 0.1 0.1
0b:111 7 1.1 1.1 1.1 -> ON for n=2, n=3
The lightbulb is on when all snappers are on and receiving power, or when we have a kth snap resulting in n 1s. Even more simply, the bulb is on when all of the snappers are ON, since they all must be receiving power to be ON (and hence the bulb). This means for every k snaps, we need n 1s.
Further, you can note that k is all binary 1s not only for k=7 that satisfies n=3, but for k=3 that satisifes n=2 and k=1 that satisifes n=1. Further, for n = 1 or 2 we see that every number of snaps that turns the bulb on, the last n digits of k are always 1. We can attempt to generalize that all ks that satisfy n snappers will be a binary number ending in n digits of 1.
We can use the expression noted by an earlier poster than 1 << n - 1 always gives us n binary digits of 1, or in this case, 1 << 3 - 1 = 0b111. If we treat our chain of n snappers as a binary number where each digit represents on/off, and we want n digits of one, this gives us our representation.
Now we want to find those cases where 1 << n - 1 is equal to some k that ends in n binary digits of 1, which we do by performing a bitwise-and: k & (1 << n - 1) to get the last n digits of k, and then comparing that to 1 << n - 1.
I suppose this type of thinking comes more naturally after working with these types of problems a lot, but it's still intimidating to me and I doubt I would ever have come up with such a solution by myself!
Here's my solution in perl:
$tests = <>;
for (1..$tests) {
($n, $k) = split / /, <>;
$m = 1 << $n - 1;
printf "Case #%d: %s\n", $_, (($k & $m) == $m) ? 'ON' : 'OFF';
}
I think we can recognize this kind of problem by calculating the answer by hand first, for some series of N (for example 1,2,3,..). After that, we will recognize the state change and then write a function to automate the process (first function). Run the program for some inputs, and notice the pattern.
When we get the pattern, write the function representing the pattern (second function), and compare the output of the first function and the second function.
For the Code Jam case, we can run both function against the small dataset, and verify the output. If it is identical, we have a high probability that the second function can solve the large dataset in time.