Graph Search Rob Platt Northeastern University Some images and - - PowerPoint PPT Presentation

graph search
SMART_READER_LITE
LIVE PREVIEW

Graph Search Rob Platt Northeastern University Some images and - - PowerPoint PPT Presentation

Graph Search Rob Platt Northeastern University Some images and slides are used from: AIMA CS188 UC Berkeley What is graph search? Start state Goal state Graph search: find a path from start to goal what are the states? what are


slide-1
SLIDE 1

Graph Search

Rob Platt Northeastern University Some images and slides are used from: AIMA CS188 UC Berkeley

slide-2
SLIDE 2

What is graph search?

Graph search: find a path from start to goal – what are the states? – what are the actions (transitions)? – how is this a graph? Start state Goal state

slide-3
SLIDE 3

What is graph search?

Graph search: find a path from start to goal – what are the states? – what are the actions (transitions)? – how is this a graph? Start state Goal state

slide-4
SLIDE 4

What is graph search?

Graph search: find a path from start to goal – what are the states? – what are the actions (transitions)? – how is this a graph? Start state Goal state

slide-5
SLIDE 5

What is graph search?

Graph search: find a path from start to goal – what are the states? – what are the actions (transitions)? – how is this a graph? Start state Goal state

slide-6
SLIDE 6

What is a graph?

Graph: Edges: Vertices: Directed graph

slide-7
SLIDE 7

What is a graph?

Graph: Edges: Vertices: Undirected graph

slide-8
SLIDE 8

What is a graph?

Graph: Edges: Vertices: Also called states Also called transitions

slide-9
SLIDE 9

Defining a graph: example

slide-10
SLIDE 10

Defining a graph: example

How many states?

slide-11
SLIDE 11

Defining a graph: example

slide-12
SLIDE 12

Defining a graph: example

Pairs of states that are “connected” by one turn of the cube.

slide-13
SLIDE 13

Example: Romania

  • On holiday in Romania; currently

in Arad. Flight leaves tomorrow from Bucharest

  • Formulate goal: Be in Bucharest
  • Formulate problem:
  • states: various cities
  • actions: drive between cities
  • Find solution:
  • sequence of cities, e.g., Arad,

Sibiu, Fagaras, Bucharest

slide-14
SLIDE 14

Graph search

Given: a graph, G Problem: find a path from A to B – A: start state – B: goal state

slide-15
SLIDE 15

Graph search

Given: a graph, G Problem: find a path from A to B – A: start state – B: goal state

How?

slide-16
SLIDE 16

Problem formulation

A problem is defined by four items:

  • initial state e.g., “at Arad”
  • successor function S(x) = set of action–state pairs

e.g., S(Arad) = { Arad → Zerind, Zerind , . . .} ⟨ ⟩

  • goal test, can be explicit, e.g., x = “at Bucharest” implicit, e.g.,

NoDirt(x)

  • path cost (additive)

e.g., sum of distances, number of actions executed, etc. c(x, a, y) is the step cost, assumed to be ≥ 0

  • A solution is a sequence of actions leading from the initial state to a

goal state

slide-17
SLIDE 17

A search tree

Start at A

slide-18
SLIDE 18

A search tree

Successors of A

slide-19
SLIDE 19

A search tree

Successors of A parent children

slide-20
SLIDE 20

A search tree

Let's expand S next

slide-21
SLIDE 21

A search tree

Successors

  • f S
slide-22
SLIDE 22

A search tree

A was already visited!

slide-23
SLIDE 23

A search tree

A was already visited!

So, prune it!

slide-24
SLIDE 24

A search tree

In what order should we expand states? – here, we expanded S, but we could also have expanded Z or T – different search algorithms expand in different orders

slide-25
SLIDE 25

Breadth first search (BFS)

slide-26
SLIDE 26

Breadth first search (BFS)

slide-27
SLIDE 27

Breadth first search (BFS)

Start node

slide-28
SLIDE 28

Breadth first search (BFS)

slide-29
SLIDE 29

Breadth first search (BFS)

slide-30
SLIDE 30

Breadth first search (BFS)

slide-31
SLIDE 31

Breadth first search (BFS)

We're going to maintain a queue called the fringe – initialize the fringe as an empty queue Fringe

slide-32
SLIDE 32

Breadth first search (BFS)

– add A to the fringe fringe Fringe A

slide-33
SLIDE 33

Breadth first search (BFS)

  • - remove A from the fringe
  • - add successors of A to the fringe

fringe Fringe B C

slide-34
SLIDE 34

Breadth first search (BFS)

  • - remove B from the fringe
  • - add successors of B to the fringe

fringe Fringe C D E

slide-35
SLIDE 35

Breadth first search (BFS)

fringe Fringe D E F G

  • - remove C from the fringe
  • - add successors of C to the fringe
slide-36
SLIDE 36

Breadth first search (BFS)

fringe Fringe D E F G Which state gets removed next from the fringe?

slide-37
SLIDE 37

Breadth first search (BFS)

fringe Fringe D E F G Which state gets removed next from the fringe? What kind of a queue is this?

slide-38
SLIDE 38

Breadth first search (BFS)

fringe Fringe D E F G Which state gets removed next from the fringe? What kind of a queue is this?

FIFO Queue! (first in first out)

slide-39
SLIDE 39

Breadth first search (BFS)

slide-40
SLIDE 40

Breadth first search (BFS)

What is the purpose of the explored set?

slide-41
SLIDE 41

BFS Properties

Is BFS complete? – is it guaranteed to find a solution if one exists?

slide-42
SLIDE 42

BFS Properties

Is BFS complete? – is it guaranteed to find a solution if one exists? What is the time complexity of BFS? – how many states are expanded before finding a sol'n? – b: branching factor – d: depth of shallowest solution – complexity = ???

slide-43
SLIDE 43

BFS Properties

Is BFS complete? – is it guaranteed to find a solution if one exists? What is the time complexity of BFS? – how many states are expanded before finding a solution? – b: branching factor – d: depth of shallowest solution – complexity =

slide-44
SLIDE 44

BFS Properties

Is BFS complete? – is it guaranteed to find a solution if one exists? What is the time complexity of BFS? – how many states are expanded before finding a solution? – b: branching factor – d: depth of shallowest solution – complexity = What is the space complexity of BFS? – how much memory is required? – complexity = ???

slide-45
SLIDE 45

BFS Properties

Is BFS complete? – is it guaranteed to find a solution if one exists? What is the time complexity of BFS? – how many states are expanded before finding a solution? – b: branching factor – d: depth of shallowest solution – complexity = What is the space complexity of BFS? – how much memory is required? – complexity =

slide-46
SLIDE 46

BFS Properties

Is BFS complete? – is it guaranteed to find a solution if one exists? What is the time complexity of BFS? – how many states are expanded before finding a solution? – b: branching factor – d: depth of shallowest solution – complexity = What is the space complexity of BFS? – how much memory is required? – complexity = Is BFS optimal? – is it guaranteed to find the best solution (shortest path)?

slide-47
SLIDE 47

Uniform Cost Search (UCS)

slide-48
SLIDE 48

Uniform Cost Search (UCS)

Notice the distances between cities

slide-49
SLIDE 49

Uniform Cost Search (UCS)

Notice the distances between cities – does BFS take these distances into account?

slide-50
SLIDE 50

Uniform Cost Search (UCS)

Notice the distances between cities – does BFS take these distances into account? – does BFS find the path w/ shortest milage?

slide-51
SLIDE 51

Uniform Cost Search (UCS)

Notice the distances between cities – does BFS take these distances into account? – does BFS find the path w/ shortest milage? – compare S-F-B with S-R-P-B. Which costs less?

slide-52
SLIDE 52

Uniform Cost Search (UCS)

Notice the distances between cities – does BFS take these distances into account? – does BFS find the path w/ shortest milage? – compare S-F-B with S-R-P-B. Which costs less?

How do we fix this?

slide-53
SLIDE 53

Uniform Cost Search (UCS)

Notice the distances between cities – does BFS take these distances into account? – does BFS find the path w/ shortest milage? – compare S-F-B with S-R-P-B. Which costs less?

How do we fix this? UCS!

slide-54
SLIDE 54

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost Length of path

slide-55
SLIDE 55

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost Length of path Cost of going from state A to B: Minimum cost of path going from start state to B:

slide-56
SLIDE 56

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost Length of path Cost of going from state A to B: Minimum cost of path going from start state to B: BFS: expands states in order of hops from start UCS: expands states in order of

slide-57
SLIDE 57

Uniform Cost Search (UCS)

Same as BFS except: expand node w/ smallest path cost Length of path Cost of going from state A to B: Minimum cost of path going from start state to B: BFS: expands states in order of hops from start UCS: expands states in order of

How?

slide-58
SLIDE 58

Uniform Cost Search (UCS)

Simple answer: change the FIFO to a priority queue – the priority of each element in the queue is its path cost.

slide-59
SLIDE 59

Uniform Cost Search (UCS)

slide-60
SLIDE 60

UCS

Fringe A Path Cost Explored set:

slide-61
SLIDE 61

UCS

140 118 75

Explored set: A Fringe A S T Z Path Cost 140 118 75

slide-62
SLIDE 62

UCS

140 118 75 146

Explored set: A, Z Fringe A S T Z T Path Cost 140 118 75 146

slide-63
SLIDE 63

UCS

140 118 75 146 229

Explored set: A, Z, T Fringe A S T Z T L Path Cost 140 118 75 146 229

slide-64
SLIDE 64

UCS

140 118 75 239 220 146 229

Explored set: A, Z, T, S Fringe A S T Z T L F R Path Cost 140 118 75 146 229 239 220

slide-65
SLIDE 65

UCS

140 118 75 239 220 146 229

Explored set: A, Z, T, S Fringe A S T Z T L F R Path Cost 140 118 75 146 229 239 220

slide-66
SLIDE 66

UCS

140 118 75 239 220 336 317 146 229

Explored set: A, Z, T, S, R Fringe A S T Z T L F R C P Path Cost 140 118 75 146 229 239 220 336 317

slide-67
SLIDE 67

UCS

140 118 75 239 220 336 317 146 229 299

Explored set: A, Z, T, S, R, L Fringe A S T Z T L F R C P M Path Cost 140 118 75 146 229 239 220 336 317 299

slide-68
SLIDE 68

UCS

140 118 75 239 220 336 317 146 229 299

Explored set: A, Z, T, S, R, L Fringe A S T Z T L F R C P M Path Cost 140 118 75 146 229 239 220 336 317 299

When does this end?

slide-69
SLIDE 69

UCS

140 118 75 239 220 336 317 146 229 299

Explored set: A, Z, T, S, R, L Fringe A S T Z T L F R C P M Path Cost 140 118 75 146 229 239 220 336 317 299

When does this end? – when the goal state is removed from the queue

slide-70
SLIDE 70

UCS

140 118 75 239 220 336 317 146 229 299

Explored set: A, Z, T, S, R, L Fringe A S T Z T L F R C P M Path Cost 140 118 75 146 229 239 220 336 317 299

When does this end? – when the goal state is removed from the queue – NOT when the goal state is expanded

slide-71
SLIDE 71

UCS

slide-72
SLIDE 72

UCS Properties

Is UCS complete? – is it guaranteed to find a solution if one exists? What is the time complexity of UCS? – how many states are expanded before finding a solution? – b: branching factor – C*: cost of optimal solution – e: min one-step cost – complexity = What is the space complexity of BFS? – how much memory is required? – complexity = Is BFS optimal? – is it guaranteed to find the best solution (shortest path)?

slide-73
SLIDE 73

S

a b d p a c e p h f r q q c

G

a q e p h f r q q c

G

a Strategy: expand cheapest node first: Fringe is a priority queue (priority: cumulative cost) 3 9 1 16 4 11 5 7 13 8 10 11 17 11 6 Cost contours

UCS vs BFS

slide-74
SLIDE 74

UCS vs BFS

S

a b d p a c e p h f r q q c

G

a q e p h f r q q c

G

a Search Tiers Strategy: expand a shallowest node fjrst Implementation: Fringe is a FIFO queue

slide-75
SLIDE 75

UCS vs BFS

Start Goal … c ≤ 3 c ≤ 2 c ≤ 1

  • Remember: UCS explores

increasing cost contours

  • The good: UCS is complete and
  • ptimal!
  • The bad:
  • Explores options in every

“direction”

  • No information about goal

location

  • We’ll fjx that soon!
slide-76
SLIDE 76

Depth First Search (DFS)

slide-77
SLIDE 77

DFS

fringe Fringe A

slide-78
SLIDE 78

DFS

fringe Fringe A B C

slide-79
SLIDE 79

DFS

fringe Fringe A B C F G

slide-80
SLIDE 80

DFS

Fringe A B C F G H I

slide-81
SLIDE 81

DFS

Fringe A B C F G H I Which state gets removed next from the fringe?

slide-82
SLIDE 82

DFS

Fringe A B C F G H I Which state gets removed next from the fringe? What kind of a queue is this?

slide-83
SLIDE 83

DFS

Fringe A B C F G H I Which state gets removed next from the fringe? What kind of a queue is this?

LIFO Queue! (last in first out)

slide-84
SLIDE 84

DFS vs BFS: which one is this?

slide-85
SLIDE 85

DFS vs BFS: which one is this?

slide-86
SLIDE 86

BFS/UCS: which is this?

slide-87
SLIDE 87

BFS/UCS: which is this?

slide-88
SLIDE 88

DFS Properties: Graph search version

Is DFS complete? – only if you track the explored set in memory What is the time complexity of DFS (graph version)? – how many states are expanded before finding a solution? – complexity = number of states in the graph What is the space complexity of DFS (graph version)? – how much memory is required? – complexity = number of states in the graph Is DFS optimal? – is it guaranteed to find the best solution (shortest path)? This is the “graph search” version of the algorithm

slide-89
SLIDE 89

DFS Properties: Graph search version

Is DFS complete? – only if you track the explored set in memory What is the time complexity of DFS (graph version)? – how many states are expanded before finding a solution? – complexity = number of states in the graph What is the space complexity of DFS (graph version)? – how much memory is required? – complexity = number of states in the graph Is DFS optimal? – is it guaranteed to find the best solution (shortest path)? This is the “graph search” version of the algorithm

So why would we ever use this algorithm?

slide-90
SLIDE 90

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm

slide-91
SLIDE 91

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm What is the space complexity of DFS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity =

slide-92
SLIDE 92

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm What is the space complexity of DFS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = This is why we might want to use DFS

slide-93
SLIDE 93

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm What is the space complexity of DFS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity =

slide-94
SLIDE 94

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm What is the space complexity of DFS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity = Is it complete?

slide-95
SLIDE 95

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm What is the space complexity of DFS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity = Is it complete?

NO!

slide-96
SLIDE 96

DFS: Tree search version

Suppose you don't track the explored set. – why wouldn't you want to do that? This is the “tree search” version of the algorithm What is the space complexity of DFS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity = Is it complete?

NO! What do we do???

slide-97
SLIDE 97

IDS: Iterative deepening search

What is IDS? – do depth-limited DFS in stages, increasing the maximum depth at each stage

slide-98
SLIDE 98

IDS: Iterative deepening search

What is IDS? – do depth-limited DFS in stages, increasing the maximum depth at each stage What is depth limited search? – any guesses?

slide-99
SLIDE 99

IDS: Iterative deepening search

What is IDS? – do depth-limited DFS in stages, increasing the maximum depth at each stage What is depth limited search? – do DFS up to a certain pre-specified depth

slide-100
SLIDE 100

IDS: Iterative deepening search

… b

  • Idea: get DFS’s space advantage

with BFS’s time / shallow-solution advantages

  • Run a DFS with depth limit 1. If

no solution…

  • Run a DFS with depth limit 2. If

no solution…

  • Run a DFS with depth limit 3.

…..

  • Isn’t that wastefully redundant?
  • Generally most work happens in

the lowest level searched, so not so bad!

slide-101
SLIDE 101

IDS

slide-102
SLIDE 102

IDS

What is the space complexity of IDS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity = Is it complete?

slide-103
SLIDE 103

IDS

What is the space complexity of IDS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity = Is it complete? YES!!! Is it optimal?

slide-104
SLIDE 104

IDS

What is the space complexity of IDS (tree version)? – how much memory is required? – b: branching factor – m: maximum depth of any node – complexity = What is the time complexity of DFS (tree version)? – how many states are expanded before finding a solution? – complexity = Is it complete? YES!!! Is it optimal? YES!!!

slide-105
SLIDE 105

General thoughts about search

If your model is wrong, then your solution will be wrong.

  • In November 2010, Nicaraguan troops

unknowingly crossed the border to Costa Rica, removed that country's flag and replaced it with their own. The reason: Google Maps told the troops' commander the territory belonged to Nicaragua.