Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. #include <iostream>
  2. # include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. class Node {
  6. public:
  7. vector <string > arr;
  8. int final_state=0;
  9. char name;
  10. void inti(){
  11. arr.clear();
  12. final_state=0;
  13. name ='-';
  14. }
  15. };
  16.  
  17. class table_elem {
  18. public:
  19. vector <string > arr;
  20. char name;
  21. void inti(){
  22. arr.clear();
  23. // name;
  24. }
  25. };
  26.  
  27.  
  28. int find_index(vector<vector<Node> > arr_2D,string str)
  29. {
  30. int index;
  31. for(int i = 0;i < arr_2D.size();i++)
  32. {
  33. if(str == arr_2D[i][0].arr[0])
  34. {
  35. index = i;
  36. break;
  37. }
  38. }
  39. return index;
  40. }
  41.  
  42. int vector_compare(vector<string> v1 ,vector<string> v2)
  43. {
  44. if(v1.size() != v2.size())
  45. return 0;
  46. sort(v1.begin(), v1.begin()+v1.size());
  47. sort(v2.begin(), v2.begin()+v2.size());
  48. for(int i =0; i <v1.size();i++)
  49. {
  50. if(v1[i] != v2[i]){
  51. return 0;
  52. }
  53. }
  54. return 1;
  55. }
  56.  
  57. int find_complex_index(vector<vector<Node> > arr_2D, vector<string> arr)
  58. {
  59. for(int j =0; j < arr_2D.size();j++)
  60. { cout << vector_compare(arr_2D[j][0].arr,arr)<<endl;
  61. if(vector_compare(arr_2D[j][0].arr,arr))
  62. return j;
  63. }
  64. }
  65.  
  66.  
  67. vector<vector<Node> > NFA_DFA(vector<vector<Node> > arr_2D, vector<string> transtion){
  68. stack <Node> st;
  69. vector <table_elem> table_equivalence;
  70. vector<vector<Node> > DFA_array;
  71. vector <Node>row;
  72. int current_char = 65;
  73. Node elem;
  74. table_elem tab_elem;
  75. elem.arr.push_back(arr_2D[0][0].arr[0]);
  76. elem.name=(char)current_char;
  77. current_char++;
  78. elem.final_state=arr_2D[0][0].final_state;
  79. // row.push_back(elem);
  80. st.push(elem);
  81. elem.inti();
  82.  
  83. while(!st.empty())
  84. {
  85. //cout << "yes"<<endl;
  86. int row_index;
  87. Node top = st.top();
  88. st.pop();
  89. row.push_back(top);
  90.  
  91. if(top.arr.size() == 1)
  92. row_index = find_index(arr_2D,top.arr[0]);
  93. else
  94. {
  95. vector<int> indexes;
  96. for(int k = 0; k < top.arr.size();k++)
  97. {
  98. indexes.push_back(find_index(arr_2D,top.arr[k]));
  99. }
  100. vector<Node> rowrow;
  101. rowrow.push_back(top);
  102. for(int o = 1; o < transtion.size(); o++)
  103. {
  104. rowrow.push_back(arr_2D[indexes[0]][o]);
  105. }
  106. for(int l = 1; l < indexes.size(); l++)
  107. {
  108. for(int o = 1; o < transtion.size(); o++)
  109. {
  110. for(int v = 0; v < arr_2D[indexes[l]][o].arr.size(); v++)
  111. {
  112. int matched = 0;
  113. for(int h = 0;h < rowrow[o].arr.size(); h++)
  114. {
  115. if(rowrow[o].arr[h] == arr_2D[indexes[l]][o].arr[v])
  116. matched = 1;
  117. }
  118. if(!matched)
  119. rowrow[o].arr.push_back(arr_2D[indexes[l]][o].arr[v]);
  120. }
  121. }
  122. }
  123.  
  124. arr_2D.push_back(rowrow);
  125. /*for(int g = 0;g < transtion.size(); g++)
  126. {
  127. for(int f = 0;f < arr_2D[arr_2D.size() - 1][g].arr.size();f++)
  128. cout << "hello " <<arr_2D[arr_2D.size() - 1][g].arr[f] <<'\t';
  129. cout<< endl;
  130. }*/
  131. for(int s = 0; s < arr_2D.size(); s++)
  132. {
  133. for(int r = 0; r < arr_2D[s][0].arr.size();r++)
  134. {
  135. cout<< arr_2D[s][0].arr[r] << '\t';
  136. }
  137. cout << endl;
  138. }
  139. row_index = find_complex_index(arr_2D,top.arr);
  140. for(int y = 0; y < top.arr.size(); y++)
  141. {
  142. cout << top.arr[y] << '\t';
  143. }
  144. cout << endl;
  145. }
  146.  
  147. for(int i = 1; i < transtion.size();i++)
  148. {
  149. int counter = 0;
  150. int flag = 0;
  151. for(int j= 0;j < table_equivalence.size();j++)
  152. {
  153. if( arr_2D[row_index][i].arr.size() != 0){
  154.  
  155. if(vector_compare(arr_2D[row_index][i].arr,table_equivalence[j].arr))
  156. {
  157. flag = 1;
  158. counter = j;
  159. break;
  160. }else
  161. {
  162. flag = 0;
  163. }
  164. }else //if the node is empty
  165. {
  166. flag = 2;
  167. }
  168. }
  169. if(flag == 0)
  170. {
  171. tab_elem.arr = arr_2D[row_index][i].arr;
  172. tab_elem.name = (char)current_char;
  173. table_equivalence.push_back(tab_elem);
  174. tab_elem.inti();
  175. elem.name = (char) current_char;
  176. elem.arr = arr_2D[row_index][i].arr;
  177. row.push_back(elem);
  178. st.push(elem);
  179. elem.inti();
  180. current_char++;
  181. }else if(flag == 1)
  182. {
  183. elem.name = table_equivalence[counter].name ;
  184. elem.arr = table_equivalence[counter].arr ;
  185. row.push_back(elem);
  186. elem.inti();
  187. }else
  188. {
  189. elem.inti();
  190. row.push_back(elem);
  191. }
  192. }
  193. DFA_array.push_back(row);
  194. row.clear();
  195. }
  196.  
  197. cout << DFA_array.size() << endl;
  198.  
  199. for (int x=0;x < DFA_array.size();x++){
  200. for(int y =0;y < 3; y++)
  201. {
  202. cout << DFA_array[x][y].name<<'\t';
  203. }
  204. cout<<endl;
  205. }
  206. return DFA_array;
  207. }
  208.  
  209. int main()
  210. {
  211. /*
  212. vector<string> transition;
  213. transition.push_back("a");
  214. transition.push_back("a");
  215. transition.push_back("a");
  216. Node n1;
  217. n1.arr.push_back("s1");
  218. Node n2;
  219. n2.arr.push_back("s2");
  220. Node n3;
  221. n3.arr.push_back("s3");
  222. Node n4;
  223. n4.arr.push_back("s2");
  224. n4.arr.push_back("s3");
  225. vector<vector <Node> > Nfa;
  226. vector <Node> row;
  227. row.push_back(n1);
  228. row.push_back(n4);
  229. row.push_back(n2);
  230. Nfa.push_back(row);
  231. row.clear();
  232. row.push_back(n2);
  233. row.push_back(n2);
  234. row.push_back(n3);
  235. Nfa.push_back(row);
  236. row.clear();
  237. row.push_back(n3);
  238. row.push_back(n3);
  239. row.push_back(n3);
  240. Nfa.push_back(row);
  241. NFA_DFA(Nfa,transition);*/
  242.  
  243. vector<string> transition;
  244. transition.push_back("a");
  245. transition.push_back("a");
  246. transition.push_back("a");
  247. Node n1;
  248. n1.arr.push_back("s1");
  249. Node n2;
  250. n2.arr.push_back("s2");
  251. Node n3;
  252. n3.arr.push_back("s3");
  253. Node n4;
  254. n4.arr.push_back("s4");
  255. Node n5;
  256. n5.arr.push_back("s2");
  257. n5.arr.push_back("s3");
  258. Node n6;
  259. n6.arr.push_back("s1");
  260. n6.arr.push_back("s3");
  261. Node n7;
  262. vector<vector <Node> > Nfa;
  263. vector <Node> row;
  264. row.push_back(n1);
  265. row.push_back(n5);
  266. row.push_back(n7);
  267. Nfa.push_back(row);
  268. row.clear();
  269. row.push_back(n2);
  270. row.push_back(n7);
  271. row.push_back(n6);
  272. Nfa.push_back(row);
  273. row.clear();
  274. row.push_back(n3);
  275. row.push_back(n4);
  276. row.push_back(n4);
  277. Nfa.push_back(row);
  278. row.clear();
  279. row.push_back(n4);
  280. row.push_back(n2);
  281. row.push_back(n4);
  282. Nfa.push_back(row);
  283. NFA_DFA(Nfa,transition);
  284. return 0;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement