Python - Adjacent Coordinates in N dimension
Last Updated :
11 Apr, 2023
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))
OutputInput: (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.
Similar Reads
Python - Group Adjacent Coordinates Sometimes, while working with Python lists, we can have a problem in which we need to perform the grouping of all the coordinates which occur adjacent on a matrix, i.e horizontally and vertically at distance 1. This is called Manhattan distance. This kind of problem can occur in competitive programm
5 min read
Python - Adjacent elements in List Given a List extract both next and previous element for each element. Input : test_list = [3, 7, 9, 3] Output : [(None, 7), (3, 9), (7, 3), (9, None)] Explanation : for 7 left element is 3 and right, 9. Input : test_list = [3, 7, 3] Output : [(None, 7), (3, 3), (7, None)] Explanation : for 7 left el
3 min read
Python - Convert Coordinate Dictionary to Matrix Sometimes, while working with Python Matrix, we can have problem in which we have dictionary records with key as matrix position and its value, and we wish to convert that to actual Matrix. This can have applications in many domains including competitive programming and day-day programming. Lets dis
4 min read
Calculate Difference between Adjacent Elements in Given List - Python The task of calculating the difference between adjacent elements in a list involves iterating through the list and computing the difference between each consecutive pair. For example, given a list a = [5, 4, 89, 12, 32, 45], the resulting difference list would be [-1, 85, -77, 20, 13],Using numpy Nu
3 min read
Print diagonals of 2D list - Python The task of printing diagonals of a 2D list in Python involves extracting specific elements from a matrix that lie along its diagonals. There are two main diagonals in a square matrix: the left-to-right diagonal and the right-to-left diagonal. The goal is to efficiently extract these diagonal elemen
3 min read