Top Banner
George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial Intelligence, 5 th edition. © Pearson Education Limited, 2005 1
77

George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

George F Luger

ARTIFICIAL INTELLIGENCE 5th editionStructures and Strategies for Complex Problem Solving

Programming in Logic (PROLOG)

Luger: Artificial Intelligence, 5th edition. © Pearson Education Limited, 2005

1

Page 2: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

PROLOG

• What is True

• What needs to be Proved

Not

• How to do it like in other Programming languages

2

Page 3: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

PROLOG

• Edinburgh Syntax

• Programming in PROLOG– Clocksin and Mellish, 1984

3

Page 4: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

PROLOG EXAMPLES

4

Page 5: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1Set of facts:

parent(ali,ahmad).

parent(ahmad,salem).

parent(ali,fatema).

parent(fatema,osama).

male(ali).

male(ahmad).

male(salem).

male(osama).

female(fatema).

5

Page 6: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1• Every sentence ends with a period “.”

• Names starts with lower case, constants

6

Page 7: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1? parent(ali, ahmad).

By unifying this statement with the axioms (assertions) in order.

The above query (goal) also ends with a period

Ans:

yes

?parent(ali,X).

The above statement asks about all ali’s child

Semicolon asks for more answers

Ans:

X=ahmad; Note: by unifying X with ahmad

X=fatema;

no 7

Page 8: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1?parent(X,Y).

X=ali

Y=ahmad;

X=ahmad

Y=salem;

X=ali

Y=fatema;

X=fatema

Y=osama;

no

8

Page 9: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1?parent(X,ahmad), male(X).

Comma “,” means and

Who is ahmad’s father

It first unifies the first part, i.e parent(X,ahmad).

It finds that X could take the value of ali.

It then attempts to prove male(ali)

Ans:

X=ali;

No

Backtracking on two levels to find other answers9

Page 10: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1Add the following rule (considered rule not fact):

father(X,Y):- parent(X,Y) , male(X).

father(X,Y) called head

parent(X,Y) , male(X) called body

?father(X,ahmad).

Ans:

X=ali;

No

?father(fatema,Y).

no

Because fatema is unified with X and there is no way to prove that male(fatema)

10

Page 11: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1sibling(X,Y):- parent(Z,X) , parent(Z,Y).

?sibling(X,Y)

X=ahmad,

Y=ahmad;

X=ahmad,

Y=fatema;

X=salem,

Y=salem;

X=fatema,

Y=ahmad;

X=fatema,

Y=fatema;

X=osama,

Y=osama;

no11

Page 12: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1sibling(X,Y):- parent(Z,X) , parent(Z,Y), X\

==Y.

?sibling(X,Y)

X=ahmad,

Y=fatema;

X=fatema,

Y=ahmad;

no12

Page 13: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

EXAMPLE 1Define uncle relation, uncle(X,Y).

uncle(X,Y):-

parent(Z,Y),

parent(G,Z),

parent(G,X),

X\==Z. 13

Page 14: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

HW: Define the following rules:• mother

• son

• daughter

• sister

• sibling

• grandfather(X,Y):-parent(X,Z),parent(Z,Y), male(X).

• grandmother

• cousin(X,Y)14

Page 15: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in mathematical predicates• X=:=Y true when X equals Y

• X=\=Y true when X does not equal to Y

• X<Y true when X is less than Y

• X>Y true when X is greater than Y

• X=<Y true when X is less than or equal to Y

• X>=Y true when X is greater than or equal to Y

15

Page 16: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in Mathematical predicates?6 = : = 4 +2.

yes

?8 = : = 4*2.

yes

?7= : = 4+6.

no

?6 =:= 6.

yes 16

Page 17: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in logical predicates• X= =Y true when X can be unified with

Y

• X\= =Y true when X is not the same as Y

• X=Y attempts to unify X with Y

17

Page 18: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in logical predicates?5= =5.

yes

?X= = Y.

no

?5+1 == 5+1.

yes

