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

CSC 325 AI Lecture03 Uninformed Search Fall2024 22092024 113727am

Uploaded by

jaansher9090
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)
13 views

CSC 325 AI Lecture03 Uninformed Search Fall2024 22092024 113727am

Uploaded by

jaansher9090
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/ 91

CSC-325

Artificial Intelligence

Lecture - 3:
Uninformed Search

Dr. Muhammad Tariq Siddique


Department of Computer Sciences
Bahria University, Karachi Campus, Pakistan
[email protected]
Faculty room 14, 2nd Floor, Iqbal Block
Topics to be covered
Looping
1 Uninformed Search

2 Uninformed Search Strategies

3 Breadth and Depth First Search

4 Uniform Cost Search

5 Depth Limit Search

6 Iterative Deepening Search

7 Bidirectional Search

8
Search Strategies
Section - 1
Search Strategies
▪ Definition
• 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
▪ 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 depth of the state space (may be ∞)
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 4
Types of Search Strategies
▪ Uninformed Search Algorithms
• No domain-specific knowledge beyond the problem definition.
• Systematically explores the search space.
• Does not consider proximity(nearness/closeness) to the goal.
• Relies only on the problem formulation (initial state, goal state, actions,
transition model).
• Common methods:
o Breadth-First Search (BFS)
o Depth-First Search (DFS)
o Uniform-Cost Search (UCS)

• Strengths and weaknesses depend on the problem context.


© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 5
Types of Search Strategies
▪ Informed Search Algorithms
• Uses problem-specific knowledge to guide the search.
• Employs a heuristic function to estimate the distance to the goal.
• Focuses on promising paths, improving efficiency.
• Reduces the number of states explored, speeding up the search.
• Key algorithms:
o Greedy Best-First Search
o A* Search
• Balances exploration with minimizing path cost.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 6


Breadth First Search
Section - 2
Breadth-First Search (BFS)
Definition
The shallowest unexpanded node is chosen for expansion, i.e., root node is expanded first,
then all the successors of the root node are expanded next, then their successors, and so on.

▪ They are implemented using FIFO data structures, the algorithm will maintain a
list of the currently active paths and in each round of the algorithm repeat
following three steps until solution is not found or no further expansion is
possible.
1. Remove the first path from the list of paths.
2. Generate a new path for every possible follow-up nodes.
3. Append the list of newly generated paths to the end of the list of paths (to
ensure paths are really being visited in breadth-first order).

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 8


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 0, expanded: 0 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 9


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 1, expanded: 1 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S,nil) not goal {(A,S) (B,S) (C,S)} A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 10


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 2, expanded: 2 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) not goal {(B,S) (C,S) (D,A) (E,A)}
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 11


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 3, expanded: 3 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S) (D,A) (E,A)} 9
(B,S) not goal {(C,S) (D,A) (E,A) (G,B)} 4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 12


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 4, expanded: 4 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S) (D,A) (E,A)} 9
(B,S) {(C,S) (D,A) (E,A) (G,B)} 4 6 2
(C,S) not goal {(D,A) (E,A) (G,B) (F,C)} 6 1
G
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 13


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 5, expanded: 5 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S) (D,A) (E,A)} 9
(B,S) {(C,S) (D,A) (E,A) (G,B)} 4 6 2
(C,S) {(D,A) (E,A) (G,B) (F,C)} 6 1
G
(D,A) not goal {(E,A) (G,B) (F,C) (H,D)} D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 14


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 6, expanded: 6 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S) (D,A) (E,A)} 9
(B,S) {(C,S) (D,A) (E,A) (G,B)} 4 6 2
(C,S) {(D,A) (E,A) (G,B) (F,C)} 6 G 1
(D,A) {(E,A) (G,B) (F,C) (H,D)} D E goal F
(E,A) not goal {(G,B) (F,C) (H,D) (G,E)}
7

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 15


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 7, expanded: 6 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S) (D,A) (E,A)} 9
(B,S) {(C,S) (D,A) (E,A) (G,B)} 4 6 2
(C,S) {(D,A) (E,A) (G,B) (F,C)} 6 G 1
(D,A) {(E,A) (G,B) (F,C) (H,D)} D E goal F
(E,A) {(G,B) (F,C) (H,D) (G,E)}
7
(G,B) goal {(F,C) (H,D) (G,E)} no expand
H

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 16


