How to perform spiral order traversalin one dimensional array? [duplicate] - algorithm

This question already has answers here:
Closed 10 years ago.
Possible Duplicates:
Looping in a spiral
Writing a string in a spiral
Print two-dimensional array in spiral order
2d Array in Spiral Order
We have a data in the form of matrix
0 0 0 0 0
0 1 2 3 0
0 4 5 6 0
0 7 8 9 0
0 0 0 0 0
which is stored in a 1d array in this fashion
[0 0 0 0 0 0 1 2 3 0 0 4 5 6 0 0 7 8 9 0 0 0 0 0 0]
This is a zero padded 3x3 array transformed into 5x5. We know the start index and end index.
As we see, we can perform 25 operations and print all values, but instead if we go in spiral order we should ideally do this in only 9 operations.
Does anyone know how to do this?
We know the number of rows and number of columns. Here it would be rows=5 cols=5.
Hence start index would be rows+1 and end index would be rows*cols-6
I'm visualizing it as a spiral order traversal.

Given your 5x5 matrix assuming a zero index base you know the row indices are:
0,1,2,3,4
5,6,7,8,9
10,11,12,13,14
15,16,17,18,19
20,21,22,23,24
you know your first index is 6 and the last is 18. So as a starting point you know you can eliminate the following pieces of the matrix:
0,1,2,3,4
and
19,20,21,22,23,24,25
this counts as 2 operations.
you also know that since you are starting at index 6 and it was a 3x3 you only need to go to index 8, this is one operation.
you then need to add 5 to your previous index of 6 this yields 11 and proceed again (2 operations in total) current operation count is 5)
do this again with 11 and you get 16 one operation. Get 2 more operations and you end up with 18. This is now 8 operations in total.

I'd do something like this:
POINT ul = (start_idx/width, start_idx%width);
POINT br = (end_idx/width, end_idx%width);
POINT p = ul
dir = RIGHT;
while (ul != br)
visit(ARRAY[p.x+p.y*WIDTH])
case dir
when RIGHT:
p.x+=1
if (p.x==br.x)
dir = DOWN
ul.y++;
when DOWN
p.y+=1
if (p.y==br.y)
dir = LEFT
br.x--;
when LEFT:
//like RIGHT but -1 and adjust br.y upwards when done
when UP:
//like DOWN but -1 and adjust ul.x rightward when done
endwhile
The idea is to keep track of the virtual x and y to visit. Move the point to visit along the sides of the box defined by the start and end points. And adjust the sides inwards as you complete the visits.

Related

Segment tree built on "light bulbs"

I have encountered following problem:
There are n numbers (0 or 1) and there are 2 operations. You can swich all numbers to 0 or 1 on a specific range(note that switching 001 to 0 is 000, not 110) and you can also ask about how many elements are turned on on a specific range.
Example:
->Our array is 0100101
We set elements from 1 to 3 to 1:
->Our array is 1110101 now
We set elements from 2 to 5 to 0:
->Our array is 1000001 now
We are asking about sum from 2nd to 7th element
-> The answer is 1
Brute force soltion is too slow(O(n*q), where q is number of quetions), so I assume that there has to be a faster one. Probably using segment tree, but I can not find it...
You could build subsampling binary tree in the fashion of mipmaps used in computer graphics.
Each node of the tree contains the sum of its children's values.
E.g.:
0100010011110100
1 0 1 0 2 2 1 0
1 1 4 1
2 5
7
This will bring down complexity for a single query to O(log₂n).
For an editing operation, you also get O(log₂n) by implementing a shortcut: Instead of applying changes recursively, you stop at a node that is fully covered by the input range; thus creating a sparse representation of the sequence. Each node representing M light bulbs either
has value 0 and no children, or
has value M and no children, or
has a value in the range 1..M-1 and 2 children.
The tree above would actually be stored like this:
7
2 5
1 1 4 1
1 0 1 0 1 0
01 01 01
You end up with O(q*log₂n).

APL: how to search for a value's index in a matrix

In APL, matrices and vectors are used to hold data. I was wondering if there was a way to search within a matrix for a given value, and have that values index returned. For example, say I have the following 2-dimensional matrices:
VALUES ← 1 2 3 4 5 6 7 8 9 10 11... all the way up to 36
KINDS ← 0 0 0 2 0 0 0 3 0 ... filled with 0's the rest of the way to 36 length.
If I laminated these two matrices with
kinds,[.5] values
so that they are laminated one on top of the other
1 2 3 4 5 6 7 8 9 10...
0 0 0 2 0 0 0 3 0 ....
is there a functionally easy way to search for the index of the 2 value in the "second row" of the newly laminated matrix? eg. the column containing
4
2
and return that matrix index?
The value 2 also appears in row 1 of your newly laminated matrix (nlm), and as you stated, you really do not want to search the whole matrix, but only the second row. So, since you're only searching within a given row, getting the column index in that row gives you the complete answer:
row←2
⎕←col←nlm[row;]⍳2
4
nlm[;col] ⍝ values in matched column
4 2
Try it online!

Fill matrix randomly without row-repetitions

Please help. I'm new to matlab scripting and need a bit of help. I have a series of numbers:
A=[1 1 1 2 2 2 3 3 3 4 4 4 5]
which I want to fill randomly into an 8x12 matrix without having the same number in the same row. At the end I want all the "empty" cells of the 8x12 matrix being filled with 0's or nan.
an example could be:
result=
3 1 5 2 4 5 0 0 0 0 0 0
4 1 3 2 0 0 0 0 0 0 0 0
1 3 4 2 0 0 0 0 0 0 0 0
make sure A is sorted. A = sort(A)more info
make an empty matrix.
For each number in A: more info
find out how many repetitions of the number there are -> for loop in A, start is the first occurance of the number, end is the last, n = last-first+1
find all rows that have space for an extra number, just do a double for loop and keep track of elements that are zero
randomly pick n rows -> more info. To do this, make an array R of all available row indixes. Then take a random sample between 1..size(R,2) with the provided function and get all the values, you now have your row indixes.
randomly pick one of the empty spots in each of the selected rows and assign the number

Index of a permutation

I was recently reading about Lehmer codes and how they can be used to map an index to a permutation corresponding to that index and realized they can be quite useful to quickly generate a permutation. Does anyone know how can this be done using an algorithm, and also what are the limits of such a method, I suppose we can't go above index = 1.7977e+308, but still seems quite an interesting method.
So basically lets say we have
perm
1 0 0 0
2 0 0 1
3 0 0 2
4 0 1 0
5 0 1 1
6 0 1 2
...
We should be able to deduce that the index of [ 0 1 0 ] is 4,
or that the index 6 corresponds to [ 0 1 2 ]
Thanks for any help.
The vector for each index is the base 3 representation of the index (minus one)
the functions dec2base and base2dec can be used for this with a little fiddling to get the sting outputs to the required format
index to vector
index=4; % input index
n=3; % length of vector
vec=str2num([dec2base(index-1,3,n)].').'
vec=
0 1 0
vector to index
vec=[0,1,2]; % input vector
vecstr=strcat(['' vec(:)+'0'].');
index=base2dec(vecstr,3)+1
index =
6

Matrix, algorithm interview question

This was one of my interview questions.
We have a matrix containing integers (no range provided). The matrix is randomly populated with integers. We need to devise an algorithm which finds those rows which match exactly with a column(s). We need to return the row number and the column number for the match. The order of of the matching elements is the same. For example, If, i'th row matches with j'th column, and i'th row contains the elements - [1,4,5,6,3]. Then jth column would also contain the elements - [1,4,5,6,3]. Size is n x n.
My solution:
RCEQUAL(A,i1..12,j1..j2)// A is n*n matrix
if(i2-i1==2 && j2-j1==2 && b[n*i1+1..n*i2] has [j1..j2])
use brute force to check if the rows and columns are same.
if (any rows and columns are same)
store the row and column numbers in b[1..n^2].//b[1],b[n+2],b[2n+3].. store row no,
// b[2..n+1] stores columns that
//match with row 1, b[n+3..2n+2]
//those that match with row 2,etc..
else
RCEQUAL(A,1..n/2,1..n/2);
RCEQUAL(A,n/2..n,1..n/2);
RCEQUAL(A,1..n/2,n/2..n);
RCEQUAL(A,n/2..n,n/2..n);
Takes O(n^2). Is this correct? If correct, is there a faster algorithm?
you could build a trie from the data in the rows. then you can compare the columns with the trie.
this would allow to exit as soon as the beginning of a column do not match any row. also this would let you check a column against all rows in one pass.
of course the trie is most interesting when n is big (setting up a trie for a small n is not worth it) and when there are many rows and columns which are quite the same. but even in the worst case where all integers in the matrix are different, the structure allows for a clear algorithm...
You could speed up the average case by calculating the sum of each row/column and narrowing your brute-force comparison (which you have to do eventually) only on rows that match the sums of columns.
This doesn't increase the worst case (all having the same sum) but if your input is truly random that "won't happen" :-)
This might only work on non-singular matrices (not sure), but...
Let A be a square (and possibly non-singular) NxN matrix. Let A' be the transpose of A. If we create matrix B such that it is a horizontal concatenation of A and A' (in other words [A A']) and put it into RREF form, we will get a diagonal on all ones in the left half and some square matrix in the right half.
Example:
A = 1 2
3 4
A'= 1 3
2 4
B = 1 2 1 3
3 4 2 4
rref(B) = 1 0 0 -2
0 1 0.5 2.5
On the other hand, if a column of A were equal to a row of A then column of A would be equal to a column of A'. Then we would get another single 1 in of of the columns of the right half of rref(B).
Example
A=
1 2 3 4 5
2 6 -3 4 6
3 8 -7 6 9
4 1 7 -5 3
5 2 4 -1 -1
A'=
1 2 3 4 5
2 6 8 1 2
3 -3 -7 7 4
4 4 6 -5 -1
5 6 9 3 -1
B =
1 2 3 4 5 1 2 3 4 5
2 6 -3 4 6 2 6 8 1 2
3 8 -7 6 9 3 -3 -7 7 4
4 1 7 -5 3 4 4 6 -5 -1
5 2 4 -1 -1 5 6 9 3 -1
rref(B)=
1 0 0 0 0 1.000 -3.689 -5.921 3.080 0.495
0 1 0 0 0 0 6.054 9.394 -3.097 -1.024
0 0 1 0 0 0 2.378 3.842 -0.961 0.009
0 0 0 1 0 0 -0.565 -0.842 1.823 0.802
0 0 0 0 1 0 -2.258 -3.605 0.540 0.662
1.000 in the top row of the right half means that the first column of A matches on of its rows. The fact that the 1.000 is in the left-most column of the right half means that it is the first row.
Without looking at your algorithm or any of the approaches in the previous answers, but since the matrix has n^2 elements to begin with, I do not think there is a method which does better than that :)
IFF the matrix is truely random...
You could create a list of pointers to the columns sorted by the first element. Then create a similar list of the rows sorted by their first element. This takes O(n*logn).
Next create an index into each sorted list initialized to 0. If the first elements match, you must compare the whole row. If they do not match, increment the index of the one with the lowest starting element (either move to the next row or to the next column). Since each index cycles from 0 to n-1 only once, you have at most 2*n comparisons unless all the rows and columns start with the same number, but we said a matrix of random numbers.
The time for a row/column comparison is n in the worst case, but is expected to be O(1) on average with random data.
So 2 sorts of O(nlogn), and a scan of 2*n*1 gives you an expected run time of O(nlogn). This is of course assuming random data. Worst case is still going to be n**3 for a large matrix with most elements the same value.

Resources