天道酬勤,学无止境

hamiltonian-cycle

Complete Weighted Graph and Hamiltonian Tour

I ran into a question on a midterm exam. Can anyone clarify the answer? Problem A: Given a Complete Weighted Graph G, find a Hamiltonian Tour with minimum weight. Problem B: Given a Complete Weighted Graph G and Real Number R, does G have a Hamiltonian Tour with weight at most R? Suppose there is a machine that solves B. How many times can we call B (each time G and Real number R are given),to solve problem A with that machine? Suppose the sum of Edges in G up to M. 1) We cannot do this, because there is uncountable state. 2) O(|E|) times 3) O(lg m) times 4) because A is NP-Hard, This is

2021-06-14 20:30:19    分类:问答    algorithm   graph   computation-theory   np   hamiltonian-cycle

Find shortest path from X,Y coordinates (with start ≠ end)

I have a dataframe with X and Y coordinates of points like this: structure(list(X = c(666L, 779L, 176L, 272L, 232L, 74L, 928L, 667L, 1126L, 919L), Y = c(807, 518, 724, 221, 182, 807, 604, 384, 142, 728)), .Names = c("X", "Y"), row.names = c(NA, 10L), class = "data.frame") I just want to find out the shortest path connecting all these points, and also return its total distance. There are no other conditions: every point can be connected to any other, no specific point to start or end, no weights, etc... I found lots of topics about igraph package but i can't figure out how to feed my data into

2021-06-11 00:36:06    分类:问答    r   igraph   traveling-salesman   hamiltonian-cycle

在 DAG 中寻找汉密尔顿路径的算法(Algorithm for finding a Hamilton Path in a DAG)

问题 我指的是 Skienna 的算法书。 测试图G是否包含Hamiltonian path是NP-hard ,其中哈密顿路径P是只访问每个顶点一次的路径。 与哈密顿循环问题不同,G 中从结束顶点到 P 的起始顶点不一定有边。 给定一个有向无环图 G ( DAG ),给出一个O(n + m)时间算法来测试它是否包含哈密顿路径。 我的做法, 我打算使用DFS和Topological sorting 。 但我不知道如何在解决问题时将这两个概念联系起来。 如何使用拓扑排序来确定解决方案。 有什么建议么? 回答1 您可以首先在 O(n+m) 中对 DAG 进行拓扑排序(每个 DAG 都可以进行拓扑排序)。 完成此操作后,您就会知道边从较低的索引顶点到较高的索引顶点。 这意味着存在一条哈密顿路径当且仅当连续顶点之间存在边,例如 (1,2), (2,3), ..., (n-1,n). (这是因为在汉密尔顿路径中,您不能“返回”,而必须访问所有路径,因此唯一的方法是“不跳过”) 您可以在 O(n) 中检查此条件。 因此,整体复杂度为 O(m+n)。

2021-06-10 03:15:11    分类:技术分享    algorithm   graph-algorithm   directed-acyclic-graphs   hamiltonian-cycle

Algorithm to find a random Hamiltonian path in a grid?

I'm looking for an efficient algorithm that is able to find an as random as possible Hamiltonian path in a bidirectional N*M grid. Does anyone know where I can find, or how to go about constructing such an algorithm? I've already found an efficient approach (see image below). The end result here is a Hamiltonian cycle. Removing a random edge will make it a Hamiltonian path. This algorithm is efficient, but does not provide enough randomness. This approach will always have the begin and end point of the path right next to each other, while I'd like to have those in random locations. Space

2021-05-31 12:20:26    分类:问答    algorithm   graph-algorithm   hamiltonian-cycle

在图中找到哈密顿循环的动态编程算法是什么?(What is the dynamic programming algorithm for finding a Hamiltonian cycle in a graph?)

