0% found this document useful (0 votes)
18 views36 pages

AI Lecture 06

The document describes a simple search algorithm that uses depth-first search (DFS) to traverse a tree structure. It initializes a queue with the starting node and iterates through the following steps: (1) remove the next node from the queue, (2) if it is a goal node, return it, (3) otherwise explore its children and add them to the queue and visited set. This results in a depth-first exploration that prioritizes exploring nodes deeper in the tree first before moving higher up. Examples are provided to illustrate how DFS traverses nodes in a tree structure.

Uploaded by

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

AI Lecture 06

The document describes a simple search algorithm that uses depth-first search (DFS) to traverse a tree structure. It initializes a queue with the starting node and iterates through the following steps: (1) remove the next node from the queue, (2) if it is a goal node, return it, (3) otherwise explore its children and add them to the queue and visited set. This results in a depth-first exploration that prioritizes exploring nodes deeper in the tree first before moving higher up. Examples are provided to illustrate how DFS traverses nodes in a tree structure.

Uploaded by

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

Simple Search Algorithm

Let S be the start state


1. Initialize Q with the start node Q=(S) as only entry;
set Visited = (S)
2. If Q is empty, fail. Else pick node X from Q
3. If X is a goal, return X, we’ve reached the goal
4. (Otherwise) Remove X from Q
5. Find all the children of node X not in Visited
6. Add these to Q; Add Children of X to Visited
7. Go to Step 2
DFS
• Depth First Search dives into a tree
deeper and deeper to fine the goal state.
We will use the same Simple Search
Algorithm to implement DFS by keeping
our priority function as
Simple Search Algorithm
Let S be the start state
1. Initialize Q with the start node Q=(S) as only entry;
set Visited = (S)
2. If Q is empty, fail. Else pick node X from Q
3. If X is a goal, return X, we’ve reached the goal
4. (Otherwise) Remove X from Q
5. Find all the children of node X not in Visited
6. Add these to Q; Add Children of X to Visited
7. Go to Step 2
DFS: Example
S

A B

C D E F

G H

Q Visited
1
2
3
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2
3
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
DFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
BFS: Example
S

P(n) = height (n) A B

C D E F

G H

Q Visited
1
2
3
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2
3
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5
BFS: Example
S

A B

C D E F

G H

Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5
BFS: Example
S

A B

C D E F

G H

Q Visited
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6
BFS: Example
S

A B

C D E F

G H
Q Visited
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7
BFS: Example
S

A B

C D E F

G H

Q Visited
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8
BFS: Example
S

A B

C D E F

G H

Q Visited
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9
BFS: Example
S

A B

C D E F

G H

Q Visited
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10
BFS: Example
S

A B

C D E F

G H

Q Visited
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
BFS: Example
S

A B

C D E F

G H
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H

6 E,F,G,H S,A,B,C,D,E,F,G,H

7 F,G,H S,A,B,C,D,E,F,G,H

8 G,H S,A,B,C,D,E,F,G,H

9 H S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
Problem with BFS
• Imagine searching a tree with branching
factor 8 and depth 10. Assume a node
requires just 8 bytes of storage. The breadth
first search might require up to:

= (8)10 nodes
= (23)10 X 23 = 233 bytes
= 8,000 Mbytes
= 8 Gbytes
Problems with DFS and BFS
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 lowest path even in
presence of infinite paths, but it has one great
problem
•BFS requires a great deal of space (exponential
in depth)
Progressive Deepening
S

A B

C D E F

G H I J

K L M N
Progressive Deepening
S

A B

C D E F

G H I J

K L M N
Progressive Deepening
• it guarantees to find the solution at a
minimum depth like BFS. Imagine that
there are a number of solutions below
level 4 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.
Heuristically Informed
• Heuristic Example
A B 3 C
2
5
– Here you see the

6
S
G
distances between each 4
city and the goal D E 3 F
3

– If you wish to reach the


3 3
goal, it is usually better to A B C
be in a city that is close,

2
but not necessarily; city C

4
S
G
is closer than, but city C is
not a good place to be 3
D E F
2
1 3
Hill Climbing
S
9 11
A B
7.3 8.5 9 9
C D E F
7
6
G H 5 I J
6 4
4
2 0
K L M N

Hill Climbing is DFS with a heuristic measurement


that orders choices. The numbers beside the
nodes are straight-line distances from the path-
terminating city to the goal city.
Hill Climbing
• Example:
– Blind person climbing a hill.
Hill Climbing
When we start at S we see that if we move to A we will be left with 9 units
to travel.

S
9 11
A B
7.3 8.5 7.1 9
C D E F
7
5.3
G H 5 I J
6 2
4
2.5 0
K L M N
Standing on A we see that C takes us closer to the goal hence we move
to C.

S
9 11
A B
7.3 8.5 7.1 9
C D E F
7
5.3
G H 5 I J
6 2
4
2.5 0
K L M N
From C we see that city H give us more improvement hence we move to
H and then finally to L.

S
9 11
A B
7.3 8.5 7.1 9
C D E F
7
5.3
G H 5 I J
6 2
4
2.5 0
K L M N
Beam Search
Degree = 2

At every level use only 2 S


best nodes 9 11
A B
7.3 8.5 7.1 9
C D E F
7
5.3
G H 5 I J
6 2
4
2.5 0
K L M N

You might also like