简体   繁体   English

如何使用DFS算法找到最小的产品?

[英]How to use a DFS algorithm to find the smallest product?

I've never used DFS before and I was wondering how you could use one to find the smallest product of a path if you were to traverse through the following "tree": 我以前从未使用过DFS,并且想知道如果要遍历以下“树”,如何使用DFS查找路径的最小乘积:

        3
      4   5
    7   6   4
  3   5   7   8
1   2   3   4   4

See comments below to clear any confusion(: 请参阅以下评论,以消除任何混淆(:

You need to construct a directed graph, where the nodes are the numbers, and each node has 2 edges to a number/node on the next level. 您需要构造一个有向图,其中的节点是数字,每个节点在下一级上的数字/节点都有2条边。 The graph in this particular problem will be a directed acyclic graph. 这个特定问题中的图将是有向无环图。

Then, you just run DFS on the graph constructed. 然后,您只需在构造的图形上运行DFS。 However, you will not keep track of/check whether you have visited a node before or not, since you want to revisit them. 但是,由于要重新访问节点,因此您不会跟踪/检查您是否曾经访问过该节点。 Instead, you only need to check whether the current node has 0 out-degree or node (since the nodes at the bottom will have 0 out-degree ), and update the current min when you reach the nodes at the bottom. 取而代之的是,您只需要检查当前节点是否具有0 out-degree或node(因为底部的节点将具有0 out-degree ),并在到达底部的节点时更新当前的min。 (You may also keep track of the current depth, and update the current min when the depth is reached). (您也可以跟踪当前深度,并在达到该深度时更新当前最小值)。 We can do this for this particular problem, since all the products is the result of multiplying exactly 5 numbers, one from each level. 我们可以针对这个特定问题执行此操作,因为所有乘积都是乘以5个数字而得的结果,每个数字一个。

What I describe above is called tree search variant of DFS, compared to the normal graph-search variant where you keep track whether a node has been visited before. 我上面描述的被称为DFS的树搜索变体,而普通的图搜索变体则可以跟踪以前是否访问过节点。 Tree search DFS will get stuck when there is a cycle in the graph, but since this is a directed acyclic graph, we will not run into such problem. 当图中有一个循环时,树搜索DFS将卡住,但是由于这是有向无环图,因此我们不会遇到这种问题。


If the numbers are all non-negative , we can observe that: regardless of how we reach a node from the root, the optimal path ahead is going to be the same every time. 如果这些数字均为非负数 ,则可以观察到:无论我们如何从根到达节点,前面的最佳路径每次都将是相同的。

In such case, it is faster to work backward from the bottom nodes. 在这种情况下,从底部节点向后工作会更快。 For each pairs that are adjacent to the same "parent" node, pick the smaller one and multiply with the "parent" node. 对于与同一“父”节点相邻的每一对,选择较小的一对并与“父”节点相乘。 Keep doing so until you reach the root node, and you will get the result. 继续这样做,直到到达根节点,您将获得结果。


If the numbers can be positive or negative or 0 , you need to keep track of 4 numbers: negative product with the largest and smallest absolute value, largest and smallest positive product. 如果数字可以是正数或负数或0 ,则需要跟踪4个数字:绝对值最大和最小的负数,正数最大和最小的负数。 And you also need to keep track of whether 0 product can be formed or not. 而且,您还需要跟踪是否可以形成0个产品。 The 2 largest absolute value are for the case of positive x negative . 对于正x负,最大2个绝对值。 The 2 smallest absolute value are for the case of positive x positive or negative x negative . 对于正x正负x负,最小的2个绝对值。 At the start of the algorithm, all those 5 fields are undefined. 在算法开始时,所有这5个字段都未定义。

The details of how to update are left to the readers. 如何更新的细节留给读者。 The result should be checked in the order: the negative number with the largest absolute value, 0, the positive number with the smallest absolute value. 应按以下顺序检查结果:绝对值最大的负数,0,绝对值最小的正数。 If the field is undefined, then skip it and check the next one. 如果该字段未定义,请跳过该字段并检查下一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM