Find maximum distance given a grid of nodes and a set of source nodes - graph-algorithm

Given a set of nodes arranged as an m by n grid (note: diagonal nodes are not connected), and a set of nodes marked as source nodes, find the maximum distance between nodes and the source nodes.
For example, for a 4 by 4 grid and a source node at (1, 0):
0 0 0 0
1 0 0 0
0 0 0 0
0 0 0 0
Computing the distance from each node to its closest source would produce:
1 2 3 4
0 1 2 3
1 2 3 4
2 3 4 5
And the maximum distance is therefore 5.
For a grid with more than 1 source, for example 3 source nodes:
0 0 1 0
1 0 0 0
0 0 0 0
0 0 0 1
Computing the distance from each node to its closest source would produce:
1 1 0 1
0 1 1 2
1 2 2 1
2 2 1 0
And the maximum distance is therefore 2.
I wrote up an algorithm that solves this, but it looks like the worst case scenario makes it run in O(n^4) (assume m == n):
// MaximumDistances.java
public class MaximumDistances {
public static void main(String[] args) {
int[][] sourceNodes = new int[][] {
{0, 0, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 1}
};
int maximumDistance = computeMaximumDistance(sourceNodes);
System.out.println(String.format(
"The maximum distance in this grid is %d.",
maximumDistance));
}
private static int computeMaximumDistance(int[][] sourceNodes) {
int m = sourceNodes.length;
int n = sourceNodes[0].length;
// Initializes the distance grid. Since none of the sites have been
// visited yet, the distance to any grid cell with be
// `Integer.MAX_VALUE`.
int[][] distanceGrid = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
distanceGrid[i][j] = Integer.MAX_VALUE;
}
}
// If we're at a source site, we mark its distance to each grid cell.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (sourceNodes[i][j] == 1) {
markDistancesFromSourceSite(i, j, distanceGrid);
}
}
}
// The maximum value in the distance grid will be the maximum distance.
int maximumDistance = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (distanceGrid[i][j] > maximumDistance) {
maximumDistance = distanceGrid[i][j];
}
}
}
return maximumDistance;
}
private static void markDistancesFromSourceSite(int x, int y, int[][] distanceGrid) {
int m = distanceGrid.length;
int n = distanceGrid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int distanceToSource = Math.abs(x - i) + Math.abs(y - j);
if (distanceGrid[i][j] > distanceToSource) {
distanceGrid[i][j] = distanceToSource;
}
}
}
}
}
Is there a faster algorithm that solves this?

Yes You can solve this problem using a concept called multisource BFS . The concept of multisource BFS is just like BFS but here is the minor difference .
In normal BFS we just push 1 node in the queue initially while in multisource BFS we push all the source nodes .
So after pushing all the source nodes we can just do the normal BFS operation to solve the problem .
The time complexity will be O(n*m) .
import java.util.*;
class Pair{
int x , y , dist;
Pair(int first , int second){
this.x = first;
this.y = second;
this.dist = 0;
}
Pair(int first , int second , int dist){
this.x = first;
this.y = second;
this.dist = dist;
}
}
public class MultisourceBFS {
public static void main(String[] args) {
int[][] sourceNodes = new int[][] {
{0, 0, 1, 0},
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 1}
};
int maximumDistance = computeMaximumDistance(sourceNodes);
System.out.println(String.format(
"The maximum distance in this grid is %d.",
maximumDistance));
}
private static int computeMaximumDistance(int[][] sourceNodes) {
int m = sourceNodes.length;
int n = sourceNodes[0].length;
int maximumDistance = 0;
int xx[] = {0 , 0 , 1 , -1}; // Normal array to go to up , down , left , right;
int yy[] = {1 , -1 , 0 , 0}; // Normal array to go to up ,down , left , right
Queue<Pair> q = new LinkedList<Pair>();
boolean isVisited[][] = new boolean[m][n]; // An array to check if a cell is visited or not .
// I am adding all the source nodes to the queue
for(int i = 0 ; i < m ; ++i)
for(int j = 0 ; j < n ; ++j)
if(sourceNodes[i][j]==1){
q.add(new Pair(i , j));
isVisited[i][j] = true;
}
// Now it is going to be normal BFS
while(!q.isEmpty()){
Pair node = q.remove();
for(int k = 0 ; k < 4 ; ++k){
int new_i = node.x + xx[k];
int new_j = node.y + yy[k];
if(new_i >= 0 && new_i < m && new_j >= 0 && new_j < n && isVisited[new_i][new_j]==false){
maximumDistance = Math.max(node.dist + 1 , maximumDistance);
isVisited[new_i][new_j] = true;
q.add(new Pair(new_i , new_j , node.dist + 1));
}
}
}
return maximumDistance;
}
}

Related

