Cool Programs in Prolog CS 581 022805 Flora





![Prolog n The rest of the code. path(A, B, Path) : travel(A, B, [A], Prolog n The rest of the code. path(A, B, Path) : travel(A, B, [A],](https://slidetodoc.com/presentation_image_h/e9494be46dcae819a329b964dad30ae7/image-6.jpg)
![Prolog n n Base case travel(A, B, P, [B|P]) : connected(A, B). Inductive case Prolog n n Base case travel(A, B, P, [B|P]) : connected(A, B). Inductive case](https://slidetodoc.com/presentation_image_h/e9494be46dcae819a329b964dad30ae7/image-7.jpg)
![Running the program n Load the program into SWI-Prolog ? - [swi('demo/test')]. n Run Running the program n Load the program into SWI-Prolog ? - [swi('demo/test')]. n Run](https://slidetodoc.com/presentation_image_h/e9494be46dcae819a329b964dad30ae7/image-8.jpg)
![Continue n The paths. P = [1, 4, 3, 2, 5] ; P = Continue n The paths. P = [1, 4, 3, 2, 5] ; P =](https://slidetodoc.com/presentation_image_h/e9494be46dcae819a329b964dad30ae7/image-9.jpg)



- Slides: 12
Cool Programs in Prolog CS 581 02/28/05 Flora Tan
A Graph Example A connected graph. n Vertices 1, 2, 3, 4, 5. n Edges are the connecting lines. n Find all paths between any two vertices. n Easy to implement in Prolog. n
Prolog n List the edges as facts. edge(1, 2). edge(1, 3). edge(1, 4). edge(2, 3). edge(2, 5). edge(3, 4). edge(3, 5). edge(4, 5).
Prolog n Add the rules. Options: The edges are bi-directional. We could list out another eight edges as facts. Ex: edge(2, 1), edge(3, 2), edge(4, 1), edge(4, 3), edge(5, 2), edge(5, 3), edge(5, 4) n Use disjunction. n connected(X, Y) : - edge(X, Y) ; edge(Y, X).
Prolog n Add the rules. Options (continue): n Alternatively, we could write this as two rules. connected(X, Y) : - edge(X, Y). connected(X, Y) : - edge(Y, X). n But we don’t want to do this. An infinite loop. edge(X, Y) : - edge(Y, X).
Prolog n The rest of the code. path(A, B, Path) : travel(A, B, [A], Q), reverse(Q, Path). travel(A, B, P, [B|P]) : connected(A, B). travel(A, B, Visited, Path) : connected(A, C), C == B, not(member(C, Visited)), travel(C, B, [C|Visited], Path). n Note the following: n n n Conjunction Goal matching Recursive search List processing – reverse, member Accumulator
Prolog n n Base case travel(A, B, P, [B|P]) : connected(A, B). Inductive case travel(A, B, Visited, Path) : connected(A, C), C == B, not(member(C, Visited)), travel(C, B, [C|Visited], Path). n n A path from A to B is obtained if A and B are connected. A path from A to B is obtained provided that A is connected to a vertex C different from B that is not on the previously visited part of the path, and one continues finding a path from C to B.
Running the program n Load the program into SWI-Prolog ? - [swi('demo/test')]. n Run the program. ? - path(1, 5, P). P = [1, 2, 5] ; P = [1, 2, 3, 4, 5] ; P = [1, 4, 3, 5] ;
Continue n The paths. P = [1, 4, 3, 2, 5] ; P = [1, 3, 4, 5] ; P = [1, 3, 2, 5] ;
Why is this cool? Graph search algorithms uses DFS/BFS and Adjacency List/Adjacency Matrix. n Prolog uses Depth First Search. n Other graphs examples: searching a maze, Dijkstra’s algorithm, graph coloring. n Coding in C is about 300 lines because we have to implement our own DFS and Adjacency List/Matrix. n
Adjacency List / Matrix n Adjacency List – an array of pointers to linked lists 1 -> 2 -> 3 -> 4 2 -> 1 -> 3 -> 5 3 -> 1 -> 4 -> 5 4 -> 1 -> 3 -> 5 5 -> 2 -> 3 -> 4 n n Adjacency Matrix n 1 2 3 4 5 – a 2 D array of integers 1 2 3 4 5 0 1 1 1 0 1 0 1 1 0 0 1 1 1 0 1 0 1 1 1 0
References Programming in Prolog by W. F. Clocksin and C. S. Mellish. n Prolog tutorial n n http: //www. csupomona. edu/~jrfisher/www/prolog _tutorial/2_15. html












