Open In App

Python – Adjacent Coordinates in N dimension

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python Matrix, we can have a problem in which we need to extract all the adjacent coordinates of the given coordinate. This kind of problem can have application in many domains such as web development and school programming. Lets discuss certain way in which this task can be performed.

Input : test_tup = (1, 2, 3) Output : [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 2], [0, 2, 3], [0, 2, 4], [0, 3, 2], [0, 3, 3], [0, 3, 4], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 2], [1, 3, 3], [1, 3, 4], [2, 1, 2], [2, 1, 3], [2, 1, 4], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 2], [2, 3, 3], [2, 3, 4]] Input : test_tup = (5, 6) Output : [[4, 5], [4, 6], [4, 7], [5, 5], [5, 6], [5, 7], [6, 5], [6, 6], [6, 7]]

Method : Using recursion + yield The combination of above functionalities can be used to solve this problem. In this, we extract the elements dynamically using yield for the coordinates around the query coordinate and using recursion, process for next column and row. 

Python3




# Python3 code to demonstrate working of
# Adjacent Coordinates in N dimension
# Using recursion + yield
 
# helper_fnc
def adjac(ele, sub = []):
  if not ele:
     yield sub
  else:
     yield from [idx for j in range(ele[0] - 1, ele[0] + 2)
                for idx in adjac(ele[1:], sub + [j])]
 
# initializing tuple
test_tup = (3, 4)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Initialize dictionary keys with Matrix
# Using deepcopy()
res = list(adjac(test_tup))
 
# printing result
print("The adjacent Coordinates : " + str(res))


Output : 

The original tuple : (3, 4)
The adjacent Coordinates : [[2, 3], [2, 4], [2, 5], [3, 3], [3, 4], [3, 5], [4, 3], [4, 4], [4, 5]]

Using itertools.product() function:

Approach:

We can generate all possible combinations of adjacent coordinates by using itertools.product() function. We need to generate a list of offsets for each dimension, which will be used to calculate adjacent coordinates. For example, for a 2-dimensional coordinate, we need to generate the following offsets: (-1,-1), (-1,0), (-1,1), (0,-1), (0,0), (0,1), (1,-1), (1,0), (1,1). Then, we can use itertools.product() to generate all possible combinations of these offsets and add them to the input coordinate to get all adjacent coordinates.

Python3




import itertools
 
def adjacent_coordinates(coord):
    offsets = [-1, 0, 1]
    combinations = itertools.product(offsets, repeat=len(coord))
    adj_coords = []
    for c in combinations:
        if all(o == 0 for o in c):
            continue
        adj_coord = tuple(coord[i] + c[i] for i in range(len(coord)))
        adj_coords.append(adj_coord)
    return adj_coords
test_tup = (1, 2, 3)
print("Input:", test_tup)
print("Output:", adjacent_coordinates(test_tup))


Output

Input: (1, 2, 3)
Output: [(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 2), (0, 2, 3), (0, 2, 4), (0, 3, 2), (0, 3, 3), (0, 3, 4), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 2), (1, 2, 4), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 2), (2, 3, 3), (2, 3, 4)]

Time Complexity: O(3^n), where n is the number of dimensions.
Auxiliary Space: O(3^n), because we need to generate all possible combinations of offsets.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads