1

Is there an algorithm that can efficiently solve the following question?

Given a directed acyclic graph G with n vertices each assigned a random color from a set of size <= n - 1 colors, a source vertex s, and two distinct vertices a and b both having the same color and reachable from s.

Determine if there exists at least one path from s to a and one path from s to b that have the same sequence of vertex colors.

azd01
  • 13
  • 4

2 Answers2

2

Hint:

Use the techniques in How hard is finding the shortest path in a graph matching a given regular language?.

D.W.
  • 167,959
  • 22
  • 232
  • 500
2

Let's take the initial graph $G=(V, E)$ and create a new graph $G'=(V', E')$ where every vertex $x$ is a state representing a pair ($i$, $j$) of vertices of $G$. So basically, for $N$ vertices in $G$, there are $N^2$ vertices in $G'$

In $G'$, there is an edge from $x_1 = (i_1, j_1)$ to $x_2 = (i_2, j_2)$ iif there are both an edge from $i_1$ to $i_2$ and an edge from $j_1$ to $j_2$ in $G$. Thus, a path in $G'$ of length $k$ corresponds to two different paths in $G$ having the same length $k$.

Now, let's resolve the problem, the same color sequence has to be followed by the two paths. So in $G'$, only vertices representing a pair of identical color vertices in $G$ are allowed. By running a BFS/DFS from $(s, s)$ to $(a, b)$ in this new graph, using only the allowed vertices, one can find if there is a solution to the problem.

The time complexity is $O(|E|^2)$ at worst case (all vertices having the same color). But if there are $c$ colors having rather equivalent populations in $G$, it falls to $O(|E|^2/c)$.

Also note, that you do not have to build explicitely $G'$ to explore it !

Optidad
  • 1,788
  • 1
  • 10
  • 12