0

I had an exam yesterday and I would like to check if I was answering correctly on one of the questions.

The question:

G = (V, E, w) is a directed, simple graph (V: set of vertices, E: set of edges, w: non-negative weight function). There is a non-empty subset of G denoted E(red).
A path p in G will be called n-red if there are n red edges on p. d_red(u, v) will be the lightest path from vertex u to vertex v that is at least 1-red. If all paths from u to v are 0-red, d_red(u, v) = Infinity.
The weight of a path p is the sum of all edges that are part of p.

Input:
G = (V, E, w)
s, t that are elements of V
f_red: E -> { true, false }
f_red(red edge) = true
f_red(non-red edge) = false

Output:
d_red(s, t) (the lightest path that includes at least one red edge).

Runtime Constraint: O(V log V + E)

In a few words, my solution was to use Dijkstra's algorithm. A Boolean variable that is initially false is used to keep track of whether at least one red edge has been encountered. This is checked for every iteration with f_red and the variable is set to true if f_red(current edge) = true. If the variable is still false at the end, return d_red(u, v) = Infinity.

What do you think about that?

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • 1
    I don't think your answer clearly defines an algorithm that would work. For comparison, I answered pretty much the same question over here: https://stackoverflow.com/questions/44133138/shortest-path-with-a-twist/44134692#44134692 – Matt Timmermans Feb 17 '18 at 16:00
  • (I made a pretty big edit, edit again if this is not what you wanted or something is wrong.) Your answer is pretty vague, if you wrote it down exactly as you wrote it here. One Boolean variable will only tell you that *any* edge is red that has been checked. And since Dijkstra checks every edge in the graph, this is always true because the task says the set of red edges is non-empty. This depends on your concrete implementation or answer, though, so you need to be more thorough for us to answer your question. – cadaniluk Feb 18 '18 at 18:47
  • Looks correct. The only thing that you may or may not have covered is that if you find a better path to a vertex v that was previously d_red(u,v)=∞ you only attempt to reduce that d_red(u,v) if your current path has encountered at least one red edge. Because you know that from v to the final vertex there are no red edges. – giusti Feb 18 '18 at 18:49

0 Answers0