You are given the information about the flight network between various cities as a graph G = (V, E): the vertex set V is the set of cities and there is an edge between two cities u, v if there is a direct flight between the two cities. Your goal now is to determine the worst-case number of stops (or connecting flights) a person needs to make to travel between two different cities. For example, if V = {LA, Chicago, NY C, SF, London} and E = {{LA, Chicago}, {Chicago, NY C}, {LA, NY C}, {SF, LA},{SF, Chicago}, {NY C, London}}. Then, the answer is 2 as you need that many stops to go from SF to London. Give an algorithm which runs in time O(|V | ·(|V | + |E|)) to solve this problem. You should state the algorithm clearly, prove its correctness and prove its time complexity.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |