My first thought was Rob's model, but for what it's worth here is an alternative formulation that does not require solving a bunch of shortest path problems at the outset. Whether it is faster or not I don't know.
I'm going to write $N$ for the set of nodes and $A$ for the set of directed arcs in the graph (so each line in the examples corresponds to two arcs). I assume that any node can be the start node (which means the path shown for the star graph is actually not optimal -- you can reduce it by one arc if you start anywhere except 1). I'm also going to guess an a priori upper limit $k^*$ on the number of arcs needed to visit every node, and let $K=\lbrace 1,\dots,k^* \rbrace.$ If the guess is too low, the model will be infeasible, requiring another run with a larger guess. Guessing too high does not hurt the model's accuracy but might slow it down.
Variables are as follows:
- $s_i\in \lbrace 0, 1 \rbrace$ is 1 if node $i\in N$ is the starting node for the path;
- $x_{i,j,k}\in \lbrace 0, 1 \rbrace$ is 1 if the $k$-th move is from node $i$ to node $j$ (with $x_{i,j,k}$ fixed at 0 whenever $(i,j)\notin A$);
- $y_{i,k}\ge 0$ will be 1 if the path exits node $i\in N$ at step $k\in K;$ and
- $z_{i,k}\ge 0$ will be 1 if the path enters node $i\in N$ at step $k\in K.$
The objective is to minimize the number $\sum_{i,j,k} x_{i,j,k}$ of arcs crossed (or some appropriate distance measure).
Constraints are as follows:
- $y_{i,k} = \sum_{(j,i)\in A} x_{j,i,k}\quad \forall i, k$ (defining the exit variables);
- $z_{i,k} = \sum_{(i,j)\in A} x_{i,j,k}\quad \forall i, k$ (defining the entry variables);
- $\sum_{(i,j)\in A} x_{i,j,k} \le 1\quad \forall k\in K$ (at most one arc can occupy each slot in the trip);
- $\sum_{i\in N} s_i = 1$ (there must be exactly one origin);
- $y_{i,1} = s_i \quad \forall i\in N$ (the first step must exit the origin);
- $y_{i,k} \le z_{i, k-1} \quad \forall i\in N, k > 1$ (other than the first step, exiting a node requires entering it at the previous step); and
- $s_i + \sum_{k\in K} z_{i,k} \ge 1 \quad \forall i\in N$ (every node other than the origin must be entered at least once).