Here I drove simple diagram to explain the problem. Problem consists of M number of roads between cities and N number of city in total. Algorithm should travel city A to city C with all possible ways with minimum resource usage.(CPU and RAM) Algorithm should return all possible alternative ways or one alternative per request. Recursive ways are not recommended because we're talking about millions of alternative ways. I have an iterative way solution but i'm looking for more optimised way.
Iterative solution
Total alternative ways can be calculated by multiplying total number of roads between cities.
for n 0...T
find_alternative(n, array_of_total_number_of_roads)
find_alternative function according to array_of_total_number_of_roads calculates an INDEX ARRAY for alternative roads. When calculating a binary number, we are always use same modding number (2) but in my case i am calculate corresponding index by using total_number_of_roads' index as non-static modding number.
An example
find_alternative(0, [3,1,2]) -> 0,0,0
find_alternative(1, [3,1,2]) -> 0,0,1
find_alternative(2, [3,1,2]) -> 1,0,0
find_alternative(3, [3,1,2]) -> 1,0,1
find_alternative(4, [3,1,2]) -> 2,0,0
find_alternative(5, [3,1,2]) -> 2,0,1
All routes are calculated and it works with this way but I'm just curious maybe better way exists :)