添加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. Spring Cloud系列之客户端请求带“Authorization”请求头,经过zuul转发后丢失了

    先摆解决方案: 方法一: 方法二: zuul.routes.<routeName>.sensitive-headers= zuul.routes.<routeName>.cus ...

  2. noip2013转圈游戏

    题目描述 n个小伙伴(编号从 0到 n−1)围坐一圈玩游戏.按照顺时针方向给 n个位置编号,从0 到 n−1.最初,第 0号小伙伴在第 0号位置,第 1号小伙伴在第 1 号位置,……,依此类推. 游戏 ...

  3. [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 l ...

  4. MySQL中的文件

    查看数据目录: select @@datadir; 共享表空间: ibdata1 Redo log file:ib_logfile0, ib_logfile1 二进制日志:需要配置参数 server- ...

  5. telnet强制中断登录

    在telnet登录的时候,有时我们只是想测试某个账号密码是否正确 但是telnet不像ssh一样密码试错之后可以使用Ctrl+c强制中断,使如果要输错三次五次才给退出中断交互那是十分浪费时间和心情的 ...

  6. Windows系统目录解释

    目录 说明 C:\Program Files 64位程序安装目录 C:\Program Files (x86) 32位程序安装目录 C:\Windows 操作系统主要目录 C:\Windows\Sys ...

  7. Java字符串拼接效率测试

    测试代码: public class StringJoinTest { public static void main(String[] args) { int count = 10000; long ...

  8. 常用的flex知识 ,比起float position 好用不少

      flex布局具有便捷.灵活的特点,熟练的运用flex布局能解决大部分布局问题,这里对一些常用布局场景做一些总结. web页面布局(topbar + main + footbar) 示例代码   要 ...

  9. linux下top命令参数详解

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. 内存信息.内容如下: Mem: 191272k to ...

  10. python-递归,二分查找

    # print(list("胡辣汤")) # lst = ["河南话", "四川话", "东北", "山东&q ...