You are on page 1of 53

Fall, 2020

ARTIFICIAL INTELLIGENCE
LEC-02: Uninformed Search

Lecturer: Duong Thuy Do


Outline
 Introduction
 State-Space Graphs
 Generate & Test Paradigm
 Blind Search Algorithm
 Implementing and Comparing Blind Search
Algorithms
 Summary

2
Introduction

3
Search in Intelligent Systems
 Search is a natural part of people’s lives

 Software that solves searching problems faster are


considered to be more intelligent
 Algorithms: order linear search, heuristic search, binary
search tree, balanced search tree …

4
State-space graphs

5
State Space Graphs
 A representation of a problem whereby possible
alternative paths leading to a solution can be explored and
analyzed.
 A solution to a particular problem will correspond to a
path through a state-space graph.
 Sometimes we search for any solution to a problem; at
other times we desire a shortest (or optimal) solution.

6
The False Coin Problem
 12 coins in which there is 1 counterfeit (fake coin)

 Unknown whether the fake is lighter or heavier

 Uses a balance scale to determine


o If any two sets weigh the same
o One set is lighter or heavier than the other

 Solve the problem in three weighings


7
Mini False Coin Problem
• Will use 6 coins instead of 12.
• Use notation Ci1 Ci2 … Cir: Cj1Cj2 …
Cjr to indicate weighing r coins Ci1Ci2
… Cir against r coins Cj1Cj2 … Cjr

At the top level we weigh C1C2: C3C4


• If they are equal then we branch to the
left knowing C1 C2 C3 C4 are valid
• If C1:C5 are equal then we know C6 is
fake. If they are not then C5 is fake.
• If C1C2: C3C4 is not equal then we
branch to the right knowing C5C6 are
real
8
Mini False Coin Problem

 IF C1 C2 : C5 C6 are equal then we know


either C3 or C4 is fake and we branch to the left
9
Mini False Coin Problem

At the left branch we test C1:C3 . If they are


equal then C4 is fake otherwise C3 is fake.

If C1C2 : C5C6 are not equal then to branch


right and test C1:C4 . If they are equal then we
know C2 is fake otherwise C1 is fake.

10
Generate-and-test paradigm

11
Generate and Test Paradigm

Propose possible solutions then test whether


each proposal constitutes a solution

Will illustrate with n-Queens problem

12
n-Queens Problem
 n-Queens are to be placed on an n x n board
 No two Queens should occupy the same row, column or
diagonal
 Example with a 4 x 4 board with 4 Queens

13
Generate and Test
 A total of 16C4 or 1820 ways to accomplish this
 Figure 2.4 illustrates that many of the proposed
solutions violate one or more of the constraints
 A reliable generator is:
 Complete – that is, must propose every subset of
size four that satisfies the problem constraints.
 Informed – that is, capable of excluding certain
proposals that cannot possibly be successful.
 Non-redundant – if a proposed solution is
rejected or successful, it should not be proposed
again
14
Pseudocode

{While no solution is found and more candidates


remain
Generate a possible solution
Test if all problem constraints are satisfied
End While}
If a solution has been found, announce success and
output it.
Else announce no solution found.

15
Exhaustive Enumeration

Figure (a):
Partial solution

• Start with the partial solution in the Figure a, we should


continue to place two additional Queens on the chessboard,
even though any way of placing these Queens leads to
failure. (Figure b)

16
Exhaustive Enumeration

 A search methodology that looks everywhere for a solution


to a problem

 A partial solution is developed further even after it has been


discovered that this set of steps cannot possibly lead to a
successful problem solution

 Need the tester check that no problem constraints are


violated after each portion of a proposed solution is provided.

17
Backtracking
 An improvement to exhaustive enumeration

 A proposed solution is divided into stages

 In the 4-Queens problem, placing each queen is a stage (4


queens ~ 4 stages). In Stage i, Queen i will be successfully
placed in columns i (i = 1  4).
 If no square remains on which the Queen i may be

