I have a very interesting question regarding how to find a list of reachable destinations given a source (starting point) that only takes at most n moves.
For example, I have a dictionary like:
source_destination = {
"A": ["B", "D"],
"B": ["C"],
"C": ["D", "A"],
"D": ["A"],
}
So in this example, in 1 move starting at "A", the reachable destinations are "B" and "C" because there is a move between "A" -> "B" and "A" -> "D". If only 2 moves starting at "B", then reachable destinations would be "C", "D", "A" because in 2 moves the paths are "B" -> "C" -> "D" and "B" -> "C" -> "A" which both paths are at most 2 moves starting at "B". I don't include the source (starting point) in my reachable destinations.
I need an algorithm that is simple since I am a new beginner but the idea as given above. I am totally lost on this problem but here's my attempt:
def reach_dest(source, n, connections):
while i < n:
for dest in connections[source]:
if dest in connections:
source= dest
n += 1
Where connections is a dictionary like source_destination dictionary and n is the most number of moves to take and source is the starting point.