code_idx
int64
12
284
code
stringlengths
36
24.7k
query_type
int64
0
1
query_code
stringlengths
7
49
found_solution
bool
2 classes
result_explanation
stringlengths
55
33.4k
result_variables
stringlengths
0
52
__index_level_0__
int64
0
194k
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var0,g,Var1).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var0 = [], Var1 = g.
194,204
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var0,g,Var3).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var0 = [], Var3 = g.
194,205
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var1,Var0,g).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var0 = g.
194,206
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var1,Var3,g).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var3 = g.
194,207
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var1,g,Var0).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var0 = g.
194,208
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var1,g,Var3).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var3 = g.
194,209
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var3,Var0,g).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var3 = [], Var0 = g.
194,210
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var3,Var1,g).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var3 = [], Var1 = g.
194,211
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var3,g,Var0).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var3 = [], Var0 = g.
194,212
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,Var3,g,Var1).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var3 = [], Var1 = g.
194,213
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,g,Var0,Var1).
false
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,214
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,g,Var0,Var3).
false
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,215
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,g,Var1,Var0).
false
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,216
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,g,Var1,Var3).
false
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,217
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,g,Var3,Var0).
false
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,218
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var2,g,Var3,Var1).
false
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED dfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,219
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
dfs(Var3,Var0,Var1,g).
true
CHECKING dfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED dfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var0 = [], Var1 = g.
194,220
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var1,Var3,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var3 = g.
194,221
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var1,g,Var0).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var0 = g.
194,222
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var1,g,Var3).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var3 = g.
194,223
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var3,Var0,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var3 = [], Var0 = g.
194,224
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var3,Var1,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var3 = [], Var1 = g.
194,225
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var3,g,Var0).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var3 = [], Var0 = g.
194,226
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,Var3,g,Var1).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var3 = [], Var1 = g.
194,227
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,g,Var0,Var1).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,228
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,g,Var0,Var3).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,229
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,g,Var1,Var0).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,230
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,g,Var1,Var3).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,231
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,g,Var3,Var0).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,232
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var2,g,Var3,Var1).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,233
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var0,Var1,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var0 = [], Var1 = g.
194,234
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var0,Var2,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var0 = [], Var2 = g.
194,235
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var0,g,Var1).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var0 = [], Var1 = g.
194,236
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var0,g,Var2).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var0 = [], Var2 = g.
194,237
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var1,Var0,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var0 = g.
194,238
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var1,Var2,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var2 = g.
194,239
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var1,g,Var0).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var0 = g.
194,240
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var1,g,Var2).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var2 = g.
194,241
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var2,Var0,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var2 = [], Var0 = g.
194,242
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var2,Var1,g).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var2 = [], Var1 = g.
194,243
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var2,g,Var0).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var2 = [], Var0 = g.
194,244
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,Var2,g,Var1).
true
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var2 = [], Var1 = g.
194,245
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,g,Var0,Var1).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,246
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,g,Var0,Var2).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,247
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,g,Var1,Var0).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,248
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,g,Var1,Var2).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,249
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,g,Var2,Var0).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,250
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(Var3,g,Var2,Var1).
false
CHECKING bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED bfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,251
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var0,Var1,Var2).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var1 = Var2.
194,252
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var0,Var1,Var3).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var1 = Var3.
194,253
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var0,Var2,Var1).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var2 = Var1.
194,254
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var0,Var2,Var3).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var2 = Var3.
194,255
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var0,Var3,Var1).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var3 = Var1.
194,256
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var0,Var3,Var2).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var3 = Var2.
194,257
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var1,Var0,Var2).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var1 = [], Var0 = Var2.
194,258
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var1,Var0,Var3).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var1 = [], Var0 = Var3.
194,259
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var1,Var2,Var0).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var1 = [], Var2 = Var0.
194,260
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var1,Var2,Var3).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var1 = [], Var2 = Var3.
194,261
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var1,Var3,Var0).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var1 = [], Var3 = Var0.
194,262
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var1,Var3,Var2).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var1 = [], Var3 = Var2.
194,263
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var2,Var0,Var1).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var2 = [], Var0 = Var1.
194,264
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var2,Var0,Var3).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var2 = [], Var0 = Var3.
194,265
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var2,Var1,Var0).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var2 = [], Var1 = Var0.
194,266
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var2,Var1,Var3).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var2 = [], Var1 = Var3.
194,267
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var2,Var3,Var0).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var2 = [], Var3 = Var0.
194,268
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var2,Var3,Var1).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var2 = [], Var3 = Var1.
194,269
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
bfs(g,Var3,Var0,Var1).
true
CHECKING bfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED bfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var3 = [], Var0 = Var1.
194,270
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,Var3,Var0,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var3 = [], Var0 = g.
194,271
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,Var3,Var1,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var3 = [], Var1 = g.
194,272
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,Var3,g,Var0).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var3 = [], Var0 = g.
194,273
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,Var3,g,Var1).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var3 = [], Var1 = g.
194,274
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,g,Var0,Var1).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,275
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,g,Var0,Var3).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,276
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,g,Var1,Var0).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,277
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,g,Var1,Var3).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,278
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,g,Var3,Var0).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,279
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var2,g,Var3,Var1).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,280
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var0,Var1,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var0 = [], Var1 = g.
194,281
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var0,Var2,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var0 = [], Var2 = g.
194,282
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var0,g,Var1).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var0 = [], Var1 = g.
194,283
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var0,g,Var2).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var0 = [], Var2 = g.
194,284
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var1,Var0,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var0 = g.
194,285
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var1,Var2,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var1 = [], Var2 = g.
194,286
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var1,g,Var0).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var0 = g.
194,287
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var1,g,Var2).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var1 = [], Var2 = g.
194,288
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var2,Var0,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var2 = [], Var0 = g.
194,289
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var2,Var1,g).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=g, ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g (variable) , Param4=g, ]
Var2 = [], Var1 = g.
194,290
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var2,g,Var0).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var2 = [], Var0 = g.
194,291
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,Var2,g,Var1).
true
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=FreeVariable (variable) , Param3=g, Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=FreeVariable (variable) , Param2=[] (variable) , Param3=g, Param4=g (variable) , ]
Var2 = [], Var1 = g.
194,292
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,g,Var0,Var1).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,293
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,g,Var0,Var2).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,294
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,g,Var1,Var0).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,295
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,g,Var1,Var2).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,296
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,g,Var2,Var0).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,297
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(Var3,g,Var2,Var1).
false
CHECKING fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] FAILED fastDfs : [Param1=FreeVariable (variable) , Param2=g, Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
194,298
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(g,Var0,Var1,Var2).
true
CHECKING fastDfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var1 = Var2.
194,299
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(g,Var0,Var1,Var3).
true
CHECKING fastDfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var1 = Var3.
194,300
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(g,Var0,Var2,Var1).
true
CHECKING fastDfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var2 = Var1.
194,301
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(g,Var0,Var2,Var3).
true
CHECKING fastDfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var2 = Var3.
194,302
284
% ================================================================================================= % Helper functions - list membership % ================================================================================================= %has(+E,+L) % determines whether list L has element E in it has(E, [E | _]). has(E, [X | T]) :- X \= E, has(E, T). %hasnot(+E, +L) % determines whether list L does not contain element E hasnot(_, []). hasnot(E, [X | T]) :- X \= E, hasnot(E, T). % both functions: note that because the resuult is T or F, we are using the unification in prolog to return the value and therefore only provide termination conditions that should return T as facts, the F cases will not unify and therefore prolog will return F % ================================================================================================= % Helper functions - list manipulation % ================================================================================================= %reverseList(+L, -R) % reverses the order of elements in L, the result is in R reverseList(L, R) :- reverseList(L,R,[]). reverseList([], ACC, ACC). reverseList([H | T], R, ACC) :- reverseList(T, R, [ H | ACC]). %prependList(+L, +L1, -R) % prepends all elements from L to L1, returning the result in R prependList([], L1, L1). prependList([H | T], L1, R) :- prependList(T, L1, R2), R = [ H | R2]. %appendList(+L, +L1, -R) % appends all elements from L to L1, returning the result in R appendList(X,[],X). appendList(X, [H | T], R) :- appendList(X, T, R2), R = [ H | R2]. % note that append(A,B,X) is equivalent to prepend(B,A,X) % ================================================================================================= % Helper functions - graphs % ================================================================================================= %neighbour(+G,+A,-B) % given a graph and a node, returns neighbouring nodes, i.e. nodes B such that there is an edge from A to B. We first ignore the graph nodes since we are only using the edges and then walk all the edges and if we see edge from the node in question to some other node B, then node B is our neighbour. neighbour(g(_,E) ,A, B) :- neighbour2(E, A, B). neighbour2([[A,B] | _ ], A, B). neighbour2([ _ | T ], A, B) :- neighbour2(T,A,B). % ================================================================================================= % Finding path from A to B % ================================================================================================= %findPath(+G,+A,+B,-P) % given graph G, finds a path from A to B and returns a list containing visited edges, for which we use an accumulator findPath(G,A,B,P) :- findPath(G,A,B,P,[A]). % if we are going to the same node we are now, we can stop and return the path so far findPath(_,A,A, ACC, ACC). % otherwise we must actually recurse, i.e. find a neighbour that we have not visited yet, add it to the path and try to find path from that neighbour to the end node findPath(G,A,B,P,ACC) :- neighbour(G, A, X), hasnot(X, ACC), findPath(G,X,B,P,[ X | ACC ]). % ================================================================================================= % Graph Walking - Depth First Search % ================================================================================================= %dfs(+G,+A,-R) % The idea is to copy the basic algorithm of dfs, i.e. keep a stack of nodes to visit and process the stack one node at a time. We also drag the nodes visited so far in an accumulator and when the queue to process is empty, we return the accumulator. When a node is visited, we first check if the node has already been visited and if so, we continue to the next node. Otherwise we get all neighbours of the node and prepend them to the queue, add the node to the accumulator and continue processing the queue. % first convert the original query to the new one with stack being initialized to the starting node and accumulator to empty list dfs(G,A, R) :- dfs(G, [A], [], R2), reverseList(R2, R). % when there is nothing in the stack, return the accumulator dfs(_, [], ACC, ACC). % if the next element in the stack is already in the accumulator, skip it and proceed to the next element dfs(G, [X | T], ACC, R) :- has(X, ACC), !, dfs(G, T, ACC, R). % otherwise add the element to the accumulator, then add all neighbours of the node on the stack and call itself recursively on the new stack with the new accumulator dfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), dfs(G, Q, ACC2, R). % ================================================================================================= % Graph Walking - Breadth First Search % ================================================================================================= % The idea of BFS is identical, but this time we use queue instead of stack, i.e. we append the neighbours instead of prependning them %bfs(+G,+A,-R) bfs(G, A, R) :- bfs(G, [A], [], R2), reverseList(R2, R). bfs(_, [], ACC, ACC). bfs(G, [X | T], ACC, R) :- has(X, ACC), !, bfs(G, T, ACC, R). bfs(G, [X | T], ACC, R) :- ACC2 = [X | ACC], findall(N, neighbour(G, X, N), ALL), appendList(ALL, T, Q), bfs(G, Q, ACC2, R). % ================================================================================================= % Speedier version % ================================================================================================= % greatest source of inefficiency is the fact that we always check the entire graph. We can however simply remove any edges leading to a node that is added to the queue, which means that there will be no way of actually getting to the node again. This would also make the graph smaller as we go, so even looking for neighbours will be faster. % to do this we start with the function removeEdgesTo, which removes from graph all edges leading to any of the nodes specified. %removeEdgesTo(+G, +A, -R) % takes graph and removes all edges leading to all nodes in second argument (list of nodes). Returns the new graph in third % first we extract the graph into edges only, do the magic and then reconstruct the graph back removeEdgesTo(g(N,E), A, R) :- removeEdgesTo2(E,A,R2), R = g(N,R2). % the magic: take next element from the list of nodes, remove all edges to it, and then recursively call itself on the changed graph with the rest of nodes removeEdgesTo2(E, [], E). removeEdgesTo2(E, [H | T], R) :- removeEdgesToNode(E, H, E2), removeEdgesTo2(E2, T, R). % this removes edges to a single node only removeEdgesToNode([], _, []). removeEdgesToNode([[_, N] | T ], N, R) :- !, removeEdgesToNode(T, N, R). removeEdgesToNode([ E | T], N, R) :- removeEdgesToNode(T, N, R2), R = [E | R2]. % now that we have the helper functions, we can do the fast dfs, the idea is the same as the original dfs, we keep the stack, however when we add node(s) to the stack, we remove all edges to them from the graph. This means we can skip the check whether a node has already been processed altogether: %fastDfs(+G,+A,-R) fastDfs(G,A, R) :- removeEdgesTo(G, [A], G2), fastDfs(G2, [A], [], R2), reverseList(R2, R). % when there is nothing in the queue, return empty queue and the accumulator fastDfs(_, [], ACC, ACC). % otherwise add the element to the accumulator and then add the neighbours of the element to the queue fastDfs(G, [X | T], ACC, R) :- ACC2 = [ X | ACC], findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), dfs(G2, Q, ACC2, R). % ================================================================================================= % returning single node at a time % ================================================================================================= % another interesting modification is changing the DFS algorithm so that we return one node at a time instead of the whole list at once, similarly to what neighbours do %oneAtATimeDfs(+G,+A,-N) oneAtATimeDfs(G,A, N) :- removeEdgesTo(G, [A], G2), oneAtATimeDfs2(G2, [A], N). % the idea is simple, the termination condition is now whenever we take a node from the stack, we also return it oneAtATimeDfs2(_, [A | _], A). % when prolog continues, it tries the following rule on the same node, which then processes the node oneAtATimeDfs2(G, [X | T], A) :- findall(N , neighbour(G, X, N), ALL), prependList(ALL, T, Q), removeEdgesTo(G, ALL, G2), oneAtATimeDfs2(G2, Q, A).
0
fastDfs(g,Var0,Var3,Var1).
true
CHECKING fastDfs : [Param1=g, Param2=FreeVariable (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ] PASSED fastDfs : [Param1=g, Param2=[] (variable) , Param3=FreeVariable (variable) , Param4=FreeVariable (variable) , ]
Var0 = [], Var3 = Var1.
194,303