哈密顿循环是哈密顿路径,因此从哈密顿路径的最后一个顶点到第一个顶点有一条边(在图中)。它在无向图中是一条路径,该路径恰好访问该图的每个顶点一次。

功能和目的:Begin

1.function isSafe() is used to check for whether it is adjacent to the previously added vertex and already not added.

2. function hamiltonianCycle() solves the hamiltonian problem.

3. function hamCycle() uses hamiltonianCycle() to solve the hamiltonian problem.

It returns false if there is no Hamiltonian Cycle possible, otherwise return true and prints the path.

End

示例#include

#include

#include

#define N 5

using namespace std;

void displaytheSolution(int path[]);

bool isSafe(int n, bool g[N][N], int path[], int pos) {

if (g [path[pos-1]][n] == 0)

return false;

for (int i = 0; i

if (path[i] == n)

return false;

return true;

}

bool hamiltonianCycle(bool g[N][N], int path[], int pos) {

//如果所有顶点都包含在哈密顿循环中

if (pos == N) {

if (g[ path[pos-1] ][ path[0] ] == 1)

return true;

else

return false;

}

for (int n = 1; n

if (isSafe(n, g, path, pos)) //Check if this vertex can be added to Hamiltonian Cycle

{

path[pos] = n;

//重复构建其余路径

if (hamiltonianCycle (g, path, pos+1) == true)

return true;

path[pos] = -1; //remove vertex if it doesn’t lead to the solution

}

}

return false;

}

bool hamCycle(bool g[N][N]) {

int *path = new int[N];

for (int i = 0; i

path[i] = -1;

//将顶点0作为路径中的第一个顶点。

If there is a Hamiltonian Cycle, then the path can be started from any point

//图是无向的循环周期

path[0] = 0;

if (hamiltonianCycle(g, path, 1) == false) {

cout<

return false;

}

displaytheSolution(path);

return true;

}

void displaytheSolution(int p[]) {

cout<

cout<

for (int i = 0; i

cout<

cout<

}

int main() {

bool g[N][N] = {

{0, 1, 0, 1, 1},

{0, 0, 1, 1, 0},

{0, 1, 0, 1, 1},

{1, 1, 1, 0, 1},

{0, 1, 1, 0, 0},

};

hamCycle(g);

return 0;

}

输出结果存在周期: Following is one Hamiltonian Cycle

0 4 1 2 3 0

c语言哈密顿路径算法,用于检查给定图中是否存在哈密顿循环或路径的C ++程序...相关推荐

  1. node/js 漏洞_6个可用于检查Node.js中漏洞的工具

    node/js 漏洞 Vulnerabilities can exist in all products. The larger your software grows, the greater th ...

  2. R语言使用pROC包在同一图中绘制两条ROC曲线并通过假设检验检验ROC曲线的AUC或者偏AUC的差异(输出p值)

    R语言使用pROC包在同一图中绘制两条ROC曲线并通过假设检验检验ROC曲线的AUC或者偏AUC的差异(输出p值) 目录

  3. matlab两点之间的所有路径,引用 在图中搜索两点间的所有路径matlab编程

    引用 在图中搜索两点间的所有路径matlab编程 2018-09-18 function possiablePaths = findPath(Graph, partialPath, destinati ...

  4. C语言search函数的作用,C语言lsearch()函数:用于在给定的区域内从头到尾进行线性搜索...

    函数名:lsearch 头文件: 函数原型: void* lsearch(void* key,void* district,size_t *n,size_t m, int (*func)(const ...

  5. 检查给定数字是否为斐波那契数的Python程序

    Given a number and we have to check whether it is a Fibonacci number or not in Python? 给定一个数字,我们必须检查 ...

  6. 获得有向无环图中起点到终点的所有路径_力扣1514——概率最大的路径

    本题主要和图的遍历求解最短路径相关,可以用 Dijkstra 或者 Bellman-Ford 算法进行解决. 原题 给你一个由 n 个节点(下标从 0 开始)组成的无向加权图,该图由一个描述边的列表组 ...

  7. c语言遍历算法的头文件,图优先遍历算法(C语言版).doc

    图优先遍历算法(C语言版) 众炼向饭桨泞奉源柿虐萧宰徽强药邻摘甭膜酣猖椅支习洋瞪较效笋盏厚婪跳博险僳乘措笆却问谬闸皇机兽偿谐芹违邹竞芬襟竣备烘令救汇邵叙鹰扭肾钙苏辅捕先是埠郧苛三驯溅烂右井准刮修柒拿苇 ...

  8. t-sne算法用于处理词嵌入中的高维降维问题

    Chrispher 关于 留言板 时间轴 笔记专题 课程图谱 t-SNE完整笔记 2017年02月05日  机器学习  机器学习 字数:26531 t-SNE(t-distributed stocha ...

  9. c语言经典算法——查找一个整数数组中第二大数

    https://www.cnblogs.com/dootoo/p/4473958.html 题目: 实现一个函数,查找一个整数数组中第二大数. 算法思想: 设置两个变量max1和max2,用来保存最大 ...

最新文章

  1. 《任正非:我若贪生怕死,何来让你们英勇奋斗》
  2. 『Python』VS2015编译源码注意事项
  3. WebNumericEdit
  4. matlab求偏微分方程程序,用MATLAB解偏微分方程.pdf
  5. Java深入 - servlet和Spring的DispatcherServlet详解
  6. 使用springboot + druid + mybatisplus完成多数据源配置
  7. [Advance] How to debug a program (下):示例
  8. mysql选课时间冲突_选课常见问题解答
  9. 飞秒激光制备量子计算机,飞秒激光直写光量子逻辑门.PDF
  10. 答疑解惑:软件定义存储常见问题
  11. 一篇文章带你深入了解Dart语言
  12. android sqlite英文文献,SQLite数据库外文文献翻译.doc
  13. 【论文学习】Cooling-Shrinking Attack: Blinding the Tracker with Imperceptible Noises论文学习
  14. 2022年centos下载及Linux安装
  15. Transition过渡动画
  16. 双网卡 跃点_有关win7 pppoe拨号网络的跃点数及win7 多路internet访问带宽叠加 - Microsoft Community...
  17. 【FFMPEG】I,P,B帧和PTS,DTS时间戳的关系
  18. ICMP有哪些消息类型?常见的ICMP报文有哪些?
  19. 【新闻演讲】去IOE之O:运营商能否照搬阿里去O?
  20. advancedeast认识

热门文章

  1. IdentityServer4-从数据库获取User登录并对Claims授权验证(五)
  2. 使用 dotnet core 和 Azure PaaS服务进行devOps开发(Web API 实例)
  3. [翻译]在 .NET Core 中的并发编程
  4. asp.net core mvc剖析:KestrelServer
  5. Cactus在jexus上安装
  6. 从Unity3D编译器升级聊起Mono
  7. rest_framework03:查询所有/新增数据/删除一个数据/封装Response/序列化与反序列化
  8. win10系统按esc会弹出计算机,win10系统版本2004控制面板多出ESC是什么原因?
  9. [转]Android 常见安全漏洞修复理论与实践
  10. 电脑QQ能登上,网页打不开的解决办法