Question

In: Computer Science

In Java Describe an algorithm that given a matrix described below determines whether matrix contains a...

In Java Describe an algorithm that given a matrix described below determines whether matrix contains a value k. The input matrix A[1...n, 1...n] has all rows and columns arranged in an non-descending order A[i, j] < A[i, j+1], A[j, i] < A[j + 1, i] for all 1 < i < n and 1 < j < n

Solutions

Expert Solution

One possible solution using divide and conquer approach would be:

Let’s start our search from the top-right corner of the array. There are three possible cases.

  1. The number we are searching for is greater than the current number. This will ensure, that all the elements in the current row is smaller than the number we are searching for as we are already at the right-most element and the row is sorted. Thus, the entire row gets eliminated and we continue our search on the next row. Here elimination means we won’t search on that row again.
  2. The number we are searching for is smaller than the current number. This will ensure, that all the elements in the current column is greater than the number we are searching for. Thus, the entire column gets eliminated and we continue our search on the previous column i.e. the column at the immediate left.
  3. The number we are searching for is equal to the current number. This will end our search.

Since, at each step, we are eliminating an entire row or column, the time complexity of the search becomes O(n).

Algorithm

Let x = element we’re trying to search for in the matrix,
….e = current element we’re processing in the array.

1) Start with top right element.
2) Loop: compare this element e with x
…i) if e = x, then return position of e, since we found x in the given matrix.
…ii) if e > x then move left to check elements smaller than e (if out of bound of matrix, then break and return false)
…iii) if e < x then move below to check elements greater than e (if out of bound of matrix, then break and return false)
3) repeat the i), ii) and iii) until you find the element or return false

The corresponding code


Related Solutions

Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph contains a cycle, then your algorithm should output one. (It should not output all cycles in the graph, just one of them.) The running time of your algorithm should be O(m + n) for a graph with n nodes and m edges.
1. Given the incidence matrix shown below: a. Apply the ranking order clustering algorithm in order...
1. Given the incidence matrix shown below: a. Apply the ranking order clustering algorithm in order to establish the appropriate part families and machine cells (20 points). 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 0 0 0 1 0 1 0 1 0 0 1 1 1 0 b. Based on your clustering outcome above, how many cells will you use and what machines/processes and...
Please write a Java method contains that checks whether the second given character array is contained...
Please write a Java method contains that checks whether the second given character array is contained in the first given character array. We require that both of the arrays are partially filled.
In java run through eclipse /**    *    * Given a square array, determines if...
In java run through eclipse /**    *    * Given a square array, determines if it is diagonal    * That is, returns true if all values in the array are 0    * except the main diagonal. The main diagonal is entries of the form    * data[i][j] where i == j. Elements on the main    * diagonal can be 0 or any other number.    *    * Examples:    * {{1,0,0},    * {0,2,0}   ...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in the "How it works" section), where you take some user-defined number of steps that randomly step in W and I and are scaled by a step_size. This means you don't want to take steps of a discrete size, but instead use a random distribution of step sizes that are scaled by your step_size variable. You'll want to take steps that have an equal chance...
How to write a java application that reads an integer, then determines and display whether it's...
How to write a java application that reads an integer, then determines and display whether it's odd or even. Use the remainder operator.
Given the following two matrices: Matrix A that contains the marks of the college of engineering...
Given the following two matrices: Matrix A that contains the marks of the college of engineering students (250 Student) in 12 courses Matrix B that contains the number of credit hours of each course. Write a Matlab code to Print the letter grades (A to D) of student if you are given his ID and the course number from 1 to 12 Print his GPA in this semester. Gpa=sum(mark in subject i * subiect credit i) /total number of credit,...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should...
IN JAVA: Implement an algorithm to check whether a LinkedList is a palindrome. 2 methods should be implemented: Constant space, i.e. without creating extra copies of the linked list Unrestricted space q1: / For this implementation you should use constant space   // Note: you are free to add any extra methods,   // but this method has to be used   public static boolean isPalindromeRestricted(ListNode head) {     // Your code goes here     return false;   } q2: // For this implementation the space...
Give an O(lg n)-time EREW algorithm that determines for each object in an n-object list whether...
Give an O(lg n)-time EREW algorithm that determines for each object in an n-object list whether it is the middle (n/2 th) object.
Develop a Java application that plays a "guess the number" game as described below. a) The...
Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT