-
Notifications
You must be signed in to change notification settings - Fork 1
/
NFA2DFA.java
134 lines (110 loc) · 4.03 KB
/
NFA2DFA.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.*;
public class NFA2DFA {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of states of NFA : ");
int nos = sc.nextInt();
System.out.print("Enter the states : ");
char states_ar[] = new char[nos];
HashMap<Character, Integer> states = new HashMap<Character, Integer>();
for(int i=0;i<nos;i++)
{
char ch = sc.next().charAt(0);
states_ar[i] = ch;
states.put(ch, i);
}
System.out.print("Enter the initial state : ");
char ini_state = sc.next().charAt(0);
System.out.print("Enter the no. of final states : ");
int nof = sc.nextInt();
System.out.print("Enter the set of final states : ");
HashMap<Character, Integer> fin_states = new HashMap<Character, Integer>();
for(int i=0;i<nof;i++)
fin_states.put(sc.next().charAt(0), 1);
System.out.print("Enter the no. of alpha : ");
int noa = sc.nextInt();
System.out.print("Enter the alphabets : ");
char alpha[] = new char[noa];
for(int i=0;i<noa;i++)
alpha[i] = sc.next().charAt(0);
Queue<String> q = new LinkedList<>();
HashMap<String, Integer> map = new HashMap<String, Integer>();
q.add(ini_state+"");
map.put(ini_state+"", 1);
System.out.println("Enter the table for NFA(where multilple characters if any will not be separated by anything)");
System.out.println("#for null enter any state which is not present in the state set");
String table_NFA[][] = new String[nos][noa];
System.out.print("-------------------------------------\n\t");
for(int al=0;al<noa;al++)
System.out.print(alpha[al]+"\t");
System.out.println();
for(int i=0;i<nos;i++)
{
System.out.print(states_ar[i]+"|\t");
for(int j=0;j<noa;j++)
{
table_NFA[i][j] = sc.next();
//System.out.print("\t");
}
}
System.out.println("\nThe Equivalent DFA Table is - ");
String table_DFA[][] = new String[1000][noa];
String final_states_DFA[] = new String[1000];
int top_finalstates = 0;
HashMap<Character, Integer> stts;
for(int i=0;q.size() > 0;i++)
{
String cur_state = q.remove();
System.out.print(cur_state+"|\t");
for(int j=0;j<noa;j++)
{
table_DFA[i][j] = "";
if(cur_state.length() == 1 && !states.containsKey(cur_state.charAt(0)))
{
table_DFA[i][j] = cur_state;
if(!map.containsKey(table_DFA[i][j]))
{
q.add(table_DFA[i][j]);
map.put(table_DFA[i][j], 1);
}
//System.out.println("for check"+(table_DFA[i][j].charAt(0)));
if(fin_states.containsKey((table_DFA[i][j].charAt(0))))
final_states_DFA[top_finalstates++] = table_DFA[i][j];
System.out.print(table_DFA[i][j]+"\t");
continue;
}
int flag_for_final = 0;
stts = new HashMap<Character, Integer>();
for(int k=0;k<cur_state.length();k++)
{
if(!states.containsKey(cur_state.charAt(k)))
continue;
if(fin_states.containsKey(cur_state.charAt(k)))
flag_for_final = 1;
for(int ch=0;ch<table_NFA[states.get(cur_state.charAt(k))][j].length();ch++)
{
if(!stts.containsKey(table_NFA[states.get(cur_state.charAt(k))][j].charAt(ch)) && states.containsKey(table_NFA[states.get(cur_state.charAt(k))][j].charAt(ch)))
{
table_DFA[i][j] += table_NFA[states.get(cur_state.charAt(k))][j].charAt(ch);//null state is appearing i.e AC
stts.put(table_NFA[states.get(cur_state.charAt(k))][j].charAt(ch), 1);
}
}
}
if(!map.containsKey(table_DFA[i][j]))
{
q.add(table_DFA[i][j]);
map.put(table_DFA[i][j], 1);
}
if(flag_for_final == 1)
final_states_DFA[top_finalstates++] = table_DFA[i][j];
System.out.print(table_DFA[i][j]+"\t");
}
System.out.println();
}
/*System.out.print("Initial state : "+ini_state+"\nFinal States : ");
for(int fs=0;fs<top_finalstates;fs++)
System.out.print(final_states_DFA[fs]);
System.out.println(top_finalstates);*/
}
}