Docsity

Implementing Breadth First Search with open and close class method, Lab Reports of Artificial Intelligence

Partial preview of the text

Download Implementing Breadth First Search with open and close class method and more Lab Reports Artificial Intelligence in PDF only on Docsity!

School of Information Technology (SIT)

The University of Lahore, Islamabad Campus

Lab Manual

Artificial Intelligence

CS

The University of Lahore, Islamabad Campus

School of Information Technology (SIT)

Experiment No # 4

Implementing Breadth First Search with open and close class method

The pseudocode for breadth first search is:

i. Declare two empty lists: Open and Closed.

ii. Add Start node to our Open list.

iii. While our Open list is not empty, loop the following:

a. Remove the first node from our Open List.

b. Check to see if the removed node is our destination.

i. If the removed node is our destination, break out of the loop, add

the node to our Closed list, and return the value of our Closed list.

ii. If the removed node is not our destination, continue the loop (go

to Step c).

c. Extract the neighbors of our above removed node.

d. Add the neighbors to the end of our Open list, and add the removed node

to our Closed list.

Tasks:

1. Implement BFS in C language for the following Graph:

#include #include using namespace std; #define MAX 100 int n; int adj[MAX][MAX]; int state[MAX]; int queue[MAX], front = -1, rear = -1; void BFS(int v); void insert_queue(int v);

cout<<v<<" "; state[v] = 3; //Visited State for(int i=0; i<n; i++){ if(adj[v][i] == 1 && state[i] == 1){ insert_queue(i); state[i] = 2; } } } cout<<"\n"; } void insert_queue(int v){ if(rear == MAX-1){ cout<<"Queue Overflow!\n"; } else { if (front == -1){ front = 0; } rear = rear + 1; queue[rear] = v; } } int delete_queue(){ int delete_item; if(front == -1 || front > rear) { cout<<"Queue Underflow\n"; return 0;; } delete_item = queue[front]; front = front+1; return delete_item; } bool isEmpty_queue(){ if(front == -1 || front > rear) return true; else return false; } int main(){ createGraph();

apply_BFS(); return 0; }

Output: