Upload
rosemary-hensley
View
216
Download
0
Embed Size (px)
DESCRIPTION
© 2007 Seth James Nielson What’s the Admiral to do? The fleet is in the Anoat system It has neighboring systems Those systems have neighboring systems (etc.) How can we help the Admiral not receive a Sith sponsored telekinetic larynx massage? The fleet is in the Anoat system It has neighboring systems Those systems have neighboring systems (etc.) How can we help the Admiral not receive a Sith sponsored telekinetic larynx massage?
Citation preview
© 2007 Seth James Nielson
DFS and A*
© 2007 Seth James Nielson
It is a dark time for searching…
Admiral PiettLord Vader, if the Millennium Falcon went into light-speed, it'll be on the other side of the galaxy by now.
Darth VaderAlert all commands. Calculate every possible destination along their last known trajectory.
Admiral PiettYes, my Lord. We'll find them. Darth VaderDon't fail me again, Admiral.
© 2007 Seth James Nielson
What’s the Admiral to do?
The fleet is in the Anoat system
It has neighboring systemsThose systems have
neighboring systems (etc.) How can we help the Admiral
not receive a Sith sponsored telekinetic larynx massage?
© 2007 Seth James Nielson
Anoat
© 2007 Seth James Nielson
Does this look familiar?
© 2007 Seth James Nielson
for each vertex v in V color[v] = white d[v] = INFINITY p[v] = NULL color[s] = gray d[s] = 0 Queue.clear() Queue.put(s) while (!Queue.empty()) v = Queue.get() for each u adjacent to v if (color[u] == white) color[u] = gray d[u] = d[v] + 1 p[u] = v Queue.put(u) color[v] = black
© 2007 Seth James Nielson
From a Tree View
Anoat
1 42 3
1196 8 10 125 7
1 2 3 4 5 6 7 8 9 10 11 12
Ano
at
© 2007 Seth James Nielson
Admiral Piett’s Problem
Observe the mem requirement
bd, whereb = branching factord = depth
Example: b = 4d = 2, mem = 16d = 10, mem = 1,048,576
© 2007 Seth James Nielson
Switch in a StackUse a stack instead of a
queueDescendant before siblingUse recursion for “built-in”
stackThis is called “Depth-First”
© 2007 Seth James Nielson
DFS(G) Code
1. for each vertex u in V2. color[u] = white3. p[u] = null4. for each vertex u in V5. if (color[u] == white)6. DFS-VISIT(u)
© 2007 Seth James Nielson
DFS-VISIT(u) Code
1. color[u] = gray2. for each v adjacent to u3. if (color[v] == white)4. p[v] = u5. DFS-VISIT(v)6. color[u] = black
© 2007 Seth James Nielson
Code NotesNot shortest pathDoesn’t compute distanceCreate “forest”, cover all
nodesOptions (see CLRS 541)
Node discovery time - d[v]Node completion time - f[v]
© 2007 Seth James Nielson
Parenthesis TheoremIn a dfs tree, one of the
following holds:[d[u],f[u]] & [d[v],f[v]] are
disjoint <--> neither u or v descends from the other
[d[u],f[u]] within [d[v],f[v]] and u descends from v
Same as above, switch u,v
© 2007 Seth James Nielson
Parenthesis Theorem explained
If v descends from u thend[u]<d[v]<f[v]<f[u]
Rewrite d[u] as (uf[u] as u)Then creates well formed
expression (balanced parens)e.g., (u (v (x x) v) u)
© 2007 Seth James Nielson
Two More Theorems
1. White Path Theorem2. DFS on undirected graphs
See p545-546 in text
© 2007 Seth James Nielson
An Interesting DFS Use
DFS can be used to generate perfect mazes
Perfect maze has 1, and only 1, path between any 2 points
© 2007 Seth James Nielson
Can DFS help Piett?Let’s see how DFS works
on the Piett Problem…
© 2007 Seth James Nielson
Anoat
1 2 3
96 8 105 7
`
1235678910A
noat
© 2007 Seth James Nielson
DFS and MemoryAdmiral Piett is relieved to
see that DFS will not exceed the memory resources of his Star Destroyer.
But he can’t shake the feeling there might be something better…..
© 2007 Seth James Nielson
Introducing A*Also known as “best-first”Requires
Node evaluation heuristicData struct that orders
nodes using true cost from start + estimated cost to end
True cost so far required for a guaranteed shortest path
© 2007 Seth James Nielson
Admissible Heuristicd_estimate <= d_actualSatisfy triangle inequality
Very rarely a problemNot covered hereSee online sources such as
Wikipedia for more info
© 2007 Seth James Nielson
A-Maze-Ing ExampleSearch for exit from startAssume closer nodes betterVarious heuristics (G=Goal)
Manhattan f(x,y) = |xG-x|+|yG-y|Diagonal f(x,y) = max(|xG-x|,|yG-
y|)Straight-line
f(x,y) = SQRT( (xG-x)2 + (yG-y)2 )
© 2007 Seth James Nielson
Priority QueueThe data structure A*
needsLike a queue, but
dequeues the best (max or min on some function) element
© 2007 Seth James Nielson
Visual Example
0,0 - 0+5=51,0 - 1+4=51,1 - 2+3=51,2 - 3+2=5
2,2 - 4+1=5
2,1 - 5+2=7
3,1 - 6+1=7
0,2 - 4+3=7
© 2007 Seth James Nielson
Piett and A*Piett can’t use the same
heruisticsHowever, he does have other
info (intel, stats, trace data) to rate the various nodes
Can use these, if they produce an admissible heuristic
Do you see why A* is called a informed search?
© 2007 Seth James Nielson
HeapGood for Priority QueuesAlmost complete binary
treesProperty:
Node’s value > value of all descendants (for max heaps)
Min heap swaps < for >
© 2007 Seth James Nielson
Max Heap Example
© 2007 Seth James Nielson
Heap Implementation
Single arrayOperations (1-indexed):
parent(i) = floor(i/2)left(i) = 2*iright(i) = 2*i + 1
14 10 8 716 9 3
© 2007 Seth James Nielson
NotesSubtree of heap = heapNodes stored in array in
layersHighest nodes first, etc
© 2007 Seth James Nielson
Maintaining Heap Property
MAX-HEAPIFYGiven heap A and node i
i does not satisfy heap propBUT, left and right subtrees do
Traverse down switching nodes until property is satisfied
Running time is O(log(n))
© 2007 Seth James Nielson
MAX-HEAPIFY(A, i)1. l = left(i)2. r = right(i)3. if (l<=A.size && A[l] > A[i])4. largest = l5. else6. largest = i7. if (r<=A.size && A[r]>A[largest])8. largest = r9. if (largest != i)10. exchange(A,i,largest)11. MAX-HEAPIFY(A,largest)
© 2007 Seth James Nielson
Building Max-Heap Given arbitrary array,
notice leaves satisfy heap property
BUILD-MAX_HEAP(A)1. A.size = A.length2. For (i=floor(A.length/2); i>0;
i--)3. MAX-HEAPIFY(A,i)
© 2007 Seth James Nielson
Getting the Max Easy: HEAP-MAXIMUM(A)
1. return A[1]
© 2007 Seth James Nielson
DequeueingMust maintain heap propIdea:
Exchange A[1] with last node
Remove (new) last nodeMAX-HEAPIFY(A,1)
© 2007 Seth James Nielson
HEAP-EXTRACT-MAX(A)
1. if (A.size < 1)2. error “heap underflow”3. max = A[1]4. A[1] = A[A.size]5. A.size = A.size-16. MAX-HEAPIFY(A,1)7. return max
© 2007 Seth James Nielson
Increasing Node Value
Sometimes, need to increase “priority” (like in OS)
After increasing value, need to “bubble” it up in the tree until heap property is satisfied
© 2007 Seth James Nielson
HEAP-INCREASE-KEY(A,i,key)
If (key < A[i]) error “new key smaller”A[i] = keyWhile (i>1 && A[parent(i)] <
A[i]) exchange(A, i, parent(i)) i = parent(i)
© 2007 Seth James Nielson
Heap InsertionAdd element at end of ASet value to -INFINITYIncrease value to correct
value
© 2007 Seth James Nielson
MAX-HEAP-INSERT(A,key)
1. heap-size[A] = heap-size[A]+12. A[heap-size[A]] = -INFINITY3. HEAP-INCREASE-KEY(A,heap-
size[A],key)
© 2007 Seth James Nielson
Summary - DFSLower memory
requirementsUses a stack (usually
recursion) instead of queue
© 2007 Seth James Nielson
Summary - A*Requires context and infoUses a priority queueCalled an informed searchTo get shortest path, add
true cost so far to estimate to goal
© 2007 Seth James Nielson
Summary - HeapsAlmost complete binary
treesSatisfy heap propertyCan be used as priority
queue
© 2007 Seth James Nielson
Summary - Admiral Piett
Never found the FalconBut learned a lot about
search(Was spared because
Vader had reached his “bag limit”)
© 2007 Seth James Nielson
Epilogue
LANDO: Luke, we’re ready for take offLUKE: Good luck, LandoLANDO: (into comlink) When we find Jabba the Hut and that bounty hunter, we'll contact youReturn of the Search!coming soon to a classroom near you
Remember, the Search will be with you. Always!