Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views43 pages

Cs221 LEC 3 Slides

The document outlines search problems in CS221, focusing on defining states, actions, goals, costs, and successors to find a sequence of actions that minimizes cost. It discusses various search algorithms including backtracking, depth-first search, breadth-first search, and dynamic programming, emphasizing the importance of state spaces and costs in optimizing searches. Additionally, it introduces A* search as an enhancement of uniform cost search using heuristics for more efficient pathfinding.

Uploaded by

memoko8574
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)
8 views43 pages

Cs221 LEC 3 Slides

The document outlines search problems in CS221, focusing on defining states, actions, goals, costs, and successors to find a sequence of actions that minimizes cost. It discusses various search algorithms including backtracking, depth-first search, breadth-first search, and dynamic programming, emphasizing the importance of state spaces and costs in optimizing searches. Additionally, it introduces A* search as an enhancement of uniform cost search using heuristics for more efficient pathfinding.

Uploaded by

memoko8574
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/ 43

CS221 Problem Workout

Week 3
Search Problems
● Need to define:
○ States
■ Start State
○ Actions
○ Goals
○ Costs
○ Successors

Objective?

Find a sequence of actions such


that cost is minimized

2
States
● A state space contains all the possible
configurations of the system.

● Each state tells you everything you need to know


about “where you are” towards reaching your goal.

Image Credit: Khan Academy


3
States
● A state space contains all the possible
configurations of the system.

● Each state tells you everything you need to know


about “where you are” towards reaching your goal.

● Ex) Trying to visit every Coupa Cafe on campus.


○ State = (longitude, latitude, visitedY2E2?,
visitedGSB?, visitedGreenLibrary?, …)

Image Credit: Khan Academy


4
States
● A state space contains all the possible
configurations of the system.

● Each state tells you everything you need to know


about “where you are” towards reaching your goal.

● Ex) Trying to visit every Coupa Cafe on campus.


○ State = (longitude, latitude, visitedY2E2?,
visitedGSB?, visitedGreenLibrary?, …)

● Ex) Trying to solve towers of Hanoi


○ State = position and ordering of each of the
blocks

Image Credit: Khan Academy


5
Actions
● The action space describes all the possible things
you can do to move from one state to another

● Ex) Trying to visit every Coupa Cafe on campus.


○ Walk North, Walk South, Walk East, Walk
West, take the Margueritte from stop A to
stop B, etc.

● Ex) Trying to solve towers of Hanoi


○ Move block from one pole to another

Image Credit: Khan Academy


6
Goals
● The goals decide what the end state of your search
is.

● Possible to have more than one valid goal state.

● Ex) Trying to visit every Coupa Cafe on campus.


○ Visited every Coupa Cafe on campus

● Ex) Trying to solve towers of Hanoi


○ All blocks in ascending order by size on the
final pole

Image Credit: Khan Academy


7
Costs
● The costs assign a “price” to each action you take.

● Some search algorithms break under negative costs


○ BFS
○ UCS
○ A*

● Controls what you are optimizing for in the search

● Ex) Trying to visit every Coupa Cafe on campus.


○ Option 1: How long it will take to do an
action (ex. 10 minutes on the marguerite)
○ Option 2: How far is the distance (ex. walk
100 meters north)
○ Option 3: Monetary cost (ex. $3 clipper card
fare)

● Ex) Trying to solve towers of Hanoi


○ Can assign uniform cost to each action Image Credit: Khan Academy
8
Successors
● Defines the new state you are in from a current
state after taking an action

● Successor(state, action) => new_state

● Ex) Trying to visit every Coupa Cafe on campus.


○ New longitude, New latitude, visitedY2E2?,
visitedGSB?, visitedGreenLibrary?, ...

● Ex) Trying to solve towers of Hanoi


○ New order and positions of the blocks after
the movement of a block

Image Credit: Khan Academy


9
How can we solve search problems?
Search

Tree search (NO Graph search


cycles in state (potentially HAS
space) cycles)

Exponential time Dynamic A* (UCS +


programming UCS consistent
● Backtracking (non negative cost)
● DFS (0 cost) (polynomial time) heuristic)
● BFS (constant cost)
● DFS with iterative
deepening (constant cost)

Checkout https://stanford.edu/~shervine/teaching/cs-221/cheatsheet-states-models for visualizations!


Backtracking Search
Start

Goal

11
Backtracking Search
Start

Goal

12
Depth First Search
Start

Goal

13
Depth First Search
Start

Goal

14
Breadth First Search
Start

Goal

15
Breadth First Search
Start

Goal

16
Iterative Deepening Depth First Search
Start

Goal

17
Iterative Deepening Depth First Search
Start

Goal

18
Dynamic Programming
● An algorithm that is akin to backtracking search with memoization and potentially
exponential savings!
● The states in DP contain a summary of past actions sufficient to choose future
actions optimally.

Backtracking (tree) Dynamic Programming (graph)

19
Dynamic Programming Example
● Grid Dimensions: The grid dimensions are m (rows) and n (columns)
● Movement Constraints: Wall-E can only move either down or to the right at any given point. It cannot
move diagonally or backwards.
● Problem: find the number of unique paths the Wall-E can take to get home.

20
Dynamic Programming Example
● Nodes: each cell is a node on the graph
● Edges: each edge is a possible path for the robot.
● Ideas and Intuition: ?

(0, 0) (0, 1) (0, 2)

(1, 0) (1, 1) (1, 2)

(2, 0) (2, 1) (2, 2)

21
Dynamic Programming Example
● Ideas and Intuition: use a 2D array to store the number of unique paths to each cell. A cell (i,j) can be
reached either from (i−1,j) or (i,j−1), and thus the number of unique paths to (i,j) is the sum of the
number of unique paths to these two cells.

(0, 0) (0, 1) (0, 2) 1

(1, 0) (1, 1) (1, 2)

(2, 0) (2, 1) (2, 2)

Original Grid Dynamic Programming 2D array

22
Dynamic Programming Example
● Ideas and Intuition: use a 2D array to store the number of unique paths to each cell. A cell (i,j) can be
reached either from (i−1,j) or (i,j−1), and thus the number of unique paths to (i,j) is the sum of the
number of unique paths to these two cells.

(0, 0) (0, 1) (0, 2) 1 1 1

(1, 0) (1, 1) (1, 2) 1 1+1=2 1+2

(2, 0) (2, 1) (2, 2) 1 1+2=3 3+3=6

Original Grid Dynamic Programming 2D array

23
Dynamic Programming Example
● Ideas and Intuition: use a 2D array to store the number of unique paths to each cell. A cell (i,j) can be
reached either from (i−1,j) or (i,j−1), and thus the number of unique paths to (i,j) is the sum of the
number of unique paths to these two cells.

24
DP does not work if there are cycles
- DP requires the search tree to be a Directed Acyclic Graph (DAG)
- This is because we need an ordering to fill out the entries in the memo;
otherwise, we would not know where to start!

Bad
Uniform Cost Search

Explored
Frontier
Unexplored
Uniform Cost Search
UCS - Pseudocode (from lecture slides)
UCS - Proof of Correctness
UCS - Proof of Correctness
UCS - Proof of Correctness
UCS - Proof of Correctness

Contradiction!
UCS does not work if there are negative edges

Middle
- Optimal: start -> middle -> end with a cost of -5
5 -10 - UCS finds: start -> end with a cost of 1
- Always try to come up with your own examples
- The simpler the example, the better!

Start End
1
Problem (1a)
● Describe A:

● s =
start

● Actions((x,y,A)) = {N,S,E,W}
● Succ((x,y,A),a) =

● Cost((x,y,A),a) =

● IsGoal((x,y,A)) =

34
Problem (1b)

35
Looking Ahead: Heuristics

Image Credit: Cleveland.com

36
A*: UCS with heuristics
- A* is an expansion of UCS, but we use an estimate of “future cost”
- This should give us a better estimate of total cost to END = past cost + future cost
- Leads to more efficient search!

Requirements of a good heuristic for A*:


- h(Succ(s, a)) - h(s) should a measurement of whether we are getting closer to END
- We will run UCS on the new costs, so the new costs have to be non-negative
Consistent Heuristic
- We will run UCS on the new costs, so the new costs have to be non-negative

- If the new costs are non-negative, UCS would return the correct result
Problem (1c)

39
Problem (2a)

40
Problem (2b)

41
Thank You
42
Come to our office hours!

Samantha Liu Rishi Agarwal

General OH: Thursdays 2:00-4:00 Online HW OH: Tuesdays 9am to 1pm Huang Basement
HW OH: Tuesdays 2:00-4:00 Online

43

You might also like