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

Section 3

The document discusses search algorithms, specifically Uniform Cost Search (UCS) and Dynamic Programming (DP), in the context of finding the shortest path in a graph with certain constraints. It illustrates the process of UCS through a series of examples and explains how to represent states and optimize the search using DP. The document concludes with a simulation of DP to solve a problem involving cities connected by roads with directional travel and constraints on visiting odd and even labeled cities.

Uploaded by

tanvirraihan106
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)
15 views

Section 3

The document discusses search algorithms, specifically Uniform Cost Search (UCS) and Dynamic Programming (DP), in the context of finding the shortest path in a graph with certain constraints. It illustrates the process of UCS through a series of examples and explains how to represent states and optimize the search using DP. The document concludes with a simulation of DP to solve a problem involving cities connected by roads with directional travel and constraints on visiting odd and even labeled cities.

Uploaded by

tanvirraihan106
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/ 73

CS221 Section 3: Search

DP, UCS and A*


Contents

1. Uniform Cost Search


2. Defining States
3. Dynamic Programming
4. A* Search
Uniform Cost Search
Idea: In UCS, we find the shortest cost to a node by using the fact we already
know the shortest path to a set of nodes.

Recall: We have the following three sets

● Explored Set: contains nodes we know the path length to


● Frontier Set: contains nodes that are neighbors of those in the explored set,
but we don’t know their costs yet
● Unexplored Set: Nodes in the graph we haven’t encountered
Problem - UCS
In the following graph, find the costs to reach each node given that we start on
node a.
Problem - UCS
Explored
[a : 0]

Frontier
[b : 0 + 1, e : 0 + 2, c : 0 + 3]

Unexplored

We start with node a. We add all neighbors of


a to the frontier. Note: [a : 0] means it takes 0
cost to get to node a.
Problem - UCS
Explored
[a : 0, b : 1]

Frontier
[c : 1 + 1, e : 0 + 2]

Unexplored

In the frontier, b has the lowest cost. Thus, we can add it to the
explored set. We add all neighbors of b to the frontier, updating
costs to reach some nodes if necessary (we updated c).
Problem - UCS
Explored
[a : 0, b : 1, c : 2]

Frontier
[e : 0 + 2, d : 2 + 1]

Unexplored

In the frontier, c has the lowest cost (ties broken alphabetically


here). Thus, we can add it to the explored set. We add all
neighbors of c to the frontier, updating as necessary.
Problem - UCS
Explored
[a : 0, b : 1, c : 2, e : 2]

Frontier
[d : 2 + 1, f : 2 + 1, h : 2 + 3]

Unexplored

In the frontier, e has the lowest cost. Thus, we can add it to the
explored set. We add all neighbors of e to the frontier, updating
as necessary.
Problem - UCS
Explored
[a : 0, b : 1, c : 2, e : 2, d : 3]

Frontier
[f : 2 + 1, g : 3 + 1, h : 2 + 3]

Unexplored

In the frontier, d has the lowest cost. Thus, we can add it to the
explored set. We add all neighbors of d to the frontier, updating
as necessary.
Problem - UCS
Explored
[a : 0, b : 1, c : 2, e : 2, d : 3, f : 3]

Frontier
[g : 3 + 1, h : 3 + 1]

Unexplored

In the frontier, f has the lowest cost. Thus, we can add it to the
explored set. We add all neighbors of f to the frontier, updating
as necessary.
Problem - UCS
Explored
[a : 0, b : 1, c : 2, e : 2, d : 3, f : 3, g : 4]

Frontier
[h : 3 + 1]

Unexplored

In the frontier, g has the lowest cost. Thus, we can add it to the
explored set. We add all neighbors of f to the frontier, updating
as necessary.
Problem - UCS
Explored
[a : 0, b : 1, c : 2, e : 2, d : 3, f : 3, g : 4,
h : 4]

Frontier

Unexplored

In the frontier, h has the lowest cost. Thus, we can add it to the
explored set. There are no more nodes in the frontier, so we are
done.
Uniform Cost Search
Contents

1. Uniform Cost Search


2. Defining States
3. Dynamic Programming
4. A* Search
Problem
There exists N cities, conveniently labelled from 1 to N.

There are roads connecting some pairs of cities. The road


connecting city i and city j takes c(i,j) time to traverse. However,
one can only travel from a city with smaller label to a city with
larger label (i.e. each road is one-directional).

From city 1, we want to travel to city N. What is the shortest time


required to make this trip, given the additional constraint that we
should visit more odd-labeled cities than even labeled cities?
Example

2 Best path is [1, 3, 4, 5] with


5 2 cost 16.
7
1 4 5 [1, 2, 4, 5] has cost 14 but
1
visits equal number of odd
3 and even cities.
6
3
State Representation
State Representation
We need to know where we are currently at: current_city

We need to know how many odd and even cities we have


visited thus far: #odd, #even

State Representation: (current_city, #odd, #even)

Total number of states: O(N3)


Can We Do Better?
Check if all the information is really required

We store #odd and #even so that we can check whether


#odd - #even > 0 at (N, #odd, #even)

Why not store #odd - #even directly instead?

(current_city, #odd - #even) -- O(N2) states


Solving the Problem
Since we are computing shortest path, which is some form
of optimization, we consider DP and UCS.
Recall:
● DP can handle negative edges but works only on DAGs
● UCS works on general graphs, but cannot handle
negative edges
Since we have a DAG and all edges are positive, both work!
We already went through UCS, so we solve this with DP.
Contents

1. Uniform Cost Search


2. Defining States
3. Dynamic Programming
4. A* Search
Solving the Problem: Dynamic Programming

If s has no successors, we set it as undefined


Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
5 2
7
1 1 4 5 1, 1

3 6
3
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
5 2
7
1 1 4 5 1, 1

3 6
3
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2, 0
2
5 2 5
7
1 1 4 5 1, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2, 0
2
5 2 5
7
1 1 4 5 1, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2, 0
2
5 2 5
7
1 1 4 5 1, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7
1 1 4 5 1, 1 3, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7
1 1 4 5 1, 1 3, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7
1 1 4 5 1, 1 3, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7 6
1 1 4 5 1, 1 3, 1 4, 0

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7 6
1 1 4 5 1, 1 3, 1 4, 0

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7 6
1 1 4 5 1, 1 3, 1 4, 0

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

2
2, 0 4,-1
2
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2
2, 0 4,-1
2
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2
2, 0 4,-1 (4, 0) 0+7
2
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2
2, 0 4,-1 (4, 0) 7
2 (3, 1) 7 + 6 = 13
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2
2, 0 4,-1 (4, 0) 7
2 (3, 1) 13
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2
2, 0 4,-1 (4, 0) 7
2 (3, 1) 13
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2
2, 0 4,-1 (4, 0) 7
2 (3, 1) 13
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 13+1 = 14

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3
3, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3 6
3, 2 4, 1
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3 6
3, 2 4, 1
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3 6
3, 2 4, 1
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3
3 6 7
3, 2 4, 1 5, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3 (5, 2) 0

3 6 7
3, 2 4, 1 5, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3 (5, 2) 0

3 6 7 (4, 1) 7
3, 2 4, 1 5, 2
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3 (5, 2) 0

3 6 7 (4, 1) 7
3, 2 4, 1 5, 2
(3, 2) 13
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3 (5, 2) 0

3 6 7 (4, 1) 7
3, 2 4, 1 5, 2
(3, 2) 13

(1, 1) 16
Simulation of DP Visiting Successors Completed
Cache

Regular Graph State Graph Key Value

(5, 1) 0
2 7
2, 0 4,-1 5, 0 (4, 0) 7
2 (3, 1) 13
5 2 5 1
(5, 0) ∞
7 6 7
1 1 4 5 1, 1 3, 1 4, 0 5, 1
(4, -1) ∞

(2, 0) 14

3 6 3 (5, 2) 0

3 6 7 (4, 1) 7
3, 2 4, 1 5, 2
(3, 2) 13

(1, 1) 16
Improve UCS: A* Search
Contents

1. Uniform Cost Search


2. Defining States
3. Dynamic Programming
4. A* Search
Recap of A* Search
● We want to avoid wasted effort (to go from SF to LA, we
probably don’t want to end up looking at roads to Seattle, for
example).
● To do this, we can use a heuristic to estimate how far is left
until we reach our goal.
● The heuristic must be optimistic. It must underestimate the
true cost. Why?
Recap of A* Search
● Modify the cost of edges and run UCS on the new graph
○ New cost = Current cost + future cost
○ Cost’(s, a) = Cost(s, a) + h(Succ(s, a)) - h(s)
● You can find a good consistent h by performing relaxation.
● If c is min cost on original graph, c’ is min cost on modified
graph, then c’ = c + h(s_goal) - h(s_start)
Relaxation
A good way to come up with a reasonable heuristic is to
solve an easier (less constrained) version of the problem
For example, we can use geographic distance as a
heuristic for distance if we have the positions of nodes.
Note: The main point of relaxation is to attain a problem
that can be solved more efficiently.
How to compute h for our example?
Consider again our example from before. Suppose we ignore
the constraint that there must be more odd cities visited. This
is a relaxation of the problem. The following is h for our graph:

city 1 2 3 4 5
h 14 9 13 7 0
Modified State Graph
0 0
2, 0 4,-1 5, 0
0 5
6 0
1, 1 3, 1 4, 0 5, 1

2
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (2, 0) : 5 + 9 - 14 = 0
0 5 (3, 2) : 3 + 13 - 14 = 2
6 0
1, 1 3, 1 4, 0 5, 1

2
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (3, 2) : 3 + 13 - 14 = 2
0 5 (2, 0): 0 (3, 1): 1 + 13 - 9 = 5
6 0 (4, -1): 2 + 7 - 9 = 0
1, 1 3, 1 4, 0 5, 1

2
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (3, 2) : 3 + 13 - 14 = 2
0 5 (2, 0): 0 (3, 1): 1 + 13 - 9 = 5
6 0 (4, -1): 0 (5, 0): 7 + 0 - 7 = 0
1, 1 3, 1 4, 0 5, 1

2
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (3, 2) : 3 + 13 - 14 = 2
0 5 (2, 0): 0 (3, 1): 1 + 13 - 9 = 5
6 0 (4, -1): 0
1, 1 3, 1 4, 0 5, 1 (5, 0): 0

2
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (3, 1): 1 + 13 - 9 = 5
0 5 (2, 0): 0 (4, 1): 6 + 7 - 13 = 0
6 0 (4, -1): 0
1, 1 3, 1 4, 0 5, 1 (5, 0): 0
(3, 2): 2
2
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (3, 1): 1 + 13 - 9 = 5
0 5 (2, 0): 0 (5, 2): 7 + 0 - 7 = 0
6 0 (4, -1): 0
1, 1 3, 1 4, 0 5, 1 (5, 0): 0
(3, 2): 2
2 (4, 1): 0
0 0
3, 2 4, 1 5, 2
Simulation of UCS (A*)
0 0 Explored: Frontier:
2, 0 4,-1 5, 0 (1, 1) : 0 (3, 1): 1 + 13 - 9 = 5
0 5 (2, 0): 0
6 0 (4, -1): 0
1, 1 3, 1 4, 0 5, 1 (5, 0): 0
(3, 2): 2 STOP!
2 (4, 1): 0
0 0 (5, 2): 0
3, 2 4, 1 5, 2
Comparison of States visited
UCS UCS(A*)
Explored: Frontier: Explored: Frontier:
(1, 1) : 0 (5, 1) : 19 (1, 1) : 0 (3, 1): 5
(3, 2) : 3 (2, 0): 0
(2, 0) : 5 (4, -1): 0
(3, 1) : 6 (5, 0): 0
(4, -1) : 7 (3, 2): 2
(4, 1) : 9 (4, 1): 0
(4, 0) : 12 (5, 2): 0
(5, 0) : 14
(5, 2) : 16
Summary
● States Representation/Modelling
○ make state representation as compact as possible, remove
unneccesary information
● DP
○ underlying graph cannot have cycles
○ visit all reachable states, but no log overhead
● UCS
○ actions cannot have negative cost
○ visit only a subset of states, log overhead
● A*
○ ensure that relaxed problem can be solved more efficiently

You might also like