Logarithmic way of filtering sparse values..? - java

I have a situation where I show a matrix. The matrix contain values greater than or equal to 0. So to add convenience to the user I just added a bar slider whose minimum value is 0 and maximum value is the maximum value from the matrix. So when the user slides on the slider I just filter the matrix cells and show only those cells which have value greater than the slider value user has slided.
My issue is that in the matrix table the values are quite sparse like there are a lot of values in the range sometime 1-5 and then 20-25 (can be other ranges also). So when i slider the tables gets reduced a lot.
I want something now that on moving the slider only a few values gets filtered. I was thinking if there is any logarithmic way of solving this problem...or may be some other way..

If your matrix is size M x N consider storing the data in an array like data[L][2] where L = M x N and data[i][0] is the actual value while data[i][1] is the value's position in your original M x N matrix.
If data[i][0] is sorted you can process it faster to find positions with values under a given threshold.
Example:
Matrix
1 2 3
0 7 1
5 1 9
Array (value, position)
0 1 1 1 2 3 5 7 9
4 1 6 8 2 3 7 5 9
Cells with values less than 3: 4 1 6 8 2

Related

Do QR/SVD decomposition in ojalgo require as many rows as columns?

When doing QR or SVD decomposition on an m x n matrix A in ojalgo, I've hit a snag. My purpose is to find a basis for the column null space. If m >= n, things work fine. For instance, QR decomposition of the transpose A' of a 5 x 4 matrix A with rank 2 gives me a 4 x 4 Q matrix whose last two columns span the null space of A.
On the other hand, if I start with a 5 x 7 matrix A with rank 5 (and do a QR decomposition of A'), I get the correct rank, but Q is 5 x 5 rather than 7 x 7, and I don't get the null space basis. Similarly, SVD with that same matrix A gets me five positive singular values (no zeros), and the Q2 matrix is 5 x 7 rather than 7 x 7 (no null vectors).
Is this expected behavior? I found a work-around for matrices with n > m (adding n-m rows of zeros to A), but it's clunky.
The matrices can be any size/shape, but calculating the economy sized decomposition is the default behaviour. It is what most users need/want. But there is an interface MatrixDecomposition.EconomySize that lets you control this (to optionally get the full size decomposition). Currently the QR, SVD and Bidiagonal decompositions implement it.

Suitable data structure for Java 1000*1000 matrix.

I am practising java programming. I encountered a problem which requires 1000 x 1000 matrix which stores integers value less than 1500
I would want to navigate across all the elements
I might need to fetch max element and its 4 adjacent elements.
What is the best data structure which doesn't affect performance?
1 2 3 4
5 6 7 8
9 10 11 12
12 14 15 16
for the element 11 --> 7, 10, 12, 15 are adjacent elements.
what is wrong with the 2d array data structure?
to get an adjecent of a number at i,j return [i-1][j],[i+1][j],[i][j+1],i[j-1] (you will have to deal with cases where i is zero etc)...
as performance goes, its O(1), doesn't get any better than that...
If you are talking about finding the location of the element. if the matrix is sorted you can simply do a binary search.
A 2dim Array of shorts:
-> short [][] matrix = new short[1000][1000];
| Added variable matrix of type short[][] with initial value [[S#1794d431
Generate in a second, few MB in size. What could be better than that?

Limit Rows in Result Set By Column Values

This question deals with building a correct Criteria in Hibernate for the following case.
Suppose I start with a table like this:
id ts metric value carrier
1 1 distance 4 alice
2 1 count 2 alice
3 2 distance 3 alice
4 2 count 1 alice
5 1 distance 3 becky
6 1 count 2 becky
7 2 distance 4 becky
8 2 count 1 becky
9 1 distance 10 other
10 1 count 10 other
11 2 distance 10 other
12 2 distance 10 other
What this is is a time-bucketed set of metrics recording how far alice, becky and a general other carried some count of items some distance.
I'd like to roll it up in the following fashion: metrics with 'other' or the 'winner' as decided by distance for each time bucket are kept. Thus, the above table would yield the following result set:
id ts metric value carrier
1 1 distance 4 alice
2 1 count 2 alice
7 2 distance 4 becky
8 2 count 1 becky
9 1 distance 10 other
10 1 count 10 other
11 2 distance 10 other
12 2 distance 10 other
Ultimately this is translated to this:
ts carrier distance count
1 alice 4 2
1 other 10 10
2 becky 4 1
2 other 10 10
But this translation I already understand how to do. What I'm unclear on is how to build the criteria to keep the 'top n' metrics. Which brings us to the wrinkle: while this example is simplified, there would be a large number of 'carriers', and what I'm interested in is the top n such carriers, discarding the rest. Thus in the example above n = 1, but it's likely to be greater than 1 in most cases.
I know that I can use addOrder(Order.desc("value"), but that poses both a problem in that other is intermixed and distances and counts will be incorrectly intermingled. I'm looking for something that sorts 'blocks' of rows which are decided, in order by ts, then carrier, then using the metric = "distance" for the sort order.

How to find Path of Lowest Cost in 2D matrix

i have a Challenge the objective is to get the lowest cost of the path.
The path can proceed horizontally or diagonally. not vertically. like below.
and the first and last row are also adjacent.
for example see below matrix:
output for 1st matrix :
16
1 2 3 4 4 5-->path row number
output for second matrix:
11
1 2 1 5 4 5-->path row number
am doing it in java, am getting the Lowest path but am not getting the path to print the path using row numbers.
int minCost(int cost[r][r], int m, int n)
{
if (n < 0 || m < 0)
return Integer.MAX_VALUE;;
else if ((m == r-1 && n == c-1)|| n+1>=c)
return cost[m][n];
else
return cost[m][n] + min( minCost(cost, m+1>=r?r-1:m+1,n+1),
minCost(cost, m,n+1),
minCost(cost, m-1>=0?m-1:r-1,n+1));
}
// calling it
minCost(cost, 0, 0);
How to get the row numbers for shortest path?
Your algorithm is quite inefficient. The best solution I can think is calculating it backwards(from right to left). Consider the right 2 columns of your second matrix:
8 6
7 4
9 5
2 6
2 3
If now we are on the cell with value 8, the next step can be 6/4/3. Of course we choose 3 because we want a smaller cost. If now we are on the cell with value 7, the next step can be 6/4/5, we will choose 4. So the two columns can merged into one column:
11 //8+3
11 //7+4
13 //9+4
5 //2+3
5 //2+3
Now repeat the last two columns:
2 11
2 11
9 13
3 5
1 5
Finally the matrix will be merged into one column, the smallest value in the column has the lowest cost.
I'll try to expand on fabian's comment:
It's clear that your minCost function will return the same values if called with the same arguments. In your algorithm, it indeed does get called lots of times with the same values. Every call for column zero will generate 3 calls for column 1, which in turn generate 9 calls for column 2, etc. The last column will get a huge number of calls (3^r as fabian pointed), most of them recalculating the very same values for other calls.
The idea is to store these values so they don't need to be recalculated every time they are needed. A very simple way of doing this is to create a new matrix of the same size as the original and calculating, column by column, the minimum sum for getting to each cell. The first column will be trivial (just copy from the original array, as there is only one step involved), and then proceed for the other columns reusing the values already calculated.
After that, you can optimize space usage by replacing the second matrix by only two columns, as you are not going to need column n-1 once you have column n fully calculated. This can be a bit tricky, so if you're unsure I recommend using the full array the first time.

Increasing the Elements of an Array Like a Number

I'm attempting to make a Sudoku solving program. To reach the puzzle's solution, the program interprets 0's as empty slots, and then makes an array that has a length equivalent to the number of zeros in the entire puzzle. From there, it sets all of the values in the array to 1 (the minimum value any slot can have in a Sudoku puzzle). What I'm trying to do is simulate a number's incremental pattern in the array starting from the element with the greatest index.
For example, a puzzle with three empty slots would result in an array of 3 elements. The array would then increase based on the pattern mention above:
0 0 0 (Initiation)
1 1 1 (Set to possible values)
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 2 1 (Skips 1 2 0 since it would include a 0)
1 2 2
etc.
This is a modified form of a base 10 number increment. Instead of 0-9, it uses 1-9. How may I build a method that will increment the array in this manner?
The basic algorithm here is to increment the right most digit then, if it overflows, increment the next to the left and so on. Recursion is a neat way of solving this. I'll do it in pseudocode and leave you to convert to Java
function increment(array, digit)
if (array[digit] < 9)
array[digit] += 1
else if (digit > 0)
array[digit] == 1;
increment(array, digit - 1)
else
you are finished
Then each time you call this with: increment(array, array.length - 1)

Categories

Resources