Ai practical 2:bfs

Aim: Write a program to implement breadth first search Algorithm.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[5][5],i,j,k,n,qu[5],front,rare,v,visit[5],visited[5];
int main(){
    int m;
    cout<<"Enter the number of vertices: ";
    cin>>n;
    cout<<"Enter the number of edges: ";
    cin>>m;
    cout<<"\nEDGES:\n";
    for(k=1;k<=m;k++){
        cin>>i>>j;
        cost[i][j] = 1;
    }
    cout<<"Enter initial vertex to traverse from:";
    cin>>v;
    cout<<"Visited vertices:";
    cout<<v<<" ";
    visited[v]=1;
    k=1;
    while(k<n){
        for(j=1;j<=n;j++)
            if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
            {
                visit[j] = 1;
                qu[rare++];
            }
        v=qu[front++];
        cout<<v<<" ";
        k++;
        visit[v] = 1;    
    }
    getch();
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Hey!

I’m Barbariannn. Discover the ultimate Minetest resource – your go-to guide for expert tutorials, stunning mods, and exclusive stories. Elevate your game with insider knowledge and tips from seasoned Minetest enthusiasts.

Join the club

Stay updated with our latest tips and other news by joining our newsletter.

Categories

Tags

There’s no content to show here yet.