新泽西州 SML 中的图表
我需要使用机器学习编写一些函数,该函数接收有向图的边列表[(1,2),(1,3),(3,2)] ,这意味着从 1 到 2 和从 1 到 3 的有向边...,并且我还收到两个顶点,我需要找到从第一个顶点到第二个顶点的所有可能路径以及可能路径的列表,例如对于顶点1, 2,我需要显示列表[[1,2],[1,3,2]],我该怎么做ML如果可以不存储有关顶点的数据,提前感谢您的任何想法。
I need to write some function using ML, this function receives the list of the edges of the directed graph [(1,2),(1,3),(3,2)], it means directed edge from 1 to 2 and from 1 to 3..., and I receive also two vertices, I need to find all possible ways from first vertex to second and list of the possible paths, for example for vertices 1, 2, I need to display the list [[1,2],[1,3,2]], how can I do it ML if can't store data about the vertices, thanks in advance for any idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要存储有关顶点的数据,则需要从顶点到数据的有限映射。
该地图可能提供如下签名:
要实现此签名,您可以考虑一个简单的
vertex * 'a
对列表,或者更雄心勃勃的东西,例如平衡二叉搜索树。If you want to store data about the vertices, you need a finite map from verticies to data.
The map might offer a signature like this:
To implement this signature, you could consider a simple list of
vertex * 'a
pairs, or something more ambitious like a balanced binary search tree.您可以存储有关顶点的数据!
例如,您想记录您访问过哪些顶点吗?
假设您有一个函数,可以从当前顶点递归地探索所有可能的未探索边。
它可以接受未探索的边的向量,加上当前顶点和目标顶点。它将返回成功到达目标顶点的路径向量。
在内部,它将找到从此顶点开始的边集,并针对该集中的每条边递归到自身,将所选边从未探索的边列表中删除到每个子函数中。
You can store data about the vertices!
For example, Do you want to record which vertices you have visited?
Let's say you have a function which recursively explores all possible unexplored edges from the current vertex.
It can accept a vector of unexplored edges, plus the current vertex and the target vertex. It will return a vector of paths that successfully make it to the target vertex.
Internally, it will locate the set of edges that begin on this vertex, and recurse onto itself for each edge in this set, removing the chosen edge from the list of unexplored edges into each subfunction.
抱歉,我无法抗拒,
我在 雅虎!答案,我
回答了它。
该实现最初是基于创建一棵树的设计
遍历图;但最终还是符合设计的
Alex Brown 之前曾表达过。
最初规划是在Haskell中完成的,因此这个辅助函数
:主要实现:
用户界面:
上面的代码来自 routes4.sml;
但省略了测试和IO。虽然时间不长,但我还是希望
它可以更简单。
Sorry, I Couldn't Resist
I saw this same puzzle (er question) pop-up on Yahoo! Answers, and I
answered it.
The implementation started out as a design based on creating a tree to
traverse the graph; but it finally ended up matching the design
expressed earlier by Alex Brown.
Originally the planning was done in Haskell, hence this helper function:
The main implementation:
The user interface:
The code above is from routes4.sml;
but the testing and IO is omitted. Even though this isn't too long, I am still hoping
it coud be simplier.