Breadth-First Search (BFS)
generalSearch(problem, queue)
S
# of nodes tested: 7, expanded: 6 start
5
expnd. node Frontier list 2 4
Ø {(S,nil)}
(S, nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S) (D,A) (E,A)} 9
(B,S) {(C,S) (D,A) (E,A) (G,B)} 4 6 2
(C,S) {(D,A) (E,A) (G,B) (F,C)} 6 G 1
(D,A) {(E,A) (G,B) (F,C) (H,D)} D E goal F
(E,A) {(F,C) (H,D) (G,E)}
7
(G,B) goal {(F,C) (H,D) (G,E)} no expand
H
path: (G,B) → (B,S) → (S,nil)
: S→B→G
Cost: S-B = 2, B-G = 6
: S-G = 8
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 17
Properties of Breadth-First Search

Completeness
Yes, if b is finite

Optimally
Yes (assuming cost = 1 per step) otherwise No.

Time Complexity
1+b+b2+…+bd = O(bd) i.e., exponential in d

Space Complexity
O(bd)
b – maximum branching factor of the search tree
d – is the depth of the shallowest solution
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 18
Exponential Growth
Depth Nodes Time Memory
2 110 0.11 millisecond 107 kbytes
4 11,110 11 Millisecond 10.6 megabytes
6 106 1.1 seconds 1 gigabyte
8 108 2 Minutes 103 gigabytes
10 1010 3 Hours 10 terabytes
12 1012 13 Days 1 petabyte
14 1014 3.5 years 99 petabytes
16 1016 350 years 10 exabytes

Time and memory requirements for breadth-first search, assuming a branching factor
of 10, 100 bytes per node and searching 1000 nodes/second

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 19


Depth First Search
Section - 3
Depth-First Search (DFS)
Definition (DFS)
• A graph searching algorithm that begins at the root node, and exhaustively searches each
branch to its greatest depth before backtracking to previously unexplored branches.

▪ Explanation – Nodes found but yet to be reviewed are stored in a LIFO queue
(stack).
• Exhaustive means DFS can search every node in the graph to find the goal. If the goal is
not present in the graph, the algorithm will terminate, but will search each and every node
in a systematic way.
• Therefore, the DFS need to store only single path from node to leaf along with
unexpanded sibling at each level of depth.
• Each node explored is put on the front of frontier.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 21


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 0, expanded: 0 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 22


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 1, expanded: 1 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) not goal {(A,S) (B,S) (C,S)} A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 23


Depth-First Search (DFS)
generalSearch(problem, stack)
# of nodes tested: 2, expanded: 2
S
start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) not goal {(D,A) (E,A) (B,S) (C,S)} 9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 24


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 3, expanded: 3 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(D,A) (E,A) (B,S) (C,S)} 9
(D,A) not goal {(H,D) (E,A) (B,S) (C,S)} 4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 25


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 4, expanded: 4 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(D,A) (E,A) (B,S) (C,S)} 9
(D,A) {(H,D) (E,A) (B,S) (C,S)} 4 6 2
(H,D) not goal {(E,A) (B,S) (C,S)} 6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 26


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 5, expanded: 5 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(D,A) (E,A) (B,S) (C,S)} 9
(D,A) {(H,D) (E,A) (B,S) (C,S)} 4 6 2
(H,D) {(E,A) (B,S) (C,S)} 6 G 1
(E,A) not goal {(G,E) (B,S) (C,S)} D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 27


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 6, expanded: 5 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(D,A) (E,A) (B,S) (C,S)} 9
(D,A) {(H,D) (E,A) (B,S) (C,S)} 4 6 2
(H,D) {(E,A) (B,S) (C,S)} 6 G 1
(E,A) {(G,E) (B,S) (C,S)} D E goal F
(G,E) goal {(B,S) (C,S)} no expand
7

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 28


Depth-First Search (DFS)
generalSearch(problem, stack)
S
# of nodes tested: 6, expanded: 5 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(D,A) (E,A) (B,S) (C,S)} 9
(D,A) {(H,D) (E,A) (B,S) (C,S)} 4 6 2
(H,D) {(E,A) (B,S) (C,S)} 6 G 1
(E,A) {(G,E) (B,S) (C,S)} D E goal F
(G,E) {(B,S) (C,S)}
7

path: (G,E) → (E,A) → (A,S) → (S,nil) H


: S→A→E→G
Cost: S-A = 5, A-E = 4, E-G = 6
: S-G = 15

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 29


Properties of Depth-First Search
No, fails in infinite state-space
Completeness
(yes, if finite state space)

Optimally
No

Time Complexity
O(bm)

Space Complexity
O(bm)
b – maximum branching factor of the search tree
m – maximum depth of the state space (may be infinite)
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 30
Uniform Cost Search
Section - 4
Uniform-Cost Search (UCS)
▪ Definition
• Unlike BFS, instead of expanding shallowest node, uniform-cost
Search (UCS) expands the node with lowest path cost.
▪ Explanation
• UCS finds the least-cost path through a graph by maintaining an
ordered list of nodes in order of least-greatest cost (Priority Queue).
This allows us to evaluate the least cost path first.
• Let g(n) = cost of path from start node s to current node n
• Sort nodes by increasing value of g

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 32


How Uniform-Cost Search works?
▪ The algorithm uses the accumulated path cost and a priority queue to
determine the path to evaluate.
▪ The priority queue (least cost) contains the nodes to be evaluated.
▪ As node children are evaluated, we add their cost to the node with the
aggregate sum of the current path.
▪ This node is then added to the queue, and when all children have been
evaluated, the queue is sorted in order of ascending cost.
▪ When the first element in the priority queue is the goal node, then the
best solution has been found.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 33


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 0, expanded: 0 start
expnd. node Frontier list 5
2 4
Ø {(S,nil):0}
A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 34


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 1, expanded: 1 start
expnd. node Frontier list 5
2 4
Ø {(S,nil):0}
(S,nil) not goal {(B,S):2 (C,S):4 (A,S):5} A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 35


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 2, expanded: 2 start
5
expnd. node Frontier list 2 4
Ø {(S,nil):0}
(S,nil) {(B,S):2 (C,S):4 (A,S):5} A B C
(B,S) not goal {(C,S):4 (A,S):5 (G,B):2+6} 9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 36


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 3, expanded: 3 start
expnd. node Frontier list 5 2 4
Ø {(S,nil):0}
(S,nil) {(B,S):2 (C,S):4 (A,S):5} A B C
(B,S) {(C,S):4 (A,S):5 (G,B):8}
(C,S) not goal {(A,S):5 (F,C):4+2,(G,B):8} 9 4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 37


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 4, expanded: 4 start
expnd. node Frontier list 5 2 4
Ø {(S,nil):0}
(S,nil) {(B,S):2 (C,S):4 (A,S):5} A B C
(B,S) {(C,S):4 (A,S):5 (G,B):8}
(C,S) {(A,S):5 (F,C):6 (G,B):8} 9 4 6 2
(A,S) not goal {(F,C):6 (G,B):8 (E,A):5+4 6 G 1
(D,A):5+9} D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 38


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 5, expanded: 5 start
expnd. node Frontier list 5 2 4
Ø {(S,nil):0}
(S,nil) {(B,S):2 (C,S):4 (A,S):5} A B C
(B,S) {(C,S):4 (A,S):5 (G,B):8}
(C,S) {(A,S):5 (F,C):6 (G,B):8} 9 4 6 2
(A,S) {(F,C):6 (G,B):8 (E,A):5+4 (D,A):5+9} 6 G 1
(F,C) not goal {(G,F):4+2+1 (G,B):8 (E,A):9 (D,A):14} D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 39


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list 5 2 4
Ø {(S,nil):0}
(S,nil) {(B,S):2 (C,S):4 (A,S):5} A B C
(B,S) {(C,S):4 (A,S):5 (G,B):8}
(C,S) {(A,S):5 (F,C):6 (G,B):8} 9 4 6 2
(A,S) {(F,C):6 (G,B):8 (E,A):5+4 6 G 1
(D,A):5+9} D E goal F
(F,C) {(G,F):4+2+1 (G,B):8 (E,A):9 (D,A):14}
(G,F) goal {(G,B):8 (E,A):9 (D,A):14} no expand 7

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 40


