#include #define MAX 10 class BFS { private : int n; int adj[MAX][MAX]; int visited[MAX]; public : void bfs(int); void readmatrix(); }; void BFS :: readmatrix() { int i,j; cout << "\nEnter the number of Vertices in the Graph : "; cin >> n; cout << "\nEnter the Adjacency Matrix\n\n"; for (i = 1; i <= n; i++) for (j = 1; j<= n; j++) cin >> adj[i][j]; for (i = 1; i <= n; i++) visited[i] = 0; } void BFS :: bfs(int source) { int queue[MAX]; int i, front, rear, root; front = rear = 0; visited[source] = 1; queue[rear++] = source; cout << source << " "; while (front != rear) { root = queue[front]; for (i = 1; i <= n; i++) if (adj[root][i] && !visited[i]) { visited[i] = 1; queue[rear++] = i; cout << i << " "; } front++; } } int main() { int source; BFS breadth; breadth.readmatrix(); cout << "\nEnter the Source : "; cin >> source; cout << "\nThe nodes visited in the BFS order is : "; breadth.bfs(source); return 0; }