深度优先与广度优先算法
2017-02-23 21:53
127 查看
图的遍历有深度优先和广度优先算法。
深度优先遍历可描述为一个递归算法。当到达顶点v时,具体操作是:
①访问(v);
②for(与v相邻的每个顶点w)
遍历(w);
//深度优先算法
template<int max_size>
void Diagraph<max_size>::depth_first(void(*visit)(Vertex &)) const {
bool visited[max_size]; //引入数组防止无限循环
Vertex v;
for (all v in G) visited[v] = false;
for (all v in G) {
if (!visited[v]) {
traverse(v, visited, visit);
}
}
}
template<int max_size>
void Diagraph<max_size>::traverse(Vertex &v, bool visited[],
void (*visit)(Vertex &)) const {
Vertex w;
visited[v] = true;
(*visit)(v);
for (all w adjacent to v) {
if (!visited[w]) {
traverse(w, visited, visit);
}
}
}
广度优先算法借助队列,当访问v后, 将v相邻的仍未访问过的顶点加到队列后面,然后访问队列头:
//广度优先算法
template<int max_size>
void Diagraph<max_size>::breadth_first(void (*visit)(Vertex &)) const {
Queue q;
bool visited[max_size];
Vertex v, w, x;
for (all v in G) visited[v] = false;
for (all v in G) {
if (!visited[v]) {
q.append(v);
while (!q.empty()) {
q.retrieve(w);
if (!visited[w]) {
visited[w] = true;
(*visit) (w);
for (all x adjacent to w) {
q.append(x);
}
}
q.serve();
}
}
}
}
可以使用深度优先遍历和广度优先遍历确定拓扑次序。
深度优先遍历: 时间复杂度O(n+e)(n为图的定点数,e为图的边数)。
//深度优先算法
template<int graph_size>
void Diagraph<graph_size>:: depth_sort(List<Vertex>&topological_order) {
bool visited[graph_size];
Vertex v;
for (v = 0; v < count; v++) visited[v] = false;
topological_order.clear();
for (v = 0; v < count; v++) {
if (!visited[v]) {
recursive_depth_sort(v, visited, topological_order);
}
}
}
template<int max_size>
void Diagraph<max_size>::recursive_depth_sort(Vertex v, bool *visited, List<Vertex>& topological_order) {
visited[v] = true;
int degree = neighbors[v].size();
for (int i = 0; i < degree; i++) {
Vertex w;
neighbors[v].retrieve(i, w);
if (!visited[w]) {
recursive_depth_sort(w, visited, topological_order);
}
}
topological_order.insert(0, v);
}
深度优先遍历可描述为一个递归算法。当到达顶点v时,具体操作是:
①访问(v);
②for(与v相邻的每个顶点w)
遍历(w);
//深度优先算法
template<int max_size>
void Diagraph<max_size>::depth_first(void(*visit)(Vertex &)) const {
bool visited[max_size]; //引入数组防止无限循环
Vertex v;
for (all v in G) visited[v] = false;
for (all v in G) {
if (!visited[v]) {
traverse(v, visited, visit);
}
}
}
template<int max_size>
void Diagraph<max_size>::traverse(Vertex &v, bool visited[],
void (*visit)(Vertex &)) const {
Vertex w;
visited[v] = true;
(*visit)(v);
for (all w adjacent to v) {
if (!visited[w]) {
traverse(w, visited, visit);
}
}
}
广度优先算法借助队列,当访问v后, 将v相邻的仍未访问过的顶点加到队列后面,然后访问队列头:
//广度优先算法
template<int max_size>
void Diagraph<max_size>::breadth_first(void (*visit)(Vertex &)) const {
Queue q;
bool visited[max_size];
Vertex v, w, x;
for (all v in G) visited[v] = false;
for (all v in G) {
if (!visited[v]) {
q.append(v);
while (!q.empty()) {
q.retrieve(w);
if (!visited[w]) {
visited[w] = true;
(*visit) (w);
for (all x adjacent to w) {
q.append(x);
}
}
q.serve();
}
}
}
}
可以使用深度优先遍历和广度优先遍历确定拓扑次序。
深度优先遍历: 时间复杂度O(n+e)(n为图的定点数,e为图的边数)。
//深度优先算法
template<int graph_size>
void Diagraph<graph_size>:: depth_sort(List<Vertex>&topological_order) {
bool visited[graph_size];
Vertex v;
for (v = 0; v < count; v++) visited[v] = false;
topological_order.clear();
for (v = 0; v < count; v++) {
if (!visited[v]) {
recursive_depth_sort(v, visited, topological_order);
}
}
}
template<int max_size>
void Diagraph<max_size>::recursive_depth_sort(Vertex v, bool *visited, List<Vertex>& topological_order) {
visited[v] = true;
int degree = neighbors[v].size();
for (int i = 0; i < degree; i++) {
Vertex w;
neighbors[v].retrieve(i, w);
if (!visited[w]) {
recursive_depth_sort(w, visited, topological_order);
}
}
topological_order.insert(0, v);
}
相关文章推荐
- 《算法4》图&深度优先与广度优先算法
- 二叉树的深度优先与广度优先算法
- 有向图深度优先与广度优先算法的C++实现
- 数据结构之深度优先与广度优先算法
- Python学习笔记8-类的继承 、深度优先、广度优先
- 文件搜索 深度优先 广度优先
- 图的遍历 (深度优先 广度优先)
- 无向图的邻接矩阵,深度优先遍历和广度优先遍历的递归与非递归算法
- 算法与数据结构实验5:图的深度和广度优先遍历(邻接矩阵)
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
- 邻接表 深度优先遍历 广度优先遍历
- C#一颗简单多叉树的实现(原理、广度优先遍历、深度优先遍历)
- 图的理解:深度优先和广度优先遍历
- 关于深度优先和广度优先的问题
- 蜘蛛抓取策略:广度优先和深度优先分析
- 深度优先和广度优先遍历及其 Java 实现
- C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)
- 数的广度优先和深度优先
- 邻接表深度优先遍历和广度遍历
- 深度优先与广度优先