I am trying to understand the fail driven loop for an graph for example but i dont understant how it works and when to use it. When to use fail driven loops and when to use normal recursiv? I have some examples to check if they are correct or not.
The first example has to verify that there are no vertex in graph having an edge with the given weight.
no_given_weight(W) :-
is_edge(X, _, W), !,
no_given_weight(W).
no_given_weight(D) :-
writeln("no vertex has an edge with weight D").
is_edge(X, Y, D) :-
edge(X, Y, D);
edge(Y, X, D).
This example is incorrect but i dont know what is incorrect. It has to verify that there are SOME vertex in graph having an edge with the given weight.
given_weight(W) :-
is_edge(X, _, W), !,
assertz(found(X)),
given_weight(W).
given_weight(D) :-
writeln("no vertex has an edge with weight D").
is_edge(X, Y, D) :-
edge(X, Y, D);
edge(Y, X, D).
Given the edge representation of an undirected graph, the predicate difference_graph below generates the difference graph Gdiff of G1 and G2 (assume a predicate is_edge is present for each of the 3 graphs – G1, G2, and Gdiff):
difference_graph :-
is_edge_1(X, Y),
not(is_edge_2(X, Y)), !,
not(is_edge_diff(X, Y)),
assertz(edge_diff(X, Y)),
fail.
difference_graph.
is_edge(X, Y) :-
edge(X, Y);
edge(Y, X).