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:
- Let be the graph obtained by removing all the blue-colored edges of G. Let be the graph obtained by removing all the red-colored of G.
- Let be the strongly connected graph of , computed using this algorithm.
- Let be the strongly connected graph of , computed using this algorithm.
- Color the vertices of in red, and color the vertices of in blue.
- Let be the graph obtained by merging with .
- Define the weight of each (existing) edge in G' as 0.
- For each such that u belongs to the strongly connected component and v belongs to the strongly connected component do as follows:
- Use Dijkstra algorithm to find a shortest path from the blue strongly connected component of s, to both the blue and red strongly connected components of t.
- Use Dijkstra algorithm to find a shortest path from the red strongly connected component of s, to both the blue and red strongly connected components of t.
- Let p denote the shortest path among the four we have just found. (namely, p has minimal number of color alternates). p is a series of strongly connected components. Expand each of them using DFS, to find a corresponding path in G.
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 beginningi=0
).i
.At the end the number in
t
is the minimal number of swaps performed to reacht
.