Presentation is loading. Please wait.

Presentation is loading. Please wait.

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)

Similar presentations


Presentation on theme: "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)"— Presentation transcript:

1 Semester 10 Time sure flies.

2 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

3 PotW Solution - Example Orange is travelled path Red is suboptimal – green is better

4 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 (!)

5 Dijkstra’s Shortest Path Algorithm! 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 dist is not quite finalized until the end of the algorithm o It is instead improved over time o dist(source,source) is initialized as 0 o dist(source, x) is initalized as infinity for all other x  not the shortest title

6 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)

7 Performance Number of nodes n o Number of edges: m Note that you visit each node exactly once when you perform updates O(n 2 + 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

8 Using PriorityQueue O(n 2 +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

9 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)

10 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

11 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 one- way 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.

12 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

13 Sample Input/Output Input: 11 (x) 4 6 (# of cities, # of flights) 1 2 1 (flight #1: city #1, city #2, then distance in miles) 2 3 3 (flight #2) 1 3 6 (etc.) 3 1 7 3 4 6 4 1 2 Output: 3 (she can visit nodes #1,2, or 3, but not 4)

14 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.


Download ppt "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)"

Similar presentations


Ads by Google