Perl 中的 DFS(或 Java 或 C++ ...)
我在 3D 计算机图形学方面做了一些工作,但对图形学有些陌生 理论。 特别是我一直在研究并尝试使用 深度优先搜索 (DFS),如 Mastering Algors w/ Perl (Jarkko 希塔涅米)。到目前为止我还没能得到它:-(但我很确定 DFS 就是我想要的。
它不一定是 Perl 语言(只是想学习该语言),但 Java 或 C++ 就可以了。
我有 53 个位置向量,即 (x,y,z),我将其表示为
(x1,y1,z1)
(x2,y2,z2)
.
.
.
(x53,y53,z53)
然后运行一个 Perl 程序,我编写该程序来生成之间的随机链接 节点,分配一些最大数量。跳数,比如 6。所以拓扑可能看起来像 我
5 <-- node 1 has 5 links to
18 4 23 6 48, <-- node 18, node 4, node 23, node 6, node 48
2 <-- node 2 has 2 links to
14 5, <-- node 14, node 5
0 <-- node 3 is a leaf since it has no links
.
.
.
2 <-- node 18 has 2 links to
3 17 <-- node 3, node 17
.
.
.
4 <-- node 53 has 4 links to
10 46 49 22 <-- node 10, node 46, node 49, node 22
想确定“运行”路径,直到遇到水槽,即 0。 例如节点1到节点 18 到节点 3,... 这条路已经完成了。 。 。 。
我想我想要DFS;这似乎是一个递归练习。
如果有人理解并且可以给我代码,那就太好了。我不是学生,但我已经 51 岁了!也许这与我没有得到这个有关:-)
我查看了我的q,出于某种原因(可能是我:-(它是“乱码”
拓扑应该看起来像 5 <--节点1有5条链路; 18 4 23 6 48 <-- 节点 18、节点 4、节点 23、节点 6、节点 48 2 <--节点2有2条链路; 14 5,<--节点14,节点5 0 <-- 节点 3 是叶子,因为它没有链接 。 。 。 2<--节点18有2条链路; 3 17 <-- 节点3,节点17 。 。 。 4<--节点53有4条链路; 10 46 49 22 <-- 节点 10、节点 46、节点 49、节点 22
只是想弄清楚,以防有人可以提供代码(Perl、Java、c++/C ...)
谢谢。
I have done some in 3D computer graphics but am somewhat new to graph
theory.
In particular I have been looking at and trying to solve my problem using a
depth first search (DFS) as described in Mastering Algors w/ Perl (Jarkko
Hietaniemi). So far I have not been able to get it :-( but I am pretty-sure a DFS
is what I want.
It does not have to be in Perl (just trying to learn the language), but Java or C++ would be good.
I have 53 position vectors, ie (x,y,z), which I represent as
(x1,y1,z1)
(x2,y2,z2)
.
.
.
(x53,y53,z53)
I then run a Perl program that I wrote to generate random links between
nodes, assigning some max no. of hops, say 6. So the topology may look like
this
5 <-- node 1 has 5 links to
18 4 23 6 48, <-- node 18, node 4, node 23, node 6, node 48
2 <-- node 2 has 2 links to
14 5, <-- node 14, node 5
0 <-- node 3 is a leaf since it has no links
.
.
.
2 <-- node 18 has 2 links to
3 17 <-- node 3, node 17
.
.
.
4 <-- node 53 has 4 links to
10 46 49 22 <-- node 10, node 46, node 49, node 22
I would like to determine the path "run" till I hit a sink, ie a 0.
e.g. node 1 to node
18 to node 3, ...
This path is completed already.
.
.
.
I think I want DFS; it seems like a recursive exercise.
If someone understands and could give me code, that would be great. I am not a student but am 51! Maybe that has something to do with me not getting this :-)
I looked at my q and for some reason (probably me :-( it was "garbled"
Topology should look like
5 <-- node 1 has 5 links;
18 4 23 6 48 <-- node 18, node 4, node 23, node 6, node 48
2 <-- node 2 has 2 links;
14 5, <-- node 14, node 5
0 <-- node 3 is a leaf since it has no links
.
.
.
2 <-- node 18 has 2 links;
3 17 <-- node 3, node 17
.
.
.
4 <-- node 53 has 4 links;
10 46 49 22 <-- node 10, node 46, node 49, node 22
Just want to be clear in case someone can provide code (Perl, Java, c++/C ...)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
深度优先搜索的想法是首先搜索尽可能“深”的查询,然后在火车上移动。从数据树的角度来看,这很容易想到:
搜索将从节点 1 -> 开始。 53、搜索顺序将是
1-> 18-> 3-> 17-> 4-> 23-> 6-> 48-> 2-> 5-> 14 ....
它转到节点 1,查看其第一个链接:节点 18,然后节点 18 的第一个链接节点 3,命中没有链接的节点。然后返回到节点 17 的相同深度级别进行搜索,依此类推。在您的情况下,您只需要停在那里。
下面有完整的Java解决方案,抱歉我不熟悉perl,所以我把伪代码逻辑写出来了。
问题相当简单,除了存在可能导致无限循环的循环链接的情况,因此我添加了先前访问过的节点的列表并对此进行检查以避免冗余或无限搜索。
这是一个完整的工作解决方案:
The idea of a depth first search is to search a "deep" as possible for your query first and then move across the train. This is easy to think of in terms of a data tree:
The search will go from nodes 1 -> 53, the search order will be
1 -> 18 -> 3 -> 17 -> 4 -> 23 -> 6 -> 48 -> 2 -> 5 -> 14 ....
It goes to Node 1, looks at its first link: node 18, then node 18's first link node 3, hits a node without a link. Then goes back to search on the same depth level to node 17, etc. In your case you will only need to stop there.
There is the full solution in Java below, sorry I am not familiar with perl so I have written the pseudo-code logic out instead.
The problem is fairly straight forward except for the case where there is a circular linkage which could cause an infinite loop, so I have added a list of previously visited nodes and check against this to avoid redundant or infinite searching.
And here is a full working solution: