添加DFS函数:

 #ifndef GRAPH_H
#define GRAPH_H #include "Object.h"
#include "SharedPointer.h"
#include "Array.h"
#include "DynamicArray.h"
#include "LinkQueue.h"
#include "LinkStack.h" namespace DTLib
{ template < typename E >
struct Edge : public Object
{
int b;
int e;
E data; Edge(int i=-, int j=-)
{
b = i;
e = j;
} Edge(int i, int j, const E& value)
{
b = i;
e = j;
data = value;
} bool operator == (const Edge<E>& obj)
{
return (b == obj.b) && (e == obj.e); //在这里不关注权值大小
} bool operator != (const Edge<E>& obj)
{
return !(*this == obj);
}
}; template < typename V, typename E >
class Graph : public Object
{
protected:
template < typename T >
DynamicArray<T>* toArray(LinkQueue<T>& queue)
{
DynamicArray<T>* ret = new DynamicArray<T>(queue.length()); if( ret != NULL )
{
for(int i=; i<ret->length(); i++, queue.remove())
{
ret->set(i, queue.front());
}
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create ret object...");
} return ret;
}
public:
virtual V getVertex(int i) = ;
virtual bool getVertex(int i, V& value) = ;
virtual bool setVertex(int i, const V& value) = ;
virtual SharedPointer< Array<int> > getAdjacent(int i) = ;
virtual E getEdge(int i, int j) = ;
virtual bool getEdge(int i, int j, E& value) = ;
virtual bool setEdge(int i, int j, const E& value) = ;
virtual bool removeEdge(int i, int j) = ;
virtual int vCount() = ;
virtual int eCount() = ;
virtual int OD(int i) = ;
virtual int ID(int i) = ;
virtual int TD(int i)
{
return ID(i) + OD(i);
} SharedPointer< Array<int> > BFS(int i)
{
DynamicArray<int>* ret = NULL; if( ( <= i) && (i < vCount()) )
{
LinkQueue<int> q;
LinkQueue<int> r;
DynamicArray<bool> visited(vCount()); for(int i=; i<visited.length(); i++)
{
visited[i] = false;
} q.add(i); while( q.length() > )
{
int v = q.front(); q.remove(); if( !visited[v] )
{
SharedPointer< Array<int> > aj = getAdjacent(v); for(int j=; j<aj->length(); j++)
{
q.add((*aj)[j]);
} r.add(v); visited[v] = true;
}
} ret = toArray(r);
}
else
{
THROW_EXCEPTION(InvalidParameterException, "Index i is invalid...");
} return ret;
} SharedPointer< Array<int> > DFS(int i)
{
DynamicArray<int>* ret = NULL; if( ( <= i) && (i < vCount()) )
{
LinkStack<int> s;
LinkQueue<int> r;
DynamicArray<bool> visited(vCount()); for(int j=; j<visited.length(); j++)
{
visited[j] = false;
} s.push(i); while( s.size() > )
{
int v = s.top(); s.pop(); if( !visited[v] )
{
SharedPointer< Array<int> > aj = getAdjacent(v); for(int j=aj->length() - ; j>=; j--)
{
s.push((*aj)[j]);
} r.add(v); visited[v] = true;
}
} ret = toArray(r);
}
else
{
THROW_EXCEPTION(InvalidParameterException, "Index i is invalid...");
} return ret;
}
}; } #endif // GRAPH_H

测试程序如下:

 #include <iostream>
#include "BTreeNode.h"
#include "ListGraph.h"
#include "MatrixGraph.h" using namespace std;
using namespace DTLib; int main()
{
MatrixGraph<, char, int> g;
const char* VD = "ABEDCGFHI"; for(int i=; i<; i++)
{
g.setVertex(, VD[i]);
} g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); SharedPointer< Array<int> > sa = g.DFS(); for(int i=; i<sa->length(); i++)
{
cout << (*sa)[i] << " ";
} cout << endl; return ;
}

结果如下:

深度优先的思想就是二叉树的先序遍历思想。

递归版的深入优先算法如下:

 #include <iostream>
#include "BTreeNode.h"
#include "ListGraph.h"
#include "MatrixGraph.h" using namespace std;
using namespace DTLib; template < typename V, typename E>
void DFS(Graph<V, E>& g, int v, Array<bool>& visited)
{
if( ( <= v) && (v < g.vCount()) )
{
cout << v << endl; visited[v] = true; SharedPointer< Array<int> > aj = g.getAdjacent(v); for(int i=; i<aj->length(); i++)
{
if( !visited[(*aj)[i]] )
{
DFS(g, (*aj)[i], visited);
}
}
}
else
{
THROW_EXCEPTION(InvalidParameterException, "Index v is invalid...");
}
} template < typename V, typename E >
void DFS(Graph<V, E>& g, int v)
{
DynamicArray<bool> visited(g.vCount()); for(int i=; i<visited.length(); i++)
{
visited[i] = false;
} DFS(g, v, visited);
} int main()
{
MatrixGraph<, char, int> g;
const char* VD = "ABEDCGFHI"; for(int i=; i<; i++)
{
g.setVertex(, VD[i]);
} g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); g.setEdge(, , );
g.setEdge(, , ); SharedPointer< Array<int> > sa = g.DFS(); for(int i=; i<sa->length(); i++)
{
cout << (*sa)[i] << " ";
} cout << endl; DFS(g, ); return ;
}

结果如下:

小结:

第七十五课 图的遍历(DFS)的更多相关文章

  1. 第七十四课 图的遍历(BFS)

    广度优先相当于对顶点进行分层,层次遍历. 在Graph.h中添加BFS函数: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" ...

  2. NeHe OpenGL教程 第四十五课:顶点缓存

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. NeHe OpenGL教程 第三十五课:播放AVI

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. NeHe OpenGL教程 第十五课:纹理图形字

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. 第三百七十五节,Django+Xadmin打造上线标准的在线教育平台—创建课程机构app,在models.py文件生成3张表,城市表、课程机构表、讲师表

    第三百七十五节,Django+Xadmin打造上线标准的在线教育平台—创建课程机构app,在models.py文件生成3张表,城市表.课程机构表.讲师表 创建名称为app_organization的课 ...

  6. NeHe OpenGL教程 第二十五课:变形

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  7. PMP十五至尊图(第六版)

    PMP(Project Management Professinoal)项目经理专业资格认证,由美国项目管理学会PMI(Project Management Institute)发起并组织的一种资格认 ...

  8. “全栈2019”Java第七十五章:内部类持有外部类对象

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  9. 孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5

    孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...

随机推荐

  1. 使用iText快速更新书签

    一.介绍 pdfbox基于Apache协议,商用无需开放源代码. iText基于APGL协议,打包和修改需发布源码,除非花钱买断. 二.用途 下载的电子书,有的书签是FitHeight,也就是缩放后整 ...

  2. Spring注解之@Lazy注解

    @Lazy用于指定该Bean是否取消预初始化.主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为, 使用该Annotation时可以指定一个boolean型的value属性,该属性 ...

  3. <Closing connections idle longer than 60000 MILLISECONDS> <Closing expired connections>

    日志信息如下: 2017-07-05 18:28:34 -18705 [idle_connection_reaper] DEBUG   - Closing expired connections 20 ...

  4. RockerMQ消息消费、重试

    消息中间件—RocketMQ消息消费(一) 消息中间件—RocketMQ消息消费(二)(push模式实现) 消息中间件—RocketMQ消息消费(三)(消息消费重试) MQ中Pull和Push的两种消 ...

  5. linux常用文本编缉命令(strings/sed/awk/cut)

    一.strings strings--读出文件中的所有字符串 二.sed--文本编缉 类型 命令 命令说明 字符串替换 sed -i 's/str_reg/str_rep/' filename 将文件 ...

  6. 运维相关 docker

  7. C++ 保留有效小数 保留有效数字

    1.需要头文件 #include <iomanip> 2. 要保留两位有效小数 cout<<setiosflags(ios::fixed)<<setprecisio ...

  8. 逆袭之旅DAY14.东软实训.Oracle.多表连接、分组函数、子查询

    2018-07-10 08:29:55 思考应用场景 异常数据的测试 6.显示能挣得奖金的雇员的姓名.工资.奖金,并以工资和奖金降序排列.select ename,sal,commfrom empWH ...

  9. jquery select使用

    <select class="selector"></select> 1) 设置选中值为pxx的选项 $('.selector').val('pxx'); ...

  10. Java使用POI插件将数据以excel形式备份

    将数据以表格形式进行备份 (1)导入poi的jar包 放入lib下:  WebRoot\WEB-INF\lib\poi-3.2-FINAL-20081019.jar 下载链接:https://gith ...