Advertisement
Guest User

Code

a guest
Mar 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 KB | None | 0 0
  1. /* ***************************************************
  2.  * Devon Knudsen
  3.  * Last Updated - 3/16/19
  4.  *
  5.  *
  6.  *************************************************** */
  7.  
  8. import java.io.*;
  9. import java.time.*;
  10. import java.util.*;
  11.  
  12. public class Main {
  13.     private static boolean DEBUG = true;
  14.  
  15.     public static void main(String args[]) throws java.io.IOException {
  16.         DoublyLinkedList<String> text1 = new DoublyLinkedList<>();
  17.         DoublyLinkedList<String> text2 = new DoublyLinkedList<>();
  18.         DoublyLinkedList<String> pattern = new DoublyLinkedList<>();
  19.  
  20.         Scanner inFile1 = new Scanner(new File("input1.txt"));
  21.         charsToList(fileScan(inFile1), text1);
  22.  
  23.         Scanner inFile2 = new Scanner(new File("input2.txt"));
  24.         charsToList(fileScan(inFile2), text2);
  25.  
  26.         if (DEBUG) {
  27.             System.out.println("list 1 = " + text1);
  28.             System.out.println("list 2 = " + text2);
  29.         }
  30.  
  31.         //Ask the user for the pattern they are looking for and convert it into a linked tex
  32.         Scanner input = new Scanner(System.in);
  33.         System.out.print("Enter the pattern you are searching for: ");
  34.         String[] searchFor = input.nextLine().split("");
  35.         charsToList(searchFor, pattern);
  36.  
  37.         if (DEBUG) {
  38.             System.out.println("pattern = " + pattern);
  39.         }
  40.  
  41.         System.out.println(kmp(pattern, text1));
  42.         System.out.println(bruteForce(pattern, text1));
  43.     }
  44.  
  45.     // function to convert the chars from a read file into a string array
  46.     private static String[] fileScan(Scanner inFile) {
  47.         String fileString = "";
  48.         while (inFile.hasNext())
  49.             fileString += inFile.nextLine();
  50.  
  51.         String[] chars = fileString.split("");
  52.  
  53.         inFile.close();
  54.  
  55.         return chars;
  56.     }
  57.  
  58.     // function to convert a string array into a linked tex with nodes holding only one char
  59.     private static void charsToList(String[] chars, DoublyLinkedList<String> tex) {
  60.         for (String c : chars)
  61.             tex.InsertAfter(c.toLowerCase());
  62.     }
  63.  
  64.     /*
  65.     Brute Force algorithm for pattern matching
  66.     (I got some help from the book for this portion of the code)
  67.      */
  68.     private static DoublyLinkedList<Integer> bruteForce(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex) {
  69.  
  70.         int numMatches = 0;
  71.  
  72.         // stopwatch starts
  73.         Instant start = Instant.now();
  74.  
  75.         DoublyLinkedList<Integer> matchAt = new DoublyLinkedList<>();
  76.  
  77.         // t goes up to the last index that is able to start the pattern which is being searched for
  78.         for(int t = 0; t  < tex.GetSize() - pat.GetSize(); t++) {
  79.  
  80.             // j = index in pattern
  81.             int p = 0;
  82.  
  83.             tex.SetPos(t);
  84.             pat.First();
  85.  
  86.             // if characters matching pattern are found
  87.             while (p < pat.GetSize() && tex.GetValue().equals(pat.GetValue())) {
  88.                 p++;
  89.                 pat.Next();
  90.                 tex.Next();
  91.             }
  92.  
  93.             // if the end of the pattern is reached
  94.             if (p == pat.GetSize()) {
  95.                 matchAt.InsertAfter(t);
  96.  
  97.                 if (DEBUG)
  98.                     numMatches++;
  99.             }
  100.         }
  101.  
  102.         // stopwatch stops
  103.         Instant finish = Instant.now();
  104.  
  105.         long timeElapsed = Duration.between(start, finish).toMillis();
  106.         System.out.println(tellTime(timeElapsed, "Brute Force"));
  107.  
  108.         if(DEBUG)
  109.             System.out.println("Num of matches: " + numMatches);
  110.  
  111.         if (!matchAt.IsEmpty())
  112.             return matchAt;
  113.         else
  114.             return null;
  115.     }
  116.  
  117.     /*
  118.     Boyer-Moore algorithm for pattern matching
  119.     (I got some help from the book for this portion of the code)
  120.      */
  121.     //private static DoublyLinkedList<Integer> bm(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex) {
  122.     //}
  123.  
  124.     /*
  125.     KMP algorithm for pattern matching
  126.     (I got some help from the book for this portion of the code)
  127.      */
  128.     private static DoublyLinkedList<Integer> kmp(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex) {
  129.  
  130.         int numMatches = 0;
  131.  
  132.  
  133.         DoublyLinkedList<Integer> matchAt = new DoublyLinkedList<>();
  134.         if (pat.GetSize() == 0) {
  135.             return null;
  136.         }
  137.         int[] fails = failureFun(pat);
  138.  
  139.         // stopwatch starts
  140.         Instant start = Instant.now();
  141.  
  142.         // t = index in text
  143.         // p = index in patten
  144.         int p = 0;
  145.         for(int t = 0; t < tex.GetSize() - pat.GetSize(); t++) {
  146.             tex.SetPos(t);
  147.             pat.SetPos(p);
  148.             if (tex.GetValue().equals(pat.GetValue())) {
  149.  
  150.                 // if the entire pattern is found within the text
  151.                 if (p == pat.GetSize() - 1) {
  152.                     matchAt.InsertAfter(t - p);
  153.  
  154.                     if(DEBUG)
  155.                         numMatches++;
  156.  
  157.                     t = t - p;
  158.                     p = 0;
  159.                 }
  160.                 else {
  161.                     p++;
  162.                 }
  163.             }
  164.  
  165.             // the "shifting" of the pattern
  166.             else if (p > 0) {
  167.                 //if (fails[p - 1] == 0)
  168.                    // p = 0;
  169.                // else
  170.                     p = fails[p - 1];
  171.             }
  172.         }
  173.  
  174.         // stopwatch stops
  175.         Instant finish = Instant.now();
  176.  
  177.         long timeElapsed = Duration.between(start, finish).toMillis();
  178.         System.out.println(tellTime(timeElapsed, "KMP"));
  179.  
  180.         if(DEBUG)
  181.             System.out.println("Num of matches: " + numMatches);
  182.  
  183.         if (!matchAt.IsEmpty())
  184.             return matchAt;
  185.         else
  186.             return null;
  187.     }
  188.  
  189.     /*
  190.     Failure Function needed for the KMP algorithm
  191.     (I got some help from the book for this portion of the code)
  192.      */
  193.     private static int[] failureFun(DoublyLinkedList<String> pat) {
  194.         int[] failure = new int[pat.GetSize()];
  195.         int f = 1;
  196.         int p = 0;
  197.         while(f < pat.GetSize()) {
  198.             pat.SetPos(f);
  199.             String x = pat.GetValue();
  200.             pat.SetPos(p);
  201.             String y = pat.GetValue();
  202.             if (x.equals(y)) {
  203.                 failure[f] = p + 1;
  204.                 f++;
  205.                 p++;
  206.             }
  207.             else if (p > 0)
  208.                 p = failure[p - 1];
  209.             else
  210.                 f++;
  211.         }
  212.  
  213.         return failure;
  214.     }
  215.  
  216.     // function to display the elapsed time of each search algorithm
  217.     private static String tellTime(long tE, String searchType){
  218.         return "Search type: " + searchType + ". Elapsed Time: " + tE + " milliseconds.";
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement