What is the fastest way of exploring an array from a point (i,j) , using Chebysev distance?
My aproach:
I am currently defining 2 one dimensional arrays that store the directions for the start then compute with a For what is left when radius > 1 ( radius is the "radius" of the chebysev circle I wanna explore the array) . I am finding I am exploring some elements twice . Is there an algorithm that shows what is the best aproach ?
Be 0 the distance between (i,j) and himself . I would want the matrix to be explored like this ( the numbers represent the distance between i,j and them). Ofcourse i,j is not always it the middle , it must be any point I choose .
2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2
Thank you and excuse my english :)
You can use the BFS algorithm. It is just a simple loop with a queue.
Your "edges" on are links between the position (i, j) and its 8 neighbors:
(i-1,j)
(i-1,j-1)
...
(i+1,j+1)
Related
We are given array-'a' and array-'b' consisting of positive integers.
How can I count all the permutation of array 'a' which are strictly lexicographically smaller than array-'b'?
Arrays can contain as many as 10^5 integers(positive)
Example:
1 2 3 is lexicographically smaller than 3 1 2
1 2 3 is lexicographically smaller than 1 4 5.
I would like the solution to be in C++.
Input : 3
1 2 3
2 1 3
Output : 2
Only permutations 1,2,3 and 1,3,2 are lexicographically smaller than 2 1 3
Let's just tackle the algorithm. Once you get that figured out, the implementation should be pretty straightforward. Does this look like it does what you're looking for?
Pseudo code:
function get_perms(a,b)
#count the number of digits in a that are <b[0]
count = sum(a<b[0])
Nperms = (len(a)-1)! #modify this formula as needed
N = count*Nperms
if sum(a==b[0]) > 0
remove 1 b[0] from a
# repeat the process with the substring assuming a[0]==b[0]
N += sum(a==b[0])*get_perms(a,b[1:end])
return N
main()
get_perms(a,b)
Edit: I did a little searching. I believe that this is what you are looking for.
I confuse how to define the program that will find the matrix of edge in a graph.
The problem is if one inputs the value of adjacency matrix that give information about connection of vertices in a graph, example : there are 3 vertices, then V1 connected to V2 but not with V3, then V2 connected to V3, it gives :
0 1 0
1 0 1
0 1 0
now, with that information, I want to make the program which find the connection of edge to edge, example there are 3 edges : 1-2 edge, 2-3 edge, and its output is :
0 1
1 0
I know to make the first output "Adjacency matrix", but the second.
Thanks in advance.
An adjacency matrix is used to represent a graph which means you have both vertices and edges in it. So if the input is :
0 1 1
1 0 1
1 1 0
It means your graph is complete. If you read it with putting numbers on your matrix:
X 1 2 3
1 0 1 1
2 1 0 1
3 1 1 0
You can see that:
V1 connects to V2 and V3
V2 connects to V1 and V3
V3 connects to V1 and V2
By reading row n you see which vertice Vn connects to and by reading column n you can see which vertices are connected to Vn.
In your example, your graph is non-oriented because your matrix is diagonal.
If your aim is to find what are the edges of your graph, you already have the information in the adjacency matrix. If you want to see what vertices are "double connected" you need to find if for vertices Vi and Vj the M[i][j] and the M[j][i] members of your matrix both have the value 1 (It is always the case in non-oriented graph). A possible algorithm for that is just a double loop on your matrix representation (generally two vectors/lists/arrays).
I've got a 2D vector which holds a matrix of integers, which looks like this:
vector<vector<int>> Members;
What I am trying to find is a way on how to extract every possible sub matrix of a NxN matrix.
For example if I had a 2x2 matrix:
0 -2
9 2
It would output:
0
-2
9
2
0
9
-2
2
0 -2
9 2
Sub-Matrix depends on the left-up point and right-bottom point, so you can give all possible locations and print them one by one like this:
//data stored in mat[max][max]
int max=5;//size of matrix
int i,j,m,n;
for(i=0;i<=max-1;i++)
for(j=0;j<=max-1;j++)
for(m=i;m<=max-1;m++)
for(n=j;n<=max-1;j++)
print(i,j,m,n);//a simple function
I'm trying to transpose a sparse matrix in c++. I'm struggling with the traversal of the new transposed matrix. I want to enter everything from the first row of the matrix to the first column of the new matrix.
Each row has the column index the number should be in and the number itself.
Input:
colInd num colInd num colInd num
Input:
1 1 2 2 3 3
1 4 2 5 3 6
1 7 2 8 3 9
Output:
1 1 2 4 3 7
1 2 2 5 3 8
1 3 2 6 3 9
How do I make the list traverse down the first column inserting the first element as it goes then go back to the top inserting down the second column. Apologies if this is two hard to follow. But all I want help with is traversing the Transposed matrix to be in the right place at the right time inserting a nz(non zero) object in the right place.
Here is my code
list<singleRow> tran;
//Finshed reading so transpose
for (int i = 0; i < rows.size(); i++){ // Initialize transposed matrix
singleRow trow;
tran.push_back(trow);
}
list<singleRow>::const_iterator rit;
list<singleRow>::const_iterator trowit;
int rowind;
for (rit = rows.begin(), rowind = 1; rit != rows.end(); rit++, rowind++){//rit = row iterator
singleRow row = *rit;
singleRow::const_iterator nzit;
trowit = tran.begin(); //Start at the beginning of the list of rows
trow = *trowit;
for (nzit = row.begin(); nzit != row.end(); nzit++){//nzit = non zero iterator
int col = nzit->getCol();
double val = nzit->getVal();
trow.push_back(nz(rowind,val)); //How do I attach this to tran so that it goes in the right place?
trowit++;
}
}
Your representation of the matrix is inefficient: it doesn't use the fact that the matrix is sparse. I say so because it includes all the rows of the matrix, even if most of them are zero (empty), like it usually happens with sparse matrices.
Your representation is also hard to work with. So i suggest converting the representation first (to a regular 2-D array), transposing the matrix, and convert back.
(Edited:)
Alternatively, you can change the representation, for example, like this:
Input: rowInd colInd num
1 1 1
1 2 2
1 2 3
2 1 4
2 2 5
2 3 6
3 1 7
3 2 8
3 3 9
Output:
1 1 1
2 1 2
3 1 3
1 2 4
2 2 5
3 2 6
1 3 7
2 3 8
3 3 9
The code would be something like this:
struct singleElement {int row, col; double val;};
list<singleElement> matrix_input, matrix_output;
...
// Read input matrix from file or some such
list<singleElement>::const_iterator i;
for (i = matrix_input.begin(); i != matrix_input.end(); ++i)
{
singleElement e = *i;
std::swap(e.row, e.col);
matrix_output.push_back(e);
}
Your choice of list-of-list representation for a sparse matrix is poor for transposition. Sometimes, when considering algorithms and data structures, the best thing to do is to take the hit for transforming your data structure into one better suited for your algorithm than to mangle your algorithm to work with the wrong data structure.
In this case you could, for example, read your matrix into a coordinate list representation which would be very easy to transpose, then write into whatever representation you like. If space is a challenge, then you might need to do this chunk by chunk, allocating new columns in your target representation 1 by 1 and deallocating columns in your old representation as you go.
Description:
There are m * n (m <= 100, n <=100) coins on the desktop forming a m row n column coin matrix. Every coin either face upward, or face backward, represented by 0 or 1.
Rules of the game are:
(1) every time, you are allowed to inverse one row of coins.
(2) every time, you are allowed to swap two columns.
Object:
from initial matrix -> target matrix
Input:
1. k the count of test caese
2. m n the count of rows and columns
3. the numbers of the inital matrix and the target matrix
Output
the least steps from initial matrix to target matrix, if it is not possible to transfer from initial to target, output -1.
sample intput
2
4 3
1 0 1
0 0 0
1 1 0
1 0 1
1 0 1
1 1 1
0 1 1
1 0 1
4 3
1 0 1
0 0 0
1 0 0
1 1 1
1 1 0
1 1 1
0 1 1
1 0 1
sample output
2
-1
I have coded one solution: mysolution.cc, which enumerate all posibilities and which is correct but it is too slow, could you provide a correct but fast solution.
Thanks.
The rows always stay in the same place, so if row r starts with k ones, it will always have either k ones or columns - k.
for each row, check if count_of_ones(initial,row) == count_of_ones(target,row), if yes, fine, else check if count_of_ones(initial,row) = columns - count_of_ones(target,row), if so, flip row, else output -1. As #maniek pointed out, it's not so easy when exactly half of the columns contain ones. Such rows would have to be treated in step 2 to try and form the required columns.
for each column, count the number of ones int the target and the working matrix (after flipping rows as appropriate). If the sequences of counts are not permutations of each other, output -1, otherwise try to find a permutation of columns that transforms working to target (any columns identical between working and target have to be kept fixed). If not possible, output -1, otherwise find minimum number of swaps necessary to achieve that permutation.
I will give you some thoughts. You compare row by row. If the i-th row of the first matrix has the same number of 1 as in the i-th row of the second matrix - then you don't inverse. If the i-th row of the first matrix has the same number of 1 as the 0 in the i-th row of the second matrix - then you must inverse. If neither of this is true, then there is no solution. This is all about inversing.
Now all columns are equal but in a different order(the second matrix has permuted columns from the first matrix). If the columns are not permutation of each other - return -1. This problem is equal to find the minimum number of swaps to convert a one permutation to other.