Question

In: Computer Science

how to find all the neighbors in a n-dimensional array in python? thank you in advance...

how to find all the neighbors in a n-dimensional array in python?
thank you in advance

all the neighboors of a coordinate*

Solutions

Expert Solution

We can do that recursively easily.

We can use the concept of Cartesian Product here to do that .

Try to understand and visualize the n-dimensional array , we are currently on some cordinate * we want all the co-ordinates in range (-1 to 1) calculating from current co-ordinate

So for example in 2-D matrix if we are on co-ordinate (x,y) we can say the neighbors are (x-1,y) , (x-1,y-1) ,(x-1,y+1) , (x,y-1) ,(x,y+1) , (x+1,y+1) ,(x+1,y) , (x+1,y-1)

As it is for 2-D matrix we can easily get the neighbors all we have done is consider all cases where the difference between the co-ordinates is in between range -1 to 1 . ( Just the reference co-ordinate is to be ignored )

Now what cartesian product do is : It gives the set of all ordered pairs ( Giving all possibilities under given circumstances)

SOLUTION :

Here what we will do is perform Cartesian Product of set {-1 , 0 , 1 } with the each i-th dimension and we will do this for all N-dimensions so that no possible neighbor is left behind.

(Just keep in mind to remove the entry with all n-zeroes as it will point to the reference co-ordinate ( who's neighbors we are finding)

We can write a recursive algorithm to do the above task :

algo
 func(n : dimension
                    index : n-tuple)
    answer := empty list
    for ind in cpower({-1, 0, 1}, n) do
        if not (ind is all zeros then)
            temp := [index[i] + ind[i] #for i in 1..n]
            answer := append(answer, temp)
        end
    loop
    return answer

Here is the recursive algorithm to do that.

I tried my best to explain the question but as this is quite tough problem so if you need any more help or clarification in answer you can comment and I will help you out in best possible way .


Related Solutions

Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Please answer all questions, thank you in advance! A) What is the lowest possible energy (in...
Please answer all questions, thank you in advance! A) What is the lowest possible energy (in electron volts) of an electron in hydrogen if its orbital angular momentum is ?2 h-bar? Express your answer in electronvolts to three significant figures. B) What is the largest value of the z component of the orbital angular momentum (in terms of h-bar) for the electron in part A? Express your answer as an integer to one significant figure. C) What is the smallest...
Please show the work for how to get the answer. Thank you in advance. Assume that...
Please show the work for how to get the answer. Thank you in advance. Assume that all of the mass of a bicycle wheel is concentrated at its rim. Such a wheel of mass 1.2 kg and radius 30 cm starts from rest at the top of a hill 100 m long and inclined at 20� to the horizontal. What will be the speed of the wheel at the bottom of the hill if it rolls without slipping? (Answer: 15...
Please answer the below, all parts! Thank you in advance. Option 1: Think of a problem...
Please answer the below, all parts! Thank you in advance. Option 1: Think of a problem dealing with two possibly related variables (Y and X) that you may be interested in. Share your problem and discuss why a regression analysis could be appropriate for this problem. Specifically, what statistical questions are you asking? Why would you want to predict the value of Y? What if you wanted to predict a value of Y that’s beyond the highest value of X...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
Describe how you would produce an anti-mouse IgG antibody in goats Thank you in advance
Describe how you would produce an anti-mouse IgG antibody in goats Thank you in advance
Create a function to output a one dimensional double array Mwith n elements where the...
Create a function to output a one dimensional double array M with n elements where the first three elements are 1 and each subsequent element is the sum of previous three elements before it. Name the function myArray. Write the function in the correct format to be used to create a Matlab function. Call the function in correct format to output the array with 7 elements.
please answer all 4 questions. I need brief and correct answers. thank you in advance. 1....
please answer all 4 questions. I need brief and correct answers. thank you in advance. 1. Why is it important to mix the solutions after adding the reagents? How is mixing done in this experiment? 2. What is the reason for rinsing the precipitates before further testing? 3. What two things are extremely important to remember when centrifuging? 4. Why is it possible to separate Cu+2 from Al+3 and Fe+3 with NH4OH but not with NaOH? (Hint, consider solubility properties.)
Pandas exercises: 1. Write a python program using Pandas to create and display a one-dimensional array-like...
Pandas exercises: 1. Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. 2. Write a python program using Pandas to convert a Panda module Series to Python list and print it's type. (Hint: use ds.tolist() to convert the pandas series ds to a list) 3. Create a pandas dataframe called **my_df** with index as integer numbers between 0 to 10, first column (titled "rnd_int") as 10 integer random numbers between...
n this lab, you use what you have learned about searching an array to find an...
n this lab, you use what you have learned about searching an array to find an exact match to complete a partially prewritten C++ program. The program uses an array that contains valid names for 10 cities in Michigan. You ask the user to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user the city name is not found in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT