Question

The following program needs to be answered in Java and needs to read the matrix from...

The following program needs to be answered in Java and needs to read the matrix from a txt file. Design a recursive algorithm to find the longest increasing sequence of numbers in a rectangular grid. For example, if the grid contains 97 47 56 36 35 57 41 13 89 36 98 75 25 45 26 17 then the longest increasing sequence of numbers is the sequence of length eight consisting of 17, 26, 36, 41, 47, 56, 57, 97. Note that there are no duplicates in the increasing sequence. b. Design an algorithm that solves the same problem but allows for nondecreasing sequences; thus there may be duplicates in the increasing sequence.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have pasted the code and screenshot of successful run below.
  • Input filename is assumed to be input.txt
  • In case of any doubts just comment down.

---------------------------Screenshot---------------------------

---------------------------input.txt---------------------------

4 4
97 47 56 36
35 57 41 13
89 36 98 75
25 45 26 17

---------------------------LIS.java---------------------------

import java.util.*;
import java.io.*;

public class LIS{

   public static int A[][];
   public static int m, n;

   public static void readFile(){
       try{
           FileInputStream inDataStream = new FileInputStream("input.txt");
           Scanner fileIn = new Scanner(inDataStream);
           m = fileIn.nextInt();
           n = fileIn.nextInt();
           A = new int[m][n];
           for(int i=0;i<m;i++){
               for(int j=0;j<n;j++){
                   A[i][j] = fileIn.nextInt();
               }
           }
       }catch(Exception e){}
   }

   public static Boolean[][] copy(Boolean A[][]){
       Boolean B[][] = new Boolean[m][n];
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
               B[i][j] = A[i][j];
           }
       }
       return B;
   }

   public static int search(int i, int j, int prev, Boolean[][] visited){
       if(i<0 || i>=m || j<0 || j>=n){return 0;}
       if(A[i][j]<=prev){return 0;}
       else{
           visited[i][j] = true;
           int X[] = {-1,0,1,-1,1,-1,0,1};
           int Y[] = {-1,-1,-1,0,0,1,1,1};
           int currmax = 0;
           int bi=-1, bj=-1;
           for(int k=0;k<X.length;k++){
               try{
                   if(!visited[i+X[k]][j+Y[k]]){
                       int temp = search(i+X[k],j+Y[k],A[i][j],copy(visited));
                       if(temp>currmax){
                           currmax = temp;
                           bi = i+X[k];
                           bj = j+Y[k];
                       }
                   }
               }catch(Exception e){}
           }
           // if(bi!=-1){
           //    System.out.print(i + "," + j + "-----");
           //    System.out.println(bi + "," + bj);
           // }
           return 1+currmax;
       }
   }

   public static int[] search2(int i, int j, int prev, Boolean[][] visited){
       int emptyarr[] = {};
       if(i<0 || i>=m || j<0 || j>=n){return emptyarr;}
       if(A[i][j]<=prev){return emptyarr;}
       else{
           visited[i][j] = true;
           int X[] = {-1,0,1,-1,1,-1,0,1};
           int Y[] = {-1,-1,-1,0,0,1,1,1};
           int[] currmax = {};
           int bi=-1, bj=-1;
           for(int k=0;k<X.length;k++){
               try{
                   if(!visited[i+X[k]][j+Y[k]]){
                       int temp[] = search2(i+X[k],j+Y[k],A[i][j],copy(visited));
                       if(temp.length>currmax.length){
                           currmax = temp;
                           bi = i+X[k];
                           bj = j+Y[k];
                       }
                   }
               }catch(Exception e){}
           }
           int ret[] = new int[1+currmax.length];
           ret[0] = A[i][j];
           for(int k=0;k<currmax.length;k++){
               ret[1+k] = currmax[k];
           }
           return ret;
       }
   }

   public static int[] getSequence(int x, int y){
       Boolean visited[][] = new Boolean[m][n];
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
               visited[i][j] = false;
           }
       }
       return search2(x,y,0,visited);
   }

   public static int startSearch(int x, int y){
       Boolean visited[][] = new Boolean[m][n];
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
               visited[i][j] = false;
           }
       }
       return search(x,y,0,visited);
   }

   public static void main(String[] args) {
       readFile();
       int longestLength = 0;
       int bi=-1, bj=-1;
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
               if(startSearch(i,j)>longestLength){
                   longestLength = startSearch(i,j);
                   bi = i;bj = j;
               }
           }
       }
       System.out.println(longestLength);
       System.out.println(bi + ", " + bj);

       int longestSequence[] = getSequence(bi,bj);
       for(int i=0;i<longestSequence.length-1;i++){
           System.out.print(longestSequence[i] + " -> ");
       }
       System.out.println(longestSequence[longestSequence.length-1]);
   }

}

ksmukta:7.java-fileread-recursive -> javac LIS.java && java LIS 8 3, 3 17 - 26 - 36-41- 47-> 56-57-97

Add a comment
Know the answer?
Add Answer to:
The following program needs to be answered in Java and needs to read the matrix from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT