diff --git a/BMA.java b/BMA.java index 79b5f01..f365e7b 100644 --- a/BMA.java +++ b/BMA.java @@ -1,12 +1,13 @@ package Algorithms; public class BMA { + String zx=" "; /*public static void main(String[] args) { //(BoyerMoore("Is the pattern a substring?", "substr")); }*/ - public void BoyerMoore(String T, String P) + public boolean BoyerMoore(String T, String P) { int flag=0,pos=0; int i = P.length() -1; @@ -20,6 +21,7 @@ public void BoyerMoore(String T, String P) pos=i; flag=1;// pattern found System.out.println("Pattern found at position: "+ pos); + zx=zx+" "+pos; break; } @@ -37,10 +39,14 @@ public void BoyerMoore(String T, String P) } while(i <= T.length()-1); if(flag==1) + { System.out.println("Pattern found at position: "+ pos); - else + return true; + } + else { System.out.println("Pattern not found"); - + return false; + } } // Returns index of last occurrence of character in pattern. @@ -67,4 +73,4 @@ else if (b < a) else return a; } -} +} \ No newline at end of file diff --git a/Execute.java b/Execute.java index d51726b..833a9ef 100644 --- a/Execute.java +++ b/Execute.java @@ -12,8 +12,8 @@ public static void main(String args[]) Scanner sc=new Scanner(System.in); System.out.println("Enter the string: "); - String givenstr=sc.next(); - System.out.println("Enter the Pattern to be matched: "); + String givenstr=sc.nextLine(); + System.out.println("Enter the Pattern to be matchAed: "); String pattern = sc.next(); while(true) { diff --git a/KMP.java b/KMP.java index 1360d7a..5988abc 100644 --- a/KMP.java +++ b/KMP.java @@ -1,18 +1,18 @@ package Algorithms; public class KMP { - + String zx=" "; /* public static void main(String[] args) { final String givenstr = "AAAAABAAABA"; //This is the full string final String pattern= "AAAA"; //This is the substring that we want to find KMPmatcher(haystack, needle); }*/ - public static void KMPmatcher(final String givenstr, final String pattern) { - final int m = givenstr.length(); - final int n = pattern.length(); + boolean KMPmatcher(String givenstr, String pattern) { + int m = givenstr.length(); + int n = pattern.length(); int flag=0; - final int[] pi = computePrefixFunction(pattern); + int[] pi = computePrefixFunction(pattern); int q = 0; for (int i = 0; i < m; i++) { while (q > 0 && givenstr.charAt(i) != pattern.charAt(q)) { @@ -27,10 +27,16 @@ public static void KMPmatcher(final String givenstr, final String pattern) { flag=1; System.out.println("Pattern found at " + (i + 1 - n)); q = pi[q - 1]; + zx=zx+" "+(i + 1 - n); } } - if(flag==0) + if(flag==0) { System.out.println("Pattern not found"); + return false; + } + else { + return true; + } } // return the prefix function diff --git a/Naive.java b/Naive.java index 01cf3e6..8a87b27 100644 --- a/Naive.java +++ b/Naive.java @@ -2,11 +2,12 @@ import java.util.*; public class Naive { - public static void Naivesearch(String pattern, String givenstr) + int flag=0; + public boolean Naivesearch(String pattern, String givenstr) { int m = pattern.length(); int n = givenstr.length(); - int flag=0,i=0; + int i=0; for (i = 0; i <= n - m; i++) { int j; @@ -21,12 +22,16 @@ public static void Naivesearch(String pattern, String givenstr) } } if(flag==1) - System.out.println("Pattern found"); - else + { + System.out.println("Pattern present in the string"); + return true; + } + else { System.out.println("Pattern not found"); - } - - public static void main(String[] args) + return false; + } + } + /*public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string: "); @@ -34,7 +39,7 @@ public static void main(String[] args) System.out.println("Enter the Pattern to be matched: "); String pattern = sc.next(); Naivesearch(pattern,givenstr); - } + } */ } diff --git a/README.md b/README.md new file mode 100644 index 0000000..175e039 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Advanced Algorithm Project +## Search Algorithms Visualizer in Java Swing +Algorithms like +- Naive Bayes +- Knuth,Morris & Pratt +- Boyer Moore +- Rabin Karp +Are demonstrated diff --git a/Rabin_karp.java b/Rabin_karp.java index 31381a9..7e2b866 100644 --- a/Rabin_karp.java +++ b/Rabin_karp.java @@ -1,14 +1,16 @@ package Algorithms; public class Rabin_karp { + int flag=0; + String zx=" "; //number of characters in the input alphabet public final static int d = 256; - void RKsearch(String pat, String txt, int q) + boolean RKsearch(String pat, String txt, int q) { int M = pat.length(); int N = txt.length(); - int i, j,flag=0; + int i, j; int p = 0; // hash value for pattern int t = 0; // hash value for txt int h = 1; @@ -42,7 +44,10 @@ void RKsearch(String pat, String txt, int q) // if pattern exists in text if (j == M) { flag=1; - System.out.println("Pattern found at index " + i); } + System.out.println("Pattern found at index " + i); + zx=zx+" "+i; + System.out.println(zx); + } } if ( i < N-M ) @@ -54,8 +59,13 @@ void RKsearch(String pat, String txt, int q) t = (t + q); } } - if(flag==0) + if(flag==0) { System.out.println("Pattern not found"); + return false; + } + else { + return true; + } } diff --git a/bleh.java b/bleh.java deleted file mode 100644 index b1009e5..0000000 --- a/bleh.java +++ /dev/null @@ -1,64 +0,0 @@ -package Algorithms; -import java.awt.*; -import java.awt.event.*; -import java.util.Scanner; -import java.awt.GraphicsConfiguration; -import java.awt.FlowLayout; -import javax.swing.*; - - -public class bleh -{ - static JTextField textfield1, textfield2; - static GraphicsConfiguration gc; - public static void main(String[] args) - { - Naive obj1 =new Naive(); - Rabin_karp obj2=new Rabin_karp(); - KMP obj3=new KMP(); - BMA obj4=new BMA(); - - JFrame frame= new JFrame(gc); - frame.getContentPane().setLayout(new FlowLayout()); - frame.setTitle("PATTERN MATCHING"); - frame.setSize(1000, 1000); - frame.setLocationRelativeTo(null); //Setting the window to the centre of the screen - frame.setVisible(true); - frame.setBackground(Color.cyan); - - textfield1 = new JTextField("Enter the String",20); - textfield2 = new JTextField("Enter the Pattern",20); - //textfield3 = new JTextField("Text field 3",10); - frame.getContentPane().add(textfield1); - frame.getContentPane().add(textfield2); - // f.getContentPane().add(textfield3); - frame.pack(); - - JButton n=new JButton("Naive"); - n.setBounds(100,100,140, 40); - JButton rk=new JButton("Rabin Karp"); - rk.setBounds(100,100,140, 40); - JButton kmp=new JButton("Knuth Morris Pratt"); - kmp.setBounds(100,100,140, 40); - JButton bma=new JButton("Boyer Moore"); - bma.setBounds(100,100,140, 40); - - frame.add(n); - frame.add(rk); - frame.add(kmp); - frame.add(bma); - - frame.validate(); - - Scanner sc=new Scanner(System.in); - String givenstr= textfield1.getText(); - String pattern = textfield2.getText(); - - n.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - - obj1.Naivesearch(pattern, givenstr); - } - }); -} -} diff --git a/index.java b/index.java new file mode 100644 index 0000000..7ecdd19 --- /dev/null +++ b/index.java @@ -0,0 +1,187 @@ +package Algorithms; +import java.awt.*; +import java.awt.event.*; +import java.util.Scanner; +import java.awt.GraphicsConfiguration; +import java.awt.FlowLayout; +import javax.swing.*; + + +public class index +{ + static JTextField txt1, txt2; + static GraphicsConfiguration gc; + public static void main(String[] args) + { + + Naive obj1 =new Naive(); + Rabin_karp obj2=new Rabin_karp(); + KMP obj3=new KMP(); + BMA obj4=new BMA(); + JPanel top=new JPanel(); + top.setLayout(new GridBagLayout()); + + GridBagConstraints c = new GridBagConstraints(); + + JFrame frame= new JFrame(gc); + frame.getContentPane().setLayout(new FlowLayout()); + frame.setTitle("PATTERN MATCHING"); + frame.setSize(1000, 1000); + frame.setLocationRelativeTo(null); //Setting the window to the centre of the screen + frame.setVisible(true); + frame.setBackground(Color.cyan); + + + + + txt1 = new JTextField("Enter the String",20); + txt2 = new JTextField("Enter the Pattern",20); + //textfield3 = new JTextField("Text field 3",10); + //frame.getContentPane().add(textfield1); + // frame.getContentPane().add(textfield2); + // f.getContentPane().add(textfield3); + + + JButton n=new JButton("Naive"); + n.setBounds(100,100,140, 40); + JButton rk=new JButton("Rabin Karp"); + rk.setBounds(100,100,140, 40); + JButton kmp=new JButton("Knuth Morris Pratt"); + kmp.setBounds(100,100,140, 40); + JButton bma=new JButton("Boyer Moore"); + bma.setBounds(100,100,140, 40); + JButton res=new JButton("Reset"); + res.setBounds(100,100,140, 40); + + + JLabel op1=new JLabel(" "); + JLabel op2=new JLabel(" "); + JLabel op3=new JLabel(" "); + JLabel op4=new JLabel(" "); + + c.gridx=1;//TEXT FIELDS + c.gridy=1; + top.add(txt1, c); + c.gridx=2; + c.gridy=1; + top.add(txt2, c); + + c.gridx=1; //BUTTONS + c.gridy=2; + c.fill=c.HORIZONTAL; + c.insets = new Insets(3,3,3,3); + + top.add(n, c); + c.gridx=1; + c.gridy=3; + top.add(rk, c); + c.gridx=1; + c.gridy=4; + top.add(kmp, c); + c.gridx=1; + c.gridy=5; + top.add(bma, c); + c.gridx=1; + c.gridy=6; + c.fill=c.NONE; + c.gridy=GridBagConstraints.RELATIVE; + top.add(res, c); + + + c.gridx=2;//LABELS + c.gridy=2; + top.add(op1, c); + c.gridx=2; + c.gridy=3; + top.add(op2, c); + c.gridx=2; + c.gridy=4; + top.add(op3, c); + c.gridx=2; + c.gridy=5; + top.add(op4, c); + + + frame.add(top); + frame.validate(); + + Scanner sc=new Scanner(System.in); + String givenstr= txt1.getText(); + String pattern = txt2.getText(); + + n.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + op1.setText(" "); + String x=txt1.getText(); + String y=txt2.getText(); + boolean f =obj1.Naivesearch(y, x); + if(f) + op1.setText("Pattern found in the string"); + else + op1.setText("Pattern not found in the string"); + + } + }); + res.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + op1.setText(" "); + op2.setText(" "); + op3.setText(" "); + op4.setText(" "); + obj2.zx=" "; + obj3.zx=" "; + obj4.zx=" "; + } + }); + + rk.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + op2.setText(" "); + String x=txt1.getText(); + String y=txt2.getText(); + String w=obj2.zx; + int q=101; + + boolean f = obj2.RKsearch(y,x,q); + if(f) + op2.setText("Pattern found in the string at: "+ obj2.zx ); + else + op2.setText("Pattern not found in the string"); + + } + }); + + kmp.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + op3.setText(" "); + String x=txt1.getText(); + String y=txt2.getText(); + String w=obj3.zx; + + boolean f = obj3.KMPmatcher(x,y); + if(f) + op3.setText("Pattern found in the string at: " + obj3.zx); + else + op3.setText("Pattern not found in the string"); + + } + }); + + bma.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + op4.setText(" "); + String x=txt1.getText(); + String y=txt2.getText(); + + boolean f = obj4.BoyerMoore(x,y); + String w=obj4.zx; + if(f) + op4.setText("Pattern found in the string at: "+ obj4.zx); + else + op4.setText("Pattern not found in the string"); + + } + }); + frame.pack(); +} +}