Let S be a set of 10 digit numbers. Given any two numbers v and w in S, I’d like to know if there is a sequence of numbers v=u_0, u_1, … , u_k=w such that:
- each u_i is in S
- for each i=1,..,k, the numbers u_{i-1} and u_i differ in exactly one position
As a plus, it would be even better to find an algorithm to find the shortest such sequence.
Ideally, I would prefer a C (or pseudo-code) solution, but I really, really appreciate any and all suggestions on this one! Thanks!
Form a graph from elements of S: u and v are adjacent iff they differ in exactly one coordinate.
Now given u, do a breadth first search till you hit v.