#include
bool visited[MAX_VERTEX_NUM];
//用辅助队列的方式进行广度优先排序
void BFSTraverse(Graph G) {
for (int i = 0;i < G.vexnum;++i) {
visited[i] = false;
}
InitQueue(Q);
for (int i = 0;i < G.vexnum;++i) {
if (!visited[i])
BFS(G, i);
}
}
void BFS(Graph G, int v) {
visit(v);
visited[v] = true;
Enqueue(Q, v);
while (!isEmpty(Q)) {
DeQueue(Q, v);
for (w = FirstNeighbor(G, v);w >= 0;w = NextNeighbor(G, v, w)) {
if (!visited[w])
{
visit(w);
visited[w] = true;
Enqueue(Q, w);
}
}
}
}
void BFS_Min_Distance(Graph G, int u) {//求u到i的最短路径
for (int i = 0;i < G.vexnum;++i)
d[i] = ∞;
visited[u] = true;
d[u] = 0;
Enqueue(Q, u);
while (!isEmpty(Q)) {
Dequeue(Q);
for (w = FirstNeighbor(G, u);w >= 0;w = NextNeighbor(G, u, w)
{
if (!visited[w]) {
visited[w] = true;
d[w] = d[u] + 1;
Enqueue(Q, w);
}
}
}
//利用递归工作栈的方式进行深度优先遍历
void DFSTraverse(Graph G) {
for (int v = 0;v < G.vexnum;++v)
visited[v] = false;
for (int v = 0;v < G.vexnum;++v)
if (!visited[v])
DFS(G, v);
}
void DFS(Graph G, int v) {
visit v;
visited[v] = true;
for (w = FirstNeighbor(G, v);w >= 0;w = nextNeighbor(G, v, w)) {
if (!visited[w])
DFS(G, w);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)