I'm a little squirrel living in a forest with, say, eight trees $A,B,C,\ldots,H$. I can jump from tree to tree, though not all jumps are possible. (The fact that I can jump from $A$ to $B$, for example, doesn't guarantee that I can jump from $B$ to $A$.) Basically, I live in a directed graph whose adjacency matrix contains a few (non-symmetrically distributed) zeros.
I have stored five sorts of nuts $a,b,c,d,e$ in my little forest, each tree containing some portion of these. For example, tree $A$ contains nuts $a,d,e$ (quantities don't matter).
Now my question is, does there exist an efficient algorithm to calculate a path (always starting in my home tree $A$) through the forest so that I will be able to pick up a given sequence of nuts along the way, say $d,c,e,a,a,b,c,d,c$? (I can only pick up one kind of nut per tree.)
Same question, but with weighted edges (that measure energy expenditure). In that case, how can I find the path with least total weight?
Final question, how long does an algorithm have to run as a function of the nut sequence length $N$?
You can solve this problem backwards with dynamic programming, where the value function $F$ has parameters for the starting vertex $i$ and for the number of nuts you collect $k$. Let me give an example for the sequence $d,c,e,a,a,b,c,d,c$.
Let $d_{ij}$ be the distance from $i$ to $j$ (obtained by, e.g., applying the Floyd–Warshall algorithm to the adjacency matrix), and let $N(x)$ be the set of nodes with nut $x$.
You initialize the method by setting $F(i,1) = \min_{j \in N(c)} \{d_{ij} \}$ to $0$ (nut $c$ since we work backwards), so $F(i,1)=0$ if nut $c$ is present at node $i$. You proceed by setting $F(i,2) = \min_{j \in N(d)} \{d_{ij} +F(j,1) : i \neq j\}$, where you minimize only over the nodes $j$ where the set $N(d)$ is used since nut $d$ is the second last one. Then set $F(i,3) = \min_{j \in N(c)} \{d_{ij} +F(j,2) : i\neq j\}$, etc.
The runtime is quadratic in the number of nodes and linear in the number of nuts (except for the preprocessing step of finding $d_{ij}$).