0% found this document useful (0 votes)
25 views

371CPE Lectures Part1

Uploaded by

ayyash187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

371CPE Lectures Part1

Uploaded by

ayyash187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

King Khalid UNIVERSITY

College of Computer Science


Department of Computer Engineering

371CPE: Intelligent Systems

Dr. Mohammad Yahya Hadi Alshamri


2024/2025

Student Name

Student ID
References
• Adrian A. Hopgood, Intelligent Systems for Engineers and Scientists: A
Practical Guide to Artificial Intelligence, 4th edition, CRC Press, 2021.
ISBN-13:978-0367336165.
• C. Grosan and A. Abraham, Intelligent Systems: A Modern Approach,
Springer, 2011, e-ISBN 978-3-642-21004-4.

Student Learning Outcomes


NQF Learning Domains and Course Learning Outcomes
Knowledge and understanding
Define and explain the fundamental principles of intelligent systems
Outline contemporary issues for designing intelligent systems.
Cognitive Skills
Design intelligent systems for applications to meet specific requirements
Conduct experiments to solve Intelligent system problems
Demonstrate how to communicate intelligent systems’ recent ideas.
Values
Describe different codes of ethics for building and using Intelligent systems
Demonstrate lifelong learning to understand recent techniques of intelligent systems

Marks Distribution
Quizzes Assignments Mid Exam 1 Lab Final
15 5 20 20 40
Table of Contents
Chapter 0: Discrete Structures: Revisited
0.1 Data Structures: Revisited
0.2 Blind Search Algorithms: Revisited
0.3 Heuristic Search Algorithms: Revisited
0.4 Propositional Logic: Revisited
0.5 Predicate Logic: Revisited
0.6 Inference (Reasoning): Revisited

Chapter 1:
1.1 Intelligent Systems: An Introduction
1.2 Machine Learning: An Introduction

Chapter 2:
2.1 Knowledge-Based Systems
2.2 Decision Trees

Chapter 3:
3.1 Intelligent Agent
3.2 Goal-based Problem-Solving Agent

Chapter 4:
4.1 Fuzzy Sets and Fuzzy Logic
4.2 Fuzzy Expert Systems

Chapter 5:
5.1 Neural Networks
5.2 Deep Learning: An Introduction

Chapter 6:
6.1 Interpretation and Diagnosis Intelligent Systems
6.2 Design and Selection Intelligent Systems
6.3 Planning Intelligent Systems
6.4 Control Intelligent Systems
Intelligent Systems, 2024 Chapter 0.1 Dr. Mohammad Alshamri

Data Structures: Revisited


Basic Features of Stack
1. Stack is an ordered list of similar data type.
2. In stack, elements are stored and accessed in Last In First Out (LIFO) manner
(elements are added to the top of the stack and removed from the top of the stack).
3. The main stack operations:
push(data) adds an element to the top of the stack. The top is increased by 1.
pop() removes an element from the top of the stack. The top is decreased by 1.
peek() returns a copy of the element on the top of the stack without removing it.
is_empty() checks whether a stack is empty. Stack is in Underflow state if it is completely empty.
When the stack is empty the value of the top variable is -1.
is_full() checks whether a stack is at maximum capacity when stored in a static (fixed-size)
structure. Stack is said to be in Overflow state when it is completely full.

4. Static array implementation

Computer Engineering Department - College of Computer Science Page 1


Intelligent Systems, 2024 Chapter 0.1 Dr. Mohammad Alshamri

Analysis of Stack Operations


The time complexities for various operations that can be performed on the Stack data
structure are:
• Push Operation: 𝑂(1) • Top Operation: 𝑂(1)
• Pop Operation: 𝑂(1) • Search Operation: 𝑂(𝑛)

Basic Features of Queue


1. Queue is an ordered list of elements of similar data types.
2. Queue is a FIFO (First In First Out) structure, which means that element inserted first
will also be removed first.
3. The first element in queue is inserted from one end called REAR (also called tail),
and the deletion of existing element takes place from the other end called as FRONT
(also called head).
4. The process to add an element into queue is called Enqueue and the process of
removal of an element from queue is called Dequeue.
5. The main queue operations:
enqueue(data) add an element to the queue. The rear index pointer will be incremented every time you
add an item to the queue
dequeue() return an element from the front of the queue
is_empty() check whether the queue is empty
is_full() check whether the queue is at maximum capacity (if the size of the queue is
constrained)

Computer Engineering Department - College of Computer Science Page 2


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

5. Linear queue

Analysis of Queue Operations


The time complexities for various operations that can be performed on the Queue are:
• Enqueue Operation: 𝑂(1) • Dequeue Operation: 𝑂(1)

Computer Engineering Department - College of Computer Science Page 3


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

Tree Data Structure


Tree is a non-linear data structure. Tree organizes data hierarchically.
Root The topmost node in a tree.
Child A node directly connected to another node when moving away from the Root.
Parent The converse notion of a child.
Siblings A group of nodes with the same parent.
Descendant A node reachable by repeated proceeding from parent to child
Ancestor A node reachable by repeated proceeding from child to parent.
Leaf (External node) A node with no children.
Branch (Internal node) A node with at least one child.
Degree The number of subtrees of a node.
Edge The connection between one node and another.
Path A sequence of nodes and edges connecting a node with a descendant.
Level (Depth) The node distance from the root (the number of edges). The root is level 0.
Height(node) The number of edges on the longest path between that node and a leaf.
Height(tree) The length of the longest path to a leaf. The maximum level in a tree.
Forest A forest is a set of n ≥ 0 disjoint trees.

Binary Trees
A binary tree is a tree data structure in
which each node has at the most two
children, which are referred to as the left
child and the right child.

Computer Engineering Department - College of Computer Science Page 4


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

Tree Traversal
We have two options here:
• Depth-First Search (DFS): It starts at the root and explores as far as possible along
each branch before backtracking.
• Breadth-First Search (BFS): It starts at the tree root and explores the neighbor
nodes first, before moving to the next level neighbors.

Depth-First Search (DFS)


▪ DFS explores a path all the way to a leaf before backtracking and exploring
another path.
▪ Types of DFS: pre-order, in-order, and post-order.

Computer Engineering Department - College of Computer Science Page 5


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

Inorder Traversal
Visit the nodes in the left subtree of a node, then visit the node, and then visit the nodes
in the right subtree of the node.

Postorder Traversal
Visit the nodes in the left subtree of a node, then visit the nodes in the right subtree of the
node, and then visit the node.

Preorder Traversal
Visit a node, then visit the nodes in the left subtree of that node, and then visit the nodes
in the right subtree of the node.

Example 1

Computer Engineering Department - College of Computer Science Page 6


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

DFS Tree Traversal: An Easy Way

Breadth-First Search (BFS)


BFS algorithm traverses the tree level by level and depth by depth.

Example2

BFS traverses level by level. In this example, the result is 1–2–5–3–4–6–7.

Computer Engineering Department - College of Computer Science Page 7


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

Graph Data Structure


▪ A graph is a pictorial representation of a
set of objects where some pairs of objects
are connected by links.
▪ The interconnected objects are represented
by points termed as vertices, and the links
that connect the vertices are called edges.
▪ Formally, a graph is a pair of sets (V,E),
where V is the set of vertices and E is the
set of edges, connecting the pairs of
vertices.

Example 3

Expanding a Node
Expanding a node means:
✓ generating all the successor nodes of it, and
✓ adding them and their associated vertices to the state space graph.

Example 4
The following shows how to expand nodes
Arad and Sibiu.

Computer Engineering Department - College of Computer Science Page 8


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

DFS Graph Search (Start Node)


Create empty VISITED array
Add the starting node to VISITED array, PUSH it to STACK and PRINT it out
while STACK is not empty:
Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F and add it to VISITED array.
PUSH it to STACK and PRINT it out.
end

Example 5 (DFS)

Computer Engineering Department - College of Computer Science Page 9


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

BFS Graph Search (Start Node)


Create empty VISITED array
Add the starting node to VISITED array
Enqueue the starting node to QUEUE
while QUEUE is not empty
Dequeue the first node F from QUEUE
PRINT node F
Expand node F
For each unvisited child of F
Add the child to VISITED array
Enqueue the child to QUEUE
End
end

Computer Engineering Department - College of Computer Science Page 10


Intelligent Systems, 2021 Chapter 1.2 Dr. Mohammad Alshamri

Example 6 (BFS)
To avoid processing a node more than once, we use a Boolean visited array.

Computer Engineering Department - College of Computer Science Page 11


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Blind Search Algorithms


1. A state space is usually represented by a graph or a tree, where nodes represent
the search state and arcs represent operators applied to change state.
2. Tree has one and only one path from any point to any other point.
3. Graph has several paths to a given node.

Specifying a Search Problem


1. Initial state: Starting state of the problem [where the search starts].
2. Operator: Function taking one state to another state.
3. Goal state (Final state): Finish state.

No information about the problem other than its Some guidance on where to look for solutions is
definition is given. given.

1. Breadth First Search


2. Depth First Search 1. Greedy search
3. Uniform Cost search 2. A* Search
4. Depth Limied Search

Important Terms
Branching factor (𝒃): it is the maximum number of successors of any node.

Maximum depth (𝒎): It is the maximum depth of a leaf node.

Shallowest depth (𝒅): It is the depth of the shallowest solution.

Computer Engineering Department - College of Computer Science Page 1


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Example 2 (search tree for the 8-puzzle problem)

Breadth-First Search
BFS expands all the nodes at a given depth in the search tree before any nodes at the next
level are expanded.

BFS-tree Algorithm (Start Node)


Create an empty VISITED array
Add the starting node to VISITED array
Enqueue the starting node to QUEUE

Computer Engineering Department - College of Computer Science Page 2


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Make the starting node the ROOT of BFS-tree


while QUEUE is not empty:
Dequeue the first node F from QUEUE
Expand node F
For each unvisited child of F
Add the child to its parent in the BFS-tree
Add the child to VISITED array
Enqueue the child to QUEUE
End
end
Example 3
What is the BFS tree of the following
graph if the starting node is A?

Answer 3

BFS Search Algorithm (Start Node, Goal Node)


Create an empty VISITED array
Add the starting node to VISITED array and enqueue it to QUEUE
Make the starting node the root of BFS-tree

Computer Engineering Department - College of Computer Science Page 3


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

IF the starting node is a goal node, EXIT on success


while QUEUE is not empty:
Dequeue the first node F from QUEUE
Expand node F
For each unvisited child of F
Add the child to its parent in the BFS-tree
Add the child to VISITED array
Enqueue the child to QUEUE
IF the child is a goal node, EXIT on success
End
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the BFS-tree.

Example 4
What BFS search output of the following graph
if the start node is A and Goal node is G?

Answer 4
Visited
A B C D E F G H I G

QUEUE

The result is: ADG

Depth First Search


DFS always expands the deepest node in the current frontier of the search tree.

DFS-tree Algorithm (Start Node)


Create an empty VISITED array
Add the starting node to VISITED array
PUSH the starting node to STACK
Make the starting node the ROOT of DFS-tree

Computer Engineering Department - College of Computer Science Page 4


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

while STACK is not empty:


Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F:
Add it to VISITED array.
PUSH it to STACK.
Add it to its parent (top node) in the DFS-tree.
END
end

Example 5
What is the DFS tree of the following graph
if the starting node is A?

Answer 5

Computer Engineering Department - College of Computer Science Page 5


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

DFS Search Algorithm (Start Node, Goal Node)


Create an empty VISITED array
Add the starting node to VISITED array
PUSH the starting to STACK
Make the starting node the ROOT of DFS-tree
while STACK is not empty:
IF the top node F is a goal node, THEN EXIT on success
Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F:
Add it to VISITED array.
PUSH it to STACK.
Add it to its parent (top node) in the DFS-tree.
END
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the DFS-tree.

Example 6
What is the DFS search output of the following
graph if the start node is A and the goal node is G?

Answer 6

Visited
A B C D E F G H I G

STACK

The result is: ABEG

Computer Engineering Department - College of Computer Science Page 6


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Example 7
What are the DFS and BFS trees of the
following graph if the starting node is 0?

Answer 7

Example 8
What are the DFS and BFS trees of the
following directed graph if the starting
node is 0?

Answer 8

Computer Engineering Department - College of Computer Science Page 7


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Search Algorithm Performance Evaluation Criteria


▪ Completeness: Is the algorithm guaranteed to find a solution when there is one?
▪ Optimality: Does the strategy find the optimal solution?
▪ Time complexity: How long does it take to find a solution?
▪ Space complexity: How much memory is needed to perform the search?

Example 9
What is the time complexity for BFS of a uniform tree where every state has 𝑏
successors?
Answer 9
L1: The root of the search tree generates 𝑏 nodes.
L2: Each node generates 𝑏 more nodes, for a total of 𝑏 2 .
L3: Each node generates 𝑏 more nodes, yielding 𝑏 3 .
Then the total number of nodes generated is:
1 + 𝑏 + 𝑏 2 + 𝑏 3 + 𝑏 4 + ⋯ + 𝑏 𝑑 = 𝑂(𝑏 𝑑 )

Performance Measures of BFS


Completeness: BFS is complete because it visits all levels given that 𝑑 factor is finite, so in some 𝑑
it will find a solution.
Optimality: BFS is not optimal until all actions have the same cost.
Time Then the total number of nodes generated is:
Complexity: 1 + b + b2 + b3 + b4 + ⋯ + bd = O(bd )
Space As all the nodes must retain in memory while we expand our search, then the space

Computer Engineering Department - College of Computer Science Page 8


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Complexity: complexity is like the time complexity plus the root node

Performance Measures of DFS


Completeness: DFS is not complete. For unbounded tree, the search will continue going deep
infinitely.
Optimality: DFS is not optimal.
Time Complexity: For a state space with branching factor 𝑏 and maximum depth 𝑚, 𝑂(𝑏 𝑚 )
Space ▪ DFS needs to store only the path from the root to the leaf node, beside the
Complexity: siblings of each node on the path.
▪ DFS removes a node from memory once all its descendants have been
expanded.
▪ DFS requires (𝑏𝑚 + 1) then 𝑂(𝑏𝑚)

Example 10
BFS is implemented on 1MHZ processor for a problem with 𝑏 = 110, and 𝑑 = 2. Each
node requires 1000 bytes of storage.
1. What is the time complexity?
2. What is the consumed time?
3. What is space complexity?
4. What is the required memory in KB?

Answer 10

Computer Engineering Department - College of Computer Science Page 9


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

