After moving to a new city, you decide to choose a walking route from your home to your new office. To get a good daily workout, your route must consist of an uphill path (for exercise) followed by a downhill path (to cool down), or just an uphill path, or just a downhill path.1 (You'll walk the same path home, so you'll get exercise one way or the other.) But you also want the shortest path that satisfies these conditions, so that you actually get to work on time. Your input consists of an undirected graph G, whose vertices represent intersections and whose edges represent road segments, along with a start vertex s and a target vertex t. Every vertex v has a value h(v), which is the height of that intersection above sea level, and each edge uv has a value l(uv), which is the length of that road segment.
(a) Describe and analyze an algorithm to find the shortest uphill-downhill walk from s to t. Assume all vertex heights are distinct.
(b) Suppose you discover that there is no path from s to t with the structure you want. Describe an algorithm to find a path from s to t that alternates between "uphill" and "downhill" subpaths as few times as possible, and has minimum length among all such paths. (There may be even shorter paths with more alternations, but you don't care about them.) Again, assume all vertex heights are distinct.