placed that does not violate any of the constraints,


then we must backtrack to Stage i-1

18
Backtracking
 Undo the placement of the Queen at Stage i-1, make the next
choice for this Queen, and return to Stage i. If it is not
possible to successfully place the Queen (i-1), then
backtracking continues to Stage i-2
 Can use Backtracking with Generate and Test
 The Generator will attempt to place a Queen in each column.
 The Test module will view a possible solution as it is being
developed.

19
Backtracking

The algorithm contains four stages.


 Stage 1: Place Queen 1 in column 1.
Queen 1 is placed on row 1 (a)

 Stage 2: Place Queen 2 in column 2.


Queen 2 is placed (b) but it violates one of the constraints so it
20 placed in a different spot  New location for Queen 2 (c)
Backtracking

 Stage 3: Place Queen 3 in column 3.


No more Queens can be placed that will not violate any
of the constraints (d)  Algorithm will backtrack to
Stage 2 and replace the Queen 2.

21
 Backtrack to Stage 2: Queen 2 is placed in row 4 (a)
 Stage 3: Queen 3 is placed in row 2 (not row 1 since
violation) (a)  Solution vector: (1,4,2,_)
 Stage 4: Unable to place Queen 4 (b)  Backtrack to
Stage 3
 Stage 3: No more place in column 3 for Queen 3 (c) 
Backtrack to Stage 2
 Stage 2: No more place in column 2 for Queen 2 (d) 
Backtrack to Stage 1
 Stage 1: Queen 1 is placed in row 2 (e)
22
Backtracking
 Stage 2: Queen 2 is placed in row 4
 Stage 3: Queen 3 is placed in row 1
 Stage 4: Queen 4 is placed in row 3

Solution (2,4,1,3)
Any other solutions?

23
Backtracking

To find another solution, backtrack to:


Stage 4: Nowhere else for Queen 4 (b)
Stage 3: Nowhere else for Queen 3 (c)
Stage 2: Nowhere else for Queen 2 (d)
Stage 1: Place the Queen 1 in row 3  Solution (e)
24
Backtracking
These two solutions have a symmetric relationship.
The 2nd solution is found by vertically flipping the
board.

(2,4,1,3) (3,1,4,2)
25
The Greedy Algorithm
Operates by dividing a problem into stages

Always contains an objective function that


must be optimized

 Selects the locally best choice at each stage

26
The Greedy Algorithm
 Dijkstra’s algorithm is an example
of a greedy algorithm
 Suppose that a salesperson is
starting from Chengdu and wants
to find the shortest path to Harbin,
that is, from points v1 to v3

Edsgar Dijkstra
(1930-2002)

27
The Greedy Algorithm
v1 v2 v3 v4 v5
Dijkstra’s 1 0 ∞ ∞ ∞ ∞
algorithm is an 2 1518,v1 ∞ 1539,v1 606,v1
example of a
greedy 3 1518,v1 2579,v2 1539,v1
algorithm 4 2579,v2 1539,v1
5 2579,v2

28
The Traveling Salesperson

 Given n-vertices on a weighted graph

 Start at point v1 and traverse through each


vertex only once and end up back at v1

29
The Traveling Salesperson

30
The Traveling Salesperson

 Using the greedy algorithm, our salesperson will travel a


total of 6157 km. However if the salesperson were to
travel to Beijing, Harbin, Hangzhou, Chengdu, and then
return to Xi’an, the total cost would be 5942 km.
 Some problems in computer science cannot be solved
using greedy algorithm.

31
Blind search

32
Blind Search Algorithms
 Three principal algorithms
 depth first search (DFS)
 breadth first search (BFS)
 depth first search with iterative deepening (DFS-ID)
 Two key properties
 They do not use heuristics
 Their aim is to find some solution to a problem

33
Depth First Search
 Attempts to plunge as deeply into a tree as quickly as
possible
 If there is a choice, it selects the leftmost branch

34
Breadth First Search
 Nodes are visited from the top of the tree to the
bottom, from left to right
 All nodes on level i are visited before any nodes on
level i+1 are visited

35
Implementing and comparing blind
search algorithms

36
Implementing Depth First Search
 Shares an attribute with the other search algorithms
 Maintains two lists:
 open list – list of nodes still being explored
 closed list – list of nodes already explored and
no longer under consideration
 Depth first search is accomplished by maintaining the
open list as a stack

37
Depth First Search

Algorithm stops once it reaches the goal at G1


38
Implementing BFS

BFS maintains the open list as a queue.


Once a node is expanded, its children move to the rear of the
open list  these children are explored only after every
other node at its parent’s level has been visited.

39
Implementing BFS

Algorithm stops once goal G1 is reached .


40
Measuring Problem Solving
Performance

Completeness – an algorithm is complete when it is


guaranteed to find a solution when there is one
Optimality – an algorithm is optimal if it provides
the lowest cost path amongst all solutions
Time Complexity – measured in the number of
nodes generated during the search
Space Complexity – how much memory is required
to perform the search. We must also determine the
maximum number of nodes
41
The Branching Factor
The branching factor of a node is the
number of branches emanating from it

The branching factor of A is three


If every node in a search tree has a branching
factor of b, then the branching factor of the
tree is b
42
Branching Factor

 d : this parameter measures the depth of the


shallowest goal node
 m : this parameter measures the maximum
length of any path

43
Comparing BFS and DFS
 BFS is preferred if
The branching factor is not too large
(hence memory costs)
 A solution appears at a relatively shallow level
 No path is excessively deep

 DFS is preferred if
The Tree is deep
 The branching factor is not excessive
 Solutions occur deeply in the tree
44
Comparing BFS and DFS / cont.
 requires b*m+1 nodes which is O (b*m)
DFS
 DFS is not complete and is not optimal

45
Comparing BFS and DFS
BFS:
Time complexity

T(n) = b + b2 + b3 + … + (bd-1 – b) = O (bd+1)

 Every node that is generated must also remain


in memory. Space complexity

 (S(n)) = O (bd+1)

 BFS is complete when the search space is finite


46
DFS with Iterative Deepening
(DFS-ID)

 Performs a DFS with a depth bound of 0


 If goal is not found then another DFS is
performed with a depth bound of 1
 The search continues with depth bound
increasing by one each time

47
DFS-ID / cont.
A complete DFS
is performed to
the current
depth each time

48
DFS-ID
 Each tree is redrawn from scratch

 No tree is built from a tree with depth bound one lower

 Total number of nodes generated is


((d+ 1) * 1) + (d*b) + (d-1) * b2 + … + 1* bd

 Time complexity for DFS-ID is O (b d)

49
DFS-ID
 A path is erased from memory each time depth bound
increases so space complexity is also O(b*d)
 DFS-ID is complete when branching factor is finite
 Optimal when path costs are a non-decreasing function
of a node’s depth

50
Keywords

 Backtracking  Optimal
State Space Graph
 Depth First Search
 Blind search algorithms
State Space Tree (DFS)
 Breadth First Search  Iterative Deepening
(BFS) Traveling  Generate and Test
Salesperson
 Greedy Algorithm
 Closed List
Problem (TSP)
 Complete

51
Summary
Overview of Blind or uninformed search algorithms
including DFS, BFS, DFS-ID.
Generate and Test Paradigm
Exhaustive Enumeration – looks everywhere
Backtracking – abandons partial solution dead ends
Greedy Algorithm – a useful approach to solve some
problems
All Blind Search Algorithms exhibit exponential
time-complexity
52
Review Question
 Why is search an important component of an AI
system?
 What is a state-space graph?
 Name three blind search algorithms
 When is DFS preferable to BFS?

53

You might also like