1 / 14

# Semester 10 - PowerPoint PPT Presentation

Semester 10. Time sure flies. PotW Solution. One possible solution is to randomly search the grid: At each point in your search, look at the (up to four) empty neighboring spaces For each neighbor, conduct a flood-fill, and count the number of empty spaces in that region

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Semester 10' - spence

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Semester 10

Time sure flies.

PotW Solution

• One possible solution is to randomly search the grid:

• At each point in your search, look at the (up to four) empty neighboring spaces

• 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

• Out of the neighboring spaces with the maximal heuristic, pick a random one.

• 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

• A shortest path between two nodes on a graph is exactly what it sounds like

• "Distance" in this case is measured as the sum of edge weights

• Note that this measure is still well-defined if edges are:

• unidirectional: you can only travel across certain edges in one direction

• negative: travelling across the edge sends you backwards in time (!)

Dijkstra’s ShortestPath 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

• 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

• At each step of the algorithm, it finds the closest node, cur, and marks it as visited

• This node's distance is now finalized

• It then updates all neighbors of cur, neigh

• Update dist(source,neigh) using cur as the intermediary node

• This greediness only works if all edge weights are nonnegative

• "Updating" the distance from a to b given an intermediary m:

• dist(a,b)=min(dist(a,b),dist(a,m)+dist(m,b))

• Note that order matters: dist(x,y) may not be the same as dist(y,x)

• Number of nodes n

• 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

• 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

• No advantage in practice over previous option

• This is essentially optimal in terms of theoretical complexity

Using PriorityQueue

• O(n2+m) can be turned into O((m+n) logn):

• The PriorityQueue data structure helps you maintain the smallest item in a dynamic (changing) set

• In this case, it helps you find the closest node quickly

• 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

• Every time you query the closest node, pop it from the queue: this takes O(nlogn) time

• To reconstruct the shortest path:

• For each node, remember its "parent" node

• 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

• What if you're allowed to magically skip (teleport across) up to k edges?

• Extension: In 1994, David Eppstein published an elegant solution for the kth shortest path

• Amazingly, its complexity is basically the same as Dijkstra's: O(m+nlogn+k)

• Today is the last day to take the January USACO!

• 4 hours instead of 3! (supposedly b/c problems are tougher than usual)

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

• 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 (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 73 4 64 1 2

• Output:3 (she can visit nodes #1,2, or 3, but not 4)

• Note that this is very similar to a standard Dijkstra's problem

• cities = nodes, and flights = unidirectional edges

• However, how do you take into account Bessie having to come back from her destination?

• Run Dijkstra's twice:

• Once with the given edges, and once with the given edges, but reversed

• For every city, add these two distances up, and if they're less than or equal to x, you can indeed visit the city.