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:
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:
orThe former one only holds when we have a really fancy data structure, while the latter’s more usual.
Bellman-Ford:
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
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,
where 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.
来源:CSDN
作者:Yuechen_
链接:https://blog.csdn.net/qq_34762105/article/details/103978778