伪代码
#define Black 2
#define Gray 1
#define White 0
#define Infinite -1
#define NIL -1
struct map{
int d;
int f;
int color;
int parent;
} vertix[100];
int time;
void DSF(int a[100][100],int n){
int i;
for(i = 0;i<n;i++){
vertix[i].color = White;
vertix[i].parent = NIL;
}
time = 0;
for(i = 0;i<n;i++)
if(vertix[i] == White)
DSFvisit(a,n,i);
}
void DSFvisit(int a[100][100],int n,int num){
int i;
vertix[num] = Gray;
time++;
vertix[num].d = time;
for(i = 0;i<n;i++)
if(a[num][i])
if(vertix[i].color == White){
vertix[i].parent = num;
DSFvisit(a,n,i);
}
time++;
vertix[num].f = time;
vertix[num].color = Black;
}
本文介绍了一种图遍历算法——深度优先搜索(DFS),详细展示了其伪代码实现过程。通过定义颜色标志和时间戳来记录节点的状态及访问顺序,利用递归方式实现了对图中各个节点的遍历。
1310

被折叠的 条评论
为什么被折叠?



