## ACM 2008 SCUSA Wormholes

South Central USA has to be one of the odder acronyms in common use… a single character away from SCUBA no less.

The last time I went to ACM, this problem was delegated to one of my team mates, and for one reason or another we never got it solved. I was reading through an Analysis of Algorithms book and came across a section on All Pairs Shortest Path and thought ‘This would apply perfectly and elegantly to this situation’. (I was wrong, but we’ll get to that soon). (Also, judge data can be had at this location, and I have been cheating miserably).

**Synopsis**

You work at Time Travel Inc. in the payroll department. Sick of keeping up with all the math related to time travel, you contracted yourself (while in college) to develop an application to determine how much a person has aged during their time travel, and whether or not their last journey was even physically possible. More specifically, time travel is acheived by portals which appear at specific moments in time, and exit at other specific moments, either aging or youthifying the human in the process, according to a specific formula; and ignoring the concept that you could die, or be negative years old.

**Analysis**

This is a graph traversal problem with negative weight directed edges. The goal is to find the minimum weight path that takes you from a start, to a destination, and back to a start; or equivelantly, two graph traversals.

As I said, I had read a bit on All Pairs Shortests Paths and was interested to see how it would handle this. Specifically, I was thinking ‘if you treat each year as a vertex, then each vertex has a weight one edge to the next year; and if a wormhole is present, an edge to that destination year. At this point in time, I thought the input was constricted to a few hundred years near the 2000’s (where else would you time travel to?). The actual range of years is from 1-10,000. 10,000 verticies, 2 10,000 x 10,000 matricies for storing path weights… no. Not gonna work. (As we’ll see in a bit, this is actually a bit of a lie).

Still curious to further my learning, I read up on the Bellman-Ford algorithm, which is a single source shortest paths algorithm. It works on the assumption that the absolute longest path will go through every single vertex in V, and therefore you can determine the minimum cost to get anywhere by going through each edge, relaxing the edge’s destination weight if appropriate, and then repeating that |V| times. Complexity of |V|*|E|. That actually sort of worked, in the sense that it produced an answer for most problems in under a minute. But, under a minute isn’t quite ideal.

To get reasonable response times, you can’t simulate every single meaningless year. You have to focus on only the meaningful ones, and declare edges between them. If you’re in 1950 and want to get to 2050, yes, you can look at it as 100 1 year edges, but it would be less pain to look at it as 1 100 year edge… or, if a wormhole happens to open up in 2012, a 62 year edge followed by a 38 year edge. The downside to ignoring meaningless years is this adds a fair bit of complex non intuitive code, just for the purpose of filling in the ‘time moves forward at a constant rate’ bits.

With that said, you could apply the same reasoning to all pairs shortest path, so long as you only kept track of the vertexes you actually cared about.

**Source Code**

As this one is a tad bit long, and far from being the greatest, I’m only going to post bits and peices of it.

public static void ProcessProblem(int id, TextReader input) { Console.WriteLine("DATA SET #" + id); Timeline timeline = new Timeline(); int wormHoleCount = ReadInt(input); for (int i = 0; i < wormHoleCount; i++) timeline.AddWormhole(ReadInts(input)); int startingYear = ReadInt(input); int missionCount = ReadInt(input); for (int m = 0; m < missionCount; m++) { int destination = ReadInt(input); int distance = timeline.ShortestPath(startingYear, destination); int returnDistance = timeline.ShortestPath(destination, startingYear); if(distance==Int32.MaxValue || returnDistance==Int32.MaxValue) Console.WriteLine("IMPOSSIBLE"); else Console.WriteLine(distance+returnDistance); } } |

Above is the primary problem solving bit. We create a timeline, register wormholes, then go through each mission and determine if the destination can be reached from the starting point, and if the starting point can be reached from the destination. Note that we’re using the max value of an int as a ‘no path’ return code.

class Timeline { private List<int[]> wormholes=new List<int[]>(); public void AddWormhole(int[] wormhole) { wormholes.Add(wormhole); } public int ShortestPath(int sourceYear, int destYear) { List edges = new List(); List vertexes=new List(); vertexes.Add(sourceYear); vertexes.Add(destYear); foreach (int[] wormhole in wormholes) { vertexes.Add(wormhole[0]); vertexes.Add(wormhole[1]); edges.Add(new Edge(wormhole[0], wormhole[1], true)); } vertexes.Sort(); for (int i = 1; i < vertexes.Count; i++) if (vertexes[i] == vertexes[i - 1]) vertexes.RemoveAt(i--); for (int i = 1; i < vertexes.Count; i++) edges.Add(new Edge(vertexes[i - 1], vertexes[i], false)); Dictionary<int, int> distances = new Dictionary<int, int>(); distances[sourceYear] = 0; for (int i = 0; i < vertexes.Count; i++ ) foreach (Edge edge in edges) { if (!distances.ContainsKey(edge.start)) continue; int distance = distances[edge.start] + edge.weight; if (!distances.ContainsKey(edge.end) || distances[edge.end] > distance) distances[edge.end] = distance; } if (distances.ContainsKey(destYear)) return distances[destYear]; return Int32.MaxValue; } } |

And here we have the meat of the problem. The graph traversal algorithm/timeline class. The first portion of the ShortestPath method is soley responsible for building a list of edges (note, third parameter to edge constructor is ‘isWormhole’). Once that is constructed, the Bellman Ford algorithm is repeatedly applied to get the distance from the start point to every other point. Bellman Ford performance can actually be improved slightly by not continuing to iterate if no relaxations were needed on a given iteration. On my machine, execution time went from about 3s to .5s. (Also, as a fun aside, note the sad lack of a .NET Set class).

That’s all for now. I think I’ll reimplement this with an all pairs shortest path just to see if it would meet time constraints on execution; and make a post about that later.

Full Source Code for First Solution.