Question

In: Computer Science

Python 3: Part B needed Write a function "closest_pair" to determine the closest pair in Theta(N^2)...

Python 3: Part B needed

Write a function "closest_pair" to determine the closest pair in Theta(N^2) time, return that pair as a tuple. The closest pair is defined as the pair of numbers whose difference is the minimum of all pairs in the list. For example:

Given the list [ 3,6,4,10,23] the closest pair would be (3,4) since the difference is just 1.

Given the list [10, 45, 55, 42, 20], the closest pair would be (42,45) since the different is just 3

Given the list [4,10,6,7,10,5], the closest pair would be (10,10) since the distance is zero.

B) Write a function "fast_closest_pair" to determine the closest pair (same as in part A), except this time you should do it in better than Theta (N^2).

Solutions

Expert Solution

Hello, I am putting the code for Both A as well as B. Where I put the comment also for your better understanding where first program is taking O(n*n) time complexity and second will take O(n*log n) time complexity. which is better than first one. Although you have any doubt, feel free to comment down and if you like my answer, don't forget to give me upvote.

Thank you, Enjoy your answer.
# Answer : A)

mylist = [4,6,10,7,10,5];

tuplee = (mylist[0], mylist[1])
minimum = abs(mylist[0] - mylist[1])


# this loop will take O(n)
for i in range(0, len(mylist)-1):
        # below loop also will take O(n) times
        # As It is using nested loop 
        # So here complexity will be multiplied.
        # so complexity = O(n)* O(n) = O(n^2)
        for j in range(i+1, len(mylist)):
                a = mylist[i]
                b = mylist[j]
                if(minimum > abs(a-b)):
                        minimum = abs(a-b)
                        tuplee = (a, b)
print("using n*n time complexity: ",tuplee)

Now Second Program:


mylist = [4,6,10,7,10,5];


# this will take O(n*logn)
mylist = sorted(mylist)

tuplee = (mylist[0], mylist[1])
minimum = abs(mylist[0] - mylist[1])

tuplee = (mylist[0], mylist[1])

# this will take O(n), but it is not using nested loops
# so, here Complexity will added.
# so complexity = O(n*log n) + O(n) = O(n*log n)
# which means this is better than first one
for i in range(1,len(mylist)):
        a = mylist[i-1]
        b = mylist[i]

        if(minimum > abs(a-b)):
                minimum = abs(a-b)
                tuplee = (a, b)
print("using n*log n time complexity: ",tuplee)

Hope You understood this answer.


Related Solutions

PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
Prove that 1+ cos theta + cos 2theta + .... cos ntheta = 1/2 + (sin(n+1/2)theta)/2sin(theta/2)
Prove that 1+ cos theta + cos 2theta + .... cos ntheta = 1/2 + (sin(n+1/2)theta)/2sin(theta/2)
Write a function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled...
Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled from the distribution P. * Sample run : print(average_sample_median(10,[0.2,0.1,0.15,0.15,0.2,0.2])) print(average_sample_median(10,[0.3,0.4,0.3])) print(average_sample_median(10,P=[0.99,0.01]) * Expected Output : 3.7855 2.004 1
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 *...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 * 2 * 1 For example, 5! = 5 * 4 * 3 * 2 * 1 = 120 Write a function called factorial that takes as input an integer. Your function should verify that the input is positive (i.e. it is greater than 0). Then, it should compute the value of the factorial using a for loop and return the value. In main, display...
Find the order of growth for the following function ((n^3) − (60n^2) − 5)(nlog(n) + 3^n...
Find the order of growth for the following function ((n^3) − (60n^2) − 5)(nlog(n) + 3^n )
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT