1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <stdio.h> #include <queue> using namespace std;
#define MAXVEX 100 #define INFINITY 65535
bool visited[MAXVEX]; typedef char VertexType; typedef int EdgeType; typedef int InforType;
typedef struct Graph{ VertexType vexs[MAXVEX]; EdgeType arc[MAXVEX][MAXVEX]; int numVertexes, numEdge; }Graph;
void BFS(Graph g) { int i, j; queue <int> q; for(i = 0; i < g.numVertexes; i++) visited[i] = false; for(i = 0; i < g.numVertexes; i++) { if(!visited[i]) { visited[i] = true; printf("%c ", g.vexs[i]); q.push(i); while(!q.empty()) { int m = q.front; q.pop(); for(j = 0; j < g.numVertexes; j++) { if(g.arc[m][j] == 1 && !visited[j]) { visited[j] = true; printf("%c ", g.vexs[j]); q.push(j);
} } } } } }
|