Question

In: Computer Science

Given a matrix ? (i.e., a 2D table) of positive integers of size ? × ?....

  1. Given a matrix ? (i.e., a 2D table) of positive integers of size ? × ?. A robot starts at the top- left position ?[?, ?] and must reach the bottom-right position ?[?, ?]. From a position ?[?, ?], the robot can only go either right to ?[?, ? + ?] or down to ?[? + ?, ?]. Let us define the cost of a path the total values of integers in it. Find the least-cost path for the robot to reach to bottom- right. How about the path with the maximum cost? Prove python source code, time complexity, and pseudocode.

    Input: Matrix ? and its sizes ? and ?
    Output: A sequence of pairs of index (?, ?)’s for the least-cost path from ?[1,1] to ?[?, ?]

Solutions

Expert Solution

//psuedo code
minPath(matrix,n,m):
    if(n<0 or m<0):
        return maxvalue
    else if(n==0 or m==0):
        return matrix[n][m]
    else
       return matrix[n][m]+min(minCost(minCost(cost, m-1, n),minCost(cost, m, n-1))
//Time Complexity will be
O(n+m)
as we just doing one pass here 

//max path
if we want to find max cost path here instead of taking min of bottom and right we shall take max of bottom and right





//python code
import sys



def minCost(cost, m, n):
   if (n < 0 or m < 0):
      return sys.maxsize
   elif (m == 0 and n == 0):
      return cost[m][n]
   else:
      return cost[m][n] + min(minCost(cost, m-1, n),
                        minCost(cost, m, n-1))

 


R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
cost = []
print("Enter the entries rowwise:")


for i in range(R):
    a = []
    for j in range(C):
        a.append(int(input()))
    cost.append(a)

for i in range(R):
    for j in range(C):
        print(cost[i][j], end=" ")
    print()

print(minCost(cost, R-1 ,C-1))

Related Solutions

Show that in 2D, the general orthogonal transformation as matrix A given by {{cos, sin}, {-sin,...
Show that in 2D, the general orthogonal transformation as matrix A given by {{cos, sin}, {-sin, cos}}. Verify that det[A] = 1 and that the transpose of A equals its inverse. Let Tij be a tensor in this space. Write down in full the transformation equations for all its components and deduce that Tii is an invariant.
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
Create a random matrix of size 10x10, of integers between 1 and 1000, and use nested...
Create a random matrix of size 10x10, of integers between 1 and 1000, and use nested for loops over the elements of the matrix to find the number of prime elements , as well as the sum of the prime elements in the matrix. You can use MATLAB's built-in function isprime to check if an element is a prime number. You should have these variable: cnt (counts the number of prime elements) sm (sum of prime elements) In the command...
Given a square matrix of integers m, your task is to rearrange its numbers in the...
Given a square matrix of integers m, your task is to rearrange its numbers in the following way: First, sort its values in ascending order of how frequently the number occurs in m. In the case of a tie, sort the equally frequent numbers by their values, in ascending order. Second, place the sorted numbers diagonally, starting from the bottom right corner, like this: Example For m = [[ 1, 4, -2], [-2, 3, 4], [ 3, 1, 3]] the...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Given that the square matrix, A is nilpotent (Ak = 0 for some positive integer k)....
Given that the square matrix, A is nilpotent (Ak = 0 for some positive integer k). If A is n by n, show that An = 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT