Download dist

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Red–black tree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

Binary search tree wikipedia , lookup

B-tree wikipedia , lookup

Transcript
Semester 10
Time sure flies.
PotW Solution
• One possible solution is to randomly search
the grid:
o At each point in your search, look at the (up to four) empty
neighboring spaces
o For each neighbor, conduct a flood-fill, and count the number of
empty spaces in that region
• This tells you the best possible result if you go in that direction
• You might call this the "heuristic" function
o Out of the neighboring spaces with the maximal heuristic, pick a
random one.
o If you have no empty spaces, you restart the search, and keep
looking until time runs out
PotW Solution - Example
• Orange is travelled path
• Red is suboptimal – green is better
General Shortest Path Info
• A shortest path between two nodes on a
graph is exactly what it sounds like
o "Distance" in this case is measured as the sum of edge weights
• Note that this measure is still well-defined if
edges are:
o unidirectional: you can only travel across certain edges in one
direction
o negative: travelling across the edge sends you backwards in time
(!)
Dijkstra’s Shortest
Path Algorithm!
 not the
shortest title
• Given a source node s, Dijkstra's can quickly tell you
the distance from a single source node to every
other node
• Dijkstra's Algorithm works for all graphs, except those
that have negative edges
• At each step of Dijkstra's, the algorithm maintains a
list of distances, dist, from the source to every other
node
o
o
o
o
dist is not quite finalized until the end of the algorithm
It is instead improved over time
dist(source,source) is initialized as 0
dist(source, x) is initalized as infinity for all other x
Dijkstra’s Algorithm (cont.)
• It is, in essence, a greedy algorithm
o At each step of the algorithm, it finds the closest node, cur, and
marks it as visited
• This node's distance is now finalized
o It then updates all neighbors of cur, neigh
• Update dist(source,neigh) using cur as the intermediary node
o This greediness only works if all edge weights are nonnegative
• "Updating" the distance from a to b given
an intermediary m:
o dist(a,b)=min(dist(a,b),dist(a,m)+dist(m,b))
o Note that order matters: dist(x,y) may not be the same as dist(y,x)
Performance
• Number of nodes n
o Number of edges: m
• Note that you visit each node exactly once when
you perform updates
• O(n2 + m) with looping to find node with minimum
distance
o Look over each node O(n) times
• O((m + n) logn) with binary heaps - found in Java's
PriorityQueue
• O(m + nlogn) with complex data structures
o No advantage in practice over previous option
o This is essentially optimal in terms of theoretical complexity
Using PriorityQueue
• O(n2+m) can be turned into O((m+n) logn):
o The PriorityQueue data structure helps you maintain the smallest
item in a dynamic (changing) set
o In this case, it helps you find the closest node quickly
o Every time you update a node's distance, add it to the queue:
this takes O(mlogn) time
• Note that this allows duplicate items
• This is necessary because only the top item in the queue is
accessible
• Just make sure you skip over duplicates when you query
(peek) later on
o Every time you query the closest node, pop it from the queue: this
takes O(nlogn) time
Examples/Extensions
• To reconstruct the shortest path:
o For each node, remember its "parent" node
o Parent = the node that was last used to update its distance
• Usually, in contest problems, constructing the graph
is the hardest part of the problem
o What if you're allowed to magically skip (teleport across) up to k edges?
o What if you multiply edge weights instead of adding?
• Extension: In 1994, David Eppstein published an
elegant solution for the kth shortest path
o Amazingly, its complexity is basically the same as Dijkstra's: O(m+nlogn+k)
USACO!
• Today is the last day to take the January USACO!
o 4 hours instead of 3! (supposedly b/c problems are tougher than usual)
o As usual, participation is worth 5 points of PotW credit
• December and January USACO problems will be
covered next meeting
PotW: Round Trip
Today, Bessie wants to take a trip to a city, but doesn't
know where yet. She has x dollars to spend, and the
airline she is using charges $1 per mile. Tell her how
many cities she might be able to visit, if she starts at
node #1. Please note that all possible flights are oneway only, and after visiting her destination, she must
also return. She can also take as many flights as she
wants. Please also include node #1 in your count.
Round Trip: Details
• Assume that every city is visitable from every other
city given an infinite amount of money
• Also assume that all plane flight distances are less
than 1000
• Let N=# of cities, and M=# of flights
• For 25 points, solve for N<100 and M<100
• For 40 points, solve for N<10000 and M<100000
Sample Input/Output
• Input:
11
46
121
233
136
317
346
412
• Output:
3
(x)
(# of cities, # of flights)
(flight #1: city #1, city #2, then distance in miles)
(flight #2)
(etc.)
(she can visit nodes #1,2, or 3, but not 4)
Hints
• Note that this is very similar to a standard
Dijkstra's problem
o cities = nodes, and flights = unidirectional edges
o However, how do you take into account Bessie having to come
back from her destination?
• Run Dijkstra's twice:
o Once with the given edges, and once with the given edges, but
reversed
o For every city, add these two distances up, and if they're less than
or equal to x, you can indeed visit the city.