Definition of a path/trail/walk

后端 未结 3 1200
你的背包
你的背包 2020-11-22 04:21

Many predicates define some kind of an acyclic path built from edges defined via a binary relation, quite similarly to defining transitive closure. A generic definition is t

3条回答
  •  广开言路
    2020-11-22 05:03

    I want to focus on naming the predicate.

    • Unlike maplist/2, the argument order isn't of primary importance here.

    • The predicate name should make the meaning of the respective arguments clear.

    So far, I like path_from_to_edges best, but it has its pros and cons, too.

    path_from_to_edges(Path,From,To,Edges_2) :-
        path(Edges_2,Path,From,To).
    

    Let's pick it apart:

    • pro: path is a noun, it cannot be mis-read a verb. To me, a list of vertices is implied.

    • pro: from stands for a vertex, and so does to.

    • con: edges is somewhat vague, but using lambdas here is the most versatile choice.

    • con: According to Wikipedia, a path is a trail in which all vertices (except possibly the first and last) are distinct. So that would need to be clarified in the description.


    Using lambdas for a lists of neighbor vertices Ess:

    ?- Ess  = [a-[b],b-[c,a]], 
       From = a,
       path_from_to_edges(Path,From,To,\X^Y^(member(X-X_neibs,Ess),member(Y,X_neibs))).
    Ess = [a-[b],b-[c,a]], From = a, To = a, Path = [a]     ;
    Ess = [a-[b],b-[c,a]], From = a, To = b, Path = [a,b]   ;
    Ess = [a-[b],b-[c,a]], From = a, To = c, Path = [a,b,c] ;
    false.
    

    Edit 2015-06-02

    Another shot at better naming! This leans more on the side of maplist/2...

    graph_path_from_to(P_2,Path,From,To) :-
       path(P_2,Path,From,To).
    

    Here, graph, of course, is a noun, not a verb.

    Regarding the meaning of "path": paths definitely should allow From=To and not exclude that by default (with pairwise term inequalities). It is easy to exclude this with an additional dif(From,To) goal, but not the other way round.

提交回复
热议问题