0% found this document useful (0 votes)
4 views

bfs

The document contains C++ code that implements a Breadth-First Search (BFS) algorithm for graph traversal. It includes functions for inputting graph data, initializing structures, performing BFS, and outputting results. The program reads a graph's adjacency matrix from a file and displays the traversal order of its vertices.

Uploaded by

cutevlss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

bfs

The document contains C++ code that implements a Breadth-First Search (BFS) algorithm for graph traversal. It includes functions for inputting graph data, initializing structures, performing BFS, and outputting results. The program reads a graph's adjacency matrix from a file and displays the traversal order of its vertices.

Uploaded by

cutevlss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

#define MAX 20
#include<queue>
#include<fstream>
char vertext[MAX] = {'A','B','C','D','E','F'};
int bfs[MAX];
int a[MAX][MAX];
int nbfs = 0;
int n;
int C[MAX];
using namespace std;
//struct node
//{
// int info;
// node* next;
//};
//struct Queue
//{
// node *head;
// node *tail;
//};
//void init(Queue &q)
//{
// q.head = q.tail = NULL;
//}
//bool empty(Queue q)
//{
// return q.head == NULL?true :false;
//}
//node * createNode(int x)
//{
// node * p = new node;
// p->info = x;
// p->next = NULL;
// return p;
//}
//void enQueue(Queue &q,int x)
//{
// node * p = createNode(x);
// if (empty(q)) {
// q.head = p;
// }
// else
// {
// node *last = q.head;
// while (last->next!=NULL)
// {
// last = last->next;
// }
// last->next = p;
// q.tail = p;
// }
//
//}
//void deQueue(Queue &q)
//{
// if (!empty(q))
// {
// node * p = q.head;
// q.head = p->next;
// delete p;
//
//
// }
//}
void intput(int a[][MAX],int & n)
{
do {
cout << "nhap so dinh:";
cin >> n;
} while (n <= 0 || n > MAX);
cout << "Nhap ten dinh:";

for (int i = 0; i < n; i++)


{
cin >> vertext[i];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
}
void inf()
{
ifstream infile("matrix.txt");
if (infile.is_open())
{
infile >> n;
//for (int i = 0; i < n; i++)
//{
// //getline(infile ,vertext[i]);
// infile >> vertext[i];
//
//}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
infile >> a[i][j];
}
}
infile.close();
}
}
void output(int a[][MAX], int n)
{
for (int i = 0; i < n; i++)
{
cout<< "\t" << vertext[i];
}
cout << endl;
for (int i = 0; i < n; i++)
{
cout << vertext[i] << "\t";
for (int j = 0; j < n; j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
}
void khoitaochuaxet()
{
for (int i = 0; i<n; i++)
C[i] = 1;
}
void BFS(int v)
{
queue<int >q;
q.push(v);
int w, p = v;
C[v] = 0;
while (q.front() != NULL) {
q.pop();
bfs[nbfs++] = p;
for (w = 0; w < n; w++)
{
if (C[w] && a[p][w] == 1)
{
q.push(w);
C[w] = 0;
}
}
}
}
void outputbfs()
{
for (int i = 0; i < nbfs; i++)
{
cout << bfs[nbfs] << " ";
}
}
int main()
{

/*intput(a, n);*/
inf();
output(a, n);
khoitaochuaxet();
BFS(0);
outputbfs();
system("pause");
return 0;
}

You might also like