图的广度遍历(BFS)与深度遍历(DFS)

时间:2021-03-22 12:36:25

转载注明出处,原文地址:http://blog.csdn.net/powerwoo25/article/details/47869457


图的广度遍历(BFS)与深度遍历(DFS)


思路: 读取用户输入的结点个数、边的两端顶点,用一个邻接矩阵来代表图内的连通情况。然后取第一个结点放入双端队列中进行BFS


转载注明出处,原文地址:http://blog.csdn.net/powerwoo25/article/details/47869457
/* BFS */
#include <stdio.h>
#include <deque>
using namespace std;

typedef struct node
{
int flag, d, pre;
node(): flag(0), d(10000), pre(-1){};
}vertex;

const int maxV = 10000;
int matrix[maxV][maxV];

void ReadEdge(int num)
{
int startV, endV;
printf("Enter your edges:\n");
while(~scanf("%d,%d", &startV, &endV) && (startV < num) && (startV >= 0) && (endV < num) && (endV >= 0))
{
matrix[startV][endV] = matrix[endV][startV] = 1;
}
}

void BFS(deque<int> Q, vertex *A, int num)
{
int i;
A[0].flag = 1;
A[0].d = 0;

Q.push_back(0);

while(!Q.empty())
{
int u = Q.front();
Q.pop_front();
for(i = 0; i < num; i++)
{
if(matrix[u][i] != 0 && A[i].flag == 0)
{
A[i].flag = 1;
A[i].d = A[u].d + 1;
A[i].pre = u;
Q.push_back(i);
}
}
A[u].flag = 2;
}
}

void PrintVertex(vertex* A, int num)
{
int i, j, k;

for(j = 0; j < num; ++j)
{
for(k = 0; k < num; ++k)
{
printf("%d ", matrix[j][k]);
}
printf("\n");
}
for(i = 0; i < num; ++i)
{
printf("Vertex: %d, flag: %d, pre: %d, d: %d\n", i, A[i].flag, A[i].pre, A[i].d);
}
}

int main()
{
freopen("J://test.txt", "r", stdin);

int vertexN;
printf("Please enter the quantity of your vertex\n");
scanf("%d", &vertexN);
if(vertexN < 1 || vertexN > maxV)
{
perror("Invalid parameter: vertexN\n");
return -1;
}
vertex vertexs[vertexN];

ReadEdge(vertexN);

deque<int> vertexQ;
vertexQ.clear();

BFS(vertexQ, vertexs, vertexN);

PrintVertex(vertexs, vertexN);

return 0;
}

思路:DFS主要利用递归来进行深度优先的遍历


转载注明出处,原文地址:http://blog.csdn.net/powerwoo25/article/details/47869457
/* DFS */
#include <stdio.h>
using namespace std;

typedef struct node
{
int flag;
int pre;
int d, f;
node(): flag(0), pre(-1), d(0), f(0){};
}vertex;

const int maxV = 10000;
int time = 0;
int matrix[maxV][maxV];

void ReadEdge(int num)
{
int startV, endV;
printf("Enter your edges:\n");
while(~scanf("%d,%d", &startV, &endV) && (startV < num) && (startV >= 0) && (endV < num) && (endV >= 0))
{
matrix[startV][endV] = matrix[endV][startV] = 1;
}
}

void PrintVertex(vertex* A, int num)
{
int i, j, k;

for(j = 0; j < num; ++j)
{
for(k = 0; k < num; ++k)
{
printf("%d ", matrix[j][k]);
}
printf("\n");
}
for(i = 0; i < num; ++i)
{
printf("Vertex: %d, flag: %d, pre: %d, d: %d, f: %d\n", i, A[i].flag, A[i].pre, A[i].d, A[i].f);
}
}

void DFS_VISIT(vertex *A, int index, int num)
{
int i;
A[index].d = ++time;
A[index].flag = 1;
for(i = 0; i < num; ++i)
{
if(matrix[index][i] != 0)
{
if(A[i].flag == 0)
{
A[i].pre = index;
DFS_VISIT(A, i, num);
}
}
}
A[index].flag = 2;
A[index].f = ++time;
}

void DFS(vertex *A, int num)
{
int i;
for(i = 0; i < num; ++i)
{
if(A[i].flag == 0)
{
DFS_VISIT(A, i, num);
}
}
}

int main()
{
freopen("J://test.txt", "r", stdin);
int vertexN;
printf("Please enter the quantity of your vertex\n");
scanf("%d", &vertexN);
if(vertexN < 1 || vertexN > maxV)
{
perror("Invalid parameter: vertexN\n");
return -1;
}
vertex vertexs[vertexN];

ReadEdge(vertexN);

DFS(vertexs, vertexN);

PrintVertex(vertexs, vertexN);

return 0;
}


# test.txt100,23,42,44,13,15,62,67,93,88,9