Thursday, 25 July 2013

Deterministic finite automata ( DFA ) implemented using Java....

 

This is a java program to simulate the DFA( deterministic finite automata ) developed using J2SE . It works for any general dfa that you have specified through five tuples during the execution of the program. 

To know that how to give input to the program please click here

here is the code  :-


       

import java.io.*;
import java.util.*;
public class Dfasimulator {

 public int[][] Transition = new int[20][20];
 public int alphaset, stateset;
 public ArrayList < Character > alphabet;
 public Set < Integer > finalstates;

 public void builtdfa() throws Exception {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("please enter the no. of alphabets in the alphabet set of DFA");
  alphaset = Integer.parseInt(br.readLine());
  //System.out.println(alphaset);
  System.out.println("please enter the no. of states in the set of DFA");
  stateset = Integer.parseInt(br.readLine());

  System.out.println("please enter all the alphabets of the DFA simultaneosly");
  String alphabets = br.readLine();

  alphabet = new ArrayList < Character > ();
  for (int i = 0; i < alphabets.length(); i++) {
   alphabet.add(alphabets.charAt(i));

  }
  //System.out.println(alphabet);

  for (int s = 0; s < stateset; s++) {
   System.out.println("enter the row entries of the transition table");
   for (int r = 0; r < alphaset; r++) {
    int int1 = Integer.parseInt(br.readLine());
    Transition[s][r] = int1;
   }
  }

  finalstates = new HashSet < Integer > ();
  System.out.println("please enter the final states of the DFA\n");
  System.out.println("when you are done of giving the inputs enter -2 to stop feeding final states");

  int int2 = Integer.parseInt(br.readLine());

  while (int2 != -2) {
   finalstates.add(int2);
   int2 = Integer.parseInt(br.readLine());
  }
  System.out.println(finalstates);
  DfaTest();
 }

 public void DfaTest() throws Exception {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  int state;
  System.out.println("enter the starting state");
  state = Integer.parseInt(br.readLine());

  System.out.println("give input to the DFA\n");
  System.out.println("end your input with # character\n");
  String input = br.readLine();
  int index = 0;

  while (input.charAt(index) != '#') {
   char char1 = input.charAt(index);
   int index1 = alphabet.indexOf(char1);
   state = Transition[state][index1];
   index++;
  }
  System.out.println("\nfinal state after giving the input is " + state);
  if (finalstates.contains(state))
   System.out.print("your input string is accepted by the DFA");
  else
   System.out.println("the input string is rejected by  the DFA");
 }

 public static void main(String args[]) throws Exception {
  Dfasimulator raw = new Dfasimulator();
  raw.builtdfa();
  //System.out.println(raw.alphaset);
 }
}

       
 

20 comments:

  1. I need code of DFA to Pumping lemma ...Give Transition table of DFA As input and get three strings that will accept pumping lemma...

    ReplyDelete
  2. kindly send me code if you or help me please

    ReplyDelete
  3. Hi Gaurav ,

    Hope you’re enjoying a good life with programming !

    Dear I need a little bit help of you regarding your one code on internet related to DFA ,Link is below !

    http://javaprogrammingstuff.blogspot.com/2013/07/this-is-java-program-to-simulate.html

    Can you let me know how to feed Inputs in it for Transition Table ! As I tried to feed but always its end with error ! Plz give me a screenshot of it that how you feed inputs in it as sample !

    I will be very grateful for your help ! I have also attached screen shot here by highlighting the problem !






    Thanks

    ReplyDelete
    Replies
    1. Hi Jawad,
      I have updated a file link in the post itself which describes that how to give input to the DFA program please have a look at it.

      Delete
  4. Thanks Dear ! Plz Provide access to that file as that is not publicly shared to demanding access permission !

    Thansk

    ReplyDelete
  5. Kindly share a Public Link it is not giving permission to access the file! Due to permission set as Private !

    ReplyDelete
  6. Source code file is not found. Kindly share it again.

    ReplyDelete
    Replies
    1. Yeah,sorry for that I've updated the link now you can try again,although you can also copy above code also it is same.

      Delete
  7. it would be really great Gaurav if you could write a turing machine implementation in java as well...

    ReplyDelete
    Replies
    1. Yes I will definitely think about it ,actually these programs were written by me when I was still in college but now I've a full time job but anyway I will try to cover that too.

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Can you have a program for NFA in java.If you have, please post it

    ReplyDelete
  10. If anyone have NFA in java,please post it here.

    ReplyDelete
  11. If anyone have NFA in java,please post it here.

    ReplyDelete
  12. Thank you.. you just saved my day.. :)

    ReplyDelete
  13. Hi Gaurav ,

    Hope you’re enjoying a good life with programming !

    I have to build an DFA that accept the set of all strings that, viewed as natural numbers in unsigned binary notation, represent numbers divisible by 5.

    i try to give input like this....


    please enter the no. of alphabets in the alphabet set of DFA
    2
    please enter the no. of states in the set of DFA
    5
    please enter all the alphabets of the DFA simultaneosly
    01
    enter the row entries of the transition table
    1
    12
    enter the row entries of the transition table
    2
    53
    enter the row entries of the transition table
    3
    25
    enter the row entries of the transition table
    4
    34
    enter the row entries of the transition table
    5
    14
    please enter the final states of the DFA

    when you are done of giving the inputs enter -2 to stop feeding final states
    5
    -2
    [5]
    enter the starting state
    1
    give input to the DFA

    end your input with # character

    101#
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 53
    at Dfasimulator.DfaTest(Dfasimulator.java:65)
    at Dfasimulator.builtdfa(Dfasimulator.java:48)
    at Dfasimulator.main(Dfasimulator.java:77)
    Press any key to continue . . .


    The Transition Table is

    -|0|1
    1|1|2
    2|5|3
    3|2|5
    4|3|4
    5|1|4


    Please can u help me!! I really need help.

    Thank You

    ReplyDelete
  14. THANK YOU SO MUCH

    ReplyDelete
  15. Respect and I have a super give: What Was The First Home Renovation Show house renovation shows on netflix

    ReplyDelete