Let be a directed graph, and let be an edge-coloring in red and blue. Let s,t be vertices in G. Find a path from s to t (if exists) so that the number of color changes along this path is minimal.
I have tried to do as follows:
This algorithm can run in O(E+V*log(v)). Can it be improved or simplified?
I don't fully understand your algorithm, specifically in stage 4 you will color every vertex with two different colored edges in two colors - blue and red...
Therefor I will not try and improve your algorithm but will present one of my own - a variant of BFS with time of O(E + V).
The idea: Iterate over the edges of the graph and measure depth as the number of times you switched colors.
Note: We will run the algorithm twice, first assume that the first edge of the path is red, second assume that the first edge of the path is blue, than take the minimum.
i
(at the beginning i=0
).i
.At the end the number in t
is the minimal number of swaps performed to reach t
.