







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Went through 12 states FOUND!!! Went through 53 states
complexity in Depth-first search is linear when in Breadth-first search it’s exponential. In case of the missionaries and cannibals problem the solution found by Depth-first search happened to be the optimal one. Actually, it seems that in this problem there is only one solution, which was found by both Depth-first and Breadth-first searches. For the details on how the program was developed please see comments in the
MisRight=0; CanLeft=0; next=NULL; CanRight=0; } void State::Initialize() { Boat=right; MisLeft=0; MisRight=3; CanLeft=0; CanRight=3; operation=any; next=NULL; } bool State::IsValid() { //Check if # of missioners>=0, number of cannibals>=0, //and missioners>=cannibals if (MisRight==0) { if ((MisLeft>=CanLeft) && (MisLeft>=0) && (CanRight>=0) && (CanLeft>=0)) return (true); else return (false); } else { if (MisLeft==0) { if ((MisRight>=CanRight) && (MisRight>=0) && (CanRight>=0) && (CanLeft>=0)) return (true); else return (false); } else { if ((MisRight>=CanRight) && (MisLeft>=CanLeft) && (MisRight>=0) && (MisLeft>=0) && (CanRight>=0) && (CanLeft>=0)) return (true); else return (false); } } } void State::PrintState() { //Print out this state cout<<"ML:"<<MisLeft<<" MR:"<<MisRight<< " CL:"<<CanLeft<<" CR:"<<CanRight<< " B:"<<Boat<<endl;
bool State::IsGoal() { //Checks whether it is a goal state //(whether ML=3, MR=0, CL=3, CR=0, B=left) if ((MisLeft==3) && (CanLeft==3) && (MisRight==0) && (CanRight==0) && (Boat==left)) { return (true); } else return (false); } State* State::GetChildQueue() { State* TempChild_two_m; State* TempChild_two_c; State* TempChild_one_m; State* TempChild_one_c; State* TempChild_one_m_one_c; State* Head; //pointer to the head of the child queue to be formed State* temptr; //pointer to move along child queue TempChild_two_m = new State; TempChild_two_c = new State; TempChild_one_m = new State; TempChild_one_c = new State; TempChild_one_m_one_c = new State; Head=new State; temptr=new State; Head=NULL; temptr=NULL; if (Boat==right) { //boat is on the right bank //try to transport 2 missioners TempChild_two_m->Boat=left; TempChild_two_m->MisRight=MisRight-2; TempChild_two_m->MisLeft=MisLeft+2; TempChild_two_m->CanRight=CanRight; TempChild_two_m->CanLeft=CanLeft; if (TempChild_two_m->IsValid() && operation!=two_m) { //the generated child is valid and is not the result of the same operation as parent TempChild_two_m->next=NULL; TempChild_two_m->operation=two_m; if (Head==NULL) { //it is the first child in the queue, make it head Head=TempChild_two_m; temptr=Head;
else { temptr->next=TempChild_one_m; temptr=TempChild_one_m; } } else { TempChild_one_m=NULL; delete TempChild_one_m; } //try to transport 1 cannibal TempChild_one_c->Boat=left; TempChild_one_c->MisRight=MisRight; TempChild_one_c->MisLeft=MisLeft; TempChild_one_c->CanRight=CanRight-1; TempChild_one_c->CanLeft=CanLeft+1; if ((TempChild_one_c->IsValid()) && (operation!=one_c)) { TempChild_one_c->next=NULL; TempChild_one_c->operation=one_c; if (Head==NULL) { Head=TempChild_one_c; temptr=Head; } else { temptr->next=TempChild_one_c; temptr=TempChild_one_c; } } else { TempChild_one_c=NULL; delete TempChild_one_c; } //try to transport 1 missioner and 1 cannibal TempChild_one_m_one_c->Boat=left; TempChild_one_m_one_c->MisRight=MisRight-1; TempChild_one_m_one_c->MisLeft=MisLeft+1; TempChild_one_m_one_c->CanRight=CanRight-1; TempChild_one_m_one_c->CanLeft=CanLeft+1; if ((TempChild_one_m_one_c->IsValid()) && (operation! =one_m_one_c)) { TempChild_one_m_one_c->next=NULL; TempChild_one_m_one_c->operation=one_m_one_c; if (Head==NULL) { Head=TempChild_one_m_one_c; temptr=Head; } else
temptr->next=TempChild_one_m_one_c; temptr=TempChild_one_m_one_c; } } else { TempChild_one_m_one_c=NULL; delete TempChild_one_m_one_c; } } else {//boat is on the left bank //try to transport 2 missioners TempChild_two_m->Boat=right; TempChild_two_m->MisRight=MisRight+2; TempChild_two_m->MisLeft=MisLeft-2; TempChild_two_m->CanRight=CanRight; TempChild_two_m->CanLeft=CanLeft; if ((TempChild_two_m->IsValid()) && (operation!=two_m)) { TempChild_two_m->next=NULL; TempChild_two_m->operation=two_m; if (Head==NULL) { Head=TempChild_two_m; temptr=Head; } else { temptr->next=TempChild_two_m; temptr=TempChild_two_m; } } else { TempChild_two_m=NULL; delete TempChild_two_m; } //try to transport 2 cannibals TempChild_two_c->Boat=right; TempChild_two_c->MisRight=MisRight; TempChild_two_c->MisLeft=MisLeft; TempChild_two_c->CanRight=CanRight+2; TempChild_two_c->CanLeft=CanLeft-2; if ((TempChild_two_c->IsValid()) && (operation!=two_c)) { TempChild_two_c->next=NULL; TempChild_two_c->operation=two_c; if (Head==NULL) { Head=TempChild_two_c; temptr=Head; } else
else { TempChild_one_c=NULL; delete TempChild_one_c; } //try to transport 1 missioner and 1 cannibal TempChild_one_m_one_c->Boat=right; TempChild_one_m_one_c->MisRight=MisRight+1; TempChild_one_m_one_c->MisLeft=MisLeft-1; TempChild_one_m_one_c->CanRight=CanRight+1; TempChild_one_m_one_c->CanLeft=CanLeft-1; if ((TempChild_one_m_one_c->IsValid()) && (operation! =one_m_one_c)) { TempChild_one_m_one_c->next=NULL; TempChild_one_m_one_c->operation=one_m_one_c; if (Head==NULL) { Head=TempChild_one_m_one_c; temptr=Head; } else { temptr->next=TempChild_one_m_one_c; temptr=TempChild_one_m_one_c; } } else { TempChild_one_m_one_c=NULL; delete TempChild_one_m_one_c; } } //return pointer to the head of the child queue return (Head); } void State::SearchDeep() {//deep search algorithm State* ChildQueuePtr; ChildQueuePtr=new State; ChildQueuePtr=NULL; if (Found) //check whether recursion has to be stopped return; NumStates++; PrintState(); if (!IsGoal()) //if this is not goal state { ChildQueuePtr=GetChildQueue(); //get child queue for this state
while (!ChildQueuePtr==NULL) { ChildQueuePtr->SearchDeep(); //perform recursive deep search on the head //of the child queue ChildQueuePtr=ChildQueuePtr->next; //move along child queue } return; } else { cout<<"FOUND!!!"<<endl; cout<<"Went through "<<NumStates<<" states"<<endl; Found=true; //set Found=true to stop recursion return; } } State* State::GetTail() {//get tail of the queue referenced by this state State* temptr; temptr=new State; temptr=this; while (!temptr->next==NULL) temptr=temptr->next; return (temptr); } void State::SearchWide() { /wide search algorithm this function first checks all states at the certain depth, then, if it does not come across the goal state, gets children for all the states at that depth, links all the children into one queue and passes that queue back to this function for recursion/ State* ChildQuePtr; State* temptr; State* ChildHead; State* NextChild; ChildQuePtr=new State; temptr=new State; ChildHead=new State; NextChild=new State; ChildQuePtr=NULL; ChildHead=NULL; NextChild=NULL; if(Found) //stop recursion if goal state is found return; temptr=this;
Found=false; NumStates=0; cout<<"Searching Deep"<<endl<<endl; //perform deep search InitState.SearchDeep(); //re-initialize global variables Found=false; Depth=0; NumStates=0; cout<<"Searching Wide"<<endl<<endl; //perform wide search InitState.SearchWide(); }