Hardy decides to start running to work in San Francisco city to get in shape. He prefers a route that goes entirely uphill and then entirely downhill so that he could work up a sweat uphill and get a nice, cool breeze at the end of his run as he runs faster downhill. He starts running from his home and ends at his workplace. To guide his run, he prints out a detailed map of the roads between home and work with $k$ intersections and $m$ road segments (any existing road between two intersections). The length of each road segment and the elevations at every intersection are given. Assuming that every road segment is either fully uphill or fully downhill, give an efficient algorithm to find the shortest path (route) that meets Hardy's specifications. If no such path meets Hardy's specifications, your algorithm should determine this. Justify your answer.
Answer:
To determine which paths are valid, perform a breadth-first search beginning from his home, expanding outward on every road with an incline. Next, perform a breadth-first search beginning from his workplace, expanding outward on every road with a decline. Take the union of the sets of intersections that are in both searches. If the union is empty, then no path meets the specifications. Otherwise, the path is valid. Next, to find the shortest path, perform Dijkstra's algorithm on the set of intersections in the union, both from home and from work. The shortest path is the one with the lowest combined distance.
You are given a graph representing the several career paths available in industry. Each node represents a position and there is an edge from node $v$ to node $u$ if and only if $v$ is a prerequisite for $u$. Top positions are the ones which are not prerequisites for any positions. The cost of an edge $(v, u)$ is the effort required to go from one position $v$ to position $u$. Ivan wants to start a career and achieve a top position with minimum effort. Using the given graph can you provide an algorithm with the same run time complexity as Dijkstra’s? You may assume the graph is a DAG.
Answer:
The runtime complexity of Dijkstra's algorithm is $O(|E|+|V|\log{|V|})$ when using a Fibonacci heap.
Starting from each top position, perform Dijkstra's algorithm on the graph, and exit when we reach a node that is an entry-level position. Once we have finished running Dijkstra's algorithm on the last top position, return the shortest path encountered thus far.