MIT6.006 R15. Shortest Paths

倾然丶 夕夏残阳落幕 提交于 2020-01-16 02:59:50

BFS for Shortest Paths

BFS can actually be used to find shortest paths!
…provided that all weights equal 1.
How can we fix this to apply to more general cases?

We may use a priority queue instead. Each time we pop out only the nearest guy and deal with it, but that would be Dijkstra.

A simpler thing that we can do is to add fake nodes to the graph. For example, 3 fake nodes for an edge with length 4, just to split up the edge into small unit edges.

Analysis for the tweaked BFS:
E=WE,V=WE+VE=W'E',V=W'E'+V'T(n)=O(V+WE)T(n)=O(V'+W'E')where V’ , E’ and W’ are the number of vertices, edges, and the maximum edge cost of the former graph.
T(n) can be derived since we know T(n) for BFS is O(E+V).

Note that things like this happen a lot. In real life we don’t usually invent a new algorithm for a certain purpose. More likely we tweak existing ones to meet our needs.

A review of Dijkstra & Bellman-Ford:
Dijkstra:
T(n)=O(VlgV+E)T(n) = O(VlgV+E)orT(n)=O(ElgV)T(n)=O(ElgV)The former one only holds when we have a really fancy data structure, while the latter’s more usual.
Bellman-Ford:
T(n)=O(VE)T(n)=O(VE)

We can find out to our surprise that this little tweaked BFS can be almost as fast as Dijkstra.

States of a graph

Simple problem

We already know how to find shortest paths without Dijkstra or Bellman-Ford. Now suppose we want to find shortest paths with odd number of edges.

Solution:
One technique is to duplicate every node of the graph, so that they represent different states: even and odd.
When we arrive at an odd node, this means we have traveled an odd number of edges to get here.
The edges have to be modified, too, following
(u,v)(ueven,vodd)and(uodd,veven)(u,v)\rightarrow (u_{even},v_{odd})and(u_{odd},v_{even})This makes sense because as we travel from one node to another, the number of edges increases by 1, thus causing parity to flip over.

Now just apply any shortest path algorithm to this new graph here, and we will get what we want by looking at the shortest path from u_even to v_odd.

Variable weights problem

This is a problem quite close to real life. So we have a graph, showing the road connections between cities. Each road has two attributes: the time cost, and the fuel cost. We are starting from city U, and want to reach city V as early as possible, with the lowest fuel cost.

Problem is, the time cost of roads are not fixed. They vary from minute to minute, maybe due to the traffic condition. We can solve this using a technique similar to the one above.

Solution:
Suppose M = 24*60, which is the number of minutes in a day. We make M copies of the original graph, each representing the state of the cities at the given time.
For the edges,
(u,v)(ut,vt+tc(u,v,t)),t=0,1,2,...,M1(u,v)\rightarrow (u_t,v_{t+t_c(u,v,t)}),t=0,1,2,...,M-1where t_c is simply the time it takes to travel through this road. You can see it depends not only on which road it is, but also on the time at which we begin the travel.

Apply a shortest path algorithm to this big graph, and we will get a 1-dimensional array of results, namely, dp[V_0], dp[V_1],…, dp[V_M-1]. What’s stored in these memories are the least amounts of fuel required to reach V at time 0, 1, …, M-1.
Apparently, dp[V_0] should be INF, for there’s no way we can teleport to V right at the start. Similarly, some following entries should also have a result of INF, indicating it is impossible to reach V so early. But at some point in this sequence, the result should start to make sense, and that index would be the earliest time of arrival we can expect from this trip.

Summary

This is a powerful trick to use when we have too many situations to deal with at a single node. Just unfold the graph, allow it to expand to higher dimensions, to get comfortable, and boom! We have a familiar problem that is for sure solvable.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!