Advertisement
Guest User

code

a guest
Mar 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. import static java.lang.Math.max;
  5. import static java.lang.Math.min;
  6. import static java.lang.System.nanoTime;
  7.  
  8. public class Main {
  9.     private static boolean DEBUG = false;
  10.  
  11.     public static void main(String[] args) throws java.io.IOException {
  12.         DoublyLinkedList<String> text1 = new DoublyLinkedList<>();
  13.         DoublyLinkedList<String> text2 = new DoublyLinkedList<>();
  14.         DoublyLinkedList<String> pattern = new DoublyLinkedList<>();
  15.  
  16.         Scanner inFile1 = new Scanner(new File("input1.txt"));
  17.         charsToList(fileScan(inFile1), text1);
  18.  
  19.         Scanner inFile2 = new Scanner(new File("input2.txt"));
  20.         charsToList(fileScan(inFile2), text2);
  21.  
  22.         if (DEBUG) {
  23.             System.out.println("list 1 = " + text1);
  24.             System.out.println("list 2 = " + text2);
  25.         }
  26.  
  27.         //Ask the user for the pattern they are looking for and convert it into a linked tex
  28.         Scanner input = new Scanner(System.in);
  29.         System.out.print("Enter the pattern you are searching for: ");
  30.         String[] searchFor = input.nextLine().split("");
  31.         charsToList(searchFor, pattern);
  32.  
  33.         if (DEBUG) {
  34.             System.out.println("pattern = " + pattern);
  35.         }
  36.  
  37.         System.out.println(bruteForce(pattern, text1));
  38.         System.out.println(kmp(pattern, text1));
  39.         System.out.println(bm(pattern, text1));
  40.  
  41.         if (DEBUG)
  42.             for (int i = 0; i < text2.GetSize(); i++) {
  43.                 text2.SetPos(i);
  44.                 System.out.println(i + ":" + text2.GetValue());
  45.             }
  46.     }
  47.  
  48.     // function to convert the chars from a read file into a string array
  49.     private static String[] fileScan(Scanner inFile) {
  50.         String fileString = "";
  51.         while (inFile.hasNext())
  52.             fileString += inFile.nextLine();
  53.  
  54.         String[] chars = fileString.split("");
  55.  
  56.         inFile.close();
  57.  
  58.         return chars;
  59.     }
  60.  
  61.     // function to convert a string array into a linked tex with nodes holding only one char
  62.     private static void charsToList(String[] chars, DoublyLinkedList<String> tex) {
  63.         for (String c : chars)
  64.             tex.InsertAfter(c.toLowerCase());
  65.     }
  66.  
  67.     /*
  68.     Brute Force algorithm for pattern matching
  69.     (I got some help from the book for this portion of the code)
  70.      */
  71.     private static DoublyLinkedList<Integer> bruteForce(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex) {
  72.  
  73.         // stopwatch starts
  74.         long start = nanoTime();
  75.  
  76.         int numMatches = 0;
  77.  
  78.         int n = tex.GetSize();
  79.         int m = pat.GetSize();
  80.  
  81.         if (m == 0)
  82.             return null;
  83.  
  84.         DoublyLinkedList<Integer> matchAt = new DoublyLinkedList<>();
  85.  
  86.         // t goes up to the last index that is able to start the pattern which is being searched for
  87.         for (int t = 0; t <= n - m; t++) {
  88.  
  89.             // p = index in pattern
  90.             int p = 0;
  91.             tex.SetPos(t);
  92.             pat.First();
  93.  
  94.             // if characters matching pattern are found
  95.             while (p < m && tex.GetValue().equals(pat.GetValue())) {
  96.                 p++;
  97.                 pat.Next();
  98.                 tex.Next();
  99.             }
  100.  
  101.             // if the end of the pattern is reached
  102.             if (p == m) {
  103.                 matchAt.InsertAfter(t);
  104.  
  105.                 if (DEBUG)
  106.                     numMatches++;
  107.  
  108.             }
  109.         }
  110.  
  111.         // stopwatch starts
  112.         long finish = nanoTime();
  113.  
  114.         long timeElapsed = finish - start;
  115.         System.out.println(tellTime(timeElapsed, "Brute Force"));
  116.  
  117.         if (DEBUG)
  118.             System.out.println("Num of matches: " + numMatches);
  119.  
  120.         if (!matchAt.IsEmpty())
  121.             return matchAt;
  122.         else
  123.             return null;
  124.     }
  125.  
  126.     /*
  127.     Boyer-Moore algorithm for pattern matching
  128.     Got help from geeksforgeeks on this one:
  129.         URL -> https://www.geeksforgeeks.org/boyer-moore-algorithm-for-pattern-searching/
  130.      */
  131.     private static DoublyLinkedList<Integer> bm(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex) {
  132.  
  133.         // stopwatch starts
  134.         long start = nanoTime();
  135.  
  136.         int numMatches = 0;
  137.  
  138.         int n = tex.GetSize();
  139.         int m = pat.GetSize();
  140.  
  141.         if (m == 0)
  142.             return null;
  143.  
  144.         DoublyLinkedList<Integer> matchAt = new DoublyLinkedList<>();
  145.  
  146.         int s = 0;
  147.         while (s <= n - m) {
  148.             int j = m - 1;
  149.             int k = 0;
  150.  
  151.             pat.SetPos(j);
  152.             tex.SetPos(s + j);
  153.  
  154.             while (j >= 0 && pat.GetValue().equals(tex.GetValue())) {
  155.                 pat.Prev();
  156.                 tex.Prev();
  157.                 j--;
  158.                 k++;
  159.             }
  160.  
  161.             if (j < 0) {
  162.                 matchAt.InsertAfter(s);
  163.  
  164.                 if (DEBUG)
  165.                     numMatches++;
  166.  
  167.                 tex.SetPos(s + m);
  168.                 int gek = backCheckBC(pat, tex, "true");
  169.                 if (pat.GetSize() == 1)
  170.                     s += 1;
  171.                 else
  172.                     s += (s + m < n) ? m - gek : 1;
  173.             } else {
  174.                 int gek = 0;
  175.                 int bc = backCheckBC(pat, tex, "false");
  176.                 int gs = backCheckGS(pat, k - 1);
  177.  
  178.                 if (bc == -1 && gs > -1)
  179.                     gek = gs;
  180.                 else if (bc > -1 && gs == -1)
  181.                     gek = bc;
  182.                 else if (bc > -1 && gs > -1)
  183.                     gek = min(bc, gs);
  184.                 else
  185.                     gek = -1;
  186.  
  187.  
  188.                 s += max(1, j - gek);
  189.             }
  190.         }
  191.  
  192.         // stopwatch starts
  193.         long finish = nanoTime();
  194.  
  195.         long timeElapsed = finish - start;
  196.         System.out.println(tellTime(timeElapsed, "Boyer Moore"));
  197.  
  198.         if (DEBUG)
  199.             System.out.println("Num of matches: " + numMatches);
  200.  
  201.         if (!matchAt.IsEmpty())
  202.             return matchAt;
  203.         else
  204.             return null;
  205.     }
  206.  
  207.     private static int backCheckBC(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex, String matched) {
  208.         if (matched.equals("true"))
  209.             pat.Last();
  210.  
  211.         int tempPos = 0;
  212.  
  213.         if (matched.equals("false"))
  214.             tempPos = pat.GetPos();
  215.  
  216.         int jah = 0;
  217.         for (int k = pat.GetPos(); k >= 0; k--) {
  218.  
  219.             if (pat.GetValue().equals(tex.GetValue())) {
  220.                 jah = pat.GetPos();
  221.                 if (matched.equals("false"))
  222.                     pat.SetPos(tempPos);
  223.  
  224.                 return jah;
  225.             }
  226.  
  227.             pat.Prev();
  228.  
  229.         }
  230.  
  231.         if (matched.equals("false"))
  232.             pat.SetPos(tempPos);
  233.  
  234.         return -1;
  235.     }
  236.  
  237.     private static int backCheckGS(DoublyLinkedList<String> pat, int numMatched) {
  238.         if (numMatched <= 0)
  239.             return -1;
  240.  
  241.         String[] moonlightUH = new String[numMatched];
  242.         int tempPos = pat.GetPos();
  243.         pat.Last();
  244.         for (int j = 0; j < numMatched; j++) {
  245.             moonlightUH[j] = pat.GetValue();
  246.             pat.Prev();
  247.         }
  248.  
  249.         int posArray = numMatched - 1;
  250.         for (int i = pat.GetPos(); i >= 0; i--) {
  251.             while (pat.GetValue().equals(moonlightUH[posArray])) {
  252.                 posArray--;
  253.                 pat.Prev();
  254.                 if (posArray == -1)
  255.                     return pat.GetPos();
  256.             }
  257.  
  258.             pat.SetPos(i);
  259.         }
  260.  
  261.         pat.SetPos(tempPos);
  262.         return -1;
  263.     }
  264.  
  265.     /*
  266.     KMP algorithm for pattern matching
  267.     (I got some help from the book for this portion of the code)
  268.      */
  269.     private static DoublyLinkedList<Integer> kmp(DoublyLinkedList<String> pat, DoublyLinkedList<String> tex) {
  270.  
  271.         // stopwatch starts
  272.         long start = nanoTime();
  273.  
  274.         int numMatches = 0;
  275.  
  276.         int n = tex.GetSize();
  277.         int m = pat.GetSize();
  278.  
  279.         if (m == 0)
  280.             return null;
  281.  
  282.         DoublyLinkedList<Integer> matchAt = new DoublyLinkedList<>();
  283.  
  284.         int[] fails = failureFun(pat);
  285.  
  286.         // t = index in text
  287.         // p = index in patten
  288.         int p = 0;
  289.         int t = 0;
  290.  
  291.         tex.First();
  292.         while (t < n) {
  293.             //tex.SetPos(t);
  294.             pat.SetPos(p);
  295.             if (tex.GetValue().equals(pat.GetValue())) {
  296.  
  297.                 // if the entire pattern is found within the text
  298.                 if (p == m - 1) {
  299.                     matchAt.InsertAfter(t - m + 1);
  300.  
  301.                     if (DEBUG)
  302.                         numMatches++;
  303.  
  304.                     t++;
  305.                     tex.Next();
  306.                     p = fails[p];
  307.                 } else {
  308.                     t++;
  309.                     tex.Next();
  310.                     p++;
  311.                 }
  312.             }
  313.  
  314.             // the "shifting" of the pattern
  315.             else if (p > 0)
  316.                 p = fails[p - 1];
  317.             else {
  318.                 t++;
  319.                 tex.Next();
  320.             }
  321.         }
  322.  
  323.         // stopwatch stops
  324.         long finish = nanoTime();
  325.  
  326.         long timeElapsed = finish - start;
  327.         System.out.println(tellTime(timeElapsed, "KMP"));
  328.  
  329.         if (DEBUG)
  330.             System.out.println("Num of matches: " + numMatches);
  331.  
  332.         if (!matchAt.IsEmpty())
  333.             return matchAt;
  334.         else
  335.             return null;
  336.     }
  337.  
  338.     /*
  339.     Failure Function needed for the KMP algorithm
  340.     (I got some help from the book for this portion of the code)
  341.      */
  342.     private static int[] failureFun(DoublyLinkedList<String> pat) {
  343.         int[] failure = new int[pat.GetSize()];
  344.         int f = 1;
  345.         int p = 0;
  346.         while (f < pat.GetSize()) {
  347.             pat.SetPos(f);
  348.             String x = pat.GetValue();
  349.             pat.SetPos(p);
  350.             String y = pat.GetValue();
  351.             if (x.equals(y)) {
  352.                 failure[f] = p + 1;
  353.                 f++;
  354.                 p++;
  355.             } else if (p > 0)
  356.                 p = failure[p - 1];
  357.             else
  358.                 f++;
  359.         }
  360.  
  361.         return failure;
  362.     }
  363.  
  364.     // function to display the elapsed time of each search algorithm
  365.     private static String tellTime(long tE, String searchType) {
  366.         return "Search type: " + searchType + ". Elapsed Time: " + tE + " nanoseconds.";
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement