首页 > 代码库 > 图的遍历

图的遍历

void Graph::DFStra(int xuhao) {
cout << vertex[xuhao] << endl;
visited[xuhao] = 1;
for (int j = 0; j < verNum; j++) {
if (arc[xuhao][j] == 1 && visited[j] == 0)
DFStra(j);
}
}


void Graph::BFStra(int xuhao) {
int str[maxSize];
int front, rear;
front = -1; rear = -1;
str[++rear] = xuhao;
cout << vertex[xuhao];
visited[xuhao] = 1;
while (front != rear) {
xuhao=str[++front];
for (int j = 0; j < verNum; j++)
if (arc[xuhao][j] == 1 && visited[j] == 0) {
cout << vertex[j];
visited[j] = 1;
str[++rear] = j;
}
}

}

图的遍历