Sei sulla pagina 1di 9

‫جامعة الملك خالد‬

‫كلية علوم الحاسب اآللي‬


‫قسم علوم الحاسب اآللي‬

King Khalid University


College of Computer Science
Department of Computer Sciences

STUDENT LAB/TUTORIAL ACTIVITY REPORT

Course Code : CSM 375


Course Name : Artificial Intelligence
Section Number: 3102
Student ID : 437818630
Student Name : Mohammed Hezam
EXPERIMENT/EXERCISE NO. : 05

TITLE OF EXPERIMENT/EXERCISE: To Demonstrate Edge, Path and Graph.

Goal AIM/OBJECTIVE (similar to the manual):

Description

Finding all answers


• findall(Things,GoalExpression,Bag)

Compute all Things which satisfy the GoalExpresssion and collect them in the
list Bag. If the GoalExpression fails, Bag will be the empty list []. findall treats
all variables in GoalExpression as if they are existentially quantified.

• bagof(Things,GoalExpression,Bag) Compute all Things which satisfy the


GoalExpresssion and collect them in the list Bag. bagof fails if GoalExpression
fails. Free variables in GoalExpression could be bound, yielding many bags.

• setof(Things,GoalExpression,Bag) Compute all Things which satisfy the


GoalExpresssion and collect them in the list Bag. Similar to bagof except that
Bag will not contain duplicates and it will be sorted.

Example.Consider the following facts.

son(X,Y):-

father(Y,X),

male(X).

daughter(X, Y):-

father(Y, X),

female(X).
grandfather(X, Y):-

father(X, Z),

father(Z,Y).

father(afzal, bilal).

father(afzal, zubair).

father(nazir, afzal).

father(afzal, humara).

father(afzal, sumara).

female(sumara).

female(humara).

Consider the Queries…….

?- findall(X, father(X,Y), Bag).

Bag = [afzal, afzal, nazir,

afzal, afzal].

?- findall(Y, father(X,Y), Bag).

Bag = [bilal, zubair, afzal,

humara, sumara].

Prolog has three built-in predicates designed to collect together objects resulting
from successful computations:

bagof(Things, GoalCondition, Bag)


setof(Things, GoalCondition, Bag)
findall(Things,GoalCondition, Bag)
Objectives: To Learn and apply Edges and find path in a graph write our own programs.

CASE STUDY/ RELEVANT PROGRAMS / DESIGN/CONFIGURATION /


IMPLEMENTATION/COMMANDS. (A PART FROM THE TEACHER TAUGHT IN THE

LAB)

To illustrate the differences consider a little example:

listing(p).

p(1,3,5).
p(2,4,1).
p(3,5,2).
p(4,3,1).
p(5,2,4).

Activity for Students: (Practical Exercise 5)


Try the following goals. (The answer displays have been modified to save space.)
1.?- bagof(Z,p(X,Y,Z),Bag).

X = 1,
Y = 3,
Bag = [5] ;
X = 2,
Y = 4,
Bag = [1] ;
X = 3,
Y = 5,
Bag = [2] ;
X = 4,
Y = 3,
Bag = [1] ;
X = 5,
Y = 2,
Bag = [4].

2. findall(Z,p(X,Y,Z),Bag).
Bag = [5, 1, 2, 1, 4].

3. bagof(Z,X^Y^p(X,Y,Z),Bag).
Bag = [5, 1, 2, 1, 4].

4. setof(Z,X^Y^p(X,Y,Z),Bag).
Bag = [1, 2, 4, 5].

As an example, consider the following connected graph:


/* Graph structures and paths */

edge(1,2).
edge(1,4).
edge(1,3).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).

connected(X,Y) :- edge(X,Y) ; edge(Y,X).

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,
\+member(C,Visited),
travel(C,B,[C|Visited],Path).

?- path(1,5,P).
Eg-1: Find the path between the given two nodes?
P = [1, 2, 5] ;
P = [1, 2, 3, 5] ;
P = [1, 2, 3, 4, 5] ;
P = [1, 4, 5] ;
P = [1, 4, 3, 5] ;
P = [1, 4, 3, 2, 5] ;
P = [1, 3, 5] ;
P = [1, 3, 4, 5] ;
P = [1, 3, 2, 5] ;
false.

The graph is defined by the edges as edge (start, end).


After compiling, type the query in the prompt, which will give the results shown.

%Edge List (Knowledge Base)


edge(1,2).
edge(1,4).
edge(2,4).
edge(3,6).
edge(3,7).
edge(4,3).
edge(4,5).
edge(5,6).
edge(5,7).
edge(6,5).
edge(7,5).
edge(8,6).
edge(8,7).
%Program
path(X,Y,[X,Y]):- edge(X,Y).
path(X,Y,[X|Xs]):- edge(X,W), path(W,Y,Xs).

%Query
path(1, 7, P).

%Results
P = [1, 2, 4, 3, 7] ;
P = [1, 2, 4, 3, 6, 5, 7] ;
P = [1, 2, 4, 3, 6, 5, 6, 5, 7] ;
P = [1, 2, 4, 3, 6, 5, 6, 5, 6|...] .

Eg-1: Find the path between the given two nodes, where nodes appear in
ascending order?

Knowledge Base is the same.

-------------------------------------------------
%Program
path(X,Y,[X,Y]):- edge(X,Y),X<Y.
path(X,Y,[X|Xs]):-edge(X,W), X<W, path(W,Y,Xs).
-------------------------------------------------

%Query
path(1, 7, P).

%Results
P = [1, 2, 4, 5, 7] ;
P = [1, 4, 5, 7] ;
false.

Eg-3: Find the path between the given two nodes with the given length?

The graph is defined by the edges as edge (start, end, cost).


-----------------------------------------------------------------
%Edge List (Knowledge Base)
edge(1, 2, 3).
edge(1, 4, 5).
edge(2, 4, 2).
edge(3, 6, 6).
edge(3, 7, 5).
edge(4, 3, 7).
edge(4, 5, 4).
edge(5, 6, 8).
edge(5, 7, 1).
edge(6, 5, 2).
edge(7, 5, 2).
edge(8, 6, 3).
edge(8, 7, 4).
%Program
path(X,Y,C,[X,Y]):- edge(X,Y,C).
path(X,Y,C,[X|Xs]):- edge(X,W,C1), plus(C1,C2,C),C2>0, path(W,Y,C2,Xs).
-----------------------------------------------------------------
%Query
path(1, 7, 17, P).

%Results
P = [1, 2, 4, 3, 7] ;
P = [1, 4, 3, 7] ;
false.

ANALYSIS and OBSERVATION

CONCLUSION:

Reserved for the lab teacher

Observation
Awarded Mark:
SIGNATURE :
DATE:

Potrebbero piacerti anche