-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
110 lines (85 loc) · 3.09 KB
/
Graph.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
import java.util.LinkedList;
import java.util.ListIterator;
/**
*
* @author chamath
*/
public class Graph {
LinkedList<Node> nodes[]=new LinkedList[28];
public Graph() {
for(int i=0;i<28;i++){
nodes[i]=new LinkedList<Node>();
}
}
private void buildGraph(){
nodes[0].add((new Node('c',1,false)));
nodes[0].add((new Node('i',2,false)));
nodes[0].add((new Node('l',3,false)));
nodes[0].add((new Node('s',4,false)));
nodes[0].add((new Node('t',5,false)));
nodes[1].add((new Node('e',6,false)));
nodes[2].add((new Node('l',7,false)));
nodes[3].add((new Node('a',8,false)));
nodes[4].add((new Node('r',9,false)));
nodes[5].add((new Node('a',10,false)));
nodes[6].add((new Node('y',11,false)));
nodes[7].add((new Node('a',12,false)));
nodes[8].add((new Node('n',13,false)));
nodes[9].add((new Node('i',14,false)));
nodes[10].add((new Node('p',15,false)));
nodes[11].add((new Node('l',16,false)));
nodes[12].add((new Node('n',17,false)));
nodes[13].add((new Node('k',18,false)));
nodes[14].add((new Node('l',3,false)));
nodes[15].add((new Node('r',19,false)));
nodes[16].add((new Node('o',20,false)));
nodes[17].add((new Node('g',21,false)));
nodes[18].add((new Node('a',22,true)));
nodes[19].add((new Node('o',23,false)));
nodes[20].add((new Node('n',22,true)));
nodes[21].add((new Node('a',24,false)));
nodes[23].add((new Node('b',25,false)));
nodes[24].add((new Node('i',22,true)));
nodes[25].add((new Node('a',26,false)));
nodes[26].add((new Node('n',27,false)));
nodes[27].add((new Node('e',22,true)));
}
public void checkString(String string){
buildGraph();
int currentState=0;
int nextState=0;
Node node;
ListIterator listIterator;
boolean isAccepted=false;
boolean matched=false;
System.out.println("INPUT STRING - " + string);
for(int i=0;i<string.length();i++){
listIterator= nodes[currentState].listIterator();
matched=false;
while(listIterator.hasNext()){
node=(Node)listIterator.next();
// System.out.println("string char= "+ string.charAt(i) + " node char= "+node.getCharacter());
if(node.getCharacter()==string.charAt(i)){
// System.out.println("matched!!!!");
matched=true;
nextState=node.getState();
if(i==string.length()-1){
isAccepted=node.isAcceptedState();
}
break;
}
}
if(matched)
currentState=nextState;
else
{
isAccepted=false;
break;
}
}
if(isAccepted)
System.out.println("RESULT - ACCEPTED");
else
System.out.println("RESULT - REJECTED");
}
}