# 图的深度优先搜索算法DFS

1.问题描述与理解

1. 算法伪代码
DFS(G)
1  for each vertex uV[G]
2       do color[u]←WHITE
3         [u] ←NIL
4  time← 0
5  S←
6  for each vertex s V[G]
7       do if color[s] = WHITE
8       then   color[s] ← GRAY
9                   d[s] ← time← time +1
10                  PUSH(S, s)
11                  while S≠
12      do u←TOP(S)
13            if v Adj[u] and color[v] = WHITE
14                  then color[v] ←GRAY
15                            [v] ←u
16          d[v] ← time← time +1
17          PUSH(S, v)
18                  else color[u] ← BLACK
19                          f [u] ← time ← time +1
20                         POP(S)
21 return d, f, and 


DFS施于一个有向图的过程

3.程序实现

/***********************************
*@file:graph.h
*@ brif:图的邻接表的算法实现类
*@ author:sf
*@data：20150704
*@version 1.0
*
************************************/
#ifndef _GRAPH_H
#define _GRAPH_H

#include <list>
using namespace std;
struct vertex//邻接表节点结构
{
double weight;//边的权值
int index;//邻接顶点
};
class Graph
{
public:
int n;//顶点个数
Graph(double *a,int n);
~Graph();

};

#endif // _GRAPH_H
#include "stdafx.h"
#include "Graph.h"

Graph::Graph(float *a,int n):n(n)//a是图的邻接矩阵
{
for (int i = 0; i < n;i++)//对每个顶点i
for (int j = 0; j < n;j++)
if (a[i*n+j]!=0.0)
{
vertex node = { a[i*n + j], j };//a[i,j]=weight 边的权重 j,邻接节点号
}
}
Graph::~Graph()
{
}
#ifndef _DFS_H
#define _DFS_H
/***********************************
*@file:dfs.h
*@ brif:深度优先搜索算法实现
*@ author:sf
*@data：20150708
*@version 1.0
*
************************************/
#include "Graph.h"
#include <stack>
struct Parameter3
{
int* first;
int* second;
int* third;
};
/***********************************
*@function:dfs
*@ brif:图的邻接表的图的深度优先搜索(Depth First Search, DFS)算法实现
*@ input param: g 图的邻接表
*@ output param: pi g的深度优先森林 d ：发现时间  f:完成时间
*@ author:sf
*@data：20150708
*@version 1.0
*
************************************/
Parameter3 dfs(const Graph& g);
#endif
#include "dfs.h"
enum vertex_color{WHITE,GRAY,BLACK};
typedef enum vertex_color Color;
Parameter3 dfs(const Graph& g)
{
int n = g.n, u, v, s;
Color *color = new Color[n];
int *pi = new int[n], *d = new int[n], *f = new int[n], time = 0;
fill(pi, pi + n, -1);
fill(color, color + n, WHITE);
stack<int> S;//栈
list<vertex>::iterator *pos = new list<vertex>::iterator[n];
for (u = 0; u < n; ++u)
for (s = 0; s < n;++s)
{
if (color[s]==WHITE)//以顶点s为根创建一颗深度优先树
{
color[s] = GRAY;
d[s] = ++time;
S.push(s);
while (!S.empty())
{
u = s = S.top();
list<vertex>::iterator p;
p = pos[u];
//目前程序有些问题，在访问这个末尾迭代器是出错，目前还没有解决
{
v = (*p).index;
if (color[v] == WHITE)
break;
else
++p;//u的邻接点中找尚存在的未发现点
}
pos[u] = p;
{
color[v] = GRAY;
d[v] = ++time;
pi[v] = u;
S.push(v);
}
else//否则完成对u的访问
{
color[u] = BLACK;
f[u] = ++time;
S.pop();
}
}
}
}
delete[]color; delete[]pos;
Parameter3 reParams;
reParams.first = pi;
reParams.second = d;
reParams.third = f;
return reParams;
}
// DFS.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "dfs.h"
#include "Graph.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int s = 1, n = 8;
Parameter3 ret;
double a[] =
{
0, 1, 0, 0, 1, 1,
1, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 1,
0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 0
};
Graph g(a, 6);
ret = dfs(g);
for (int i = 0; i < 6; ++i)
{
if (i != s)
{
cout << i << ";" << "parent " << ret.first[i];
cout << " discover/finish: " << ret.second[i]<<"/"<<ret.third[i] << endl;
}
}
system("pause");
return (EXIT_SUCCESS);
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：图的深度优先搜索算法DFS 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)