UVa 10000 - Longest Paths

10000 - Longest Paths

 * http://acm.uva.es/p/v100/10000.html

Summary
Finding the longest path in a general graph is NP-Complete. However, since the graph we are given is a acyclic digraph with a small N, we can use a relatively naive exhaustive search algorithm to solve this. Alternatively, we can use dynamic programming to solve this.

Exhaustive Search Explanation
We can use exhaustive search on the graph, since it's a directed acyclic graph. Recursion should suffice.

Exhaustive Search Optimizations
Store the best path from the start node to the all the nodes using linear space. Only branch if the current path is longer than the longest path to current node.

Dynamic Programming Explanation
Since the graph is acyclic, there is a topological ordering. Thus, we can topological sort the graph, and use the following recurrence:

$$ f( x_i ) = max_{j: 1 \to N}( f(x_j) + p(i,j) ) $$

where p(i,j) is is defined as 1 if there is a path from node i to node j, and 0 otherwise.

Input
Input Here

Output
Output Here