# Find shortest path of R,G,B Graph

Let $$G=(V,E)$$ be a directed graph, $$ω:E→\mathbb{R}$$ a weight function, and $$s,t\in V$$ a pair of different nodes. It's given that $$G$$ doesn't have a negative cycle. Each edge has the color R or G or B. Find the shortest path under the condition:

1. we can use green edge after using at least one red edge
2. we can use blue edge after using at least one red edge and at least one green edge.

The shortest path do not have to be colorful.

• So you can use a green edge only right after a red edge, or can we use a green edge as long as we've used a red edge somewhere? – Juho Dec 28 '19 at 14:06
• I can use green edge after using at least one red edge – Assia Khteb Dec 28 '19 at 14:20
• Does "the shortest path do not have to be colorful" mean "the path does not have to use all three colors"? (I assumed this interpretation in my answer.) – Aaron Rotenberg Dec 28 '19 at 14:54
• yes that is what I meant @AaronRotenberg – Assia Khteb Dec 28 '19 at 15:15
• You can also tweak the states of the dynamic programming of Bellman and Ford algorithm (also works for Dijkstra but you need Bellman Ford anyway because of the negative weights), to compute $d_R[u], d_G[u]$ and $d_B[u]$, the shortest path from $s$ to $u$, to $u$ going through at least one red edge, and to $u$ going through at least one green edge (and hence one red) respectively. It is easy to tweak the dp transitions accordingly. – narek Bojikian Dec 28 '19 at 16:37

Let's look at the form of the allowed paths. As a regular expression, it looks like:

$$r^*\ |\ r(r|g)^*\ |\ rr^*g(r|g|b)^*$$

We can test the three cases separately and then choose the shortest path found among the three cases.

The first case is easy: just remove all the green and blue edges and then find the shortest path from $$s$$ to $$t$$ as normal. Of course, there may not be a path from $$s$$ to $$t$$ in this red-only subgraph; if this happens, we can say the distance for this case is $$\infty$$.

For the second case, start with the original graph and remove all the blue edges, leaving only red and green edges. Then add a new vertex $$s^\prime$$, which has a directed edge to each vertex $$v$$ exactly when there is a red edge from $$s$$ to $$v$$ in the original graph. Then check for the shortest path from $$s^\prime$$ to $$t$$ in this modified graph; this corresponds exactly to the shortest path from $$s$$ to $$t$$ in the original graph that has the form $$r(r|g)^*$$. Again, if no such path exists, we can return $$\infty$$ from this case.

See if you can figure out the third case now. 🙂

I have asked and answered a more general form of this question here.

• (Using $^\prime$ is overdoing it and spoils the proximity between $name^\prime$ and prime.) – greybeard Dec 28 '19 at 17:08
• (I can see how removing green edges starting at $s$ is fallacious.) – greybeard Dec 28 '19 at 17:16
• @greybeard I'm not sure what you are saying is the issue with the primes? For reference: with caret $s^\prime$ $s^\prime$ without caret $s\prime$ $s\prime$ with apostrophe $s'$ $s'$ – Aaron Rotenberg Dec 28 '19 at 17:53