• Source
    1. /// This is C++ implementation of well knowned uninformed search algorithms of AI.
    2. /// BFS, DFS, DLS, IDS
    3. /// This program takes input and gives output with small letters
    4. /// But works with integer.
    5. /// A sample input output.
    6. /*
    7.  
    8. 14 14
    9.  
    10. a b
    11. a c
    12. a d
    13. b e
    14. b f
    15. b g
    16. e k
    17. e l
    18. g m
    19. d n
    20. d j
    21. k n
    22. c i
    23. c h
    24.  
    25. a n
    26.  
    27. */
    28.  
    29.  
    30. #include<bits/stdc++.h>
    31. using namespace std;
    32.  
    33. int n,strt,goal; ///Global variable for no. of nodes, start and end point.
    34. bool graph[101][101]; ///Global 2D array to store graph.
    35. bool vis[101]; ///Global Boolean array to trace visited graph.
    36. bool get_goal=false; ///Global boolean var to determine get goal or not.
    37. int dls_level=-1; ///Level for DLS & IDS, -1 coz source starts from 0.
    38. vector<int>path; ///STL Vector for storing paths.
    39. queue<int>q; ///STL Queue for BFS
    40.  
    41. ///Here is the C++ standerd function prototyping.
    42. void dfs(int node);
    43. void bfs(int node);
    44. void dls(int node,int lim);
    45. void ids(int node,int lim);
    46. void input();
    47. void output();
    48. void reset();
    49.  
    50.  
    51. int main()
    52. {
    53. ///Menu function
    54. int select_menu=0;
    55. string algos[5]={"BFS", "DFS", "DLS", "IDS", "Exiting"}; ///Used for printing.. :p
    56.  
    57. while(select_menu!=5) ///Iterate until user wants to exit.
    58. {
    59. cout<<"Select a menu:\n1. BFS\n2. DFS\n3. DLS\n4. IDS\n5. Exit."<<endl;
    60. cin>>select_menu;
    61. cout<<"You selected "<<algos[select_menu-1]<<endl;
    62. if(select_menu==5) return 0;
    63. switch(select_menu)
    64. {
    65. case 1: ///This segment works for BFS algorithm.
    66. reset();
    67. input();
    68. q.push(strt);
    69. bfs(strt);
    70. output();
    71. break;
    72. case 2: ///This segment works for DFS algorithm.
    73. reset();
    74. input();
    75. dfs(strt);
    76. output();
    77. break;
    78. case 3: ///This segment works for DLS algorithm.
    79. reset();
    80. input();
    81. int lim;
    82. cout<<"Enter limit: ";
    83. cin>>lim;
    84. dls(strt,lim);
    85. output();
    86. break;
    87. case 4: ///This segment works for IDS algorithm.
    88. reset();
    89. input();
    90. while(!get_goal)
    91. {
    92. memset(vis,0,sizeof(vis));
    93. path.clear();
    94. ids(strt,lim++);
    95. }
    96. output();
    97. break;
    98. case 5: ///This segment works for Exciting
    99. break;
    100. }
    101. }
    102. return 0;
    103. }
    104.  
    105. ///DFS function
    106. void dfs(int node)
    107. {
    108. ///This segment works to return if goal acheived. (Main modification of Standerd algo).
    109. if(get_goal) return;
    110. if(node==goal)
    111. {
    112. path.push_back(node);
    113. get_goal=true;
    114. return;
    115. }
    116.  
    117. vis[node]=1;
    118. path.push_back(node);
    119. for(int i=1;i<=n;i++)
    120. {
    121. if(graph[node][i] && !vis[i])
    122. {
    123. dfs(i);
    124. }
    125. }
    126. }
    127.  
    128. ///BFS function
    129. void bfs(int node)
    130. {
    131. ///This segment works to return if goal acheived. (Main modification of Standerd algo).
    132. vis[node]=1;
    133. if(get_goal) return;
    134. if(node==goal)
    135. {
    136. path.push_back(node);
    137. get_goal=true;
    138. return;
    139. }
    140.  
    141. path.push_back(node);
    142. if(q.empty()) return;
    143. int x=q.front();
    144. q.pop();
    145. for(int i=1;i<=n;i++)
    146. {
    147. if(graph[x][i] && !vis[i])
    148. {
    149. q.push(i);
    150. }
    151. }
    152. bfs(q.front());
    153. }
    154.  
    155. ///DLS function
    156. void dls(int node, int lim)
    157. {
    158. dls_level++; ///If function enters in a new/child node level incriments.
    159.  
    160. ///This segment works to return if goal acheived. (Main modification of Standerd algo).
    161. if(get_goal) return;
    162. if(node==goal)
    163. {
    164. path.push_back(node);
    165. get_goal=true;
    166. return;
    167. }
    168.  
    169. path.push_back(node);
    170. vis[node]=1;
    171. for(int i=1;i<=n;i++)
    172. {
    173. if(graph[node][i] && !vis[i])
    174. {
    175. if(dls_level<lim) dls(i,lim);
    176. }
    177. }
    178.  
    179. dls_level--; ///If Function returns from a node, level decrements.
    180. }
    181.  
    182. ///IDS function
    183. void ids(int node, int lim)
    184. {
    185. dls_level++;
    186.  
    187. ///This segment works to return if goal acheived. (Main modification of Standerd algo).
    188. if(get_goal) return;
    189. if(node==goal)
    190. {
    191. path.push_back(node);
    192. get_goal=true;
    193. return;
    194. }
    195.  
    196. path.push_back(node);
    197. vis[node]=1;
    198. for(int i=1;i<=n;i++)
    199. {
    200. if(graph[node][i] && !vis[i])
    201. {
    202. if(dls_level<lim) ids(i,lim);
    203. }
    204. }
    205. dls_level--;
    206. }
    207.  
    208.  
    209. ///This function takes input in the global graph 2D array and Source and Destination.
    210. void input()
    211. {
    212. int edge;
    213. char s,g;
    214. cout<<"Enter number of nodes: ";
    215. cin>>n;
    216. cout<<"Enter number of edges: ";
    217. cin>>edge;
    218.  
    219. reset();
    220. cout<<"Enter edges:"<<endl;
    221. for(int i=0;i<edge;i++)
    222. {
    223. char u,v;
    224. cin>>u>>v;
    225. graph[u-96][v-96]=1;
    226. }
    227. cout<<"Enter starting & goal: ";
    228. cin>>s>>g;
    229. strt=s-96; ///This program takes input and gives output with small latter,
    230. goal=g-96; ///So it converts ascii char to integer.
    231. }
    232.  
    233. ///This function resets the graph array, Source, Destination and get_goal variable
    234. void reset()
    235. {
    236. get_goal=false; ///CLearing goal
    237. memset(graph,0,sizeof(graph)); ///Clearing Graph
    238. memset(vis,0,sizeof(vis)); ///Clearing Visited array
    239. path.clear(); ///Clearing Path Vector
    240. }
    241.  
    242. ///This function Prints the output.
    243. void output()
    244. {
    245. ///If last digit of path vector is not the goal, So....
    246. if(path[path.size()-1]!=goal) {cout<<"There is no path... :("<<endl; return;}
    247. cout<<char(path[0]+96); ///This program takes input and gives output with small latter,
    248. for(int i=1;i<path.size();i++)
    249. cout<<"->"<<char(path[i]+96); ///So it converts integer to assci char.
    250. cout<<"\nGoal Goal... :D"<<endl;
    251. }
    252.