The solution turned out to be quite simple and understandable, but a little longer:
First, a list of all the paths through the graph is created, so that no path contains the same edge more than once.
From this information we take a list of paths that begin and end in the same node.
From this list of βfinalβ edges, we restore all paths with loops based on the calculation of the previous two steps.
I posted the complete solution in TSQL on my blog.
source share