• C#,哈密顿环问题(Hamiltonian Cycle problem)的算法与源程序


     

    哈密顿圈问题

    无向图中的哈密顿路径是一条恰好访问每个顶点一次的路径。哈密顿循环(或哈密顿回路)是一条哈密顿路径,从哈密顿路径的最后一个顶点到第一个顶点有一条边(在图中)。确定给定的图是否包含哈密顿圈。如果包含,则打印路径。以下是所需功能的输入和输出。

    输入:

    二维数组图[V][V],其中V是图中的顶点数,图[V][V]是图的邻接矩阵表示。如果从i到j有一条直边,则值图[i][j]为1,否则图[i][j]为0。

    在无向图中,哈密顿路径是一条恰好访问每个顶点一次的路径,哈密顿圈或回路是一条哈密顿路径,即从最后一个顶点到第一个顶点有一条边。

    在这个问题中,我们将尝试确定一个图是否包含哈密顿圈。当存在哈密顿循环时,也打印该循环。

    算法思路

    给定一个图G=(V,E),我们必须使用回溯方法找到哈密顿回路。我们从任意顶点开始搜索,比如“a”。该顶点“a”成为隐式树的根。部分解的第一个元素是要构造的哈密顿循环的第一个中间顶点。按字母顺序选择下一个相邻顶点。如果在任何阶段,任何任意顶点与顶点“a”以外的任何顶点进行循环,则我们称达到了死端。在这种情况下,我们回溯一步,然后再次开始搜索,选择另一个顶点并从部分回溯元素;必须移除溶液。如果得到一个哈密顿循环,则使用回溯的搜索是成功的。

    From backtracking, the vertex adjacent to 'e' is b, c, d, and f from which vertex 'f' has already been checked, and b, c, d have already visited. So, again we backtrack one step. Now, the vertex adjacent to d are e, f from which e has already been checked, and adjacent of 'f' are d and e. If 'e' vertex, revisited them we get a dead state. So again we backtrack one step.

    Now, adjacent to c is 'e' and adjacent to 'e' is 'f' and adjacent to 'f' is 'd' and adjacent to 'd' is 'a.' Here, we get the Hamiltonian Cycle as all the vertex other than the start vertex 'a' is visited only once. (a - b - c - e - f -d - a).

    参考:

    C#,图(Graph)的数据结构设计与源代码https://blog.csdn.net/beijinghorn/article/details/125133711

    源代码(POWER BY TRUFFER):

    1. using System;
    2. using System.Text;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. namespace Legalsoft.Truffer.Algorithm
    6. {
    7. public partial class Graph
    8. {
    9. private bool IsSafe(int v, int[] path, int pos)
    10. {
    11. if (Matrix[path[pos - 1], v] == 0)
    12. {
    13. return false;
    14. }
    15. for (int i = 0; i < pos; i++)
    16. {
    17. if (path[i] == v)
    18. {
    19. return false;
    20. }
    21. }
    22. return true;
    23. }
    24. private bool Hamiltonian_Cycle_Utility(ref int[] path, int pos)
    25. {
    26. if (pos == Node_Number)
    27. {
    28. if (Matrix[path[pos - 1], path[0]] == 1)
    29. {
    30. return true;
    31. }
    32. else
    33. {
    34. return false;
    35. }
    36. }
    37. for (int v = 1; v < Node_Number; v++)
    38. {
    39. if (IsSafe(v, path, pos))
    40. {
    41. path[pos] = v;
    42. if (Hamiltonian_Cycle_Utility(ref path, pos + 1) == true)
    43. {
    44. return true;
    45. }
    46. path[pos] = -1;
    47. }
    48. }
    49. return false;
    50. }
    51. public bool Hamiltonian_Cycle(out int[] path)
    52. {
    53. path = new int[Node_Number];
    54. for (int i = 0; i < Node_Number; i++)
    55. {
    56. path[i] = -1;
    57. }
    58. path[0] = 0;
    59. if (Hamiltonian_Cycle_Utility(ref path, 1) == false)
    60. {
    61. return false;
    62. }
    63. return true;
    64. }
    65. }
    66. public static partial class GraphDrives
    67. {
    68. private static string PrintPath(int[] path)
    69. {
    70. StringBuilder sb = new StringBuilder();
    71. sb.AppendLine("Solution Exists: Following is one Hamiltonian Cycle<br>");
    72. for (int i = 0; i < path.Length; i++)
    73. {
    74. sb.Append(path[i] + " -> ");
    75. }
    76. sb.Append(path[0] + " ");
    77. return sb.ToString();
    78. }
    79. public static string HamiltonianCycle()
    80. {
    81. StringBuilder sb = new StringBuilder();
    82. int[,] graph1 = {
    83. { 0, 1, 0, 1, 0 },
    84. { 1, 0, 1, 1, 1 },
    85. { 0, 1, 0, 0, 1 },
    86. { 1, 1, 0, 0, 1 },
    87. { 0, 1, 1, 1, 0 }
    88. };
    89. Graph g1 = new Graph(graph1);
    90. if (g1.Hamiltonian_Cycle(out int[] path1))
    91. {
    92. sb.AppendLine(PrintPath(path1));
    93. }
    94. else
    95. {
    96. sb.AppendLine("Solution does not exist");
    97. }
    98. int[,] graph2 = {
    99. { 0, 1, 0, 1, 0 },
    100. { 1, 0, 1, 1, 1 },
    101. { 0, 1, 0, 0, 1 },
    102. { 1, 1, 0, 0, 0 },
    103. { 0, 1, 1, 0, 0 }
    104. };
    105. Graph g2 = new Graph(graph2);
    106. if (g2.Hamiltonian_Cycle(out int[] path2))
    107. {
    108. sb.AppendLine(PrintPath(path2));
    109. }
    110. else
    111. {
    112. sb.AppendLine("Solution does not exist");
    113. }
    114. return sb.ToString();
    115. }
    116. }
    117. }

  • 相关阅读:
    如何理解Spring中的IOC
    PHP毕业设计毕设辅导课(1):PHP 基础语法
    1.3数据结构之复杂度 力扣题目移除元素
    高级词汇和句子(二)-day15
    集合深度学习10—同步类容器
    PyTorch实战:实现Cifar10彩色图片分类
    【MATLAB源码-第49期】基于蚁群算法(ACO)算法的栅格路径规划,输出最佳路径图和算法收敛曲线图。
    MySQL最详DDL和DML操作
    Mybatis04关联关系映射
    毕业设计python选题高校研究生管理系统java选题版本源码 调试 开题 lw
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/125341055