SlideShare a Scribd company logo
1 of 2
//B.F.S
#include <stdio.h>
void main(void)
{
int n,i,j,graph[100][100]={0},k,l=-1,flag=0,flag2=0,f=-1,r=-1;
char vertex[100],v,queue[100],vs[100]={0},u;
fflush(stdout);
printf(" n ENTER : ");
fflush(stdin);
scanf("%d", &n);
for(i=0;i<n; i++)
vertex[i]=i+65;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
fflush(stdout);
printf("%c->%c exists : ", vertex[i],vertex[j]);
fflush(stdin);
scanf("%d", &graph[i][j]);
if(graph[i][j]==1)
graph[j][i]=1;
}
}
for(k=0;k<n;k++)
{
v=k+65;
fflush(stdout);
printf(" n %c-> ",v);
for(i=0;i<n;i++)
{
if(vertex[i]==v)
break;
}
for(j=0;j<n;j++)
{
if(graph[i][j]==1)
{
fflush(stdout);
printf("%c ",vertex[j]);
}
}
}
printf("nn");
printf(" n B.F.S : ");
queue[r+1]='A';
r++;
while(f!=r)
{
v=queue[f+1];
queue[f+1]=NULL;
f++;
for(k=0;k<=l;k++)
{
if(vs[k]==v)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
printf("%c ",v);
vs[l+1]=v;
l++;
}
if(flag==0)
{
for(i=0;i<n;i++)
{
if(vertex[i]==v)
break;
}
for(j=0;j<n;j++)
{
if(graph[i][j]==1)
{
u=vertex[j];
for(k=0;k<=l;k++)
{
if(vs[k]==u)
{
flag2=1;
break;
}
else
flag2=0;
}
if(flag2==0)
{
queue[r+1]=u;
r++;
}
}
}
}
}
printf("nn");
}

More Related Content

What's hot

What's hot (20)

Bubble sort
Bubble sortBubble sort
Bubble sort
 
Union
UnionUnion
Union
 
(Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++ (Meta 4) ejemplo calcular la mitad de un numero dev c++
(Meta 4) ejemplo calcular la mitad de un numero dev c++
 
Fibonacci
FibonacciFibonacci
Fibonacci
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3
 
[程式設計]標準差
[程式設計]標準差[程式設計]標準差
[程式設計]標準差
 
Heep implementation in c
Heep implementation in cHeep implementation in c
Heep implementation in c
 
Infitopost notepad
Infitopost   notepadInfitopost   notepad
Infitopost notepad
 
Cpro
CproCpro
Cpro
 
Ds program
Ds programDs program
Ds program
 
MAIN BEACH DEVELOPMENT[1] copy
MAIN BEACH DEVELOPMENT[1] copyMAIN BEACH DEVELOPMENT[1] copy
MAIN BEACH DEVELOPMENT[1] copy
 
Pruebabfs
PruebabfsPruebabfs
Pruebabfs
 
Linker
LinkerLinker
Linker
 
C언어 스터디 강의자료 - 2차시
C언어 스터디 강의자료 - 2차시C언어 스터디 강의자료 - 2차시
C언어 스터디 강의자료 - 2차시
 
Rafaeltorres
RafaeltorresRafaeltorres
Rafaeltorres
 
Dado un vector imprimir cuantas vocales tiene
Dado un vector imprimir cuantas vocales tieneDado un vector imprimir cuantas vocales tiene
Dado un vector imprimir cuantas vocales tiene
 
C언어 스터디 강의자료 - 4차시
C언어 스터디 강의자료 - 4차시C언어 스터디 강의자료 - 4차시
C언어 스터디 강의자료 - 4차시
 
C언어 스터디 강의자료 - 3차시
C언어 스터디 강의자료 - 3차시C언어 스터디 강의자료 - 3차시
C언어 스터디 강의자료 - 3차시
 
Numeros primos
Numeros primosNumeros primos
Numeros primos
 
TUGAS BAHASA C
TUGAS BAHASA CTUGAS BAHASA C
TUGAS BAHASA C
 

B.f.s