[英]Traverse every edge of a grid (graph) with least number of runs
I have an (mxn) grid where each edge has the same unit length of 1. Each run starts from the start-point (0, 0) and moves to the endpoint (m, n). 我有一个(mxn)网格,其中每个边的单位长度相同,为1。每次运行都从起点(0,0)开始,然后移动到端点(m,n)。 Each run can only move rightwards or upwards, ie no backward travel allowed, and hereby each run must consist of a length of exactly (m+n). 每个行程只能向右或向上移动,即不允许向后移动,因此每个行程必须由正好为(m + n)的长度组成。
How many runs do I need so that I can make sure that I have traversed every edge in this grid? 我需要运行多少次才能确保遍历此网格中的每个边? Ie what's the minimum number of runs from (0, 0) to (m, n) is required to make sure that each edge (corner) of the (mxn) grid has been gone through for at least once? 也就是说,从(0,0)到(m,n)的最小游标数目是多少,以确保(mxn)网格的每个边(角)至少经过了一次? Note that I'm not aiming to find number of paths from (0, 0) or (m, n). 请注意,我的目的不是从(0,0)或(m,n)查找路径数。 I only want to explore all edges in the entire grid. 我只想探索整个网格中的所有边缘。
Thanks! 谢谢!
您必须遍历每一行和每一列,才能给出n + m条路径的答案。
Turn the grid so that (0,0) is at the top, and (m, n) is at the bottom. 旋转网格,使(0,0)在顶部,而(m,n)在底部。 You now have a nice top-to-bottom flow that lets "gravity" provide the edge directions for a directed graph. 现在,您具有从上到下的良好流程,可让“重力”为有向图提供边缘方向。
Adjust the diagram so that the edges are on the nearest 45-degree angle. 调整图表,使边缘在最近的45度角上。 (0,0) is at the top; (0,0)在顶部; the second layer consists of (0, 1) and (1, 0); 第二层由(0,1)和(1,0)组成; the third is (0, 2), (1, 1), (2, 0), and so on. 第三个是(0,2),(1、1),(2、0),依此类推。 After i
moves, you'll be at a node whose coordinates sum to i
. i
移动之后,您将到达一个坐标的总和为i
的节点。
Now, since each node has 1 or 2 edges in, and 1 or 2 edges out, it's trivial to show that the maximum layer size is min(m, n)
nodes, and the maximum number of edges at any given step is 2*(min(m, n) - 1)
. 现在,由于每个节点都具有1或2条边,以及1或2条边,因此可以很容易地看出最大层大小为min(m, n)
节点,并且任何给定步骤的最大边数为2*(min(m, n) - 1)
It's also trivial to construct a series of paths that will traverse each layer or step in that number of runs. 构造一系列遍历该运行次数的每一层或每一步的路径也很简单。 Number the nodes/edges from one side to the other; 从一侧到另一侧编号节点/边; use corresponding numbers to plan each run. 使用相应的数字来计划每次运行。 When you hit the limit in one dimension, just stick to the edge for the rest of that run. 当您在一个维度上达到极限时,在接下来的行程中只要坚持住边缘即可。
So, if you need only to hit every node, you can do it in min(m, n)
runs. 因此,如果只需要击中每个节点,则可以在min(m, n)
运行中进行。 If you need every edge, it's 2*(min(m, n) - 1)
runs. 如果需要每个边缘,则运行2*(min(m, n) - 1)
。
To cover every edge, you need m+(n-2)
paths. 要覆盖每个边缘,您需要m+(n-2)
条路径。 A simple example of this consists of horizontal and vertical paths, as shown below. 一个简单的示例包括水平和垂直路径,如下所示。 Note that the red paths are duplicates, so the red paths in the second diagram are not needed. 请注意,红色路径是重复的,因此不需要第二张图中的红色路径。 That's the reason for the -2
in the equation. 这就是方程式中-2
的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.