Looking for an more efficient way to create a 4 directional linked-list

I am trying to create a method that will link all Cell objects set up in a 2D array named CellGrid[,]
My question is: Since most of the code in SetDirection() is so similar, it seem there is a better way to achieve my goal.
(Side note: This is functional but the execution feels "off" )
private void SetDirection()
{
int x = 0, y = 0;
for ( int i = 0 ; i < Size * (Size - 1);)//setting all UP pointers
{
if ( i == 0 ) { x = 0; y = 1;}//initial setup
for ( x = 0 ; x < Size ; x++ )
{
CellGrid[x,y].SetPointer(CellGrid[x,y-1] , Direction.Up );
i++;
}
y++;
}
for ( int i = 0 ; i < Size * (Size - 1);) //setting all DOWN pointers
{
if ( i == 0 ) { x = 0; y = 0;}//initial setup
for ( x = 0 ; x < Size ; x++ )
{
CellGrid[x,y].SetPointer(CellGrid[x,y+1], Direction.Down);
i++;
}
y++;
}
for ( int i = 0 ; i < Size * (Size - 1);)//setting all LEFT pointers
{
if ( i == 0 ) { x = 1; y = 0;}//initial setup
for ( y = 0 ; y < Size ; y++ )
{
CellGrid[x, y].SetPointer( CellGrid[x-1,y], Direction.Left);
i++;
}
x++;
}
for ( int i = 0 ; i < Size * (Size - 1);) //setting all RIGHT pointers
{
if ( i == 0 ) { x = 0; y = 0;}//initial setup
for ( y = 0 ; y < Size ; y++ )
{
CellGrid[x, y].SetPointer( CellGrid[x+1,y], Direction.Right);
i++;
}
x++;
}
}
public void SetPointer( Cell cellRef ,GridBuilder.Direction dir)
{
switch ( dir )
{
case GridBuilder.Direction.Up:
this.Up = cellRef;
break;
case GridBuilder.Direction.Down:
this.Down = cellRef;
break;
case GridBuilder.Direction.Left:
this.Left = cellRef;
break;
case GridBuilder.Direction.Right:
this.Right = cellRef;
break;
}
}
You can indeed use one set of loops to make the links in all four directions. This is based on two ideas:
When setting a link, immediately set the link in the opposite direction between the same two cells.
When setting a link, immediately set the link between the two cells that are located in the mirrored positions -- mirrored by the main diagonal (x <--> y).
private void SetDirection() {
for (int i = 1; i < Size; i++) {
for (int j = 0; j < Size; j++) {
CellGrid[i, j].SetPointer(CellGrid[i-1, j], Direction.Left);
CellGrid[i-1, j].SetPointer(CellGrid[i, j], Direction.Right);
CellGrid[j, i].SetPointer(CellGrid[j, i-1], Direction.Up);
CellGrid[j, i-1].SetPointer(CellGrid[j, i], Direction.Down);
}
}
}

Network Delay Problem - Complexity Analysis

Below is a solution Network delay problem of leetcode. I have written a all test case success solution. But not able to analyse the time complexity. I believe its O(V^2 + E) where V is the number of nodes and E edges.
In this solution though I am adding all adjacents of each node every time, but not processing them further if there exists a min distance for that node already.
Leetcode question link https://leetcode.com/problems/network-delay-time
public int networkDelayTime(int[][] times, int n, int k) {
int[] distances = new int[n+1];
Arrays.fill(distances , -1);
if(n > 0){
List<List<int[]>> edges = new ArrayList<List<int[]>>();
for(int i = 0 ; i <= n ; i++){
edges.add(new ArrayList<int[]>());
}
for(int[] time : times){
edges.get(time[0]).add(new int[]{time[1] , time[2]});
}
Queue<Vertex> queue = new LinkedList<>();
queue.add(new Vertex(k , 0));
while(!queue.isEmpty()){
Vertex cx = queue.poll();
int index = cx.index;
int distance = cx.distance;
//process adjacents only if distance is updated
if(distances[index] == -1 || distances[index] > distance){
distances[index] = distance;
List<int[]> adjacents = edges.get(index);
for(int[] adjacent : adjacents){
queue.add(new Vertex(adjacent[0] , adjacent[1]+distance));
}
}
}
}
int sum = 0;
for(int i = 1 ; i <= n; i++){
int distance = distances[i];
if(distance == -1){
return -1;
}
sum = Math.max(sum , distance);
}
return sum;
}
public static class Vertex{
int index;
int distance;
public Vertex(int i , int d){
index = i;
distance = d;
}
}
You should use PriorityQueue instead of LinkedList

decomposeProjectionMatrix gives unexpected result

I have the following projection matrix P:
-375 0 2000 262500
-375 2000 0 262500
-1 0 0 700
This projection matrix projects 3D points in mm on a detector in px (with 1px equals to 0.5mm) and is built from the intrinsic matrix K and the extrinsic matrix [R|t] (where R is a rotation matrix and t a translation vector) according the relation P = K [R|t].
2000 0 375 0 0 1 0
K = 0 2000 375 R = 0 1 0 t = 0
0 0 1 -1 0 0 700
For some reasons I need to decompose P back into these matrices. When I use decomposeProjectionMatrix I get this as a rotation matrix:
0 0 0
0 0 0
-1 0 0
Which doesn't look like a rotation matrix to me.
Moreover when I build back the projection matrix from the Open CV decomposition I get this matrix:
-375 0 0 262500
-375 0 0 262500
-1 0 0 700
Looks similar but it is not the same.
I'm wondering if I'm doing something wrong or if I'm unlucky and that was one of the rare cases where this function fails.
Note that I did the decomposition by myself and I get coherent results but I would rather use Open CV functions as much as possible.
The problem seems to be in the RQ decomposition used by decomposeProjectionMatrix.
Even though the first square of the matrix P is non singular, the RQDecomp3x3 function gives incorrect results:
0 0 375 0 0 0
R = 0 0 375 Q = 0 0 0
0 0 1 -1 0 0
So a work around is to use a homemade function (here written in Python) based on the section 2.2 of Peter Sturm's lectures:
def decomposeP(P):
import numpy as np
M = P[0:3,0:3]
Q = np.eye(3)[::-1]
P_b = Q # M # M.T # Q
K_h = Q # np.linalg.cholesky(P_b) # Q
K = K_h / K_h[2,2]
A = np.linalg.inv(K) # M
l = (1/np.linalg.det(A)) ** (1/3)
R = l * A
t = l * np.linalg.inv(K) # P[0:3,3]
return K, R, t
I use the anti-identity matrix Q to build the non conventional Cholesky decomposition U U* where U is upper triangular.
This method differs slightly from the Peter Sturm's one as we use the relation P = K[R|t] while in Peter Sturm's lectures the relation used is P = K[R|-Rt].
A C++ implementation using only Open CV is trickier as they don't really expose a function for Cholesky decompostion:
void chol(cv::Mat const& S, cv::Mat& L)
{
L = cv::Mat::zeros(S.rows, S.rows, cv::DataType<double>::type);
for (int i = 0; i < S.rows; ++i) {
for (int j = 0; j <= i ; ++j) {
double sum = 0;
for (int k = 0; k < j; ++k)
sum += L.at<double>(i,k) * L.at<double>(j,k);
L.at<double>(i,j) = (i == j) ? sqrt(S.at<double>(i,i) - sum) : (S.at<double>(i,j) - sum) / L.at<double>(j,j);
}
}
}
void decomposeP(cv::Mat const& P, cv::Mat& K, cv::Mat& R, cv::Mat& t)
{
cv::Mat M(3, 3, cv::DataType<double>::type);
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
M.at<double>(i, j) = P.at<double>(i ,j);
cv::Mat Q = cv::Mat::zeros(3, 3, cv::DataType<double>::type);
Q.at<double>(0, 2) = 1.0;
Q.at<double>(1, 1) = 1.0;
Q.at<double>(2, 0) = 1.0;
cv::Mat O = Q * M * M.t() * Q;
cv::Mat C;
chol(O, C);
cv::Mat B = Q * C * Q;
K = B / B.at<double>(2,2);
cv::Mat A = K.inv() * M;
double l = std::pow((1 / cv::determinant(A)), 1/3);
R = l * A;
cv::Mat p(3, 1, cv::DataType<double>::type);
for (int i = 0; i < 3; ++i)
p.at<double>(i, 0) = P.at<double>(i ,3);
t = l * K.inv() * p;
}

stripes while calculating image gradient with CUDA

I'm writing a code for the image denoising and came across a strange problem with stripes in the processed images. Basically when I'm calculating X-gradient of image the horizontal stripes appear (or vertical for Y direction) Lena X gradient.
The whole algorithm works OK and it looks like I'm getting the correct answer (I'm comparing with program in C) except those annoying stripes Lena result.
The distance between stripes is changing with different block sizes. I'm also having different stripes positions each time I run the program! Here is the part of the program related to the gradient calculation. I have a feeling that I'm doing something very stupid :) Thank you!
#define BLKXSIZE 16
#define BLKYSIZE 16
#define idivup(a, b) ( ((a)%(b) != 0) ? (a)/(b)+1 : (a)/(b) )
void Diff4th_GPU(float* A, float* B, int N, int M, int Z, float sigma, int iter, float tau, int type)
{
float *Ad;
dim3 dimBlock(BLKXSIZE,BLKYSIZE);
dim3 dimGrid(idivup(N,BLKXSIZE), idivup(M,BLKYSIZE));
cudaMalloc((void**)&Ad,N*M*sizeof(float));
cudaMemcpy(Ad,A,N*M*sizeof(float),cudaMemcpyHostToDevice);
cudaCheckErrors("cc1");
int n = 1;
while (n <= iter) {
Diff4th2D<<<dimGrid,dimBlock>>>(Ad, N, M, sigma, iter, tau, type);
n++;
cudaDeviceSynchronize();
cudaCheckErrors("kernel");}
cudaMemcpy(B,Ad,N*M*sizeof(float),cudaMemcpyDeviceToHost);
cudaCheckErrors("cc2");
cudaFree(Ad);
}
__global__ void Diff4th2D(float* A, int N, int M, float sigma, int iter, float tau, int type)
{
float gradX, gradX_sq, gradY, gradY_sq, gradXX, gradYY, gradXY, sq_sum, xy_2, Lam, V_norm, V_orth, c, c_sq, lam_t;
int i = blockIdx.x*blockDim.x + threadIdx.x;
int j = blockIdx.y*blockDim.y + threadIdx.y;
int index = j + i*N;
if ((i < N) && (j < M))
{
float gradX = 0, gradY = 0, gradXX = 0, gradYY = 0, gradXY = 0;
if ((i>1) && (i<N)) {
if ((j>1) && (j<M)){
int indexN = (j)+(i-1)*(N);
if (indexN > ((N*M)-1)) indexN = (N*M)-1;
if (indexN < 0) indexN = 0;
int indexS = (j)+(i+1)*(N);
if (indexS > ((N*M)-1)) indexS = (N*M)-1;
if (indexS < 0) indexS = 0;
int indexW = (j-1)+(i)*(N);
if (indexW > ((N*M)-1)) indexW = (N*M)-1;
if (indexW < 0) indexW = 0;
int indexE = (j+1)+(i)*(N);
if (indexE > ((N*M)-1)) indexE = (N*M)-1;
if (indexE < 0) indexE = 0;
gradX = 0.5*(A[indexN]-A[indexS]);
A[index] = gradX;
}
}
}
}
You have a race condition inside your kernel, as elements of A may or may not be overwritten before they are used.
Use different arrays for input and output.

Shift (like Matlab function) rows or columns of a matrix in OpenCV

In Matlab there is a shift function in order to perform a circular shift of the columns or rows of a matrix. There is a similar function in OpenCV?
I was searching for same question but since there is none, I wrote by myself. Here is another option. In my code you can shift right or left n times: for left numRight is -n, right +n.
void shiftCol(Mat& out, Mat in, int numRight){
if(numRight == 0){
in.copyTo(out);
return;
}
int ncols = in.cols;
int nrows = in.rows;
out = Mat::zeros(in.size(), in.type());
numRight = numRight%ncols;
if(numRight < 0)
numRight = ncols+numRight;
in(cv::Rect(ncols-numRight,0, numRight,nrows)).copyTo(out(cv::Rect(0,0,numRight,nrows)));
in(cv::Rect(0,0, ncols-numRight,nrows)).copyTo(out(cv::Rect(numRight,0,ncols-numRight,nrows)));
}
Hope this will help to some people. Similarly, shiftRows can be written
Here is my implementation of the circular matrix shift. Any suggestion is welcome.
//circular shift one row from up to down
void shiftRows(Mat& mat) {
Mat temp;
Mat m;
int k = (mat.rows-1);
mat.row(k).copyTo(temp);
for(; k > 0 ; k-- ) {
m = mat.row(k);
mat.row(k-1).copyTo(m);
}
m = mat.row(0);
temp.copyTo(m);
}
//circular shift n rows from up to down if n > 0, -n rows from down to up if n < 0
void shiftRows(Mat& mat,int n) {
if( n < 0 ) {
n = -n;
flip(mat,mat,0);
for(int k=0; k < n;k++) {
shiftRows(mat);
}
flip(mat,mat,0);
} else {
for(int k=0; k < n;k++) {
shiftRows(mat);
}
}
}
//circular shift n columns from left to right if n > 0, -n columns from right to left if n < 0
void shiftCols(Mat& mat, int n) {
if(n < 0){
n = -n;
flip(mat,mat,1);
transpose(mat,mat);
shiftRows(mat,n);
transpose(mat,mat);
flip(mat,mat,1);
} else {
transpose(mat,mat);
shiftRows(mat,n);
transpose(mat,mat);
}
}
Short answer, no.
Long answer, you can implement it easily if you really need it, for example using temporary objects using cv::Mat::row(i), cv::Mat::(cv::Range(rowRange), cv::Range(cv::colRange)).
Or if you're using Python, just the roll() method.

Resources