Search
Search
The operation can be a search process. It can be reasoning like we do. It can be direct computation of
the solution. Or something else.
2
One thing seems to be certain is that all the operations have one thing in common:
Representation can take may forms. They can be words in a language. They can be vectors of values
that represent those words. They can be pixels in an image. They can be digitized soundwaves.
It all starts with representation. Let’s look at one from machine learning.
3
Say, we want our computer to learn about fish and we care only about these features of fish:
4
Here’s a machine learning dataset showing examples or instances of fish:
5
Here’s a machine learning dataset showing examples or instances of fish:
6
Imagine a table with 96 rows, each a unique instance. The salmon are indicated by a 1.
… … … … … … …
Each such table denotes a concept. How many unique concepts can there be?
7
Imagine a table with 96 rows, each a unique instance. The salmon are indicated by a 1.
… … … … … … …
Each such table denotes a concept. How many unique concepts can there be? 296
Of all those concepts, the table above is the concept of salmon, at least per the given features.
8
Now imagine a table with 296 rows, each a unique concept. Allowed concepts are indicated by a 1.
Concept Allow
1 1
2 0
3 1
4 1
… …
296 0
22
96
Each such table denotes a hypothesis. How many unique hypotheses can there be?
9
Now imagine a table with 296 rows, each a unique concept. Allowed concepts are indicated by a 1.
Concept Allow
1 1
2 0
3 1
296 0
22
96
Each such table denotes a hypothesis. How many unique hypotheses can there be?
10
Now imagine a decision or thought is plucked out of the hypothesis space we just saw.
Imagine a search of that hypothesis space.
If a mere handful of features can spawn such a gigantic number of hypotheses, how much more
gigantic is the hypothesis space we humans search? Or do we?
We’ll answer that question later, but let’s see how algorithmic search can be performed.
11
Search can be done upfront (given complete information and feasibility). That would be akin to
planning followed by execution.
Or actions can be interleaved with search.
Search can occur in a geographical space: think finding a route from point A to point B.
Search can also be fruitful for abstract spaces (e.g., concept spaces).
12
8-Puzzle
5 4 1 2 3
6 1 8 8 4
7 3 2 7 6 5
14
Breadth-first Search (BFS) – one way to search the tree
And so on…
Breadth
15
Depth-first Search (DFS) – another way to search the tree
And so on…
16
Tree Search
Goal: Find the path from the start state to the goal state
Strategy: Methodically search until the goal is reached
3. Repeat.
17
States vs. Nodes
parent
A state is a (representation of) a configuration of the world.
te
sta
18
Tree Search Algorithm Pseudocode
19
There’s this thing called the fringe (aka frontier).
It’s what stores the nodes slated to be checked.
20
How the fringe is tracked differentiates between BFS and DFS:
The first one in will be the first one out. The last one going on the stack will be the first one out.
21
Let’s do BFS. Say, our goal state is what’s there in node 16.
Remember, we’ll use a queue.
Fringe now:
???
Breadth
22
Let’s do BFS. Say, our goal state is what’s there in node 16.
Remember, we’ll use a queue.
Fringe now:
2, 3, 4
Breadth
23
We pop our fringe and get 2.
We check 2. We don’t find the goal.
We expand and get 5.
We push it into our fringe.
Fringe now:
???
Breadth
24
We pop our fringe and get 2.
We check 2. We don’t find the goal.
We expand and get 5.
We push it into our fringe.
Fringe now:
3, 4, 5
Breadth
25
We pop our fringe and get 3.
We check 3. We don’t find the goal.
We expand and get 6, 7, 8.
We push them into our fringe.
Fringe now:
???
Breadth
26
We pop our fringe and get 3.
We check 3. We don’t find the goal.
We expand and get 6, 7, 8.
We push them into our fringe.
Fringe now:
4, 5, 6, 7, 8
Breadth
27
We pop our fringe and get 4.
We check 4. We don’t find the goal.
We expand and get 9.
We push it into our fringe.
Fringe now:
???
Breadth
28
We pop our fringe and get 4.
We check 4. We don’t find the goal.
We expand and get 9.
We push it into our fringe.
Fringe now:
5, 6, 7, 8, 9
Breadth
29
We pop our fringe and get 5.
We check 5. We don’t find the goal.
We expand and get 10, 11.
We push them into our fringe.
Fringe now:
???
Breadth
30
We pop our fringe and get 5.
We check 5. We don’t find the goal.
We expand and get 10, 11.
We push them into our fringe.
Fringe now:
6, 7, 8, 9, 10, 11
Breadth
31
After a while, we’re done checking 7.
Note that in the fringe, 7 is followed by 8.
Fringe now:
???
Breadth
32
After a while, we’re done checking 7.
Note that in the fringe, 7 is followed by 8.
Fringe now:
9, 10, 11, 12, 13, 14, 15, 16, 17
Breadth
33
With BFS, we’ve always expanded the shallowest unexpanded node.
Breadth
34
Now let’s do DFS. Say, our goal state is again what’s there in node 16.
Remember, we’ll use a stack now.
Fringe now:
[ 2, 3, 4
Stack bottom
35
Halt!!! Hold your horses!
What do we get if we pop our fringe?
Fringe now:
[ 4, 3, 2
36
We pop our fringe and get 2. Depth
Fringe now:
[ ???
37
We pop our fringe and get 2. Depth
Fringe now:
[ 4, 3, 5
38
We pop our fringe and get 5. Depth
Fringe now:
[ ???
39
We pop our fringe and get 5. Depth
Fringe now:
[ 4, 3, 11, 10
40
Notice that we’re going down the leftmost branch.
So far, we’ve checked 1, 2, and 5.
Fringe now:
[ ???
41
Notice that we’re going down the leftmost branch.
So far, we’ve checked 1, 2, and 5.
Fringe now:
[ 4, 3, 11, 20
42
We pop our fringe and get 20. Depth
Fringe now:
[ ???
43
We pop our fringe and get 20. Depth
Fringe now:
[ 4, 3, 11, 35, 34
44
We pop our fringe and get 34. Depth
Fringe now:
[ ???
45
We pop our fringe and get 34. Depth
Fringe now:
[ 4, 3, 11, 35
46
Notice that we’re done going down the leftmost branch.
We’ve now shifted focus to the right branch below 20.
Fringe now:
[ ???
47
Notice that we’re done going down the leftmost branch.
We’ve now shifted focus to the right branch below 20.
Fringe now:
[ 4, 3, 11
48
We’ve now searched the entire left branch under 5.
As before, we’ll now shift focus to the right branch under 5.
Fringe now:
[ ???
49
We’ve now searched the entire left branch under 5.
As before, we’ll now shift focus to the right branch under 5.
Fringe now:
[ 4, 3, 23, 22, 21
50
As before, we’ll now go down the left branch under 11, then right.
So, we’re searching left to right.
51
With DFS, we’ve always expanded the deepest unexpanded node.
52
Why do both BFS and DFS and not just one?
It has to do with memory and computation speed.
53
Our worries, tabulated:
54
BFS and DFS compared:
The worry What it means for BFS What it means for DFS
Space and time complexities use Big O notation, depicting the worst case.
b = branching factor
d = depth of shallowest solution
m = max depth of tree 55
BFS and DFS compared:
In plain English,
BFS has a high memory requirement.
DFS is neither necessarily complete nor optimal.
56
A tree for 8-Puzzle graphs all possible move sequences.
Only some paths from root to leaf will lead to the goal. We want that path.
We don’t need an exhaustive search of the whole tree. We can use DFS.
A tree for your LinkedIn graphs all your connections, level (degree) by level.
We want to find the shortest path to a connection.
We can do BFS. If we find the connection, we know that’s the shortest path.
57
We want this:
Optimality and completeness of BFS + memory-friendliness of DFS
58
Depth-limited DFS is DFS but with a depth limit of L specified.
Space O(bL)
Time O(bL)
Complete? Not if solution is longer than L
Optimal? No, for the same reason why DFS isn’t
b = branching factor
59
Iterative Deepening is Depth-limited DFS with iterative increase in depth, i.e., 0, 1, 2, … , ∞.
60
Iterative Deepening is wasteful, but that’s not such a bad thing, really.
Cost for L = 0 is 1
Cost for L = 1 is 1 + b
Cost for L = 2 is 1 + b + b2
Cost for L = 3 is 1 + b + b2 + b3
…
Cost for L = d is L = d: 1 + b + b2 + b3 + … + bd
61
Iterative Deepening DFS characteristics:
Space O(bd)
Time O(bd)
Complete? Yes
Optimal? Yes
b = branching factor
d = depth of shallowest solution
62
BFS and DFS are uninformed search strategies.
They use only the information available in the basic problem definition, i.e., the tree of possible states.
They don’t look at the states themselves except to ask: Is this the goal? Have I been here before?
In summary:
63
Can we do better than uninformed search? Can we make an intelligent (think informed) choice?
We can heuristics.
64
Two heuristics for 8-Puzzle
5 6
1 2 3
3 4 8 4
7 6 5
5 6
Goal State
65
Best-first Search
Special cases:
§ Greedy best-first search
§ A* search
66
Greedy Best-first Search (GBFS)
Just like BFS uses a queue and DFS uses a stack, GBFS uses a priority-queue.
A priority-queue stays sorted based on the priority of its items, e.g., from smallest to largest distance.
67
A* Search
68
A* Search outperforms BFS and DFS for 8-Puzzle
BFS
DFS
A* Search
69
In Romania, what are ways to go from Sibiu to Bucharest? Easy to figure out.
70
But what about Oradea to Neamt? Not so easy now, is it?
71
How can search handle combinatorial explosion?
72
Let’s A* Search through a map.
73
We add A to the Open set. It’s our current vertex.
We measure A’s distance from the start, which is 0. We record it in the table.
We also record the heuristic distance from A to L, which is 36.1.
We then record the sum, which is also 36.1. Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
10 B
C
0 36.1 0 36.1 0 36.1 0 36.1
D
36.1 36.1 36.1 36.1 E
A 14 B C D
10 F
0 36.1 0 36.1 0 36.1 0 36.1 Open = {A}
G
36.1 36.1 36.1 36.1 H
E F G H Closed = {} I
0 36.1 0 36.1 0 36.1 0 36.1 J
74
We now want to look at A’s neighbors, so we add B, E, and F to the Open set.
We record the distance from the start for each node: 10 for B and E, 14 for F.
We record the heuristic distances and the sums.
We record A as each node’s previous vertex. Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C
0 36.1 10 28.3 0 36.1 0 36.1
D
36.1 38.3 36.1 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 0 36.1 0 36.1 Open = {A, B, E, F}
G
41.6 36.4 36.1 36.1 H
E F G H Closed = {} I
0 36.1 0 36.1 0 36.1 0 36.1 J
75
Now that everything involving A is complete, we move it to the Closed set.
We now need a new current vertex. We look at the sums of B, E, and F.
F has the lowest sum, so it’s our new current vertex.
Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C
0 36.1 10 28.3 0 36.1 0 36.1
D
36.1 38.3 36.1 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 0 36.1 0 36.1 Open = {B, E, F}
G
41.6 36.4 36.1 36.1 H
E F G H Closed = {A} I
0 36.1 0 36.1 0 36.1 0 36.1 J
76
We now want to look at F’s 8 neighbors: A, B, C, E, G, I, J, and K.
A is in Closed. We add the other 7 to Open. It already has B and E, so we end up adding the other 5.
We record the distances, sums, and previous vertices of all 7.
The values for B and E don’t change. Why? Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C 28 22.4 50.4 F
0 36.1 10 28.3 28 22.4 0 36.1
D
36.1 38.3 50.4 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 24 14.1 0 36.1 Open = {B, C, E, F, G, I, J, K}
G 24 14.1 38.1 F
41.6 36.4 38.1 36.1 H
E F G H Closed = {A} I 28 30 58 F
28 30 24 20 28 10 0 36.1 J 24 20 44 F
58 44 38 36.1 K 28 10 38 F
I J K L L
77
Since B is F’s neighbor, B’s distance from the start via F is 14 + 10 = 24.
However, B already has a shorter distance from the start. As such, we don’t update B.
Likewise for E.
Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C 28 22.4 50.4 F
0 36.1 10 28.3 28 22.4 0 36.1
D
36.1 38.3 50.4 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 24 14.1 0 36.1 Open = {B, C, E, F, G, I, J, K}
G 24 14.1 38.1 F
41.6 36.4 38.1 36.1 H
E F G H Closed = {A} I 28 30 58 F
28 30 24 20 28 10 0 36.1 J 24 20 44 F
58 44 38 36.1 K 28 10 38 F
I J K L L
78
Now that everything involving F is complete, we move it to the Closed set.
We now need a new current vertex again. K has the lowest sum, so it’s our new current vertex.
(When two nodes tie on the sum, we can use the heuristic distance to break the tie.)
Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C 28 22.4 50.4 F
0 36.1 10 28.3 28 22.4 0 36.1
D
36.1 38.3 50.4 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 24 14.1 0 36.1 Open = {B, C, E, G, I, J, K}
G 24 14.1 38.1 F
41.6 36.4 38.1 36.1 H
E F G H Closed = {A, F} I 28 30 58 F
28 30 24 20 28 10 0 36.1 J 24 20 44 F
58 44 38 36.1 K 28 10 38 F
I J K L L
79
We now want to look at K’s 5 neighbors: F, G, H, J, and L.
Previously looked-at neighbors either are in Closed or have shorter distances from the start.
So, only H and L get updated.
Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C 28 22.4 50.4 F
0 36.1 10 28.3 28 22.4 0 36.1
D
36.1 38.3 50.4 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 24 14.1 42 10 Open = {B, C, E, G, I, J, K}
G 24 14.1 38.1 F
41.6 36.4 38.1 52 H 42 10 52 K
E F G H Closed = {A, F} I 28 30 58 F
28 30 24 20 28 10 38 0 J 24 20 44 F
58 44 38 38 K 28 10 38 F
I J K L L 38 0 38 K
80
Now that everything involving K is complete, we move it to the Closed set.
We now need a current vertex yet again. L has the lowest sum, and it’s also our goal state!
The path from A to L can be traced backward using the previous vertices!
The solution path: A–F–K–L Vertex Distance Heuristic Sum Previous
from Start Distance Vertex
A 0 36.1 36.1
B 10 28.3 38.3 A
C 28 22.4 50.4 F
0 36.1 10 28.3 28 22.4 0 36.1
D
36.1 38.3 50.4 36.1 E 10 31.6 41.6 A
A B C D
F 14 22.4 36.4 A
10 31.6 14 22.4 24 14.1 42 10 Open = {B, C, E, G, I, J}
G 24 14.1 38.1 F
41.6 36.4 38.1 52 H 42 10 52 K
E F G H Closed = {A, F, K} I 28 30 58 F
28 30 24 20 28 10 38 0 J 24 20 44 F
58 44 38 38 K 28 10 38 F
I J K L L 38 0 38 K
81
Something curious to notice: C’s distance from the start
It’s 28. Why not 20?!
58 44 38 38 K 28 10 38 F
I J K L L 38 0 38 K
82
All trees are graphs, but not all graphs are trees.
A tree is a Directed Acyclic Graph (DAG) with one parent per node.
83
The graph shown here is not a tree but can be traversed like one, which is tree search.
Caveat: The same node may be visited more than once.
84
Admissible heuristics:
A heuristic, h(n), is admissible if for every node n, h(n) ≤ h*(n), where h*(n) is the true cost to reach
the goal state from n.
Example:
hSLD(n) (straight-line distance or “as the crow flies”) never overestimates the actual driving
distance.
85
Consistent heuristics:
A heuristic, h(n), is consistent, or monotone, if for every node n and each successor p,
h(n) ≤ c(n, p) + h(p), where c(n, p) is the actual cost to reach p from n.
In other words, while moving along a given path, the total estimated cost for that path, including the
real distance discovered so far + the remaining estimated distance, never gets smaller.
86
Some facts about heuristics:
87
Adversarial search is one way in which computers play games.
A two-player game (think chess) typically has an adversary, hence the name.
Strategic thinking is key in playing such games.
88
Here’s Tic-Tac-Toe as a graph of moves:
Initial state
Successor function
Terminal state
Utility
89
Pseudocode for Minimax, an optimal-strategy algorithm for games like Tic-Tac-Toe:
90
Max(imizer) and Min(imizer) both assume each other is playing optimally.
Minimax applies the assumption and the goal to each move of the game to foresee every move
sequence resulting in a win or a loss, picking moves accordingly.
91
Minimax has a shortcoming: searching the whole tree is redundant.
But depth-limiting this DFS-style search may miss out on the good moves that are beyond the frontier
of cut-off. This is the horizon effect.
We can prune the tree: eliminate parts of the tree from consideration using Alpha-Beta Pruning:
It prunes away branches that can’t possibly influence the final decision.
Consider a node n:
If a player has a better choice m (at a parent or further up), then n will never be reached.
So, once we know enough about n by looking at some successors, we can prune it.
92
Pseudocode for Minimax with Alpha-Beta Pruning:
93
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a = -¥
b=¥
a = -¥
b=¥
94
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a = -¥
b=¥
a = -¥
b=3
95
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a = -¥
b=¥
a = -¥
b=3
3 17
96
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a=3
b=¥
a = -¥
3 b=3
3 17
97
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a=3
b=¥
a = -¥ a=3
3 b=3 b=¥
3 17
98
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a=3
b=¥
a = -¥ a=3
3 b=3 b=2
=
3 17 2
99
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=¥
a=3
b=¥
a = -¥ a=3
3 2
b=3 b=2
=
3 17 2
100
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=3
a=3
3 b=¥
a = -¥ a=3
3 2
b=3 b=2
=
3 17 2
101
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=3
a=3 a = -¥
3 b=¥ b=3
a = -¥ a=3 a = -¥
3 2
b=3 b=2 b=3
=
3 17 2
102
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=3
a=3 a = -¥
3 b=¥ b=3
a = -¥ a=3 a = -¥
3 2
b=3 b=2 b=3
=
3 17 2 15
103
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=3
a=3 a = 15
3 b=¥ b=3
=
a = -¥ a=3 a = -¥
3 2 15
b=3 b=2 b=3
=
3 17 2 15
104
Walkthrough of Minimax with Alpha-Beta Pruning:
a = -¥
Max b=¥
Min
a = -¥
b=3
a=3 a = 15
3 b=¥ 15 b = 3
=
a = -¥ a=3 a = -¥
3 2 15
b=3 b=2 b=3
=
3 17 2 15
105
Walkthrough of Minimax with Alpha-Beta Pruning:
a=3
Max b=¥
Min
a = -¥
3
b=3
a=3 a = 15
3 b=¥ 15 b = 3
=
a = -¥ a=3 a = -¥
3 2 15
b=3 b=2 b=3
=
3 17 2 15
106
What did we see about searching through a space of possibilities?
We saw a variety of search methods with their own pros and cons.
Above all, we got a sense of the suitability of each method to a problem.
107