/// This is C++ implementation of well knowned uninformed search algorithms of AI.
/// BFS, DFS, DLS, IDS
/// This program takes input and gives output with small letters
/// But works with integer.
/// A sample input output.
/*
14 14
a b
a c
a d
b e
b f
b g
e k
e l
g m
d n
d j
k n
c i
c h
a n
*/
#include<bits/stdc++.h>
using namespace std;
int n,strt,goal; ///Global variable for no. of nodes, start and end point.
bool graph[101][101]; ///Global 2D array to store graph.
bool vis[101]; ///Global Boolean array to trace visited graph.
bool get_goal=false; ///Global boolean var to determine get goal or not.
int dls_level=-1; ///Level for DLS & IDS, -1 coz source starts from 0.
vector<int>path; ///STL Vector for storing paths.
queue<int>q; ///STL Queue for BFS
///Here is the C++ standerd function prototyping.
void dfs(int node);
void bfs(int node);
void dls(int node,int lim);
void ids(int node,int lim);
void input();
void output();
void reset();
int main()
{
///Menu function
int select_menu=0;
string algos[5]={"BFS", "DFS", "DLS", "IDS", "Exiting"}; ///Used for printing.. :p
while(select_menu!=5) ///Iterate until user wants to exit.
{
cout<<"Select a menu:\n1. BFS\n2. DFS\n3. DLS\n4. IDS\n5. Exit."<<endl;
cin>>select_menu;
cout<<"You selected "<<algos[select_menu-1]<<endl;
if(select_menu==5) return 0;
switch(select_menu)
{
case 1: ///This segment works for BFS algorithm.
reset();
input();
q.push(strt);
bfs(strt);
output();
break;
case 2: ///This segment works for DFS algorithm.
reset();
input();
dfs(strt);
output();
break;
case 3: ///This segment works for DLS algorithm.
reset();
input();
int lim;
cout<<"Enter limit: ";
cin>>lim;
dls(strt,lim);
output();
break;
case 4: ///This segment works for IDS algorithm.
reset();
input();
while(!get_goal)
{
memset(vis,0,sizeof(vis));
path.clear();
ids(strt,lim++);
}
output();
break;
case 5: ///This segment works for Exciting
break;
}
}
return 0;
}
///DFS function
void dfs(int node)
{
///This segment works to return if goal acheived. (Main modification of Standerd algo).
if(get_goal) return;
if(node==goal)
{
path.push_back(node);
get_goal=true;
return;
}
vis[node]=1;
path.push_back(node);
for(int i=1;i<=n;i++)
{
if(graph[node][i] && !vis[i])
{
dfs(i);
}
}
}
///BFS function
void bfs(int node)
{
///This segment works to return if goal acheived. (Main modification of Standerd algo).
vis[node]=1;
if(get_goal) return;
if(node==goal)
{
path.push_back(node);
get_goal=true;
return;
}
path.push_back(node);
if(q.empty()) return;
int x=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(graph[x][i] && !vis[i])
{
q.push(i);
}
}
bfs(q.front());
}
///DLS function
void dls(int node, int lim)
{
dls_level++; ///If function enters in a new/child node level incriments.
///This segment works to return if goal acheived. (Main modification of Standerd algo).
if(get_goal) return;
if(node==goal)
{
path.push_back(node);
get_goal=true;
return;
}
path.push_back(node);
vis[node]=1;
for(int i=1;i<=n;i++)
{
if(graph[node][i] && !vis[i])
{
if(dls_level<lim) dls(i,lim);
}
}
dls_level--; ///If Function returns from a node, level decrements.
}
///IDS function
void ids(int node, int lim)
{
dls_level++;
///This segment works to return if goal acheived. (Main modification of Standerd algo).
if(get_goal) return;
if(node==goal)
{
path.push_back(node);
get_goal=true;
return;
}
path.push_back(node);
vis[node]=1;
for(int i=1;i<=n;i++)
{
if(graph[node][i] && !vis[i])
{
if(dls_level<lim) ids(i,lim);
}
}
dls_level--;
}
///This function takes input in the global graph 2D array and Source and Destination.
void input()
{
int edge;
char s,g;
cout<<"Enter number of nodes: ";
cin>>n;
cout<<"Enter number of edges: ";
cin>>edge;
reset();
cout<<"Enter edges:"<<endl;
for(int i=0;i<edge;i++)
{
char u,v;
cin>>u>>v;
graph[u-96][v-96]=1;
}
cout<<"Enter starting & goal: ";
cin>>s>>g;
strt=s-96; ///This program takes input and gives output with small latter,
goal=g-96; ///So it converts ascii char to integer.
}
///This function resets the graph array, Source, Destination and get_goal variable
void reset()
{
get_goal=false; ///CLearing goal
memset(graph,0,sizeof(graph)); ///Clearing Graph
memset(vis,0,sizeof(vis)); ///Clearing Visited array
path.clear(); ///Clearing Path Vector
}
///This function Prints the output.
void output()
{
///If last digit of path vector is not the goal, So....
if(path[path.size()-1]!=goal) {cout<<"There is no path... :("<<endl; return;}
cout<<char(path[0]+96); ///This program takes input and gives output with small latter,
for(int i=1;i<path.size();i++)
cout<<"->"<<char(path[i]+96); ///So it converts integer to assci char.
cout<<"\nGoal Goal... :D"<<endl;
}