图/树表示和递归

发布于 2024-08-25 00:22:47 字数 899 浏览 5 评论 0原文

我目前正在 MATLAB 中编写一个优化算法,但我完全不擅长,因此我真的需要你的帮助。我真的很难找到一种表示图形的好方法(或者更像是一棵有多个根的树),它看起来或多或少像这样:

alt text http://img100.imageshack.us/img100/3232/graphe.png

基本上 11/12/13 是我们的根(阶段 0) ,2x 是 stage1,3x stage2 和 4x stage3。正如您所看到的,stageX 中的节点仅连接到 stage(X+1) 中的几个节点(因此它们不必连接到所有节点)。

重要提示:每个节点必须保存多个值(至少 3-4 个),其中一个是它的编号,另外至少两个变量(将用于优化决策)。

我确实有一个使用矩阵的简单表示,但它真的很难维护,所以我想知道是否有一个好的方法来做到这一点?

第二个问题:当我完成该表示后,我需要计算每条路线(从根到终点)的好坏(比如说我需要比较 11-21-31-41 是最好的还是 11-21 -31-42 更好)为此,我将使用每个节点保存的变量。但这些值必须递归计算,假设我们从 11 开始,但要计算 11-21-31-41 有多好,我们首先需要转到 41,进行一些计算,转到 31,进行一些计算,转到对 21 进行一些计算,然后我们可以使用之前的所有计算来计算 11。与 11-21-31-42 相同(我们从 42 开始,然后是 31->21->11)。我需要这样检查所有可能的路线。那么问题来了,怎么做呢?也许是 BFS/DFS?但我不太确定如何存储所有结果。

这些是一些冗长的问题,但我希望我不是要求你做我的作业(因为我得到了所有的算法,只是我不太擅长matlab,而且我的老师不让我做)爪哇)。

I'm currently writing an optimization algorithm in MATLAB, at which I completely suck, therefore I could really use your help. I'm really struggling to find a good way of representing a graph (or well more like a tree with several roots) which would look more or less like this:

alt text http://img100.imageshack.us/img100/3232/graphe.png

Basically 11/12/13 are our roots (stage 0), 2x is stage1, 3x stage2 and 4x stage3. As you can see nodes from stageX are only connected to several nodes from stage(X+1) (so they don't have to be connected to all of them).

Important: each node has to hold several values (at least 3-4), one will be it's number and at least two other variables (which will be used to optimize the decisions).

I do have a simple representation using matrices but it's really hard to maintain, so I was wondering is there a good way to do it?

Second question: when I'm done with that representation I need to calculate how good each route (from roots to the end) is (like let's say I need to compare is 11-21-31-41 the best or is 11-21-31-42 better) to do that I will be using the variables that each node holds. But the values will have to be calculated recursively, let's say we start at 11 but to calcultate how good 11-21-31-41 is we first need to go to 41, do some calculations, go to 31, do some calculations, go to 21 do some calculations and then we can calculate 11 using all the previous calculations. Same with 11-21-31-42 (we start with 42 then 31->21->11). I need to check all the possible routes that way. And here's the question, how to do it? Maybe a BFS/DFS? But I'm not quite sure how to store all the results.

Those are some lengthy questions, but I hope I'm not asking you for doing my homework (as I got all the algorithms, it's just that I'm not really good at matlab and my teacher wouldn't let me to do it in java).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2024-09-01 00:22:47

当然,它可能不是最有效的解决方案,但如果您可以访问 Matlab 2008+,您可以定义一个节点类来表示您的图。

Matlab 文档有一个很好的例子链接列表,您可以将其用作模板。

基本上,一个节点将有一个属性“linksTo”,它指向它链接到的节点的索引,以及计算每个链接成本的方法(可能还有一些描述每个链接的附加属性)。然后,您所需要的只是一个向下移动每个链接的函数,并在向上移动时带来成本。

Granted, it may not be the most efficient solution, but if you have access to Matlab 2008+, you can define a node class to represent your graph.

The Matlab documentation has a nice example on linked lists, which you can use as a template.

Basically, a node would have a property 'linksTo', which points to the index of the node it links to, and a method to calculate the cost of each of the links (possibly with some additional property that describe each link). Then, all you need is a function that moves down each link, and brings the cost(s) with it when it moves back up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文