# Thread: All Paths Between Two Nodes in Matrix

1. ## All Paths Between Two Nodes in Matrix

hi,I have a adjacency matrix.(router adjacency matrix in network).The result should be the router ID.I mean, print all the possible paths from source router to target router(ROUTER id>> 1 - 2 - 3 - 4 so there are four one router).I get the error in the output.)
Code:
```#include<stdio.h>
#include<conio.h>
/* graph:  Pointer to the starting of mXn matrix
i, j: Current position of the robot (For the first call use 0,0)
m, n: Dimentions of given the matrix
pi:   Next index to be filed in path array
*route[0..pi-1]: The path traversed by robot till now (Array to hold the
path need to have space for at least m+n elements) */

void displayAllroutesUtil(int *graph, int i, int j, int m, int n, int *route, int pi)
{
int k,l;

// Reached the bottom of the matrix so we are left with
// only option to move right
if (i == m - 1)
{
for (k = j; k < n; k++)
route[pi + k - j] = *((graph + i*n) + k);
for (l = 0; l < pi + n - j; l++)
printf("%d ",route[l]);
printf("\n ");
return;
}
// Reached the right corner of the matrix we are left with
// only the downward movement.

if (j == n - 1)
{
for (k = i; k < m; k++)
route[pi + k - i] = *((graph + k*n) + j);
for (l = 0; l < pi + m - i; l++)
printf("%d ",route[l]);
printf("\n ");
return;
}

// Add the current cell to the path being generated
route[pi] = *((graph + i*n) + j);

// Print all the paths that are possible after moving down
displayAllroutesUtil(graph, i+1, j, m, n, route, pi + 1);

displayAllroutesUtil(graph, i, j+1, m, n, route, pi + 1);
// Print all the paths that are possible after moving diagonal
// printAllPathsUtil(mat, i+1, j+1, m, n, path, pi + 1);

}

// The main function that prints all paths from top left to bottom right
void displayAllroutes(int *graph, int m, int n)
{
int route[m+n];
displayAllroutesUtil(graph, 0, 0, m, n, route, 0);
}

int main()
{
int graph = { {0, 1, 1,0}, {1, 0, 0,1},{1, 0, 0,1},{1, 1, 1,0} };

displayAllroutes(*graph, 2, 4);
return 0;
}``` 2. 1. I suggest using meaningful variable names.
2. Post the real output and what you think the output should be.
Edit: 3. Use at least meaningful parameter names

Tim S. 3. code update 4. I second stahta, post your relevant output.

Also, describe what your algorithm is. If I had to take a stab at navigating an adjacency matrix, my naive approach would be to check the first row for the target value and follow the set values from there. So if the first row is ( 1, 1, 0 0 ) and we want the node denoted by index 3, I would then check rows 0 and 1 because the 0 and 1 elements of the array are set. But because we just checked row 0, we know it's a dud so recursively checking it infinite times is pointless so I'd also include a history as well of which rows have been traversed. But that's just me. What's your approach? Popular pages Recent additions data structure, graph, program c 