深度优先非递归实现算法:

1 递归算法:

//初始化相关数据结构
DFS(G)
-----------------------------------------------------------------------------------
1 for each vertex u ∈ G.V
2 u.color ← WHITE // paint all vertices white; undiscovered
3 u.π ← NIL
4 time ← 0 // global variable, timestamps
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 DFS-VISIT(G,u) DFS-VISIT(G, u)
-----------------------------------------------------------------------------------
1 u.color ← GRAY // grey u; it is discovered
2 time ← time + 1
3 u.d ← time
4 for each v ∈ G.Adj[u] // explore edge (u,v)
5 if v.color == WHITE
6 v.π ← u
7 DFS-VISIT(G,v)
8 u.color ← BLACK // blacken u; it is finished
9 time ← time + 1
10 u.f ← time

#ifndef GCC_LINUX_
#include <stdlib.h>
#include <iostream>
using namespace std;
#endif
#include "Graph.h" enum color{WHITE, GRAY, BLACK};
int colour[MAX_VERTEX_NUM];
int time[MAX_VERTEX_NUM];
int curTime; void DFSRec(LGraph graph, int u){
cout<<"for debug"<<endl;
cout<<"u: "<<u<<endl;
colour[u] = GRAY;
time[u] = ++curTime;
//DFS adj node
adjnode *temp;
temp = graph.vertices[u].firstadj; while(temp){
int v = temp->index;
//for debug
cout<<"for debug"<<endl;
cout<<"v: "<<v<<" line: "<<__LINE__<<endl;
if(colour[v] == WHITE)
DFSRec(graph, v);
else if(colour[v] == GRAY)
cout<<"back edge between "<<u<<" and "<<v<<endl;
else
cout<<"cross edge between "<<u<<" and "<<v<<endl;
temp = temp->adjnext;
}
colour[u] = BLACK;
} void DFSTraverse(LGraph graph){
for(int k = 1; k < graph.vexnum + 1; k++){
if(colour[k] == WHITE) DFSRec(graph, k);
cout<<__LINE__<<" for debug: "<<k<<endl;
}
} int main()
{
LGraph* graph;
graph = CreateGraph(graph);
cout<<graph->edgenum<<endl;
cout<<graph->vexnum<<endl;
print(*graph);
DFSTraverse(*graph);
//print time visit
cout<<"print the visit array:"<<endl;
for(int i = 1; i < graph->vexnum + 1; i++)
cout<<time[i]<<" ";
cout<<endl; int ch;
cout<<"enter integer for terminating:"<<endl;
cin>>ch;
return 0;
}

2 非递归算法

1   for each vertex u ∈ G.V //initialize colour array and time
2 u.color ← WHITE
3 u.π ← NIL
4 time = 0
5 for each vertex u ∈ G.V
6 if u.color = WHITE
7 u.color ← GRAY
8 time ← time + 1
9 u.d ← time
7 push(u, S)
8 while stack S not empty
9 u ← pop(S)
10 for each vertex v ∈ G.Adj[u]
11 if v.color = WHITE
12 v.color = GRAY
13 time ← time + 1
14 v.d ← time
15 v.π ← u
16 push(v, S)
17 u.color ← BLACK
18 time ← time + 1
19 u.f ← time
具体实现代码如下:
栈采用数组实现:
#ifndef GCC_LINUX_
#include <stdlib.h>
#include <iostream>
using namespace std;
#endif
#include "Graph.h" int myStack[MAX_VERTEX_NUM];
int top = 0; //we not use array[0]
bool visited[MAX_VERTEX_NUM];
int timeSec[MAX_VERTEX_NUM];
int curTimeSec; void DFSNoRecur(LGraph graph, int u){
cout<<u;
myStack[top++] = u;
cout<<" top:"<<top<<endl;
adjnode* temp;
//for test
int test = 1;
while(top){
int v = myStack[top - 1];
cout<<test++<<" for test top: "<<top;
cout<<" stack elem: "<<v<<endl;
visited[v] = true;
timeSec[v] = ++curTimeSec;
temp = graph.vertices[v].firstadj;
cout<<"pointer temp: "<<temp<<endl;
top--; //Pop
cout<<top;
while(temp != NULL){
if(visited[temp->index] == false){
myStack[top++] = temp->index;
cout<<test++<<" for test top: "<<top;
cout<<" stack elem: "<<myStack[top - 1]<<endl;
}
else{
cout<<"back edge between "<<temp->index;
cout<<" and "<<v<<endl;
}
temp = temp->adjnext;
}//while
}//while(top)
} void DFSTravNRecur(LGraph graph){
cout<<"No recursive DFS starting"<<endl;
for(int v = 1; v < graph.vexnum + 1; v++)
if(visited[v] == false) DFSNoRecur(graph, v);
cout<<"No recursive DFS terminating"<<endl;
} int main()
{
LGraph* graph;
graph = CreateGraph(graph);
cout<<graph->edgenum<<endl;
cout<<graph->vexnum<<endl;
print(*graph);
DFSTraverse(*graph);
//print time visit
cout<<"print the time array:"<<endl;
for(int i = 1; i < graph->vexnum + 1; i++)
cout<<time[i]<<" ";
cout<<endl; DFSTravNRecur(*graph);
cout<<"print the visit array:"<<endl;
for(int i = 1; i < graph->vexnum + 1; i++)
cout<<time[i]<<" ";
cout<<endl; int ch;
cout<<"enter integer for terminating:"<<endl;
cin>>ch;
return 0;
}

"Graph.h"文件代码定义:

#ifndef GCC_LINUX_
#include <stdlib.h>
#include <iostream>
using namespace std;
#endif
#define MAX_VERTEX_NUM 128 struct adjnode{
int index;
struct adjnode* adjnext;
}; typedef struct vexnode{
struct adjnode* firstadj;
}VertexLink[MAX_VERTEX_NUM]; typedef struct LGraph{
VertexLink vertices;
int vexnum, edgenum; }; void AddEdge(LGraph *graph, int head, int tail){
//new a adj node
adjnode *newadjnode = (adjnode*)malloc(sizeof(struct adjnode));
if(!newadjnode)
exit(EXIT_FAILURE);
newadjnode->index = tail;
newadjnode->adjnext = NULL;
adjnode *temp; temp = graph->vertices[head].firstadj;
cout<<"this edge: "<<head<<"->"<<newadjnode->index<<endl;
newadjnode->adjnext =temp;
graph->vertices[head].firstadj = newadjnode;
//debug
cout<<"line: "<<__LINE__;
cout<<" first adj index: "<<graph->vertices[head].firstadj->index<<endl; } LGraph* CreateGraph(LGraph *graph){
graph = (LGraph*)malloc(sizeof(struct LGraph));
if(!graph)
exit(EXIT_FAILURE);
cout<<"enter the vertex number and edge number:"<<endl;
cin>>graph->vexnum>>graph->edgenum; //initialize the vertexes
for(int v = 0; v < graph->vexnum + 1; v++){
graph->vertices[v].firstadj = NULL;
} //initialize the edge
int i, j;
for(int e = 0; e < graph->edgenum; e++){
cout<<"enter edge of graph:"<<endl;
cin>>i>>j;
AddEdge(graph, i, j);
}
cout<<"graph.vertex number: "<<graph->vexnum<<endl;
return graph;
} void print(LGraph graph){
adjnode *temp = NULL;
for(int i = 1; i < graph.vexnum + 1; i++){
cout<<"vertex: "<<i;
temp = graph.vertices[i].firstadj;
// cout<<"temp"<<temp<<endl;
while(temp){
cout<<" adj vertex: "<<temp->index<<endl;
temp = temp->adjnext;
}
}
cout<<endl;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Non recursive Depth first search的更多相关文章

  1. Recursive Depth first search graph(adj matrix)

    1 深度优先遍历邻接矩阵 1 邻接矩阵初始化 2 访问数组初始化 3 深度优先遍历邻接矩阵图 算法如下: bool MGraph[128][128]; bool visit[128]; int vex ...

  2. [算法&数据结构]深度优先搜索(Depth First Search)

    深度优先 搜索(DFS, Depth First Search) 从一个顶点v出发,首先将v标记为已遍历的顶点,然后选择一个邻接于v的尚未遍历的顶点u,如果u不存在,本次搜素终止.如果u存在,那么从u ...

  3. [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript

    Depth first search is a graph search algorithm that starts at one node and uses recursion to travel ...

  4. 幸运的袋子(深度优先遍历(Depth First Search,DFS))

    题目描述 一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的).如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积. 例如:如果袋子里面的球的号码是{1, 1, 2 ...

  5. 深度优先搜索(Depth First Search)

    Date:2019-07-01 15:31:11 通俗点理解就是不撞南墙不回头的那种,用栈来实现 算法实现 /* 题目描述: 有n件物品,每件物品的重量为w[i],价值为c[i].现在需要选出若干件物 ...

  6. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. android 背景透明度渐变动画

    button.setVisibility(View.VISIBLE); // 背景透明度渐变动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(butto ...

  2. Vim 实用技术,第 2 部分: 常用插件(转)

    http://blog.jobbole.com/20619/ 2.1. gzip(压缩文件支持) 作者:Bram Moolenar 网站脚本编号:无(包含在 Vim 的标准发布之中) 安装说明:无 功 ...

  3. [js - 算法可视化] 汉诺塔(Hanoi)演示程序

    前段时间偶然看到有个日本人很早之前写了js的多种排序程序,使用js+html实现的排序动画,效果非常好. 受此启发,我决定写几个js的算法动画,第一个就用汉诺塔. 演示地址:http://tut.ap ...

  4. Codeforces 701C They Are Everywhere(Two pointers+STL)

    [题目链接] http://codeforces.com/problemset/problem/701/C [题目大意] 给出 一个字符串,里面包含一定种类的字符,求出一个最短的子串,使得其包含该字符 ...

  5. sonix uvc驱动的加入 RT5350支持H264

    依据sonix提供的驱动,须要在内核下进行配置,以加入到内核或与模块的方式进行编译: 1.makefile中加入驱动的文件夹,尽量保持和原有的一致, obj-$(CONFIG_USB_SN9C102) ...

  6. OOP中的多态

    尽管一直在说OOP,但说实话还不是真正的理解,面向对象的三个基本特性继承.封装.多态,前两个性质曾经 有接触听的比較多还好理解,以下主要介绍一下第三个特性--多态. 1. 定义     同一操作作用于 ...

  7. HTTP请求和数据安全

    /*------------------------------------- 01 HTTP请求 ---------------------------------------*/ 重点:1.超文本 ...

  8. Python学习笔记 (4) :迭代器、生成器、装饰器、递归、正则表达式等

    迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优点是 ...

  9. 2_Cat Years

    2 // // ViewController.swift // Cat Years // // Created by ZC on 16/1/6. // Copyright © 2016年 ZC. Al ...

  10. perl5 第十一章 文件系统

    第十一章  文件系统 by flamephoenix 一.文件输入/输出函数  1.基本I/O函数    1)open函数    2)用open重定向输入    3)文件重定向    4)指定读写权限 ...