产生状态转换中的最短路径
我正在尝试提出一种树遍历算法,但我陷入困境。
这是一个相当难的问题(与我问过的其他问题相比),所以我可能需要自己继续思考。但我想我会把它扔在这里。
我有以下类结构:
public class Transition
{
// The state we are moving from.
public String From { get; set; }
// All the To states for this from
public List<String>To { get; set; }
}
List<Transition> currentTransistions;
当 currentTransistions 完全填写时,它看起来像这样(对我来说):
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTransition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Transition>
<From />
<To>
<string>Not Done</string>
</To>
</Transition>
<Transition>
<From>Not Done</From>
<To>
<string>In Progress</string>
<string>Deleted</string>
</To>
</Transition>
<Transition>
<From>Deleted</From>
<To>
<string>Not Done</string>
</To>
</Transition>
<Transition>
<From>In Progress</From>
<To>
<string>Done</string>
<string>Ready For Test</string>
<string>Deleted</string>
</To>
</Transition>
<Transition>
<From>Done</From>
<To>
<string>In Progress</string>
</To>
</Transition>
<Transition>
<From>Ready For Test</From>
<To>
<string>In Progress</string>
<string>Done</string>
<string>Deleted</string>
</To>
</Transition>
</ArrayOfTransition>
这里的想法是我已经映射了 TFS 工作项的状态转换。我现在需要的是一种表达“给定当前状态,我如何到达另一个状态”的方式。
理想情况下,它看起来像这样:
foreach (string state in GetToFinalState(finalState, currentState, currentTransistions)
{
// Save the workitem at the state so we can get to the final state.
}
GetToFinalState,必须有一种方法来计算最短路径,并使用 C# 的 Yield 功能为 foreach 语句一次提供一个。
我以前使用过yield one,所以我想我可以弄清楚这一点。但我不确定如何在找到最短路径的同时做到这一点(无需在函数中每次重新计算)?
如果您已经读到这里,谢谢。如果您提供答案,请加倍感谢。
I am trying to come up with an algorithm for a tree traversal, but I am getting stuck.
This is a fairly hard question (compared to others I have asked) so I may need to keep figuring on my own. But I thought I would throw it out here.
I have the following class structure:
public class Transition
{
// The state we are moving from.
public String From { get; set; }
// All the To states for this from
public List<String>To { get; set; }
}
List<Transition> currentTransistions;
When currentTransistions is fully filled out it looks like this (for me):
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTransition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Transition>
<From />
<To>
<string>Not Done</string>
</To>
</Transition>
<Transition>
<From>Not Done</From>
<To>
<string>In Progress</string>
<string>Deleted</string>
</To>
</Transition>
<Transition>
<From>Deleted</From>
<To>
<string>Not Done</string>
</To>
</Transition>
<Transition>
<From>In Progress</From>
<To>
<string>Done</string>
<string>Ready For Test</string>
<string>Deleted</string>
</To>
</Transition>
<Transition>
<From>Done</From>
<To>
<string>In Progress</string>
</To>
</Transition>
<Transition>
<From>Ready For Test</From>
<To>
<string>In Progress</string>
<string>Done</string>
<string>Deleted</string>
</To>
</Transition>
</ArrayOfTransition>
The idea here is that I have mapped the state transitions for TFS Work items. What I need now is a way to say "Given a current state, how do I get to another state".
Ideally it would look like this:
foreach (string state in GetToFinalState(finalState, currentState, currentTransistions)
{
// Save the workitem at the state so we can get to the final state.
}
GetToFinalState, would have to have a way to caclulate the shortest path and use the yield feature of C# to offer them up one at a time for the foreach statement.
I have used yield one before, so I think I can figure that out. But I am not sure how to do that at the same time as finding the shortest path (with out recalculating on each time in the func)?
If you have read this far, thanks. If you offer an answer then double thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不计算最短路径并在整个过程完成后
产生
每个路径段,您就无法有效地做到这一点。最短路径问题的本质并不适合有效计算此类部分解决方案的算法。由于转换图没有加权,您可以简单地在其上运行 BFS 来计算最短路径。你需要做这样的事情(我不确定 TFS 对象的属性,所以这只是一个伪代码):
You can't do that efficiently without calculating the shortest path and
yield
ing each path segment after the whole process is completed. The nature of shortest path problem doesn't lend itself to algorithms that efficiently compute such partial solutions.Since the transition graph is not weighted, you can simply run a BFS on it to compute the shortest path. You need to do something like this (I'm not sure of the properties of the TFS object so this is just a pseudocode):
如果您需要找到无环树中从一个节点到后代节点的最短路径,那么 Mehrdad 的解决方案是一个不错的选择。即先进行广度优先搜索,直到找到目的节点,然后计算出从起始节点到目的节点的路径。
如果您的图不是(非循环)树,而是任意加权图,则简单的广度优先搜索不起作用。要么它进入无限循环(如果你不聪明地跟踪你已经看到一个节点的时间),要么不能保证找到最小权重路径。
如果您处于这种情况,那么著名的“A*”算法就是一个很好的算法。我在这里有一些关于如何在 C# 中实现 A* 的注释:
http://blogs.msdn.com/ericlippert/archive/tags/AStar/default.aspx
如果您有一个“估计函数”可以猜测网络上最有可能的下一个节点,那么这尤其有用最短路径是。
If you need to find the shortest path from a node to a descendent node in an acyclic tree, then Mehrdad's solution is a good one. That is, first do a breadth-first-search until you find the destination node, and then work out the path from the start node to the destination.
If your graph is not an (acyclic) tree, but rather an arbitrary weighted graph, then naive breadth-first-search does not work. Either it goes into infinite loops (if you're not clever about keeping track of when you've seen a node already), or it is not guaranteed to find the least-weight path.
If you're in that situation then a good algorithm to use is the famous "A*" algorithm. I've got some notes on how to implement A* in C# here:
http://blogs.msdn.com/ericlippert/archive/tags/AStar/default.aspx
This is particularly useful if you have an "estimating function" that can make guesses about what the most likely next node on the shortest path is.