I have a state machine that looks like this:
G--H
/
A--B--C--D--E--F
I want to have a function, goToState(target) that has as an input argument the target state, and then the function will execute all the transitions starting from the current state until it reaches the target state.
For example, let's say that the current state is B and we call goToState(F). Then the function will do the following state transitions B->C, C->D, D->E, E->F.
The transitions work both ways so if current state is F and we call goToState(G), then the function will do F->E, E->D, D->G.
If we had a linear state machine (eg, no branch G--H), then I would just do an array of functions for each transition, in the legal order, and then find the index for current state and the index for the target state and call all the transition function in between those two indexes in a for loop.
However now that I have a branch, this method would not work. What would be the most efficient way to encode the legal transitions and to implement a function that executes them in the right order based on the target state in C?
EDIT: As some other users very well pointed out, what I'm looking for is some sort of path finding algorithm that finds the shortest path between two states. I just couldn't find the right words to formulate it properly in the original post. I need the simplest path finding algorithm that would work for a state diagram as shown above. The state diagram will never get more complex than this so the algorithm doesn't need to cover any other scenarios either.
EDIT2: Updated the title to better describe the problem. Your comments helped me find the right terminology so I can search the web for the solution.