Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
More Examples for DP & Greedy
Single Source Shortest Path•Many techniques, including Dijkstra, relies on
relaxation on edges:• For each vertex v, maintain:• The shortest distance from the source to the vertex that
have been found until now (usually denoted as v.d)• The predecessor of v in the current shortest path from the
source to v (usually denoted as v.𝜋)• Relaxing an edge (u, v) then means testing whether
v.d can be improved by going through vertex u. If it can, update v.d and set v.𝜋 = u
Bellman-Ford• Can be applied to any directed graph that does not
have negative cycle (negative edge weight is fine)• Essentially, a dynamic programming on the length of
the shortest path
Dijkstra• Requires the graph is a directed acyclic graph and all
edge weight to be positive• A greedy algorithm, in the sense always choose the
vertex u with the shortest shortest path estimate from the source and relaxes all edges leaving u
Moving on to All Pairs Shortest Path• Find shortest path between any vertex to any other
vertex in the graph, assuming the graph is a directed graph with no negative cycle• One way: Call Bellman-Ford for each vertex• A better algorithm: Floyd-Warshall. It’s a DP algorithm too.
But, it’s DP w.r.t. the set of vertices that the path can passes (note: can, not have to)
• Some terminologies and notation: • Suppose a path P = <v1, v2, …, vk-1>, then we call the set {v2, …, vk-1} as
the intermediate vertices• 𝑑#$% is the shortest path from vertex-i to vertex-j when the set of
intermediate vertices are {v1, v2, …, vk}
Tutorial 10 Q2• Time complexity •Memory complexity of the above algorithm•Modify the above algorithm, so that the
memory complexity is O(|V|2 )• How to detect negative weight cycle?
Tutorial10 Q3• Tour de Canberra is a cycling route across Canberra,
which includes various natural attractions in and around Canberra. To help cyclists, water stations are provided at several locations, and are marked in the map. Ms C would like to follow this cycling route. But, she wants to plan here stops to get water before she starts cycling. Suppose she can carry n litre of water and cycle m miles before running out of water, and suppose her water intake are the same everywhere in the route (i.e., decrease in her water level are determined only by the distance she has travelled). Obviously, she never wants to run out of water. What strategy should Ms C use to select her water stops, so that the number of water stops is minimised? Please explain your answer