-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphSearch.java
158 lines (144 loc) · 4.75 KB
/
GraphSearch.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Syed Sajjad Kazmii
*/
public class GraphSearch {
//declaring public variables
public static int m = 0, n = 0, t = 0;
public static String[] stateDescriptions;
public static String[] ruleDescriptions;
public static int[][] transitionTable;
public static String[] testCases;
public static Queue<Node> frontier= new LinkedList<Node>();
public static Set<Integer> exploredSet = new HashSet<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//initializing
System.out.println("input----");
m = sc.nextInt();
n = sc.nextInt();
t = sc.nextInt();
stateDescriptions = new String[m];
ruleDescriptions = new String[n];
transitionTable = new int[m][n];
testCases = new String[t];
sc.nextLine();
sc.nextLine();
for (int i = 0; i < m; i++) {
stateDescriptions[i] = sc.nextLine();
}
sc.nextLine();
for (int i = 0; i < n; i++) {
ruleDescriptions[i] = sc.nextLine();
}
sc.nextLine();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
transitionTable[i][j] = sc.nextInt();
}
}
sc.nextLine();
sc.nextLine();
for (int i = 0; i < t; i++) {
testCases[i] = sc.nextLine();
}
String[] temp;
Node result;
for(int i=0;i<t;i++){
temp=testCases[i].split("\t");
result=graphSearch(temp[0], temp[1]);
System.out.println("");
printResult(result);
}
System.out.println("");
}
public static Node graphSearch(String initialState, String finalState){
int initStateNum=getStateNumber(initialState);
int goalStateNum=getStateNumber(finalState);
Node initialNode=new Node(initStateNum, -1, 0, null);
frontier.add(initialNode);
exploredSet.clear();
do{
if(frontier.isEmpty())
return new Node(-1, -1, -1, null);
Node leafNode=frontier.remove();
if(leafNode.getState()==goalStateNum)
return leafNode;
exploredSet.add(leafNode.getState());
Node childs[] = getChildNodes(leafNode);
int i=0;
while(i<childs.length){
if(!isNodeVisited(childs[i]))
frontier.add(childs[i]);
i++;
}
}while(true);
}
public static int getStateNumber(String state){
int i=0;
while(i<m && stateDescriptions[i].compareTo(state)!=0){
i++;
}
return i;
}
public static Node[] getChildNodes(Node parent){
Node[] childs=new Node[n];
for(int i=0;i<n;i++){
childs[i]=new Node(transitionTable[parent.getState()][i], i, parent.getCost()+1, parent);
}
return childs;
}
public static boolean isNodeVisited(Node node){
if(isNodeExistInExploredSet(node) || isNodeExistInFrontier(node))
return true;
else
return false;
}
public static boolean isNodeExistInFrontier(Node node){
int i=0;
Node temp;
boolean flag=false;
while(i<frontier.size()){
temp=frontier.remove();
if(node.getState()==temp.getState())
flag = true;
frontier.add(temp);
i++;
}
return flag;
}
public static boolean isNodeExistInExploredSet(Node node){
int i=0;
if(exploredSet.contains(node.getState()))
return true;
else
return false;
}
public static void printResult(Node node){
if(node.getState()==-1)
System.out.println("No Solution Exist");
else{
Stack<String> s=new Stack<String>();
while(node.getParent()!=null){
s.push(ruleDescriptions[node.getAction()]);
node=node.getParent();
}
for(int i=s.size()-1;i>=0;i--){
System.out.print(s.pop());
if(i!=0)
System.out.print(" => ");
}
}
}
}