determine whether undirected graph has path between two vertices

£可爱£侵袭症+ 提交于 2019-12-13 09:13:53

问题


I need a function which determines whether a path exists between vertices.

Input:

  • undirected graph as a list
  • two vertices

For example:

(is_it_a_path? '(2 ((1 2) (3 4))) 1 4)   ;; returns true

The function also needs to be tail recursive.

How do I do this?


回答1:


The (free, online) textbook How To Design Programs has several sections that may be helpful to you.

You say that the solution must be tail-recursive. If you mean that all calls to the search procedure must be in tail position, then you're going to have to keep track of visited-nodes and paths-to-nodes explicitly.

Next: I'm confused by your example; it looks like the input is... a list of length two containing a goal node and some representation of the graph? But... no, I'm still confused.

You need to explain how what the input means---for instance, how are graphs represented as inputs to your function?



来源:https://stackoverflow.com/questions/9402362/determine-whether-undirected-graph-has-path-between-two-vertices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!