问题 在无向图中寻找哈密顿循环的动态编程算法是什么? 我在某处看到存在一种算法,该算法的时间复杂度为O(n.2^n) 。 回答1 确实有一个O(n2 n )动态编程算法可以找到哈密顿循环。 这个想法可以减少许多O(n!)回溯到O(n 2 2 n )或O(n2 n )(以使用更多内存为代价)的一般想法,它是考虑设置子问题具有指定的“端点” 。 在这里,由于需要一个循环,因此可以从任何顶点开始。 因此修复一个,将其命名为x 。 子问题是:“对于一个给定的S和一个顶点v的S ,有没有开始在路径x和通过的所有顶点去S ,在结束v ?” poss[S][v] 。 与大多数动态编程问题一样,一旦定义了子问题,其他问题就显而易见了:以任何“递增”顺序循环遍历所有2 n个顶点集S,并且对于每个这样的S中的每个v,您都可以计算poss[S][v]为: poss [S] [v] =(S中存在一些u ,使得poss [S- {v}] [u]为True且边u->v存在) 最后,如果有一个顶点v使得存在边v->x且poss[S][v]为True,则存在一个哈密顿循环,其中S是所有顶点的集合(除x ,取决于您如何定义它)。 如果您想要实际的哈密顿周期而不是仅仅确定一个周期是否存在,则使poss[S][v]存储使之成为可能的实际u而不是True或False; 这样,您可以追溯到最后的路径。 回答2

2021-05-17 23:24:47    分类:技术分享    algorithm   graph   cycle   dynamic-programming   hamiltonian-cycle

骑士的巡回路线实现选择步长数组(Knight's tour backtrack implementation choosing the step array)

问题 因此,我想出了此实现方案,用于解决8 * 8棋盘的骑士巡回赛。 但是似乎要花很长时间才能运行(以至于我不得不停止它)。 但是,如果我用注释中写的那个替换dx,dy数组,程序将像魔术一样工作并给出输出。 他们说,它是明智地选择阵列的方法,因此蛮力算法很幸运能够迅速找到解决方案。 但是首先如何提出这个数组,我是从其他代码中得到的这个数组(dx,dy)。 因此,谁能解释我为什么此代码为何与这些数组(在注释中)一起使用,而不是与我的一起使用。 #include <cstdio> using namespace std; int dx[8] = { 1, 1, 2, 2, -1, -1, -2, -2}; int dy[8] = { 2, -2, 1, -1, 2, -2, 1, -1}; //int dx[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; //int dy[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; bool solve(bool arr[8][8],int x,int y,int moves){ if(moves==0)return true; if(x<0 || y<0 || x>7 || y>7 || arr[x][y])return false; arr[x][y]=1; for(int i=0;i<8;i

2021-04-30 08:22:00    分类:技术分享    c++   algorithm   backtracking   hamiltonian-cycle

What is the dynamic programming algorithm for finding a Hamiltonian cycle in a graph?

What is dynamic programming algorithm for finding a Hamiltonian cycle in an undirected graph? I have seen somewhere that there exists an algorithm with O(n.2^n) time complexity.

2021-04-22 19:20:46    分类:问答    algorithm   graph   cycle   dynamic-programming   hamiltonian-cycle

Algorithm for finding a Hamilton Path in a DAG

I am referring to Skienna's Book on Algorithms. The problem of testing whether a graph G contains a Hamiltonian path is NP-hard, where a Hamiltonian path P is a path that visits each vertex exactly once. There does not have to be an edge in G from the ending vertex to the starting vertex of P , unlike in the Hamiltonian cycle problem. Given a directed acyclic graph G (DAG), give an O(n + m) time algorithm to test whether or not it contains a Hamiltonian path. My approach, I am planning to use DFS and Topological sorting. But I didn't know how to connect the two concepts in solving the problem

2021-04-22 11:25:47    分类:问答    algorithm   graph-algorithm   directed-acyclic-graphs   hamiltonian-cycle

Knight's tour backtrack implementation choosing the step array

So I came up with this implementation for solving knights tour for a 8*8 chess board. But seems like it is taking a long time running (so long that I have to stop it). But if I replace the dx, dy arrays with the one written in comments and the program works like magic and gives the output. They say that it is cleverly choosing the array, so that the bruteforce algo is lucky to find the solution quickly. But how does one come up with this array at first place, this array (dx,dy) I got from some other code. So can anyone explain me why does this code work with those arrays (in comment) and not

2021-04-05 08:23:00    分类:问答    c++   algorithm   backtracking   hamiltonian-cycle