Introduction to Algorithms
Week 12: Exercises

1. Reachable Vertex

Write a function that takes a graph in adjacency-list representation and two vertex ids v and w. The function should return True if there is any path from v to w, otherwise False.

2. Shortest Distance

Write a function that takes a graph and two vertex ids v and w, and returns the length of the shortest possible path from v to w. If w is not reachable from v, return -1.

3. Shortest Path

Write a function that takes a graph and two vertex ids v and w, and returns a list of vertex ids containing a path from v to w that is as short as possible.

4. On the Path

Write a function that takes a graph that is a tree, i.e. a connected, undirected, acyclic graph. The function should also take three vertex ids v, w, and x. The function should return true if w is between v and x, i.e. on the (unique) path that leads from v to x.