?5+1 = = 1+5.

no 18

Page 19: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in logical predicates?X= ali.

X=ali

?X= 1/2.

X=1/2

? f(X)= f(ali).

X=ali

?f = g.

no 19

Page 20: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in logical predicates?5 \= 4.

yes

?ahmad @<basem.

yes

? 'Ali' @< 'Basem'.

yes

? X=1+2.

X=1+2 20

Page 21: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Built-in logical predicates? X is 1+2.

X=3

? X is 4*2.

X=8

?X is 7//3.

X=2

? X is 5, X is 3+3.

no 21

Page 22: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Mathematical operations+ Addition

- Subtraction

* Multiplication

/ Real division

// Integer division

mod modulus

^ Exponent

22

Page 23: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Negation as failurehome(X):-not out(X).

out(ali).

?home(ali).

no

?home(X).

no

?home(zaki).

yes

?out(zaki).

no23

Page 24: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Horn ClausesP1 ^ P2 ^ P3 ^ P4 R

R:- P1, P2, P3, P4.

24

Page 25: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Recursion

A program that calls itself

Assume the following relations and attempt to find predecessor

parent(ali,ahmad).

parent(ahmad,fatema).

parent(fatema,osama).

25

Page 26: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Recursion• A non recursion version

predecessor(X,Y):- parent (X,Y).

predecessor(X,Y):- parent (X,Z), parent(Z,Y).

predecessor(X,Y):- parent (X,Z1), parent (Z1,Z2), parent(Z2,Y).

• The above program is applicable to a limited number of generations.

26

Page 27: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Recursion• Recursion version

predecessor(X,Y):- parent (X,Y).

predecessor(X,Y):- parent (X,Z), predecessor(Z,Y).

27

Page 28: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Recursion? predecessor(ali,ahmad).

yes

? predecessor(ali,fatema).

yes

? predecessor(ali,osama).

yes.

? predecessor(ali,X).

X=ahmad 28

Page 29: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Recursion• Factorial

29

0N if1)!-(N*N

0 1!

NifN

Page 30: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

RecursionFactorial

• fact(0,1).

• fact(N,X):-

N>0, N1 is N-1,

fact(N1,X1),

X is N*X1.

?fact(0,X).

X=1.

?fact(1,X).

X=130

Page 31: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Recursion• HW:

• Write a program to calculate the sum of the numbers:

1, 2, 3, ..., N

31

Page 32: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing• Empty lists are denoted as []

• [H | T] list of :– head H representing the first element.

– tail T represents the rest of the elements.

– [5, 2, 7, 10] H is 5, T is [2, 7, 10]

32

Page 33: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing?[H | T]=[5, 2, 7, 10].

H=5

T=[2, 7, 10]

?[a,b,c,d]=[X,Y | T].

X=a

Y=b

T=[c, d]

33

Page 34: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing?[H | T]=[ali].

H=ali

T=[]

?[H | T]=[].

no

34

Page 35: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - membership• Test for a membership in a list

member(X,[X | T]).

member(X,[ _ | T]):-

member(X,T).

?member(5,[5, 7]).

yes

?member(5,[1, 5]).

yes 35

Page 36: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - membershipTo get all members of a list you can use:

?member(X,[a,b,c,d]).

X=a;

X=b;

X=c;

X=d;

no

36

Page 37: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - count• To count how many members in a list

count([],0).

count([X | T],C):-

count(T,C1),

C is C1+1.

?count([2,4,7],C).

C=3

37

Page 38: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - sum• To calculate the sum of the elements in a list

sum([],0).

sum([X | T],S):-

sum(T, S1),

S is S1+X.

?sum([2,4,7],S).

S=13

38

Page 39: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - append• To append two lists and produce a list containing the

elements of the first list then the elements of the second list

append([], L, L).

append([H | T1], L, [H | T]):

append(T1, L, T).

39

Page 40: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - append?append([r,t],[a,b,c],L).

L=[r,t,a,b,c].

?append(L1,L2,[a,b,c]).

L1=[]

L2=[a,b,c];

L1=[a]

L2=[b,c];

L1=[a,b]

L2=[c];

L1=[a,b,c]

L2=[];

no 40

Page 41: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing - split• To divide a list into two parts positive and negative

positive([],[],[]).

positive([X|Y],[X|L1], L2) :-

X >= 0,

positive(Y,L1, L2) .

positive([X|Y],L1, [X|L2]) :-

positive(Y,L1, L2).

41

Page 42: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

List Processing – write elements• To print the elements of a list

write_a_list([]).

write_a_list([H|T]):-

write(H), nl, write_a_list(T).

? write_a_list([5, 6, 7]).

5

6

7 42

Page 43: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Cut operator• To find a maximum of two numbers, you might

write:

max(X,Y,X):-X>=Y.

max(X,Y,Y).

?max(5,3,M).

M=5;

M=3;

no43

Page 44: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Cut operator• To find a maximum of two numbers, you might

write:

max(X,Y,X):-X>=Y,!.

max(X,Y,Y).

?max(5,3,M).

M=5;

no

44

Page 45: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Cut operator• Add an element to a list only if that element does

not exist already in the list

add(X, L, L):-

member(X,L), !.

add(X, L, [X|L]).

45

Page 46: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Self test list exercises1. Calculate the max/min value in a list.

? min_in_list([5,7,3,7,9], A).

A=3

? max_in_list([8,4,9,4],X).

X=9

46

Page 47: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Self test list exercises2. Calculate how many times a specific

number appears in a list.

3. Take a list as input and return its reverse in another list.

4. Take two lists as input and return their union, intersection, and difference in another list.

5. Delete one number from a list.

47

Page 48: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Homework1. Write a program to calculate the absolute

value of X, absval(X,Y) where X is the input and Y is the output.

2. Write a program to define the even(X) which is true only if X is even.

3. Write a program write_reverse to print the elements of a list in a reverse order

4. Write a program that simulates repeat assuming repeat does not exist.

48

Page 49: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

PROLOG Self test examples

?concatenate([2, 3, 5], [7, 9, 5], B).

B=[2, 3, 5, 7, 9, 5]

?order([2, 8, 3, 5], B).

B=[2, 3, 5, 8]

49

Page 50: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

The fail predicatecountry(jordan).

country(egypt).

country(england).

print_countries:-

country(X),

write(X),

nl,

fail.

print_countries.50

Page 51: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

The fail predicate

?print_countries.

jordan

egypt

england

yes

51

Page 52: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

The repeat command

do:-

repeat, read(X), square(X).

do.

square(stop):-!.

square(X):-

Y is X*X, write(Y), nl, fail.

52

Page 53: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

The repeat command

?do.

5.

25

6.

36

stop.

yes53

Page 54: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

The repeat commanddo:-

read(X), square(X).

square(stop).

square(X):-

X\==stop,

Y is X*X,

write(Y), nl,

do.

54

Page 55: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Input / output procedures - write

?X=5, write(X).

5

yes

?write([a,b,5,6]).

[a,b,5,6]

yes55

Page 56: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Input / output procedures - write

?write(date(16,11,2008)).

date(16,11,2008)

yes

56

Page 57: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Input / output procedures - read

?read(X).

a.

X=a.

?read(stop).

test

no57

Page 58: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Dynamic programs

• Programs that are able to modify themselves.

• Modification means the ability to add/ remove facts at runtime.

• Such procedures include:– assert(C)– retract(C)– abolish(C)

58

Page 59: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Dynamic programs - assert

• assert(C)

?parent(zaid, faris).

no

?assert(parent(zaid, faris)).

yes

?parent(zaid, faris).

yes59

Page 60: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Dynamic programs - assert

• asserta(C) adds the new statement C to the beginning of the program.

• assertz(C) adds the new statement C to the end of the program.

60

Page 61: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Dynamic programs - retract

• retract removes a statement from the program

?retract(parent(zaid, faris)).

yes

?parent(zaid, faris).

no

61

Page 62: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Dynamic programs - abolish

?abolish(parent,2).

yes

removes all statements defined as parent with arity 2.

62

Page 63: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structuresemployee(Name,House_no,Street_name,City_name, Day,Month,Year).

employee(ali,5,salt_street,amman,12,10,1980).

?employee(ali,No,S,C,D,M,Y).

No=5

S=salt_street

C=amman

D=12

M=10

Y=1980

63

Page 64: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structuresemployee(ali,address(5,salt_street,amman),date(12,10,1980)).

employee(hasan,address(12,university_street,zarqa),date(7,3,1985)).

employee(samer,address(9,madina_street,amman),date(2,9,1987)).

?employee(hasan,A,D).

A=address(12,university_street,zarqa)

D=date(7,3,1985)

?employee(Name,address(No,St,amman),_).

Name=ali

No=5

St=salt_street;

Name=samer

No=9

St=madina_street;

false

no

64

Page 65: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures?employee(Name,A,date(_,_,1985)).

Name=hasan

A=address(12,university_street,zarqa);

no

?employee(ali,A,date(Day, Month, Year)), 2011- Year>25.

A=address(5,salt_street,amman)

Day=12

Month=10

Year=1980;

no 65

Page 66: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees

66

2

5

10

7 9

3

b

a

c

a

Example 3

Example 2

Example 1

Page 67: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees

• Binary trees when empty are called nil.

• It may contain 3 components:– Left subtree

– Root

– Right subtree

• It can be represented using the following structure:bin_tree(Left_subtree, Root, Right_subtree).

67

Page 68: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees

bin_tree(nil,a,nil).

68

a

Example 1

Page 69: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees

bin_tree(bin_tree(nil,b,nil),a, bin_tree(nil,c,nil)).

69

Example 2

b

a

c

Page 70: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees

bin_tree(bin_tree(nil,2,nil),5, bin_tree(bin_tree(nil,7,nil),10, bin_tree(nil,9, bin_tree(nil,3,nil)))). 70

Example 3

2

5

10

7 9

3

Page 71: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees - Count

count(nil,0).

count(bin_tree(Left,Root,Right),C):-

count(Left,C1),

count(Right,C2),

C is C1+C2+1.

71

Page 72: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees – Count V2

count(nil,0).

count(bin_tree(Left,_,Right),C):-

count(Left,C1),

count(Right,C2),

C is C1+C2+1.

72

Page 73: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees - Sum

sum(nil,0).

sum(bin_tree(Left,Root,Right),S):-

sum(Left,S1),

sum(Right,S2),

S is S1+S2+Root.

73

Page 74: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees - depth

depth(nil,0).

depth(bin_tree(Left,Root,Right),D):-

depth(Left,D1),

depth(Right,D2),

D1>=D2,

D is D1+1.

depth(bin_tree(Left,Root,Right),D):-

depth(Left,D1),

depth(Right,D2),

D2>D1,

D is D2+1. 74

Page 75: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Structures – Binary Trees – depth V2

depth(nil,0).

depth(bin_tree(Left,Root,Right),D):-

depth(Left,D1),

depth(Right,D2),

max(D1,D2,M),

D is M+1.

max(X,Y,X):- X>=Y,!.

max(X,Y,Y):- X<Y.

75

Page 76: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

Binary Tree - Hw• Write a PROLOG program max_tree to find

the maximum value in a binary tree (bin_tree) assuming all values are numeric.

• Write a PROLOG program to add a new value to an ordered binary tree.

• Write a PROLOG program to display the values stored in an ordered binary tree in ascending order.

76

Page 77: George F Luger ARTIFICIAL INTELLIGENCE 5th edition Structures and Strategies for Complex Problem Solving Programming in Logic (PROLOG) Luger: Artificial.

PROLOG Resources

http://en.wikibooks.org/wiki/Prolog/Lists

http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html

77