20. Prolog
Exercises
■ Add Two Numbers:
– add(X,0,X):-!.
– add(X,Y,Z):-Y1 is Y-1,add(X,Y1,Z1),Z is Z1+1.
■ GCD:
– gcd(X,X,X):-!.
– gcd(X,Y,Z):- X>Y,W is X-Y,gcd(W,Y,Z),!.
– gcd(X,Y,Z):- X<Y,W is Y-X,gcd(X,W,Z).
2020 By Eng. Joud Khattab
21. Prolog
Exercises
■ What is the type of this code:
– f(0,1).
– f(N,X):-N>0, N1 is N-1, f(N1,S), X is S*N.
– Factorial
■ What is the type of this code:
– p(_,0,1):-!.
– p(X,1,X):-!.
– p(X,N,Result):- N1 = N-1 , p(X,N1,XN1), Result = X * XN1.
– Power
2020 By Eng. Joud Khattab
22. Prolog
List
■ Normal Representation:
– List: [1,2,3]
– Empty List [ ]
– One Item List [X]
■ Ex: [1], [name]
■ General Representation:
– [X|Y]: X is the head, Y is the list of the tail
– Ex: [2,4,6,6]
■ 2 is the head
■ [4,6,6] is the tail
2020 By Eng. Joud Khattab
23. Prolog
List Example
■ Find element in a list:
– find(X,[X|Y]):-!.
– find(X,[Z|Y]):-not(X=Z),find(X,Y).
■ Count the element of a list:
– count(0,[]):-!.
– count(W,[X|Y]):-count(Z,Y), W is X+Z.
■ Delete element from list:
– delete(X,[],[]):-!.
– delete(X,[X|Y],Z):-delete(X,Y,Z),!.
– delete(X,[Y|Z],[Y|W1]):- not(X=Y),delete(X,Z,W1).
2020 By Eng. Joud Khattab
24. Note
■ Rule:
– x(0,[]).
– x(N,[N|L]):- x(N-2,L).
■ Need a stop condition!!!
2020 By Eng. Joud Khattab
28. Question 1
■ What is a fact?
A. A term followed by a period.
B. A term.
C. A structure followed by a period.
2020 By Eng. Joud Khattab
29. Question 1
■ What is a fact?
A. A term followed by a period.
B. A term.
C. A structure followed by a period.
2020 By Eng. Joud Khattab
30. Question 2
■ Can the term in a fact be a variable?
A. Yes.
B. No.
2020 By Eng. Joud Khattab
31. Question 2
■ Can the term in a fact be a variable?
A. Yes.
B. No.
2020 By Eng. Joud Khattab
32. Question 3
■ Which of the following is not a query and doesn’t conform its syntax?
A. ?- student(Lisa, 5).
B. ?- student(Lisa, X), student(Abraham, X).
C. ?- student(Abraham, X)
2020 By Eng. Joud Khattab
33. Question 3
■ Which of the following is not a query and doesn’t conform its syntax?
A. ?- student(Lisa, 5).
B. ?- student(Lisa, X), student(Abraham, X).
C. ?- student(Abraham, X)
2020 By Eng. Joud Khattab
34. Question 4
■ Can a query be made up of more than one subgoal?
A. Yes.
B. No.
2020 By Eng. Joud Khattab
35. Question 4
■ Can a query be made up of more than one subgoal?
A. Yes. ?- student(Lisa, 5), student(John, 3).
B. No.
2020 By Eng. Joud Khattab
36. Question 5
■ What is a free variable?
A. A variable which has not been given a value.
B. A variable which has been given a value.
2020 By Eng. Joud Khattab
37. Question 5
■ What is a free variable?
A. A variable which has not been given a value.
B. A variable which has been given a value.
2020 By Eng. Joud Khattab
38. Question 6
■ What is a bound variable?
A. A variable which has not been given a value.
B. A variable which has been given a value.
2020 By Eng. Joud Khattab
39. Question 6
■ What is a bound variable?
A. A variable which has not been given a value.
B. A variable which has been given a value.
2020 By Eng. Joud Khattab
40. Question 7
■ Rules:
– check(x):- console::write(one).
– check(x):- ! , console::write(two).
■ What is the output of this query? check(1).
A. one
B. two
C. one two
D. else
2020 By Eng. Joud Khattab
41. Question 7
■ Rules:
– check(x):- console::write(one).
– check(x):- ! , console::write(two).
■ What is the output of this query? check(1).
A. one
B. two
C. one two
D. else
2020 By Eng. Joud Khattab
45. Search Strategies
(الخوارزميات مقارنة معايير)
■ A search strategy is defined by picking the order of node expansion.
■ Strategies are evaluated along the following dimensions:
– Completeness:
■ does it always find a solution if one exists?
– Optimality:
■ does it always find a least-cost solution?
– Time complexity:
■ number of nodes generated.
– Space complexity:
■ maximum number of nodes in memory.
2020 By Eng. Joud Khattab
46. Search Strategies
(الخوارزميات مقارنة معايير)
■ Time and space complexity are measured in terms of:
– b (التفرع :)معامل
■ maximum branching factor of the search tree.
– d (البحث شجرة ضمن الحل :)عمق
■ depth of the least-cost solution.
– m (البحث شجرة :)عمق
■ maximum length of any path in the state space (may be infinite).
■الحافة(Fringe:)
–بعد زيارتها تتم لم ولكن اكتشافها تم التي العقد مجموعة هي.
–البحث استراتيجيات باختالف تختلف معطيات بنية ضمن العقد هذه تخزين يتمضمن
الشجرة.
2020 By Eng. Joud Khattab
48. Uninformed Search Strategies
(العمياء البحث )خوارزميات
■ Uninformed search strategies (Blind Search) use only the information available in
the problem definition.
■ Types:
1. Breadth-first search.
2. Uniform-cost search.
3. Depth-first search.
4. Iterative deepening search.
…
2020 By Eng. Joud Khattab
52. Uninformed Search Strategies
Breadth First Search
■ Complete?
– Yes (if branching factor b is finite).
■ Optimal?
– Yes, if cost = 1 per step
■ Time?
– O(bd)
■ Space?
– O(bd)
– Space is the bigger problem (more than time)
2020 By Eng. Joud Khattab
55. Uninformed Search Strategies
Uniform Cost Search
■ Notes:
– Expand least-cost unexpanded node.
– Equivalent to breadth-first if step costs all equal.
■ Complete?
– Yes, if step cost is greater than some positive constant ε
■ Optimal?
– Yes, nodes expanded in increasing order of path cost.
■ Time?
– O(bc/ε)
–cو ،األمثل الحل كلفة هي𝜀الخطوة كلفة وسطي.
– This can be greater than O(bd): the search can explore long paths consisting of small steps before
exploring shorter paths consisting of larger steps
■ Space?
– O(bc/ε)
2020 By Eng. Joud Khattab
58. Uninformed Search Strategies
Depth First Search
■ Complete?
– Fails in infinite-depth spaces.
– complete in finite spaces.
■ Optimal?
– No, returns the first solution it finds.
■ Time?
– O(bm)
■ Space?
– O(bm)
2020 By Eng. Joud Khattab
59. Uninformed Search Strategies
Iterative Deepening Search
■ Use DFS as a subroutine:
1. Check the root.
2. Do a DFS searching for a path of length 1
3. If there is no path of length 1, do a DFS searching for a path of length 2
4. If there is no path of length 2, do a DFS searching for a path of length 3…
2020 By Eng. Joud Khattab
66. Informed Search
( التجريبية البحث خوارزميات-المطلعة )
■ Idea:
– Give the algorithm “hints” about the desirability of different states.
– Use an evaluation function to rank nodes and select the most promising one for
expansion.
■ Types:
1. Greedy best-first search.
2. A* search.
2020 By Eng. Joud Khattab
74. Informed Search
Greedy best-first search Properties
■ Complete?
– No, can get stuck in loops
■ Optimal?
– No
■ Time?
– Worst case: O(bm)
– Best case: O(bd)
■ If h(n) is 100% accurate
■ Space?
– Worst case: O(bm)
start
goal
2020 By Eng. Joud Khattab
75. Hill Climbing Search
(التلة تسلق )خوارزمية
■التابع لدينا ليكنH(n)من ًانطالقا للهدف الوصول ككلفة التقديرية القيمة يرد والذيالعقدة
الحالية.
■للتابع األقل القيمة ذات العقدة اختيار على تعتمد التلة تسلق خوارزمية إنH(n)
"طموحة خوارزمية."
■ Is it complete/optimal?
– No – can get stuck in local optima
■ Example:
– local optimum for the 8-queens problem
2020 By Eng. Joud Khattab
78. Informed Search
A* Search
■ Idea:
– avoid expanding paths that are already expensive.
■ The evaluation function f(n) is the estimated total cost of the path through node n to
the goal:
■ f(n) = g(n) + h(n)
– g(n): cost so far to reach n (path cost)
– h(n): estimated cost from n to goal (heuristic)
2020 By Eng. Joud Khattab
87. Informed Search
A* Search Properties
■ Complete?
– Yes, unless there are infinitely many nodes with f(n) ≤ C*
■ Optimal?
– Yes
■ Time?
– Number of nodes for which f(n) ≤ C* (exponential)
■ Space?
– Exponential
2020 By Eng. Joud Khattab
91. Comparison of search strategies
Algorithm Complete? Optimal?
Time
Complexity
Space
Complexity
BFS Yes
If all step costs
are equal
O(bd) O(bd)
UCS Yes Yes
O(bc/ε)
Number of nodes with g(n) ≤ C*
DFS No No O(bm) O(bm)
IDS Yes
If all step costs
are equal
O(bd) O(bd)
Greedy No No
Worst case: O(bm)
Best case: O(bd)
A* Yes Yes Number of nodes with g(n)+h(n) ≤ C*
2020 By Eng. Joud Khattab
92. Question 1
■ Which search strategy is also called as blind search?
A. Uninformed search.
B. Informed search.
C. Simple reflex search.
D. All of the mentioned.
2020 By Eng. Joud Khattab
93. Question 1
■ Which search strategy is also called as blind search?
A. Uninformed search.
B. Informed search.
C. Simple reflex search.
D. All of the mentioned.
2020 By Eng. Joud Khattab
94. Question 2
■ How many types are available in uninformed search method?
A. 3.
B. 4.
C. 5.
D. 6.
2020 By Eng. Joud Khattab
95. Question 2
■ How many types are available in uninformed search method?
A. 3.
B. 4.
C. 5.
D. 6.
■ Explanation:
– The five types of uninformed search method are:
■ Breadth-first, Uniform-cost, Depth-first, Depth-limited and Bidirectional search.
2020 By Eng. Joud Khattab
96. Question 3
■ Which search is implemented with an empty first-in-first-out queue?
A. Depth-first search.
B. Breadth-first search.
C. Bidirectional search.
D. None of the mentioned.
2020 By Eng. Joud Khattab
97. Question 3
■ Which search is implemented with an empty first-in-first-out queue?
A. Depth-first search.
B. Breadth-first search.
C. Bidirectional search.
D. None of the mentioned.
2020 By Eng. Joud Khattab
98. Question 4
■ When is breadth-first search is optimal?
A. When there is less number of nodes.
B. When all step costs are equal.
C. When all step costs are unequal.
D. None of the mentioned.
2020 By Eng. Joud Khattab
99. Question 4
■ When is breadth-first search is optimal?
A. When there is less number of nodes.
B. When all step costs are equal.
C. When all step costs are unequal.
D. None of the mentioned.
2020 By Eng. Joud Khattab
100. Question 5
■ What is the space complexity of Depth-first search?
A. O(b).
B. O(bl).
C. O(m).
D. O(bm).
2020 By Eng. Joud Khattab
101. Question 5
■ What is the space complexity of Depth-first search?
A. O(b).
B. O(bl).
C. O(m).
D. O(bm).
2020 By Eng. Joud Khattab
102. Question 6
■ How many parts does a problem consists of?
A. 1.
B. 2.
C. 3.
D. 4.
2020 By Eng. Joud Khattab
103. Question 6
■ How many parts does a problem consists of?
A. 1.
B. 2.
C. 3.
D. 4.
■ Explanation:
– The four parts of the problem are:
■ initial state, set of actions, goal test and path cost.
2020 By Eng. Joud Khattab
104. Question 7
■ Which algorithm is used to solve any kind of problem?
A. Breath-first algorithm.
B. Tree algorithm.
C. Bidirectional search algorithm.
D. None of the mentioned.
2020 By Eng. Joud Khattab
105. Question 7
■ Which algorithm is used to solve any kind of problem?
A. Breath-first algorithm.
B. Tree algorithm.
C. Bidirectional search algorithm.
D. None of the mentioned.
■ Explanation:
– Tree algorithm is used because specific variants of the algorithm embed
different strategies.
2020 By Eng. Joud Khattab
106. Question 8
■ Which search algorithm imposes a fixed depth limit on nodes?
A. Depth-limited search.
B. Depth-first search.
C. Iterative deepening search.
D. Bidirectional search.
2020 By Eng. Joud Khattab
107. Question 8
■ Which search algorithm imposes a fixed depth limit on nodes?
A. Depth-limited search.
B. Depth-first search.
C. Iterative deepening search.
D. Bidirectional search.
2020 By Eng. Joud Khattab
108. Question 9
■ Which search implements stack operation for searching the states?
A. Depth-limited search.
B. Depth-first search.
C. Breadth-first search.
D. None of the mentioned.
2020 By Eng. Joud Khattab
109. Question 9
■ Which search implements stack operation for searching the states?
A. Depth-limited search.
B. Depth-first search.
C. Breadth-first search.
D. None of the mentioned.
2020 By Eng. Joud Khattab
110. Question 10
■ Which of the following is/are Uninformed Search technique/techniques
A. Breath First Search (BFS).
B. Depth First Search (DFS).
C. Bi-directional Search.
D. All of the mentioned.
2020 By Eng. Joud Khattab
111. Question 10
■ Which of the following is/are Uninformed Search technique/techniques
A. Breath First Search (BFS).
B. Depth First Search (DFS).
C. Bi-directional Search.
D. All of the mentioned.
2020 By Eng. Joud Khattab
112. Question 11
■ The time and space complexity of BFS
A. O(bd+1) and O(bd+1).
B. O(b2) and O(d2).
C. O(d2) and O(b2).
D. O(d2) and O(d2).
2020 By Eng. Joud Khattab
113. Question 11
■ The time and space complexity of BFS
A. O(bd+1) and O(bd+1).
B. O(b2) and O(d2).
C. O(d2) and O(b2).
D. O(d2) and O(d2).
2020 By Eng. Joud Khattab
114. Question 12
■ What is the heuristic function of greedy best-first search?
A. f(n) != h(n)
B. f(n) < h(n)
C. f(n) = h(n)
D. f(n) > h(n)
2020 By Eng. Joud Khattab
115. Question 12
■ What is the heuristic function of greedy best-first search?
A. f(n) != h(n)
B. f(n) < h(n)
C. f(n) = h(n)
D. f(n) > h(n)
2020 By Eng. Joud Khattab
116. Question 13
■ Heuristic function h(n) is ____
A. Lowest path cost
B. Cheapest path from root to goal node
C. Estimated cost of cheapest path from root to goal node
D. Average path cost
2020 By Eng. Joud Khattab
117. Question 13
■ Heuristic function h(n) is ____
A. Lowest path cost
B. Cheapest path from root to goal node
C. Estimated cost of cheapest path from root to goal node
D. Average path cost
2020 By Eng. Joud Khattab
135. Alpha Beta Pruning
■ Pruning does not affect final result.
■ Amount of pruning depends on move ordering:
– Should start with the “best” moves (highest-value for MAX or
lowest-value for MIN)
– For chess, can try captures first, then threats, then forward
moves, then backward moves.
– Can also try to remember “killer moves” from other branches of
the tree
■ With perfect ordering, branching factor can be cut in two, or
depth of search effectively doubled.
2020 By Eng. Joud Khattab
136. Question 1
■ Which search is equal to minimax search but eliminates the branches that can’t
influence the final decision?
A. Depth-first search
B. Breadth-first search
C. Alpha-beta pruning
D. None of the mentioned
2020 By Eng. Joud Khattab
137. Question 1
■ Which search is equal to minimax search but eliminates the branches that can’t
influence the final decision?
A. Depth-first search
B. Breadth-first search
C. Alpha-beta pruning
D. None of the mentioned
■ Explanation:
– The alpha-beta search computes the same optimal moves as minimax, but
eliminates the branches that can’t influence the final decision.
2020 By Eng. Joud Khattab
138. Question 2
■ Which search is similar to minimax search?
A. Hill-climbing search
B. Depth-first search
C. Breadth-first search
D. All of the mentioned
2020 By Eng. Joud Khattab
139. Question 2
■ Which search is similar to minimax search?
A. Hill-climbing search
B. Depth-first search
C. Breadth-first search
D. All of the mentioned
■ Explanation:
– The minimax search is depth-first search, So at one time we just have to
consider the nodes along a single path in the tree.
2020 By Eng. Joud Khattab
142. Question 4
■الالعب أن وحيث التالية اللعب شجرة لتكنMAXيبدأ الذي هو.ألفا الخوارزمية تقوم-بيتا
التالية األوراق قيم بحساب:
.AP, Q, R, T, U, V, X, Y, Z, BB, CC, DD
.BP, Q, R, T, U, X, Y, Z, BB, CC
.CP, Q, R, T, U, V, X, Y, Z, BB
2020 By Eng. Joud Khattab
143. Question 4
■الالعب أن وحيث التالية اللعب شجرة لتكنMAXيبدأ الذي هو.ألفا الخوارزمية تقوم-بيتا
التالية األوراق قيم بحساب:
.AP, Q, R, T, U, V, X, Y, Z, BB, CC, DD
.BP, Q, R, T, U, X, Y, Z, BB, CC
.CP, Q, R, T, U, V, X, Y, Z, BB
2020 By Eng. Joud Khattab
151. And Or Graph
■ Problem Reduction:
– So far we have considered search strategies for OR graphs through which we
want to find a single path to a goal.
– Such structure represent the fact that we know how to get from a node to a
goal state if we can discover how to get from that node to a goal state along any
one of the branches leaving it.
2020 By Eng. Joud Khattab
152. And Or Graph
■ AND-OR (Graph or Tree)
– It is useful for representing the solution of problems that can solved by
decomposing them into a set of smaller problems, all of which must then be
solved.
– This decomposition, or reduction, generates arcs that we call AND arcs.
– One AND arc may point to any number of successor nodes, all of which must be
solved in order for the arc to point to a solution.
– Just as in an OR graph, several arcs may emerge from a single node, indicating
a variety of ways in which the original problem might be solved.
– This is why the structure is called not simply an AND-graph but rather an AND-
OR graph (which also happens to be an AND-OR tree).
2020 By Eng. Joud Khattab
155. AO* Algorithm
■خوارزمية بين الرئيسي الفرقA*وAO*
–ال أن هوA*خوارزمية هيللOR Graphفقط.
–ال أماAO*خوارزمية هيللAND-OR Graphفقط.
A
B C D
38
E F G H I J
17 9 27
(5) (10) (3) (4) (15) (10)
A
B
C
D(3)
(4)
(5)
(9)
2020 By Eng. Joud Khattab
182. Validity and Satisfiability
■ A sentence is valid / tautology ()صالحة if it is true in all models
– Ex: True, A A, A A, (A (A B)) B
■ A sentence is satisfiable (للتحقيق )قابلة if it is true in some model
– Ex: A B, C
■ A sentence is unsatisfiable (للتحقيق قابلة )غير if it is true in no models
– Ex: A A
■ Validity is connected to inference via the Deduction Theorem:
– KB ╞ α if and only if (KB α) is valid
■ Satisfiability is connected to inference via the following:
– KB ╞ α if and only if (KB α) is unsatisfiable
2020 By Eng. Joud Khattab
193. االستنتاج و االستتباع
■االستتباع:الحقيقة جدول نستخدم:
■الحاالت جميع أجل من صحيحة برهانها المطلوب القضية أن الحقيقة جدول من نالحظ
(A (A ⇒ B)) ⇒ BA (A ⇒ B)A ⇒ BBA
10100
10110
10001
11111
2020 By Eng. Joud Khattab
194. االستنتاج و االستتباع
■االستنتاج:النظامي الشكل إلى العبارة لنحول:
– (A (A ⇒ B)) ⇒ B ➔
– (A ( A B)) B ➔
– A ( A B) B ➔
– A (A B) B ➔
– ((A A) (A B)) B ➔
– (A B) B ➔
– A true ➔
– true
2020 By Eng. Joud Khattab
195. Question 1
■التالية القواعد مجموعة لدينا لتكن:
– P ⇒ (R S)
– P ⇒ (R S)
– S
– (R U) ⇒ Q
■برهان سبق مما يمكن:
A. Q
B. Q
C. S
D. صحيح خيار يوجد ال
2020 By Eng. Joud Khattab
196. Question 1
■التالية القواعد مجموعة لدينا لتكن:
– P ⇒ (R S)
– P ⇒ (R S)
– S
– (R U) ⇒ Q
■برهان سبق مما يمكن:
A. Q
B. Q
C. S
D. صحيح خيار يوجد ال
2020 By Eng. Joud Khattab
197. Question 2
■للتحقيق القابلة غير الصيغة حددUnsatisfiableيلي فيما:
A. ((P Q) ⇒ (P ⇒ Q))
B. ((P ⇒ Q) (P Q))
C. ( ( P Q ) ⇒ ( R ⇒ ( R ⇒ Q ) ) )
D. صحيح خيار يوجد ال
2020 By Eng. Joud Khattab
198. Question 2
■للتحقيق القابلة غير الصيغة حددUnsatisfiableيلي فيما:
A. ((P Q) ⇒ (P ⇒ Q))
B. ((P ⇒ Q) (P Q))
C. ( ( P Q ) ⇒ ( R ⇒ ( R ⇒ Q ) ) )
D. صحيح خيار يوجد ال
2020 By Eng. Joud Khattab
199. Question 3
■يلي كما كتابتها يمكن والتي ،التالية الحقائق مجموعة لدينا لتكن:
■ Choose one of three roads: short, medium or long short medium long
■ The short road is always crowded short ⇒ crowded
■ The medium road is not comfortable, but fast medium ⇒ ( comfortable fast)
■ The long road is comfortable long ⇒ comfortable
■ The chosen road should be comfortable comfortable
■الطويل الطريق هو المختار الطريق أن برهان السابقة الخمسة الحقائق باستخدام يمكنlong
A. False
B. True
C. الطريق طول حسب
D. السيارة سرعة حسب
2020 By Eng. Joud Khattab
200. Question 3
■يلي كما كتابتها يمكن والتي ،التالية الحقائق مجموعة لدينا لتكن:
■ Choose one of three roads: short, medium or long short medium long
■ The short road is always crowded short ⇒ crowded
■ The medium road is not comfortable, but fast medium ⇒ ( comfortable fast)
■ The long road is comfortable long ⇒ comfortable
■ The chosen road should be comfortable comfortable
■الطويل الطريق هو المختار الطريق أن برهان السابقة الخمسة الحقائق باستخدام يمكنlong
A. False
B. True
C. الطريق طول حسب
D. السيارة سرعة حسب
2020 By Eng. Joud Khattab
201. Question 4
■التالية للصيغة النظامي الشكل:
■ { [ Smoke ⇒ Fire ] ⇒ [ ( Smoke Heat ) ⇒ Fire ] }
A. True
B. False
C. Smoke Fire
D. ذلك غير
2020 By Eng. Joud Khattab
202. Question 4
■التالية للصيغة النظامي الشكل:
■ { [ Smoke ⇒ Fire ] ⇒ [ ( Smoke Heat ) ⇒ Fire ] }
A. True
B. False
C. Smoke Fire
D. ذلك غير
2020 By Eng. Joud Khattab
203. Question 5
■التالية للصيغة النظامي الشكل:
■ ( q ( p ⇒ q ) ) ⟺ p
A. True
B. False
C. p
D. ذلك غير
2020 By Eng. Joud Khattab
204. Question 6
■التالية للصيغة النظامي الشكل:
■ [ ( P Q ) ( P Q ) R ] ⇒ [ Q R ]
A. True
B. False
C. R
D. P R
2020 By Eng. Joud Khattab
205. Question 6
■التالية للصيغة النظامي الشكل:
■ [ ( P Q ) ( P Q ) R ] ⇒ [ Q R ]
A. True
B. False
C. R
D. P R
2020 By Eng. Joud Khattab