Given the list of states, as in the US states, I am trying to write an algorithm to determine whether states are adjacent. The order does not matter, and conditions can be reviewed.
Examples:
AZ, CA, OR, WA are adjacentAZ, CA, NM, UT are adjacentAZ, NM, OR, WA not adjacent
Assumptions:
This collection has entries in both directions:
OriginState = AZ, ConnectingState = CAOriginState = CA, ConnectingState = AZ
What have i tried?
Attempt 1: For each state in the collection, make sure that there is at least one StateConnection with a different state in the list.
Why didn't it work? This allows you to make a third example, in which there are two separate adjacent ranges.
Attempt 2: Remove the states from the list of candidate connection states after checking them. This will require a complete path that affects each state once.
Why didn't it work? This does not allow us to make a second example, when one state acts as a hub for several states.
I did not solve any problems of graph theory after a while, so I'm a little rusty.
I am not looking for anything like the shortest route or the traveling salesman. I don't care which path is taken or how many moves are used. Everything I care about is there a gap.
I am writing this in C #, but feel free to give an answer in other languages.
source share