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

USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
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...
This is all one question, thank you so much in advance! You must evaluate a proposal...
This is all one question, thank you so much in advance! You must evaluate a proposal to buy a new milling machine. The base price is $102,000, and shipping and installation costs would add another $10,000. The machine falls into the MACRS 3-year class, and it would be sold after 3 years for $61,200. The applicable depreciation rates are 33%, 45%, 15%, and 7%. The machine would require a $7,500 increase in net operating working capital (increased inventory less increased...
This is all one question, thank you so much in advance! You must evaluate the purchase...
This is all one question, thank you so much in advance! You must evaluate the purchase of a proposed spectrometer for the R&D department. The base price is $200,000, and it would cost another $30,000 to modify the equipment for special use by the firm. The equipment falls into the MACRS 3-year class and would be sold after 3 years for $100,000. The applicable depreciation rates are 33%, 45%, 15%, and 7%. The equipment would require a $13,000 increase in...
This is all one question, thank you very much in advance! You must evaluate a proposal...
This is all one question, thank you very much in advance! You must evaluate a proposal to buy a new milling machine. The base price is $189,000, and shipping and installation costs would add another $16,000. The machine falls into the MACRS 3-year class, and it would be sold after 3 years for $103,950. The applicable depreciation rates are 33%, 45%, 15%, and 7%. The machine would require a $7,000 increase in net operating working capital (increased inventory less increased...
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...
This is all one question, thank you so much in advance. The Bigbee Bottling Company is...
This is all one question, thank you so much in advance. The Bigbee Bottling Company is contemplating the replacement of one of its bottling machines with a newer and more efficient one. The old machine has a book value of $575,000 and a remaining useful life of 5 years. The firm does not expect to realize any return from scrapping the old machine in 5 years, but it can sell it now to another firm in the industry for $265,000....
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...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the number of rows and columns, respectively, define a function named sqabsmax that accepts array A as an argument (i.e. input parameter) and returns the square of the maximum absolute value element in A. Use the const qualifier if appropriate. Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sqabsmax. in C
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT