Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

27
Graphs II Robin Burke GAM 376

Transcript of Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Page 1: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Graphs II

Robin Burke

GAM 376

Page 2: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Admin

Skip the Lua topic

Page 3: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Graph search

Problemis there a path from v to w?what is the shortest / best path?

• optimality

what is a plausible path that I can compute quickly?

• bounded rationality

Page 4: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

General search algorithm

Start with "frontier" = { (v,v) }

Until frontier is empty remove an edge (n,m) from the frontier set mark n as parent of m mark m as visited if m = w,

• return otherwise

• for each edge <i,j> from m• add (i, j) to the frontier

• if j not previously visited

Page 5: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Depth First Search

Last-in first-out We continue expanding the most

recent edge until we run out of edgesno edges out orall edges point to visited nodes

Then we "backtrack" to the next edge and keep going

Page 6: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

DFS

v1 v2

v3 v4 v5

v6 v7

start

target

Page 7: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Breadth-first search

First-in first-out Expand nodes in the order in which

they are addeddon't expand "two steps" awayuntil you've expanded all of the "one

step" nodes

Page 8: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

BFS

v1 v2

v3 v4 v5

v6 v7

start

target

Page 9: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

What if edges have weight?

If edges have weight then we might want the lowest weight path a path with more nodes might have lower

weight Example

a path around the lava pit has more steps but you have more health at the end compared to the path that goes through the

lava pit We will cover this next week

Page 10: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Weighted graph

v1 v2

v3 v4 v5

v6 v7

1

1

1

2

21

5 3

3

23

1

Page 11: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Edge relaxation

It is not enough to knownode n is reachable via path P

We need to know the cost to reach node n via path Pbecause path Q might be cheaper

In which casewe discard path Pit can't enter into a solution

Page 12: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Djikstra's Algorithm

Use a priority queue a data structure in which the item with the smallest

"value" is always first items can be added in any order

Use the "value" of an edge as the total cost of the path through that edge always expand the node with the least cost so far

If an edge leads to a previously expanded node compare costs

• if greater, ignore edge• if lesser, replace path and estimate at node with new

value "Greedy" algorithm

Page 13: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Djikstra's algorithm

v1 v2

v3 v4 v5

v6 v7

1

1

1

2

21

5 3

3

23

1

3 1

4

34

65

5

5

Page 14: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Characteristics

We have discovered the cheapest route to every node nice side effect

Can be deceived by early gains garden-path phenomenon

Guaranteed to find the shortest path Complexity

O(|E| log |E|) not too bad

Page 15: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Priority Queue

This algorithm depends totally on the priority queue

Various techniques to implementsorted list

• yuck

heap• better

many proposed variants

Page 16: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Different Example

Problem: Visit too many nodes, some clearly out of the question

Page 17: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Better Solution: Heuristic

Use heuristics to guide the search Heuristic: estimation or “hunch” of how to

search for a solution We define a heuristic function:

h(n) = “estimate of the cost of the cheapest path from the starting node to the goal node"

We could use this instead of our greedy "lowest cost so far" technique

Page 18: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Use a Heuristic for cost

Heuristic: minimize h(n) = “Euclidean distance to destination”

Problem: not optimal (through Rimmici Viicea and Pitesti is shorter)

Page 19: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

The A* Search

Difficulty: we want to still be able to generate the path with minimum cost

A* is an algorithm that: Uses heuristic to guide search While ensuring that it will compute a path

with minimum cost

• A* computes the function f(n) = g(n) + h(n)

“actual cost”

“estimated cost”

Page 20: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

A* f(n) is the priority (controls which node to expand) f(n) = g(n) + h(n)

g(n) = “cost from the starting node to reach n” h(n) = “estimate of the cost of the cheapest path

from n to the goal node”

1015

20

2015

5

1825

33

ng(n)

h(n)

Page 21: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Example

A*: minimize f(n) = g(n) + h(n)

Page 22: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Properties of A*

A* generates an optimal solution if h(n) is an admissible heuristic and the search space is a tree: h(n) is admissible if it never overestimates the

cost to reach the destination node A* generates an optimal solution if h(n) is a consistent

heuristic and the search space is a graph: h(n) is consistent if for every node n and for every

successor node n’ of n:

h(n) ≤ c(n,n’) + h(n’) n

n’

dh(n)

c(n,n’) h(n’)

Page 23: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Admissible Heuristics

A heuristic is admissible if it is too optimistic, estimating the cost to be smaller than it actually is.

Example: for maps

• Euclidean distance• no path can be shorter than this

• but this requires a square root for grid maps

• Manhattan distance is sometimes used

Page 24: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Inadmissable Heuristics

If a heuristic sometimes overestimates the cost of a path then A* is not guaranteed to be optimal it might miss paths that are valid

On the other hand a stronger (higher-valued) heuristic is better it focuses the search more Djikstra is just A* with h(n) = 0 for all n

Some path planners use inadmissable heuristics on purpose if benefits of quicker planning are worth more than the

cost of the occasional missed opportunity

Page 25: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Buckland implementation

Page 26: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Lab

Page 27: Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.

Next week

Midterm Lab work on soccer team