Find cell neighbors of a given edge in a 2D grid In the figure below, cells are labeled row wise, and edges are labeled counter clockwise.  That is, vertices 1' and 2' form edge #1, vertices 2' and 5' form edge #2, vertices 5' and 8' form edge #7, etc.


vertices 1' and 2' form edge #1
vertices 2' and 5' form edge #2
vertices 4' and 5' form edge #3
vertices 1' and 4' form edge #4
vertices 2' and 3' form edge #5
vertices 3' and 6' form edge #6
vertices 6' and 5' form edge #7
vertices 5' and 8' form edge #8
vertices 7' and 8' form edge #9
vertices 4' and 7' form edge #10
vertices 6' and 9' form edge #11
vertices 8' and 9' form edge #12



  Question: given an edge number (and/or the vertices that form it), is there a way to algorithmically
  compute its corresponding cell neighbors?
There's no need for any special algorithm.  There are only 12 edges, so it's enough to simply use a switch statement with 12 cases.  Or, you can use 12 if statements.  Any of these will have $O(1)$ running time, since there are only finitely many cases, so they are (asymptotically) as good as any other "algorithm".