𝑶(𝒃𝒅 ) = 𝑶(𝟏𝟏𝟎𝟐 ) = 𝑶(𝟏𝟐𝟏𝟎𝟎)


The time for each step is
1 1
𝑇= = 6 = 10−6 𝑠𝑒𝑐
𝑓 10
The consumed time is:
𝑇𝑐 = 𝑁𝑇 = 12100 × 10−6 = 0.0121 𝑠𝑒𝑐
The space complexity is:
𝑶(𝒃𝒅 ) = 𝑶(𝟏𝟏𝟎𝟐 ) = 𝑶(𝟏𝟐𝟏𝟎𝟎)
The required memory is:
𝑆 = 𝑁𝑆𝑛 = 12100 × 1000𝐵 = 12100000𝐵 = 11.54𝑀𝐵

Example 11
Repeat Example 10 if 𝑏 = 1000, and 𝑑 = 12?

Example 12
What is the space complexity for DFS with branching factor 𝑏 and maximum depth 𝑚?

Answer 12
The depth-first search requires storage of only 𝑂(𝑏𝑚) nodes.

Uniform Cost Search (UCS) Algorithm


▪ Uniform-cost search expands the node 𝑛 with the lowest path cost, 𝑔(𝑛).
▪ UCS uses a priority queue ordered by 𝑔(𝑛).
▪ UCS does not care about the number of steps a path has, but only about their total
cost.
▪ The goal test is applied to a node when it is selected for expansion rather than
when it is first generated.
▪ UCS tests again a node currently on the priority queue if a better path is found.

Computer Engineering Department - College of Computer Science Page 10


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

▪ UCS will get stuck in an infinite loop if there is a path with an infinite sequence of
zero-cost actions—for example, a sequence of NoOp actions.
▪ When all step costs are the same, UCS is similar to BFS, except that the latter stops
as soon as it generates a goal, whereas UCS examines all the nodes at the goal’s
depth to see if one has a lower cost; thus, UCS does strictly more work by
expanding nodes at depth 𝑑 unnecessarily.

UCS-tree Algorithm (Start Node)


Create an empty VISITED array
Enqueue the starting node to PQUEUE
Make the starting node as the ROOT of UCS-tree with zero cost
do
IF PQUEUE is empty, THEN return FAILURE
Dequeue the first node F from PQUEUE
Add F to VISITED array
Expand node F
For each child of F
cost(child) = cost(parent) + cost(parent-child)
IF child is in PQUEUE with higher cost THEN
Replace the child in PQUEUE with its new cost
Remove the child from the UCS-tree
Add the child to its new parent in the UCS-tree
ELSE IF child is NOT visited OR NOT available in PQUEUE THEN
Enqueue the child with its cost
Add the child to its parent in the UCS-tree
end

Example 13
What is the UCS tree if the start state is Frankfurt?

Computer Engineering Department - College of Computer Science Page 11


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

PQUEUE Visited
F M W K Kar A Mu E N S

UCS Search Algorithm (Start Node, Goal Node)


UCS-tree Algorithm (Start Node)
The SEARCH RESULT is the path from the ROOT to the GOAL in the UCS-tree.

Example 14
What is the UCS search output if the start state is Frankfurt and goal state is Munchen?

Answer 14
The output is Frankfurt – Wurzburg – Nurnberg – Munchen. The cost is 487Km.

Computer Engineering Department - College of Computer Science Page 12


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Example 15
What is the UCS search output if the start state is Sibiu and goal state is Bucharest?

Visited
PQUEUE B/310 S F R P B
← S/0 R/80 F/99 P/177
B/278

The output is Sibiu – Rimnicu Vilcea – Pitesti – Bucharest. The cost is 278Km

Example 16
What is the time complexity of UCS?

Answer 16
𝐶∗ 𝐶∗
1+⌊ ⌋ ⌊ ⌋
𝑂 (𝑏 𝜖 ) ≈ 𝑂 (𝑏 𝜖 )

𝐶 ∗ : is the cost of the optimal solution.


𝜖: is the minimum cost for every action.
∗ /𝜖⌋
𝑂(𝑏1+⌊𝐶 ) can be much greater than 𝑂(𝑏𝑑 )

Performance Measures of UCS


Completeness: Yes, if step cost ≥ 𝑒.
Optimality: Yes, for any step cost
∗ ∗
Time Complexity: 𝑂(𝑏1+⌊𝐶 /𝜖⌋ ) ≈ 𝑂(𝑏 ⌊𝐶 /𝜖⌋ )
∗ ∗
Space Complexity: 𝑂(𝑏1+⌊𝐶 /𝜖⌋ ) ≈ 𝑂(𝑏 ⌊𝐶 /𝜖⌋ )

Computer Engineering Department - College of Computer Science Page 13


Intelligent Systems, 2024 Chapter 0.2 Dr. Mohammad Alshamri

Depth Limited Search (DLS) Algorithm


▪ DLS is DFS with a predetermined depth limit 𝑙 (nodes at depth 𝑙 are treated as if they
have no successors).
▪ DLS solves the infinite-path problem of DFS.
▪ DLS can be used when there is prior knowledge of the problem, which is always
not the case.

Performance Measures of DLS


Completeness: DLS is not complete (if 𝑙 < 𝑑, we may not reach a goal)
Optimality: Not optimal
Time Complexity: 𝑂(𝑏 𝑙 )
Space Complexity: 𝑂(𝑏𝑙)

Comparing Uninformed Search Strategies

a
𝑏: is the branching factor. complete if 𝑏 is finite.
𝑑: is the depth of the shallowest solution. b
complete if step costs ≥ 𝜖 for positive 𝜖.
𝑚: is the maximum depth of the search tree. c
optimal if step costs are all identical.
𝑙: is the depth limit.

DFS BFS
Advantages ✓ requires less memory ✓ If there are multiple solutions, BFS
✓ faster in execution finds the shortest path.
✓ If there is a solution BFS guarantee
to find it. (Optimal)
Disadvantage there is no guarantee of ✓ takes more memory
solution (not optimal) ✓ slow

Computer Engineering Department - College of Computer Science Page 14


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Heuristic Search Algorithms


Heuristic Function [ℎ(𝑛)]
▪ Heuristic function is an estimate of how close a state is to a goal.
▪ Th heuristic function is designed for a particular search problem.
1. ℎ(𝑛) represents the estimated cost of the cheapest path from the state n to a
goal state.
2. If 𝑛 is a goal node, then ℎ(𝑛) = 0.
▪ Heuristic functions are the most common form in which additional knowledge of
the problem is imparted to the search algorithm.

Example1
Consider 8-puzzle problem with the following current and goal states. Find the heuristic
function, ℎ(𝑛), output if it is defined as:
1. Number of misplaced tiles in the current state relative to the goal state.
2. Sums of the Manhattan distances for each tile in the puzzle (Manhattan distance of a
tile represents the number of squares from the desired location).

Answer1
ℎ1 (𝑛) = 3
because tiles 1, 4, and 7 are out of place.
ℎ2 (𝑛) = 1 + 0 + 0 + 1 + 0 + 0 + 1 + 0 = 3
Both functions estimate the number of moves we will
have to make to reach a goal state.

Example 2
Find the city-block distance of the following?
Answer 2
ℎ3 (𝑛) = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18

Computer Engineering Department - College of Computer Science Page 1


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Example 3
Find ℎ(𝑛) of Pac Man problem if the
heuristic function is defined as the:
1. Manhattan distance between the
current and goal state.
2. Euclidean distance between the
current and goal state.

Answer 3
ℎ2 (𝑛) = 10 + 5 = 15

ℎ4 (𝑛) = √102 + 52 = 11.5

Greedy Search
▪ The idea is to expand the node with the smallest estimated cost to reach the goal
(the node which appears to be closest to the goal).
▪ It is called “greedy” because at each step it tries to get as close to the goal as it can.
▪ The heuristic function, ℎ(𝑛), estimates the remaining distance to a goal.
▪ Greedy search is simple and finds a solution quickly.
▪ The time and space complexity for the tree version of greedy search is 𝑂(𝑏𝑚).
However, with a good heuristic function, the complexity can be reduced
substantially.
▪ Greedy algorithm is neither complete nor optimal. It is susceptible to false start.

Computer Engineering Department - College of Computer Science Page 2


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Greedy Search Algorithm (Start Node, Goal Node)


Enqueue the starting node to PQUEUE
Make the starting node as the ROOT of Greedy-tree
do
Dequeue the first node F from PQUEUE
IF F is a goal node, EXIT on success
Empty PQUEUE
Expand node F
For each child of F
Calculate cost(child)
Enqueue the child with its cost to PQUEUE
Add the child to its parent (F) in the Greedy-tree
End
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the Greedy-tree.

Example 4
Apply greedy search for
the 8-puzzle problem
using the number of
misplaced tiles as the
heuristic function?

Answer 4
The goal state is reached
in the optimal way.
The same tree will be
obtained if we use
Manhattan distance

Computer Engineering Department - College of Computer Science Page 3


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Example 5
The distance between any two cities and direct distances from each city to Bucharest are
given below.
1. Find the shortest path from Barcelona to Bucharest using the greedy algorithm.
2. Find the complete path cost.

Computer Engineering Department - College of Computer Science Page 4


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Answer 5
• For this problem, the direct distances
from a city to Bucharest will be used
as the heuristic function.
• The complete path is:
Barcelona – Rome – Palermo – Athens
– Bucharest
• The path cost is:
𝑃𝑐𝑜𝑠𝑡 = 1471 + 1043 + 907 + 1300
= 4,721𝐾𝑚

A* Search
▪ The A* algorithm combines the UCS and the Greedy search to determinate the
ordering.
▪ The evaluation function 𝑓 is given by:
𝑓(𝑛) = ℎ(𝑛) + 𝑔(𝑛)
o ℎ(𝑛) is a heuristic function that estimates the cost from the node 𝑛 to the goal
o 𝑔(𝑛) is the cost so far to reach 𝑛.
▪ A* search is complete, optimal and speedy.
▪ 𝑓(𝑛) estimates total cost of path through the node 𝑛 to goal.
▪ The queue will be sorted based on estimates of full path costs (not just the cost so
far, and not just the estimated remaining cost, but the two together).

Computer Engineering Department - College of Computer Science Page 5


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

A* Search Algorithm (Start Node, Goal Node)


Add the starting node to visited array
cost(start) =h(start)
Enqueue the starting node to PQUEUE with its cost
Make the starting node as the ROOT of A*-tree
do
Dequeue the first node F from PQUEUE
IF F is a goal node, EXIT on success
Expand node F
For each unvisited child of F
g(child) = g(parent) + g(child-parent)
cost(child) =h(child) + g(child)
Enqueue the child with its cost to PQUEUE
Add the child to its parent (F) in the A*-tree
Add the child to visited array
End
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the A*-tree.

Example 6
Find the
shortest
path from
X to Y
using A*
algorithm.

Computer Engineering Department - College of Computer Science Page 6


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Example 7
Find the
shortest path
from S to E
using A*
algorithm.

Example 8
Find the shortest path from Barcelona to Bucharest using A* algorithm.
The distance between any two cities and direct distances from each city to Bucharest are
given. Assume the following:
1. 𝒉(𝒏): the direct distance to Bucharest from the node 𝑛.
2. 𝒈(𝒏): the cost of the path so far (i.e., the sum of all costs in the path from Barcelona to
the current node).

Answer 8
• Barcelona is the starting point, so Barcelona will expand into 3 nodes: Madrid, Lyon
and Roma.
• Lyon will be expanded because it has the lowest 𝑓(𝑛) value among them.

Computer Engineering Department - College of Computer Science Page 7


Intelligent Systems, 2024 Chapter 0.3 Dr. Mohammad Alshamri

Computer Engineering Department - College of Computer Science Page 8


Intelligent Systems, 2024 Chapter 0.4 Dr. Mohammad Alshamri

Propositional Logic

Logic
▪ Logic describes what is TRUE and not how to solve problems.
▪ Logic consists of two parts:
1. Language: It has two aspects:
✓ Syntax.
✓ Semantics.
2. Method of reasoning.

Syntax
▪ Syntax specifies the symbols in the language and how they can be combined to
form sentences
▪ Syntax represents the:
1. Atomic symbols of the logical language, and
2. Symbol structures for constructing well-formed, non-atomic expressions of logic.

Sentence
• Sentence represents some simple or complex assertion about the world.
• Facts about the world are represented as sentences in logic.
• TRUE, FALSE or any proposition symbol is a sentence.

Axiom (Postulate)
▪ Axiom is a sentence taken as given without being derived from other sentences.
▪ Axiom serves as a premise or starting point for further reasoning and arguments

Semantics
▪ Semantics gives meaning to the atomic symbols of logic.

Computer Engineering Department - College of Computer Science Page 1


Intelligent Systems, 2024 Chapter 0.4 Dr. Mohammad Alshamri

▪ Semantic specifies how you assign a truth value to a sentence based on its meaning
in the world.

Logical Systems
There are several logical systems with different syntax and semantics.
1. Propositional logic.
2. First-order predicate logic.

Proposition
The proposition is a statement about the world which is either True or False.
"Grass is green" proposition "Close the door" Not proposition
"2 + 5 = 5" proposition "Is it hot outside?" Not proposition
"x is greater than 2" Not proposition

Propositional Logic
▪ Propositional logic is the simplest logical system in which the user defines:
1. A set of propositional symbols, like P and Q.
2. The semantics of each of these symbols
▪ For example:
P: “Sunday is a holiday” Q: “Today is Sunday”

▪ Propositional logic:
1. Expresses knowledge in the form of propositions from the environment.
2. Represents knowledge in symbols that are used to represent the propositions.
▪ A knowledge base of propositional logic is a set of sentences.

Logical Operators
R: It is raining D: It is dark C: It is cold.

AND Conjunction ∧ binary operators R∧C – It is raining AND it is cold


OR Disjunction ∨ binary operators

Computer Engineering Department - College of Computer Science Page 2


Intelligent Systems, 2024 Chapter 0.4 Dr. Mohammad Alshamri

NOT Negation ¬ unary operator

Implies Implication ⟹ binary operators R⟹C – IF it is raining THEN it is cold.


IFF (if and only if) Equivalence ⟺ binary operators

Truth Tables of Logical Operators


➢ The behavior of the logical operators is usually represented using truth tables.
➢ A truth table shows the possible values that can be generated by applying an
operator to truth values.
𝑃 𝑄 𝑃∧𝑄 𝑃∨𝑄 ¬𝑃 𝑃⟹𝑄 𝑃⟺𝑄
T T T T F T T
T F F T F F F
F T F T T T F
F F F F T T T

Sentence Types
▪ Simple sentence: It is a simple sentence (P, Q, R) that expresses ‘atomic''
propositions about the world.
Q: Today is Sunday. Q can be True or False

▪ Compound sentence: It is formed from two or more simple sentences.


It is raining (A) AND I get wet (B). A ∧ B can be True or False

Example 1
IF it is raining AND it is dark THEN it is cold (R∧D) ⟹ C
Statement form Symbolic form

Example 2
Prove using truth table the following: ¬(𝑃 ∨ 𝑄) = (¬𝑃) ∧ (¬𝑄)
𝑃 𝑄 𝑃∨𝑄 ¬(𝑃 ∨ 𝑄) ¬𝑃 ¬𝑄 (¬𝑃) ∧ (¬𝑄)
T T T F F F F
T F T F F T F
F T T F T F F
F F F T T T T

Computer Engineering Department - College of Computer Science Page 3


Intelligent Systems, 2024 Chapter 0.4 Dr. Mohammad Alshamri

Example 3
Prove using truth table the following: 𝑃 ∨ (𝑄 ∧ 𝑅) = (𝑃 ∨ 𝑄) ∧ (𝑃 ∨ 𝑅)

Example 4
Convert the statement to symbolic form and P: The Sun is hot
generate its truth table. Q: It is humid
The Sun is hot, but it is not humid 𝑃 ∧ (¬𝑄)
𝑃 𝑄 ¬𝑄 𝑃 ∧ (¬𝑄)
T T F F
T F T T
F T F F
F F T F

Example 5 (Convert the symbolic form to statement form)


A: You can drive a car B: You have a driver’s license 𝑨⟺𝑩
You can drive a car IFF you have a driver’s license.
A: Akbar likes fish 𝐴 ∧ (¬𝐵): Akbar likes fish BUT NOT chicken
B: Akbar likes chicken 𝐴 ∨ 𝐵: Akbar likes fish OR chicken
A: It is raining ¬𝐴: It is NOT raining
A: A homework is due 𝐴 ⟹ 𝐵: IF a homework is due THEN it must be Tuesday
B: It is Tuesday 𝐴 ⟹ 𝐵: A homework is due ONLY IF it is Tuesday

Example 6
Convert the statement to symbolic form and generate truth table.

Computer Engineering Department - College of Computer Science Page 4


Intelligent Systems, 2024 Chapter 0.4 Dr. Mohammad Alshamri

IF Ali passed exam, THEN he will get a job OR a car

Answer 6
P: Ali passed exam 𝑃 𝑄 𝑅 𝑄∨𝑅 𝑃 ⟹ (𝑄 ∨ 𝑅)
T T T T T
Q: Ali will get a job
T T F T T
R: Ali will get a car T F T T T
T F F F F
𝑃 ⟹ (𝑄 ∨ 𝑅) F T T T T
F T F T T
F F T T T
F F F F T

Example 7
Assume the following propositions, write symbolic forms of:
A: Angelo comes to the party C: Carlo comes to the party
B: Bruno comes to the party D: David comes to the party

1 IF David comes to the party, THEN Bruno AND Carlo come too 𝐷 ⟹𝐵∧𝐶
2 Carlo comes to the party ONLY IF Angelo AND Bruno do not come 𝐶 ⟹ ¬𝐴 ∧ ¬𝐵
3 David comes to the party IFF Carlo comes AND Angelo does not come 𝐷 ⇔ (𝐶 ∧ ¬𝐴)
IF David comes to the party, THEN IF Carlo does not come THEN Angelo
4 𝐷 ⟹ (¬𝐶 ⟹ 𝐴)
comes
Carlo comes to the party provided that David does not come BUT IF David
5 (¬𝐷 ⟹ 𝐶) ∧ (𝐷 ⟹ ¬𝐵)
comes THEN Bruno does not come
A necessary condition for Angelo coming to the party is that IF Bruno AND
6 𝐴 ⟹ ((¬𝐵 ∧ ¬𝐶) ⟹ 𝐷)
Carlo are not coming, David comes.
Angelo, Bruno, AND Carlo come to the party IFF David does not come BUT IF neither Angelo NOR Bruno
7 come THEN David comes ONLY IF Carlo comes
((𝐴 ∧ 𝐵 ∧ 𝐶) ⇔ ¬𝐷) ∧ ((¬𝐴 ∧ ¬𝐵) ⟹ (𝐷 ⇔ 𝐶))

Example 8
Consider the following propositional language
P: Ahmad is happy Q: Ahmad paints a picture R: Ali is happy

Formulate the following sentences:


1. If Ahmad is happy AND paints a picture, 1. (P∧Q) ⟹ ¬R

Computer Engineering Department - College of Computer Science Page 5


Intelligent Systems, 2024 Chapter 0.4 Dr. Mohammad Alshamri

THEN Ali is not happy.

2. Ahmad is happy ONLY IF he paints a picture. 2. P ⟹Q

Tautology
It is a compound proposition
that is always TRUE
regardless of the value of
the variables.

Contradiction
It is a compound proposition that is always FALSE.

Example 9

Computer Engineering Department - College of Computer Science Page 6


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

Predicate Logic (First-order Predicate Calculus)


• The expressive power of propositional logic is limited.
• It is much easier to model real world objects using properties and relations via
predicate logic.

Example 1
R: It is raining U: Joe takes his umbrella W: Joe gets wet

R⟹U “IF it rains, THEN Joe takes his umbrella”


U⟹ ¬W “IF Joe takes an umbrella, THEN he does not get wet”
¬R⟹ ¬W “IF it does not rain, THEN Joe does not get wet”
▪ How many propositions do we need to cover all possibilities?
▪ Instead of an infinite collection of propositions, we can define only one symbol to
be a predicate that takes one argument (𝑥) or more (𝑥, 𝑦, 𝑧, …).

Predicate
Predicates are functions of zero or more arguments that return Boolean values.
U: Joe takes his umbrella U(x): x takes his umbrella Predicate with ONE argument
W: Jane is the mother of Mary W(x,y): x is the mother of y Predicate with TWO arguments
R: It is raining R: It is raining Predicate with ZERO argument

▪ Each predicate is given a name, which is followed by the list of arguments.


▪ Predicates can be TRUE or FALSE depending on the values of their arguments.
▪ The arguments order is important mother(J,M) and mother(M,J) are different.

Example 2
R⟹W(x) For any individual x, IF it is raining, THEN x takes his or her umbrella
U(x)⟹ ¬W(x) No matter who you are, IF you take your umbrella, THEN you will not get wet
For any individual x, IF x takes an umbrella, THEN x will not get wet
¬R⟹ ¬W(x) IF it doesn’t rain, THEN nobody gets wet
For any individual x, IF it doesn’t rain, THEN x will not get wet

Computer Engineering Department - College of Computer Science Page 1


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

Atomic Formula
▪ An atomic formula is a predicate with zero or more arguments.
▪ For example, 𝑈(𝑥) is an atomic formula with predicate 𝑈 and one argument
occupied by the variable 𝑥.
▪ In general, an argument is either a variable or a constant.

Constant
• Constant represents a fixed knowledge and cannot be changed during the execution
of the knowledge-based system.
• Constant values can be integers, reals, or character strings (‘Ali’, ‘Omer’, ‘Mary’)

Variables
• Variables are symbols capable of taking on any constant as a value.
• Variables may take different values within a fixed domain.
• Variable values may change during the execution of the system from time to time.

Functions
Functions carry out processes and return a predefined value, not a Boolean value.

Elder(‘Ali’, ‘John’) = ‘Ali’ Father(‘Ali’) = ‘Taha’ Greater(4,6) = 6

Ground Atomic Formula


▪ Ground atomic formula is an atomic formula having constant arguments.
Any proposition is a ground atomic formula with no (constant) argument.
▪ Non-ground atomic formula having at least one variable argument.

Example 3
Create ground and non-ground atomic formulas for Course-Student-Grade predicate.

Answer 3

Computer Engineering Department - College of Computer Science Page 2


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

Predicate Name Arguments Ground Non-ground


csg(C,S,G) csg C, S, G csg(“CS101”, 12345, “A”) csg(“CS101”, x, “A”)

csg(“CS101”, 12345, “A”) is asserting that the student with ID 12345 got an A in CS101.

Universe of Discourse (Domain)


▪ Universe of Discourse is the collection of all elements (persons, ideas, symbols, data
structures, and so on) that affect the logical argument under consideration.
▪ The elements of the universe of discourse are called individuals.
▪ Every universe of discourse must contain at least one individual.

Example 4
What is the universe of discourse for the following language?
1. Jane is Paul’s mother. The universe of discourse for this example
2. Jane is Mary’s mother. can be the people living in a particular
3. Any two persons having the same mother are siblings. house or on a particular block
4. Paul and Mary are siblings. Family = {′Jane′, ′Jack′, ′Paul′, ′Mary′}

Arity Number
▪ It indicates the number of elements in the argument list of a predicate.
▪ A predicate with arity 𝑛 is often called an 𝑛-place predicate.
▪ The predicates indicate relations between objects.
Statement Predicate Argument List Arity Number
Tom is a cat C: "is a cat" C(‘Tom’) ‘Tom’ one-place 1
Jane is the mother of
"is the mother of" mother(‘Jane’,‘Mary’) ‘Jane’, ‘Mary’ two-place 2
Mary
The sum of 2 and 3 is 6 "is the sum of" sum(2,3,6) 2,3,6 three-place 3
Mary and Paul are
"are siblings". sibling(‘Mary’, ‘Paul’) ‘Mary’, ‘Paul’ two-place 2
siblings

Parts of Predicate Logic


1. Constants – objects e.g.: ‘Ali’, ‘Apples’
2. Predicates - properties and relations e.g.: likes(‘Ali’, ‘Apples’)
3. Variables represent any object: likes(x, Apples): x is a variable can be ‘John’.

Computer Engineering Department - College of Computer Science Page 3


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

4. Functions transform objects e.g.: “fruit_of()” in likes(‘John’, fruit_of(apple_tree))


5. Quantifiers qualify values of variables
▪ True for all objects (Universal): ∀𝑥 likes(x, ‘Apples’)
▪ Exists at least one object (Existential): ∃𝑥 likes(x, ‘Apples’)

Closed Formula
Closed formula constitutes a proposition and thus can have a truth value.

Open Formula

• Open formula is a formula that contains at least one free variable. Hence, it does
not have a truth value assigned to it before knowing the variable value.
• Open formula can be transformed into a closed formula by applying a quantifier
for each free variable to make them bound variables.

Example 5
IF 𝑥 is a cat THEN 𝑥 has a tail 𝐶(𝑥): 𝑥 is a cat 𝑇(𝑥): 𝑥 has a tail
Open formula Closed formula
𝐶(𝑥) ⇒ 𝑇(𝑥) C(‘Tom’) ⇒T(‘Tom’).

Example 6
The formula "𝑥 + 2 > 𝑦" is open since it contains the free variables 𝑥 and 𝑦.
In contrast, the formula "∃𝑦 ∀𝑥: 𝑥 + 2 > 𝑦" is closed and has truth value true.

Scope of a Quantifier
▪ Scope is the range in the formula where the quantifier "engages in"
▪ Assume we have: ∀𝑥 𝐴. Here 𝐴 is called the scope of the quantifier.
Example 17
Assume 𝑃(𝑥): 𝑥 is living 𝑄(𝑥): 𝑥 is dead

Computer Engineering Department - College of Computer Science Page 4


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

What is the meaning of:


∀𝑥(𝑃(𝑥) ∨ 𝑄(𝑥)) Everything is either living or dead.
∀𝑥 𝑃(𝑥) ∨ 𝑄(𝑥) Everything is living or 𝑥 is dead.

Example 7
Generalize the statement “I like chocolate” using quantifiers.

Answer 7
“Everyone likes chocolate” ∀𝑥 𝑃(𝑥) ⇒ 𝐿(𝑥, 𝑐)
“There exists 𝑥 such that 𝑥 likes chocolate” ∃𝑥 𝐿(𝑥, 𝑐)

Example 8
Write the predicate of “My brother likes chocolate”
L(B(me), ‘chocolate’)
b(x) means the brother of x (a function).

Example 9
Express "Everyone gets a break once in a while" in predicate logic?

Answer 9
▪ The predicate is: B: "gets a break once in a while"
▪ 𝐵(𝑥): “𝑥 gets a break once in a while”
▪ The word "everyone" indicates that is true for all 𝑥. Hence, ∀𝑥 𝐵(𝑥)

Example 10
Express "All cats have tails" in predicate logic.

Answer 10
We first have to find the scope of the universal quantifier, which is:
"IF 𝑥 is a cat, THEN 𝑥 has tails" 𝐶(𝑥): 𝑥 is a cat 𝑇(𝑥): 𝑥 has a tail

Computer Engineering Department - College of Computer Science Page 5


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

The compound formula is: 𝐶(𝑥) ⇒ 𝑇(𝑥)


After the universal quantifier: ∀𝑥 (𝐶(𝑥) ⇒ 𝑇(𝑥))

Example 11
Express "There is somebody who knows everyone" in predicate logic?

Answer 11
1. ∃𝑥(𝑥 knows everybody) 3. ∀𝑦 𝐾(𝑥, 𝑦): 𝑥 knows everybody.
2. 𝐾(𝑥, 𝑦): 𝑥 knows 𝑦. 4. ∃𝑥 ∀𝑦 𝐾(𝑥, 𝑦)

Example 12
Express "Everybody has somebody who is his or her mother" in predicate logic?

Answer 12
1. 𝑀(𝑥, 𝑦): " 𝑥 is the mother of 𝑦".
2. ∃𝑥 𝑀(𝑥, 𝑦): "Someone is the mother of 𝑦"
3. Add the universal quantifier: ∀𝑦 ∃𝑥 𝑀(𝑥, 𝑦).

Example 13
Express "Nobody is perfect" in predicate logic?

Answer 13
• "nobody" is a quantifier that indicates the absence of an individual with a certain
property.
• To express the fact that there is no x for which an expression 𝐴 is true, one can
either use:
¬∃𝑥 𝐴: "It is not the case that there is somebody who is perfect"
∀𝑥 ¬𝐴: "For everyone, it is not the case that he or she is perfect".
• The two methods are logically equivalent, i.e., ¬∃𝑥 𝐴 ⟺ ∀𝑥 ¬𝐴.

Computer Engineering Department - College of Computer Science Page 6


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

Example 14
Express "All dogs are mammals" in predicate logic.

Answer 14
Since the quantifier should be restricted to dogs, one should rephrase the statement to be:
"IF 𝑥 is a dog, THEN 𝑥 is a mammal"
This immediately leads to

∀𝑥 (dog(𝑥) ⇒mammal(𝑥)) ∀𝑥(𝑃(𝑥) ⇒ 𝑄(𝑥))

Example 15
Express "Some dogs are brown" in predicate logic.

Answer 15
▪ This statement means that there are some animals that are dogs and that are brown.
▪ Of course, the statement "x is a dog and x is brown" can be translated as
dog(𝑥)∧brown(𝑥)
▪ "There are some brown dogs" can now be translated as:

∃𝑥(dog(𝑥)∧brown(𝑥)) ∃𝑥(𝑃(𝑥) ∧ 𝑄(𝑥))

Example 16
Express "only dogs bark" in predicate logic?

Answer 16
▪ This statement must be reworded as:
"It barks ONLY IF it is a dog", or, equivalently,
"IF it barks, THEN it is a dog".
▪ ∀𝑥(barks(𝑥) ⇒ dog(𝑥))

Precedence of Operators

Computer Engineering Department - College of Computer Science Page 7


Intelligent Systems, 2024 Chapter 0.5 Dr. Mohammad Alshamri

1. Quantifiers 4. OR
2. NOT 5. ⟹, ⟺

3. AND
Example 17
Assume 𝑃(𝑥): 𝑥 is living 𝑄(𝑥): 𝑥 is dead
What is the meaning of:
∀𝑥(𝑃(𝑥) ∨ 𝑄(𝑥)) everything is either living or dead.
∀𝑥 𝑃(𝑥) ∨ 𝑄(𝑥) everything is living or 𝑥 is dead.

Example 18
Illustrate the precedence of the operators through brackets.
∀𝑥 ¬ ∃𝑦 𝑃(𝑥, 𝑦)

Answer 18
∀𝑥 (¬ ∃𝑦 𝑃(𝑥, 𝑦))
This means “For all 𝑥 there is no 𝑦 such that 𝑃(𝑥, 𝑦) is TRUE”
“There is no pair of values for 𝑥 and 𝑦 that makes 𝑃(𝑥, 𝑦) TRUE”.

Example 19
Sentence John likes cricket OR football
Predicate(s) 𝑃(𝑥, 𝑦): 𝑥 likes 𝑦
Variables 𝑥: a person - 𝑦: a sport
Constants John, cricket, football
General Symbolic form 𝑃(𝑥, 𝑦)
Sentence Symbolic form 𝑃(“𝐽𝑜ℎ𝑛”, “𝑐𝑟𝑖𝑐𝑘𝑒𝑡”) ∨ 𝑃(“𝐽𝑜ℎ𝑛”, “𝑓𝑜𝑜𝑡𝑏𝑎𝑙𝑙”)

Sentence John lives in a house AND the color of the house is green
Predicate(s) 𝑃(𝑥, 𝑦): 𝑥 lives in 𝑦
𝑄(𝑦, 𝑧): color of 𝑦 is 𝑧
Variables 𝑥: a person - 𝑦: a residence place - 𝑧: color
Constants John, house and green

Computer Engineering Department - College of Computer Science Page 8


Intelligent Systems, 2023 Chapter 4.2 Dr. Mohammad Alshamri

General Symbolic form 𝑃(𝑥, 𝑦) ∧ 𝑄(𝑦, 𝑧)


Sentence Symbolic form 𝑃(“𝐽𝑜ℎ𝑛”, “ℎ𝑜𝑢𝑠𝑒”) ∧ 𝑄(“ℎ𝑜𝑢𝑠𝑒”, “𝑔𝑟𝑒𝑒𝑛”)

IF car belongs to John, THEN it is green belongs(car,John) → color(car,green)


John did not write JAVA ¬ write(John,JAVA)
All elephants are grey x [elephant(x) → color(x, grey)]
All computer students are brilliant x [computer students(x) [computer_student(x) → brilliant(x)]
John likes all kinds of foods x [food(x) → likes(John,x)]
Everyone who is both strong and intelligent will succeed x [person(x) ^ strong(x) ^ intelligent(x)→ succeeds(x,career)]
in his career
There is person who wrote computer chess x[person(x) → wrote(x, computer-chess)]

All courses in CE department are easy x [courses(x) → CE_department(x, easy)]


Chicken is food food(chicken)
Bill eats peanuts and is still alive eats(‘Bill’,’peanuts’) ∧ alive(‘Bill’)
Sue eats everything that Bill eats x: eats(‘Bill’,x) → eats(‘Sue’,x)
The last meeting of the club was at Tom’s house last meeting (club, house(‘Tom’))
Ali is not married ¬ married (Ali)
Every country has exactly one ruler x: y: country(x) ∧ ruler(y)
A grand parent is parent of one’s parent x: y: z parent(z,y) ∧ parent(y,x) → grand_parent(z,x)

Everyone is loyal to some one x: y: loyal-to(x,y)


All Pompeian were Romans x: Pompeian(x) → Romans(x)

John introduced

Tautology
▪ An expression 𝐸 is called a tautology if for every interpretation of 𝐸, the value of
𝐸 is true.
▪ An example of a tautology is: 𝑃(𝑥) ∨ ¬ 𝑃(𝑥)
▪ Here, it does not matter what interpretation we use for predicate 𝑃, or what value
we assign to the free variable 𝑥.

Computer Engineering Department - College of Computer Science Page 9


Intelligent Systems, 2023 Chapter 4.2 Dr. Mohammad Alshamri

Expression Tree
• The predicates are the leaves.
• The operators are ordered based on their precedence.
• The higher precedence operators are close to the predicates and therefore the least precedent one
is the root of the tree.

We cannot generalize statements in proposition logic.

Well-formed formula (WFF)


• A well-formed formula (WFF) is a string of symbols that is grammatically
(syntactically) correct by virtue of belonging to some language of interest.
• Well-Formed Formula is a string of symbols (variables (capital letters),
parentheses, and connective symbols) that is generated by a formal language.
• That is, a string is a member of the language generated by the grammar of the
language.

Computer Engineering Department - College of Computer Science Page 10


Intelligent Systems, 2023 Chapter 4.2 Dr. Mohammad Alshamri

• A formal language can be identified with the set of its WFFs.

Rules of the Well-Formed Formulas


1. True and False are wffs.
2. A Statement constant or variable standing alone is a Well-Formed Formula.
For example– Statements like P, Q, are themselves Well Formed Formulas.
3. If P & Q are WFFs, then ¬P, P∨Q, P∧Q, P⇒Q, P⇔Q, etc. are also WFFs.
4. Each atomic formula (i.e. a specific predicate with variables) is a WFF.
5. If 𝑃(𝑥) is a predicate, then ∀𝑥 𝑃(𝑥) and ∃𝑥 𝑃(𝑥) are WFFs.

Computer Engineering Department - College of Computer Science Page 11


Intelligent Systems, 2024 Chapter 0.6 Dr. Mohammad Alshamri

Inference (Reasoning)
▪ Inference is the mechanism for deriving new sentences from old ones.
▪ Inference consists of the rules for determining a subset of logical expressions,
called theorems of logic.
▪ Inference must obey the primary requirement that the new sentences should follow
logically from the previous ones.

Important Definitions
1. Premises (Hypothesis): They are all propositions except the final proposition.
2. Argument: It is a sequence of propositions.
3. Conclusion: It is the final proposition.
4. Argument form: It is a sequence of compound propositions involving
propositional variables.

Argument Form

Premise (hypothesis) 𝑃1
Argument

Premise (hypothesis) 𝑃2
Premise (hypothesis) 𝑃3
Premise (hypothesis) 𝑃4
conclusion 𝑄

Valid Argument Form (𝑃1 ∧ 𝑃2 ∧ . . .∧ 𝑃𝑛 ) ⟹ 𝑄 Tautology

5. Valid argument: An argument is valid if the truth of all its premises implies that
the conclusion is true.

Computer Engineering Department - College of Computer Science Page 1


Intelligent Systems, 2024 Chapter 0.6 Dr. Mohammad Alshamri

6. Valid argument form: an argument form with premises 𝑃1 , 𝑃2 , . . . , 𝑃𝑛 and


conclusion 𝑄 is valid if and only if ((𝑃1 ∧ 𝑃2 ∧ . . .∧ 𝑃𝑛 ) ⟹ 𝑄) is a tautology.
7. If the premises are all TRUE, then the conclusion is TRUE.

Example 1
Use inference to generate a new sentence:
1. All ravens fly 1. Jane is Paul’s mother.
2. Peter is a raven 2. Jane is Mary’s mother.
3. Any two persons having the same mother
are siblings.
Peter flies Paul and Mary are siblings.

Inference Rules for Propositional Logic


Inference Name / Tautology Inference Name / Tautology
𝑝 Addition ¬(𝑝 ∧ 𝑞) DeMorgan
(Negation of Conjunction)
∴ 𝑝∨𝑞 𝑝 ⇒ 𝑝∨𝑞 ∴ ¬𝑝 ∨ ¬𝑞 ¬(𝑝 ∧ 𝑞) ⇒ ¬𝑝 ∨ ¬𝑞
𝑝∧𝑞 Simplification ¬(𝑝 ∨ 𝑞) DeMorgan
(Negation of Disjunction)
∴ 𝑝 𝑝∧𝑞 ⇒ 𝑝 ∴ ¬𝑝 ∧ ¬𝑞 ¬(𝑝 ∨ 𝑞) ⇒ ¬𝑝 ∧ ¬𝑞
𝑝 𝑝 Modus Ponens
Conjunction
𝑞 𝑝⇒𝑞 (mode that affirms)
∴ 𝑝∧𝑞 𝑝∧𝑞 ⇒ 𝑝∧𝑞 ∴ 𝑞 𝑝 ∧ (𝑝 ⇒ 𝑞) ⇒ 𝑞
𝑝∨𝑞 ¬𝑞 Modus Tollens
Resolution
¬𝑝 ∨ 𝑟 𝑝⇒𝑞 (mode that denies)
∴ 𝑞∨𝑟 (𝑝 ∨ 𝑞) ∧ (¬𝑝 ∨ 𝑟) ⇒ 𝑞 ∨ 𝑟 ∴ ¬𝑝 ¬𝑞 ∧ (𝑝 ⇒ 𝑞) ⇒ ¬𝑝
𝑝∨𝑞 𝑝⇒𝑞
Disjunctive Syllogism Hypothetical Syllogism
¬𝑝 𝑞⇒𝑟
∴ 𝑞 (𝑝 ∨ 𝑞) ∧ ¬𝑝 ⇒ 𝑞 ∴ 𝑝⇒𝑟 (𝑝 ⇒ 𝑞) ∧ (𝑞 ⇒ 𝑟) ⇒ (𝑝 ⇒ 𝑟)
𝑝⇒𝑞 Contrapositive
∴ ¬𝑞 ⇒ ¬𝑝 (𝑝 ⇒ 𝑞) ⇒ (¬𝑞 ⇒ ¬𝑝)

Modus ponens (MP): This rule states that if a conditional statement (‘𝑝 ⇒ 𝑞’) is accepted, and the
antecedent (p) holds, then the consequent (q) may be inferred.
Contraposition is an inference that says that a conditional statement is logically equivalent to
its contrapositive.

Computer Engineering Department - College of Computer Science Page 2


Intelligent Systems, 2024 Chapter 0.6 Dr. Mohammad Alshamri

Example 2
Use contrapositive for the proposition "All cats are mammals".

Answer 2
• The proposition can be restated as the conditional
"IF something is a cat, THEN it is a mammal"
• The law of contraposition says that statement is identical to the contrapositive
"IF something is not a mammal, THEN it is not a cat"

Example 3
Which rule of inference is used in each argument below:
Alice is a math major.
Addition
Therefore, Alice is either a math major OR a CSI major.
Jerry is a math major AND a CSI major. Therefore, Jerry is a math major. Simplification
IF it is rainy, THEN the pool will be closed. Modus
It is rainy. Therefore, the pool is closed. ponens
IF it snows today, THEN the university will close. Modus
The university is not closed today. Therefore, it did not snow today Tollens
IF I go swimming, THEN I will stay in the sun too long.
Hypothetical
IF I stay in the sun too long, THEN I will sunburn.
syllogism
Therefore, IF I go swimming, THEN I will sunburn.
I go swimming OR eat ice cream. Disjunctive
I did not go swimming. Therefore, I eat an ice cream. syllogism

Formal Proof
▪ Formal proof of a conclusion 𝑄 given hypotheses 𝑃1 , 𝑃1 , . . . , 𝑃𝑛 is a sequence of
steps, each of which applies some inference rule to hypotheses or previously
proven statements (antecedents) to yield a new true statement (consequent).
▪ Formal proof demonstrates that if the premises are TRUE, then the conclusion is
TRUE.
▪ Formal proof is based simply on symbol manipulation (NO need of thinking, just
apply rules)

Computer Engineering Department - College of Computer Science Page 3


Intelligent Systems, 2024 Chapter 0.6 Dr. Mohammad Alshamri

Example 4

Answer 4

Computer Engineering Department - College of Computer Science Page 4


Intelligent Systems, 2024 Chapter 0.6 Dr. Mohammad Alshamri

Inference Rules for Predicate Logic

Example 5

Computer Engineering Department - College of Computer Science Page 5


Intelligent Systems, 2024 Chapter 0.6 Dr. Mohammad Alshamri

Example 6
Show that the premises:
▪ All computer science students like programming.
▪ All computer science students.
imply the conclusion
▪ All like programming.

Answer 6
Domain: All computer science students.
P(x): "x is a computer science student" Q(x): "x likes programming"
The premises are:
▪ All computer science students like programming - ∀𝑥(𝑃(𝑥) ⇒ 𝑄(𝑥))
▪ All computer science students - ∀𝑥 𝑃(𝑥)
The conclusion is:
▪ All like programming - ∀𝑥 𝑄(𝑥)
Step Reason
1. ∀𝑥 𝑃(𝑥) Hypothesis 1
2. ∀𝑥(𝑃(𝑥) ⇒ 𝑄(𝑥)) Hypothesis 2
3. 𝑃(𝑐) Universal Instantiation from (1)
4. 𝑃 (𝑐 ) ⇒ 𝑄 (𝑐 ) Universal Instantiation from (2)
5. 𝑄(𝑐) Modus Ponens from (3) and (4)
6. ∀𝑥 𝑄(𝑥) Universal Generalization from (5)

Computer Engineering Department - College of Computer Science Page 6

You might also like