Uniform-Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list 5 2 4
Ø {(S,nil):0}
(S,nil) {(B,S):2 (C,S):4 (A,S):5} A B C
(B,S) {(C,S):4 (A,S):5 (G,B):8}
(C,S) {(A,S):5 (F,C):6 (G,B):8} 9 4 6 2
(A,S) {(F,C):6 (G,B):8 (E,A):5+4 (D,A):5+9} 6 G 1
(F,C) {(G,F):7 (G,B):8 (E,A):9 (D,A):14} D E goal F
(G,F) {(G,B):8 (E,A):9 (D,A):14
7

H
path: (G,F) → (F,C) → (C,S) → (S,nil)
: S→C→F→G
Cost: S-C = 4, C-F = 2, F-G = 1
: S-G = 7

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 41


Properties of Uniform-Cost Search
Completeness
• Yes, if arc costs > 0.

Optimally

• Yes

Time and Space Complexity


• O(bd) (i.e., exponential)
• More precisely, time and space complexity is O(bC*/ε ) where all edge costs ≥ ε > 0,
and C* is the best goal path cost

b – is the branching factor at each non-leaf node


d – is the depth of the solution
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 42
Depth Limit Search
Section - 5
Depth-Limited Search (DLS)
❖ Definition
Depth-limited search (DLS) is the variant of DFS with pre-specified
depth limit. That is if depth limit is denoted by l then no successor are
added further to the node at the depth l . The DLS overcomes the
failure of DFS in infinite space and solve the infinite path problem.
❖ If l is not properly chosen in the case when d is unknown, l < d, then
DLS is incomplete that is if the goal is beyond the depth limit.
❖ The depth limit also reduces the scope of the search.
❖ It is also suboptimal, since the algorithm may find first path to the
goal instead of shortest path.

44
Depth-Limited Search (DLS)
❖The depth limit is often based on the domain knowledge of the
problem.
❖Example
In the map of Romania, we can see that there are 20 cities, therefore
possibly l = 19 since if solution exists that would reach at the depth of
19. Or if we look at the map carefully every city from any city can be
reach in 9 steps that would be more precise depth limit. In such case it
is called diameter of the state space.

45
Depth-Limited Search (DLS)

46
Depth-Limited Search (DLS)
Given the following state space (tree search), give the sequence of visited nodes when using DLS
(Limit = 2):
Limit = 0 A

Limit = 1 B C D E

Limit = 2 F G H I J

K L M N

O
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 47
Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 0, expanded: 0
expnd. node Frontier A
Ø {(A,nil)}
B C D E

F G H I J

Limit = 2
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 48


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 1, expanded: 1
expnd. node Frontier A
Ø {(A,nil)}
(A,nil) not goal {(B,A) (C,A) (D,A) (E,A)}
B C D E

F G H I J

Limit = 2
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 49


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 2, expanded: 2
expnd. node Frontier A
Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) not goal {(F,B) (G,B) (C,A) (D,A) (E,A)}

F G H I J

Limit = 2
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 50


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 3, expanded: 2
expnd. node Frontier A
Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) not goal {(G,B) (C,A) (D,A) (E,A)}
F G H I J

Limit = 2
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 51


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 4, expanded: 2

expnd. node Frontier A


Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) not goal {(C,A) (D,A) (E,A)} F G H I J

Limit = 2
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 52


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 5, expanded: 3

expnd. node Frontier A


Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) {(C,A) (D,A) (E,A)} F G H I J
(C,A) not goal {(H,C) (D,A) (E,A)}
Limit = 2
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 53


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 6, expanded: 3
expnd. node Frontier A
Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) {(C,A) (D,A) (E,A)} F G H I J
(C,A) {(H,C) (D,A) (E,A)}
Limit = 2
(H,C) not goal {(D,A) (E,A)}
K L M N

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 54


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 7, expanded: 4
expnd. node Frontier A
Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) {(C,A) (D,A) (E,A)} F G H I J
(C,A) {(H,C) (D,A) (E,A)}
Limit = 2
(H,C) {(D,A) (E,A)}
K L M N
(D,A) not goal {(I,D) (J,D) (E,A)}

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 55


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 8, expanded: 4

expnd. node Frontier A


Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) {(C,A) (D,A) (E,A)} F G H I J
(C,A) {(H,C) (D,A) (E,A)}
Limit = 2
(H,C) {(D,A) (E,A)}
K L M N
(D,A) {(I,D) (J,D) (E,A)}
(I,D) not goal {(J,D) (E,A)}
O

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 56


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 9, expanded: 4

expnd. node Frontier A


Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) {(C,A) (D,A) (E,A)} F G H I J
(C,A) {(H,C) (D,A) (E,A)}
Limit = 2
(H,C) {(D,A) (E,A)}
K L M N
(D,A) {(I,D) (J,D) (E,A)}
(I,D) {(J,D) (E,A)}
(J,D) not goal {(E,A)} O

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 57


Depth-Limited Search (DLS)
◼ DLS algorithm returns Failure (no solution)
◼ The reason is that the goal is beyond the limit (Limit =2): the goal depth is (d=4)
depthLimitedSearch(problem)
depth: 2, # of nodes tested: 10, expanded: 4

expnd. node Frontier A


Ø {(A,nil)}
(A,nil) {(B,A) (C,A) (D,A) (E,A)}
B C D E
(B,A) {(F,B) (G,B) (C,A) (D,A) (E,A)}
(F,B) {(G,B) (C,A) (D,A) (E,A)}
(G,B) {(C,A) (D,A) (E,A)} F G H I J
(C,A) {(H,C) (D,A) (E,A)}
Limit = 2
(H,C) {(D,A) (E,A)}
K L M N
(D,A) {(I,D) (J,D) (E,A)}
(I,D) {(J,D) (E,A)}
(J,D) {(E,A)} O
(E,A) not goal {} no expansion - Fail

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 58


Properties of Depth-First Search
Completeness
• The limited path introduces another problem which is the case when we choose l < d, in
which is our DLS will never reach a goal, in this case we can say that DLS is not
complete.

Time Complexity
• O(bl)
Space Complexity
• O(bl)
Optimally
• One can view DFS as a special case of the depth DLS, that DFS is DLS with l = infinity.
• DLS is not optimal even if l > d.

DLS can be used when the there is a prior knowledge to the problem, which is always not the case,
Typically, we will not know the depth of the shallowest goal of a problem unless we solved this problem
before.
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 59
Iterative Deepening Search
Section - 6
Iterative-Deepening Search (IDS)
▪ IDS consists of repeatedly applying limited depth search (DLS), with gradually
increasing limits.
▪ The IDS ends when a solution is found, or if the limited depth search returns
failure, which means there is no solution.
▪ The problem with depth-limited search is deciding on a suitable depth
parameter.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 61


Iterative-Deepening Search (IDS)
▪ This search method tries all possible depth limits; first 0, then 1, then 2 etc.,
until a solution is found.
▪ IDS may seem wasteful as it is expanding nodes multiple times. But the
overhead is small in comparison to the growth of an exponential search tree
▪ For large search spaces where is the depth of the solution is not known IDS is
normally the preferred search method.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 62


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes expanded: 0, tested: 0 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 63


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 1, expanded: 1 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) not goal {(A,S) (B,S) (C,S)} A B C
9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 64


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 2, expanded: 1 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) not goal {(B,S) (C,S)} no expand 9
4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 65


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 3, expanded: 1 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) not goal {(C,S)} no expand 4 6 2
6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 66


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 4, expanded: 1 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) {(C,S)} 4 6 2
(C,S) not goal {} no expand-FAIL 6 G 1
D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 67


Iterative-Deepening Search (IDS)
deepeningSearch(problem) S
depth: 2, # of nodes tested: 4(1), expanded: 2 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) {(C,S)} 4 6 2
(C,S) {} 6 G 1
(S,nil) no test {(A,S) (B,S) (C,S)} D E goal F

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 68


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 4(2), expanded: 3 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) {(C,S)} 4 6 2
(C,S) {} 6 G 1
(S,nil) {(A,S) (B,S) (C,S)} D E goal F
(A,S) no test {(D,A) (E,A) (B,S) (C,S)}
7

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 69


Iterative-Deepening Search (IDS)
deepeningSearch(problem) S
depth: 2, # of nodes tested: 5(2), expanded: 3 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) {(C,S)} 4 6 2
(C,S) {} 6 G 1
(S,nil) {(A,S) (B,S) (C,S)} D E goal F
(A,S) {(D,A) (E,A) (B,S) (C,S)}
7
(D,A) not goal {(E,A) (B,S) (C,S)} no expand
H

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 70


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 6(2), expanded: 3 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) {(C,S)} 4 6 2
(C,S) {} 6 G 1
(S,nil) {(A,S) (B,S) (C,S)} D E goal F
(A,S) {(D,A) (E,A) (B,S) (C,S)}
7
(D,A) {(E,A) (B,S) (C,S)}
(E,A) not goal {(B,S) (C,S)} no expand H

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 71


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 6(3), expanded: 4 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)} 9
(B,S) {(C,S)} 4 6 2
(C,S) {} 6 G 1
(S,nil) {(A,S) (B,S) (C,S)} D E goal F
(A,S) {(D,A) (E,A) (B,S) (C,S)}
7
(D,A) {(E,A) (B,S) (C,S)}
(E,A) {(B,S) (C,S)} H
(B,S) no test {(G,B) (C,S)}

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 72


Iterative-Deepening Search (IDS)
deepeningSearch(problem) S
depth: 2, # of nodes tested: 7(3), expanded: 4 start
5
expnd. node Frontier 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)}
9
(B,S) {(C,S)} 4 6 2
(C,S) {} 6 G 1
(S,nil) {(A,S) (B,S) (C,S)} D E goal F
(A,S) {(D,A) (E,A) (B,S) (C,S)}
7
(D,A) {(E,A) (B,S) (C,S)}
(E,A) {(B,S) (C,S)} H
(B,S) {(G,B) (C,S)}
(G,B) goal {(C,S)} no expand

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 73


Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 7(3), expanded: 4 start
expnd. node Frontier 5 2 4
Ø {(S,nil)}
(S,nil) {(A,S) (B,S) (C,S)} A B C
(A,S) {(B,S) (C,S)}
(B,S) {(C,S)} 9 4 6 2
(C,S) {} 6 G 1
(S,nil) {(A,S) (B,S) (C,S)} D E goal F
(A,S) {(D,A) (E,A) (B,S) (C,S)}
7
(D,A) {(E,A) (B,S) (C,S)}
(E,A) {(B,S) (C,S)} H
(B,S) {(G,B) (C,S)}
(G,B) {(C,S)} path: (G,B) → (B,S) → (S,nil)
: S→B→G
Cost: S-B = 2, B-G = 6
: S-G = 8
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 74
Iterative Deepening Search (Exercise)
Initial state Goal state
A

B C D E F

G H I J K L M N O P

Q R S T U V W X Y Z

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 75


Properties of Iterative Deepening Search

Completeness
Yes

Time Complexity
O(bd)

Space Complexity
O(bd)

Optimally
Yes, Yes, if step cost = 1 or increasing function of depth
b – maximum branching factor of the search tree
d – depth of the least-cost solution
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 76
Bidirectional Search
Section - 7
Bidirectional search

Anti-missile system

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 78


Bidirectional search
▪ Both search forward from initial state, and backwards from goal.
▪ Stop when the two searches meet in the middle.
▪ Problem: how do we search backwards from goal?
• predecessor of node n = all nodes that have n as successor
• this may not always be easy to compute!
• if several goal states
• Efficient way to check whether a given node belongs to the other search
tree.
• Select a given search algorithm for each half.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 79


Bidirectional search
Subgraph 1 Subgraph 2

Search 1 starts from Root node Search 2 starts from Goal node

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 80


Bidirectional search
1. QUEUE1 <-- path only containing the root;
QUEUE2 <-- path only containing the goal;

2. WHILE both QUEUEs are not empty


AND QUEUE1 and QUEUE2 do NOT share a state

DO remove their first paths;


create their new paths (to all children);
reject their new paths with loops;
add their new paths to back;

3. IF QUEUE1 and QUEUE2 share a state


THEN success;
ELSE failure;
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 81
Bidirectional Search

Initial State Final State

d/2

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 82


Properties of Bidirectional Search

Completeness Yes, complete if b is finite, and the state space either has a
solution or is finite

Time Complexity
2*O(bd/2) = O(bd/2)

Space Complexity
O(bd/2)

Optimally
Yes, if both directions are breadth-first or uniform-cost

b – maximum branching factor of the search tree


d – is the depth of the shallowest solution
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 83
Comparison Uninformed Search Strategies

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 84


Tutorial
Section - 8
TUTORIAL 03
1. Define uninformed/blind search strategy.
2. What is the difference between
a) Depth First Search and Breadth First Search
b) Depth Limit Search and Iterative deepening Search
3. Prove that Breadth-first search is a special case of uniform-cost search and give a
counterexample.
4. What are the time and space complexities of these searches (BFS, DFS, UCS, DLS, IDS,
BDS).
5. Write Pros and Cons of these, algorithms (BFS, DFS, UCS, DLS IDS, BDS)
6. What data structures are required to implement these search (BFS, DFS, UCS, DLS IDS, BDS)
algorithms.
7. Are these search (BFS, DFS, UCS, DLS IDS, BDS) algorithms optimal and complete?

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 86


TUTORIAL 03
8. Consider the search space below, where S is the start node and G1 and G2 satisfy the goal test. Arcs
are labelled with the cost of traversing them.
For each of the following search strategies, indicate which goal state is reached (if any) and list, in
order, all the states popped off the FRONTIER list (i.e. give the order in which the nodes are visited).
When all else is equal, nodes should be removed from FRONTIER in alphabetical order.
S
BFS 2
Goal state reached: ___ States popped off FRONTIER: A 3
1 2
Iterative Deepening B 1 C
Goal state reached: ___ States popped off FRONTIER:
G1
DFS 8 1
Goal state reached: ___ States popped off FRONTIER: 1
5 5

Uniform Cost 9
D
Goal state reached: ___ States popped off FRONTIER: 2

E 7 G2
What would happen to DFS if S was always visited first?

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 87


TUTORIAL 03
9. How are nodes expanded by
S
1 5 8 • Depth First Search
• Breadth First Search
• Uniform Cost Search
A B C • Iterative Deepening

3 7 9 4 5 Are the solutions the same?

D E G

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 88


Breadth First Search (BFS)
Find a path from node A to the goal nod B. Use BFS method.

O
Z S F

A R
B
P
D
T L M C
© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 89
Uniform Cost Search (UCS)
Find a path from node 1 to the goal node 11. Use UCS method.

© Dr. Tariq 2024 Department of Computer Sciences | Bahria University 90


Artificial Intelligence CSC-325

Lecture – 3 Thank Any


Uninformed Search Questions ?
You

Dr. Muhammad Tariq Siddique


Department of Computer Sciences
Bahria University, Karachi Campus, Pakistan
[email protected]
Faculty room 14, 2nd Floor, Iqbal Block

You might also like