Given a directed graph $G=(V,E)$, such that the nodes are colored with $k=\lfloor{log_2(n)\rfloor}$ colors. Describe an algorithm that determines whether a cycle of length $k$ containing every color exists.
My idea was to use dynamic programming like so: I denote the colors as $C=\{1,2,...,k\}$. For every pair of vertices $x,y$ and subset $S \subseteq C$, $dp[x,y,S]=true$ if there exists a path from $x$ to $y$ of length $|S|$ that uses all the colors in $S$.
We start building the table by $dp[x,x,\{color(x)\}]=true$ for every $x\in V$ where $color(x)$ denotes the color of vertex $x$. And then, for every pair of vertices $x,y$ and subset $S$:
If $dp[x,y,S]$ is not yet filled, then we go over every neighbor $z$ of $x$: If $color(z)\in S$ then let $S'=S\backslash\{color(z)\}$, and we check whether $dp[y,z,S']=true$. If both of the above conditions are met for some neighbor $z$, then $dp[x,y,S]=true$.
This solution seems like it works to me, but my issue is efficiency. It seems like this problem can be solved in $O(n^3)$ time, but this solution takes $O(n^4)$ time.