Implementation of any 2 uninformed search methods with some application.


 #include"iostream"  
 #include"stdlib.h"  
 #define MAX 200  
 using namespace std;  
 int q[MAX] = {0};  
 bool visited[MAX] = {false};  
 int n,i,j, front, rear;  
 struct node  
 {  
      int data;  
      struct node *next;  
 };  
 // --------------------- Creating linked list -------------------------  
 struct node* create_node(int val)  
 {  
      struct node *temp = (struct node*)malloc(sizeof(struct node));  
      temp->data = val;  
      temp->next = NULL;  
      return temp;  
 }  
 void append(struct node **head, int val)  
 {  
      struct node *temp = (struct node*)malloc(sizeof(struct node));  
      temp->data = val;  
      temp->next = NULL;  
      struct node *tmp = *head;  
      while(tmp->next !=NULL)  
           tmp = tmp->next;  
      tmp->next = temp;  
 }  
 // ---------------------- DFS Traversal -----------------------------  
 void dfs(struct node **head, int v)  
 {  
      struct node *temp;  
      visited[v-1] = true;  
      cout<<v<<" ";  
      temp = *(head+v-1);  
      while(temp != NULL)  
      {  
           if(visited[temp->data - 1] == false)  
                dfs(head, temp->data);  
           else  
                temp = temp->next;  
      }  
 }  
 // -------------------- BFS Traversal ----------------------------------  
 void addqueue(int v)  
 {  
      if(rear == n-1)  
           cout<<"\nQueue Overflow", exit(1);  
      rear++;  
      q[rear] = v;  
      if(front == -1)  
           front = 0;  
 }  
 int deletequeue()  
 {  
      int data;  
      if(front == -1)  
           cout<<"\nQueue Underflow", exit(1);  
      data = q[front];  
      if(front == rear)  
           front = rear = -1;  
      else  
           front++;  
      return data;  
 }  
 bool isempty()  
 {  
      if(front == -1)  
           return (true);  
      return (false);  
 }  
 void bfs(struct node **head, int v)  
 {  
      struct node *temp;  
      visited[v-1] = true;  
      cout<<v<<" ";  
      addqueue(v);  
      while( isempty() == false)  
      {  
           v = deletequeue();  
           temp = *(head + v - 1);  
           while( temp != NULL)  
           {  
                if( visited[temp->data-1] == false)  
                {  
                     addqueue(temp->data);  
                     visited[temp->data-1] = true;  
                     cout<<(temp->data)<<" ";  
                }  
                temp = temp->next;  
           }  
      }  
 }  
 int main()  
 {  
      cout<<"\n\nEnter no. of vertices: ";  
      cin>>n;  
      int mat[n][n];  
      struct node *arr[n];  
      cout<<"\n\nEnter Adjacency Matrix of size ["<<n<<"x"<<n<<"]: \n";  
      for(i=0; i<n; i++)  
      {  
           for(j=0; j<n; j++)  
           {  
                cin>>mat[i][j];  
           }  
      }  
      for(i=0; i<n; i++)  
      {  
           arr[i] = create_node(i+1);  
           for(j=0; j<n; j++)  
           {  
                if(mat[i][j] == 1)  
                {  
                     append(&arr[i], j+1);  
                }  
           }  
      }  
      cout<<"\n\nDFS Traversal: \n";  
      dfs(arr, 1);  
      cout<<"\n\n";  
      cout<<"\n\nBFS Traversal: \n";  
      rear = front = -1;  
      bfs(arr, 1);  
      cout<<"\n\n";  
      return 0;  
 }  

Post a Comment

Previous Post Next Post