Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code format update For the input below: S1 = ACCGGTCGACTGCGCGGAAGCCGGCCGAA S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA S3 = ATTGCATTGCATGGGCGCGATGCATTTGGTTAATTCCTCG S4 = CTTGCTTAAATGTGCA S5 = AGTTAATAGGACCTGATACG S6

Code format update 

 

For the input below:

 

S1 = ACCGGTCGACTGCGCGGAAGCCGGCCGAA 

S2 = GTCGTTCGGAATGCCGTTGCTCTGTAAA 

S3 = ATTGCATTGCATGGGCGCGATGCATTTGGTTAATTCCTCG 

S4 = CTTGCTTAAATGTGCA 

S5 = AGTTAATAGGACCTGATACG

S6 = TCGTTAATAGGACCTGATACGGTTTACTAG

 

update the code to compare S1, S2, S3, S4, S5, and S6. Also, add execution time (see  highlighted text and output below

 

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class tutorialcup

{
static String lcs(String S1, String S2, int m, int n) {
 int[][] LCS_table = new int[m + 1][n + 1];

 // Building the mtrix in bottom-up way
 for (int i = 0; i <= m; i++) {
  for (int j = 0; j <= n; j++) {
   if (i == 0 || j == 0)
    LCS_table[i][j] = 0;
   else if (S1.charAt(i - 1) == S2.charAt(j - 1))
    LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1;
   else
    LCS_table[i][j] = Math.max(LCS_table[i - 1][j], LCS_table[i][j - 1]);
  }
 }

 int index = LCS_table[m][n];
 int temp = index;

 char[] lcs = new char[index + 1];
 lcs[index] = '\0';

 int i = m, j = n;
 while (i > 0 && j > 0) {
  if (S1.charAt(i - 1) == S2.charAt(j - 1)) {
   lcs[index - 1] = S1.charAt(i - 1);

   i--;
   j--;
   index--;
  }

  else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])
   i--;
  else
   j--;
 }

// change the highlighted below to write to the output ( writer.write)
 System.out.print("\nLCS: ");
 for (int k = 0; k <= temp; k++)    
  System.out.print(lcs[k]);
  System.out.println("");
  System.out.print("String Length :" );
  System.out.println(temp);

 return printSubsequence(S1, S2, LCS_table);

}

public static String printSubsequence(String S1, String S2, int[][] lcs) {

 String output = "";
 output += "S1 : " + S1 + "\nS2 : " + S2 ;
 output += "\n\nPrinting out subseqence matrix...\n\n";


 for(int i=0; i  if(i < 2)
   output += "  ";
  else
   output += " "+S2.charAt(i-2);
 }
 output += "\n";
 
 for(int i=0; i 
  if(i!=0)
   output += S1.charAt(i-1)+" ";
  else
   output += "  ";
 
  for(int j=0; j   output += " "+lcs[i][j];
  }
  output += "\n";
 }
 
 System.out.println(output);
 
 return output;
}

public static void main(String[] args) {
 
 String S1 = null, S2 = null;
 
 Scanner keyboard = new Scanner(System.in);
 
 // Prompt for input file name
 System.out.print("Please enter input filename: ");
 String inputFile = keyboard.nextLine();
 
 // Prompt for output file name
 System.out.print("Please enter input filename: ");
 String outputFile = keyboard.nextLine();
 
 System.out.println();
 
 
 // Read S1 and S2 from input file
 try {
 
  Scanner reader = new Scanner(new File(inputFile));
 
  S1 = reader.nextLine();
  S2 = reader.nextLine();
 
 } catch (FileNotFoundException e) {
  System.out.println("Error! "+inputFile+" not found!");
 }
 
 
 
 int m = S1.length();
 int n = S2.length();
 
 String lcsMatrix = lcs(S1, S2, m, n);
 
 // Write into output file
 try {
 
  PrintWriter writer = new PrintWriter(new File(outputFile));
  writer.write(lcsMatrix);
  writer.close();  
 } catch (FileNotFoundException e) {
  System.out.println("Error! "+outputFile+" not found!");
 }
 
}
}

Output(part of the output):


image
CoursHeroTranscribedText

LCS: TAGCCACGGG COMPARISON: 5 [1] S1 ACCGGTCGACTGCGCGGAAGCCGGCCGAA [2] S6= TCGTTAATAGGACCTGATACGGTTTACTAG Execution time: 229 milliseconds Length of LCS: 16 LCS: CGTATGCCGAACGGCG COMPARISON: 6 [1] S2 GTCGTTCGGAATGCCGTTGCTCTGTAAA [2] S3 = ATTGCATTGCATGGGCGCGATGCATTTGGTTAATTCCTCG Execution time: 248 milliseconds Length of LCS: 20 LCS: GCTTCGGGCCGTGCTTGTAA COMPARISON: 7 [1] S2 GTCGTTCGGAATGCCGTTGCTCTGTAAA [2] S4= CTTGCTTAAATGTGCA Execution time: 110 milliseconds Length of LCS: 12 Ln 133, Col 32 100% Unix (LF)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres the updated code with the added comparisons fo... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Digital Logic With Verilog Design

Authors: Stephen Brown, Zvonko Vranesic

3rd Edition

978-0073380544, 0073380547

More Books

Students also viewed these Programming questions

Question

Evaluate each logarithm to four decimal places. log 0.257

Answered: 1 week ago