W4-Uninformed Searches

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

Uninformed Searches

Instructor: Dr. Ayesha Kashif


Lecturer, Riphah International University, Lahore

1
Search Problem Formulation
 Agent:entity that perceives its environment and acts upon
that environment
 State: a configuration of the agent and its environment

 Initial State: the state in which the agent begins

2
Search Problem Formulation
 Actions: choices that can be made in a state
ACTIONS(s) returns the set of actions that can be executed in
state s

3
Search Problem Formulation
 Transition Model
A description of what state results from performing any
applicable action in any state
RESULT(s, a) returns the state resulting from performing
action a in state s

4
Search Problem Formulation
 State Space
the set of all states reachable from the initial state by any
sequence of actions

5
Search Problem Formulation
 Goal Test
way to determine whether a given state is a goal state
 Path Cost
numerical cost associated with a given path

6
Search Problem Formulation
 Solution:
 a sequence of actions that leads from the initial state to a goal
state
 Optimal Solution
 a solution that has the lowest path cost among all solutions

7
Search Problem Formulation
 Node
a data structure that keeps track of
- a state
- a parent (node that generated this node)
- an action (action applied to parent to get node)
- a path cost (from initial state to node)

8
Search Problem Formulation
 We will use graphs to represent problems and their solution spaces. One
thing to be noted is that every graph can be converted into a tree, by
replicating the nodes. Consider the following example. The graph in the
figure represents a city map with cities labeled as S, A, B, C, D,
 E, F and G.

3 3
A B C A D

B D A E

S G C E E B B F

2 D F B F C E A C G
D E F
1 3
G C G F

9
Basic Search Algorithms
 Uninformed (Blind) search: breadth-first, depth-first, depth
limited, iterative deepening.

10
Map searching (navigation)

11
Linking Search to Trees and Graphs
 You can begin to visualize the concept of a graph (or Tree)
 Searching along different paths of the graph until you reach the
solution
 The nodes can be the states
 The whole graph can be the state space
 The links can be the actions……

12
Tree search example

13
13
Fringe
 Fringe: The collection of nodes that have been generated but not yet
expanded
 Each element of the fringe is a leaf node, with (currently) no
successors in the tree
 The search strategy defines which element to choose from the fringe

14 fringe
fringe 14
Queue Functions
 The fringe is implemented as a queue
 MAKE_QUEUE(element,…): makes a queue with the given
elements
 EMPTY?(queue): checks whether queue is empty
 FIRST(queue): returns 1st element of queue
 REMOVE_FIRST(queue): returns FIRST(queue) and removes it from
queue a.k.a pop(queue)
 INSERT(element, queue): add element to queue a.k.a.
push(element,queue)
 INSERT_ALL(elements,queue): adds the set elements to queue and
return the resulting queue
15
15
Search Strategies
 A search strategy is defined by picking the order of node
expansion
 Strategies are evaluated along the following dimensions:
 Completeness: Does it always find a solution if one exists?

 Optimality: Does it always find a least-cost solution?

 Time Complexity: How long does it take to find a solution. Number


of nodes generated.
 Space Complexity: Maximum number of nodes in memory

16
Time and Space Complexity ?
Time and space complexity are measured in terms of:

 The (effective) branching factor b:


 Maximum no. of successors of any node
 The average number of new nodes we create when expanding a new
node

 Depth d: Depth of the shallowest goal node


 The length of a path to a goal.

 m: The maximum length of any path in the state space.

17
Search Problem Formulation
 Approach
• Start with a frontier that contains the initial state.
• Repeat:
• If the frontier is empty, then no solution.
• Remove a node from the frontier.
• If node contains goal state, return the solution.
• Expand node, add resulting nodes to the frontier.

18
Breadth First Search

19
Breadth-first search
 Expand shallowest unexpanded node in the frontier
 Implementation:
 fringe is a FIFO queue, i.e., new successors go at end

20
3 March 2023 20
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
21
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
22
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
23
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
24
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
25
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
26
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
27
A

D B

F E
C

• Start Node: A G B
G E
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
28
A

D B

F E
C

G B
G E
• Start Node: A
• Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A } A ∅
2 {(A-D), (A,B)} D {A}
3 {(A-B),(A-D-F),(A-D-E)} B {A,D}
4 {(A-D-F),(A-D-E),(A-B-C)} F {A,D,B}
5 { (A-D-E),(A-B-C), (A-D-F-G} E {A,D,B,F}
6 {(A-B-C),(A-D-F-G)} C {A,D,B,F,E}
7 {(A-D-F-G), (A-B-C-G)} G {A,D,B,E,F,C}
8 {(A-B-C-G)} G {A,D,B,E,F,C,G}
9 ∅
• Visited path: A -> D -> B -> F -> E -> C -> G.
• Found the path: A -> D -> F -> G.
29
Breadth-first search

30
Time and Space Complexity ?
Time and space complexity are measured in terms of:

 The (effective) branching factor b:


 Maximum no. of successors of any node
 The average number of new nodes we create when expanding a new
node

 Depth d: Depth of the shallowest goal node


 The length of a path to a goal.

 m: The maximum length of any path in the state space.

31
Properties of Breadth-first search
 Complete?Yes (if b is finite)

 Time? b0+b1+b2+b3+… +bd = O(bd)


 i.e. exponential in d

 Space? O(bd) (keeps every node in memory)

 Optimal?Yes (if cost = 1 per step)

 Space is a big problem: exponential expansion

32
3 March 2023 32
Optimality
 To find path to a goal state that involves taking the least number of
steps.
 It might take a great deal of time for an optimal search method to
identify the optimal solution
 Because BFS examines all nodes at a given depth before moving on to
the next depth, if it finds a solution, there cannot be another solution
before it in the search tree.

33
Exponential Expansion Example
 The table assumes that 1 million nodes can be generated per second
and that a node requires 1000 bytes of storage.
 Many search problems fit roughly within these assumptions (give or
take a factor of 100) when run on a modern personal computer.

34
Depth First Search

35
Depth First Search (DFS)
 Main idea: Expand node at the deepest level (breaking ties left to right).

 Implementation: use of a Last-In-First-Out queue or stack(LIFO).


Enqueue nodes in LIFO (last-in, first-out) order.

36
Depth First Search

37
Start Node: A
Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {A} A ∅
2 {(A-B),(A-C)} B {A}
3 {(A-B-D),(A-B-E),(A-C)} D {A,B}
4 {(A-B-E),(A-C)} E {A,B,D}
5 {(A-C)} C {A,B,D,E}
6 {(A-C-F),(A-C-G)} F {A,B,D,E,C}
7 (A-C-G)} G {A,D,B,E,C,F,G}
8 ∅
Found the path: A -> C -> G.
38
BFS or DFS
Depth-Limited Search (DLS)
It is simply DFS with a depth bound.
▪ Searching is not permitted beyond the depth bound.
▪ Works well if we know what the depth of the solution is.

▪ Termination is guaranteed.

▪ If the solution is beneath the depth bound, the search cannot find the goal

(hence this search algorithm is incomplete).


▪ Otherwise use Iterative deepening search (IDS).

Depth Bound = 3
40
Problems with DFS and BFS
Though DFS and BFS are simple searching techniques which can get
us to the goal state very easily yet both of them have their own
problems.
DFS has small space requirements (linear in depth) but has major
problems:
 DFS can run forever in search spaces with infinite length paths
 DFS does not guarantee finding the shallowest goal
BFS guarantees finding the shallowest path even in presence of
infinite paths, but it has one great problem
 BFS requires a great deal of space (exponential in depth)
We can still come up with a better technique which caters for the
drawbacks of both these techniques. One such technique is
progressive deepening.

41
Iterative Deepening Search

42
Iterative Deepening Search (IDS)
Apply depth limited search and increase depth iteratively. The idea is to
simply apply DFS to a specific level. If you find the goal, exit, other
wise repeat DFS to the next lower level. Go on doing this until you
either reach the goal node or the full height of the tree is explored.
For example, apply a DFS to level 2 in the tree, if it reaches the goal
state, exit, otherwise increase the level of DFS and apply it again until
you reach level 3.You can increase the level of DFS by any factor.

43
Iterative deepening search l =0

44
3 March 2023 44
Iterative deepening search l =1

45
3 March 2023 45
Iterative deepening search l =2

46
3 March 2023 46
Iterative deepening search l =3

47
3 March 2023 47
Iterative deepening search
 As soon as the procedure finds the goal state it will quit.
Notice that it guarantees to find the solution at a minimum
depth like BFS.
 Imagine that there are a number of solutions below level 3 in
the tree. The procedure would only travel a small portion of
the search space and without large memory requirements,
will find out the solution.

48
Properties of iterative deepening search
 Complete?Yes

 Time? O(bd)

 Space? O(bd)

 Optimal?Yes, if step cost = 1

49
3 March 2023 49
Uniform Cost Search

50
Uniform Cost Search (UCS)
 For any step-cost function, Uniform Cost search expands the node n
with the lowest path cost.
 Implementation:
 fringe = queue ordered by path cost, lowest first
 UCS takes into account the total cost: g(n).

 UCS is guided by path costs rather than depths. Nodes are ordered
according to their path cost.

 Equivalent to breadth-first if step costs all equal

51
Uniform Cost Search (UCS)
A
5 2
[5] B C [2]
1 4 1 7
D
[6] [3] F G [9]
Goal state
[9] E
4 5
[x] = g(n) [7] H I [8]
path cost of node n
52
Uniform Cost Search (UCS)

A
5 2
[5] B C [2]

53
Uniform Cost Search (UCS)

A
5 2
[5] B C [2]

1 7
[3] F G [9]

54
Uniform Cost Search (UCS)

A
5 2
[5] B C [2]

1 7
[3] F G [9]
4 5

[7] H I [8]

55
Uniform Cost Search (UCS)

A
5 2
[5] B C [2]
1 4 1 7
D
[6] [3] F G [9]
[9] E
4 5

[7] H I [8]

56
Uniform Cost Search (UCS)

A
5 2
[5] B C [2]
Goal state 1
4 1 7
path cost
g(n)=[6] D [3] F G [9]
[9] E
4 5

[7] H I [8]

57
Uniform Cost Search (UCS)

A
5 2
[5] B C [2]
1 4 1 7
D
[6] [3] F G [9]
[9] E
4 5

[7] H I [8]

58
 Start Node: A
 Goal Node: G
Step Frontier Expand[*] Explored: a set of nodes
1 {(A,0)} A ∅
2 {(A-D,3),(A-B,5)} D {A}
3 {(A-B,5),(A-D-F,5),(A-D-E,5)} B {A,D}
4 {(A-D-F,5),(A-D-E,5),(A-B-C,6)} F {A,D,B}
5 {(A-D-E,5),(A-B-C,6),( A-D-F-G,8)} E {A,D,B,F}
6 {(A-B-C,6),(A-D-F-G,8)} C {A,D,B,F,E}
7 (A-D-F-G,8)} G {A,D,B,F,E,C}
8 ∅
 Found the path: A -> D -> F -> G.
59*B is not added to the frontier because it is found in the explored set.
Summary of Algorithms

b: Branching factor
d: Depth of solution
m: Maximum depth

l : Depth Limit

60
3 March 2023 60
Task
Simulate a real life/real world problem of your choice using:
1. Depth First Search
2. Breadth First Search
3. Make a comparison

61

You might also like