buildPaths d = mapWithKey (\pair s -> s { path = buildPath pair}) d where buildPath (i,j) | i == j = [[j]] | otherwise = do k <- path $ fromJust $ lookup (i,j) d p <- buildPath (k,j) [i : p]