新泽西州 SML 中的图表

发布于 2024-10-05 04:10:05 字数 265 浏览 8 评论 0原文

我需要使用机器学习编写一些函数,该函数接收有向图的边列表[(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 技术交流群。

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

发布评论

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

评论(3

相思碎 2024-10-12 04:10:05

如果要存储有关顶点的数据,则需要从顶点到数据的有限映射
该地图可能提供如下签名:

type 'a vmap (* vertex map *)
val empty : 'a vmap  (* empty map *)
val insert : vertex * 'a * 'a vmap -> 'a vmap  (* add info about a vertex *)
val lookup : vertex * 'a vmap -> 'a option  (* look for info about a vertex *)

要实现此签名,您可以考虑一个简单的 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:

type 'a vmap (* vertex map *)
val empty : 'a vmap  (* empty map *)
val insert : vertex * 'a * 'a vmap -> 'a vmap  (* add info about a vertex *)
val lookup : vertex * 'a vmap -> 'a option  (* look for info about a vertex *)

To implement this signature, you could consider a simple list of vertex * 'a pairs, or something more ambitious like a balanced binary search tree.

祁梦 2024-10-12 04:10:05

您可以存储有关顶点的数据!

例如,您想记录您访问过哪些顶点吗?

假设您有一个函数,可以从当前顶点递归地探索所有可能的未探索边。

它可以接受未探索的边的向量,加上当前顶点和目标顶点。它将返回成功到达目标顶点的路径向量。

在内部,它将找到从此顶点开始的边集,并针对该集中的每条边递归到自身,将所选边从未探索的边列表中删除到每个子函数中。

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.

一绘本一梦想 2024-10-12 04:10:05

抱歉,我无法抗拒,

我在 雅虎!答案,我
回答了它。

该实现最初是基于创建一棵树的设计
遍历图;但最终还是符合设计的
Alex Brown 之前曾表达过。

最初规划是在Haskell中完成的,因此这个辅助函数

fun replicate len el = 
    if len = 0 then nil else el::replicate (len -1) el

:主要实现:

fun routes  dst (edges:(int * int) list) src  = 
    let val (very_possible,remotely_possible) =
            if null edges
            then (nil,nil)
            else List.partition ((fn s=> s = src) o #1) edges 
        val (raw_solutions,dsts_is_nx_srcs) = 
            List.partition  ((fn d => d = dst) o #2) very_possible
        val solutions = replicate  (length raw_solutions) [src,dst]
        val full_rest_routes =
            let val rest_rest_routes = 
                    map (routes dst remotely_possible)  
                            ( map #2 dsts_is_nx_srcs )
              in map (fn lst => src::lst) (List.concat rest_rest_routes)
     end
     in case (very_possible, solutions, remotely_possible)
        of (nil, _,  _)       => nil
         |  (_::_, (p::ps), _) =>  solutions @ full_rest_routes
         |  (_::_, nil, _::_)  =>  full_rest_routes
  |  (_   , nil, nil )  => nil
     end

用户界面:

fun getPaths edges src dst  =  routes dst edges src 

上面的代码来自 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:

fun replicate len el = 
    if len = 0 then nil else el::replicate (len -1) el

The main implementation:

fun routes  dst (edges:(int * int) list) src  = 
    let val (very_possible,remotely_possible) =
            if null edges
            then (nil,nil)
            else List.partition ((fn s=> s = src) o #1) edges 
        val (raw_solutions,dsts_is_nx_srcs) = 
            List.partition  ((fn d => d = dst) o #2) very_possible
        val solutions = replicate  (length raw_solutions) [src,dst]
        val full_rest_routes =
            let val rest_rest_routes = 
                    map (routes dst remotely_possible)  
                            ( map #2 dsts_is_nx_srcs )
              in map (fn lst => src::lst) (List.concat rest_rest_routes)
     end
     in case (very_possible, solutions, remotely_possible)
        of (nil, _,  _)       => nil
         |  (_::_, (p::ps), _) =>  solutions @ full_rest_routes
         |  (_::_, nil, _::_)  =>  full_rest_routes
  |  (_   , nil, nil )  => nil
     end

The user interface:

fun getPaths edges src dst  =  routes dst edges src 

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.

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