f8 is a 4x4 matrix stored as a 2D array as follows: 0 1 1 0 1 0 0 1 1 0 0 1 0 1
ID: 3693007 • Letter: F
Question
f8 is a 4x4 matrix stored as a 2D array as follows:
0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0
Write a method numPaths which accepts a 2-dimensional array of integers called adjMatrix (representing the adjacency matrix of a graph) as its first parameter. The method should accept parameters of type int called initialVertex and finalVertex. The method should also accept a parameter of type int called pathLength. The method should return an integer indicating the number of paths from initialVertex to finalVertex of length pathLength in the graph represented by adjMatrix.
A. (Your output should indicate “Test Case 6A.”) Test the method by calling it on f8 from part 4 above and specifying values corresponding to vertices a and d and a length of 4. The method should return a value of 8.
B. (Your output should indicate “Test Case 6B.”) Test the method by calling it on f8 from part 4 above and specifying values corresponding to vertices a and d and a length of 1. The method should return a value of 0
C. (Your output should indicate “Test Case 6C.”) Test the method by calling it on f8 from part 4 above and specifying values corresponding to vertices a and d and a length of 2. The method should return a value of 2
Explanation / Answer
public int TotalNumberOfPaths(char [][] mat) { TotalNumberOfPathsHelper(mat, 0,0); } // total number of Paths from i,j private int TotalNumberOfPathsHelper(char [][] mat, int i, int j) { if (mat[i][j] == 'X') //no Path from here { return 0; } else if ((i >= M) || (j >= N)) { return 0; } else if ((i == M-1) && (j == N-1)) { return 1; } else { return (TotalNumberOfPathsHelper(mat, i+1, j) + TotalNumberOfPathsHelper, i, j+1)); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.