Question

I posted the question for this and I want to make sure that this answers the...

I posted the question for this and I want to make sure that this answers the question

1.a) The pseudo code for the closest pair problem is as illustrated below:

Closest pair (K, i, j)

input: Array K is indexed from i to j. closest pair returns the indices of the closest pairs.

diff = K[2] - K[1]   // This is the initial difference between the first two numbers. This is shown through the array and these differences. example: K[2] > K[1] else vice versa

loop from i to j

   loop from i to j

       //works only when I ≠ j

       if K[i] < K[j] and diff > K[j] - K[i]

       // Here, K[j] is greater than K[i] and is greater than the current value in diff

       then     diff = K[j] - K[i]

                  index1 = i

           index2 = j

       elseif K[j] < K[i] and diff > K[i] - K[j]

       // Here, K[i] is greater than K[j] and is greater than the current value in diff

       then     diff = K[i] - K[j]

                  index1 = i

           index2 = j

       else

       // Here, the values in K[i] and K[j] are same

       then   diff = 0

                  index1 = i

           index2 = j

return index1, index2

1.b) On examining the algorithm, the following points can be determined

•   The nested loop showa a complexity of O(n^2). Nested loops will show the Big-Oh notation, the value of which depends on the index.

•   The algorithm has only one input: n. It's time complexity is O(n^2), regardless of n, so there's no particular “best case” and "average case". Note same result can be in constant time.

2.a) The pseudo code for the binary search problem is as illustrated below:

binsrch(K, i, j, v)

input: array K indexed from i to j with items sorted from smallest to largest and item v. output: binsrch returns a location of item v in array K; if v is not found, -1 is returned.

if (i > j) then return (-1); m = (i + j)/2;

if (K[m] == v) then return(m);

if (K[m] < v)

then return(binsrch(K, m+1, j, v));

else return(binsrch(K, i, m-1, v));

2.b) On examining the algorithm, the following points can be deduced:

•   The non-recursive work takes constant time C.

•   The single recursive call has input half the size of the original.

•   The algorithm terminates when the array has size no larger than one.

Therefore, we have the following recurrence: T (n) = T (n/2) + C, and we conclude that T (n) = k · (C + 1), where k is the number of recursive calls. Also, we note that, if the size of array A is n, then (n/2k ) = 1, so k = lg n, and T (n) = (C + 1) · lg n.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

a)

Given solution is correct, because it checks each pair of items and checks whether current difference is less than previous difference then update difference as well as index1 and index2 value.

As it takes for all n(n+1)/2 pairs it takes O(n2) time, we can design a O(nlog n ) time algorithm as follows:

The idea to design algorithm to find closest pair of items in given array A is, first sort the array using quick sort in O(nlog n) time. Then for each consecutive pair of items in sorted array find the difference and output the pair with minimum difference.

Algorithm:(A[1,2,...,n],n)

1. sort A using Quick Sort .

2. min_difference=|A[2]-A[1]|

3. min_index=1

4. for i=2 to n-1 do

5. if(|A[i+1]-A[i]| < min_difference)

6.   do min_difference=|A[i+1]-A[i]|

7. min_index=i

8. print (A[i],A[i+1])

Above algorithm takes O(n log n) time is best,worst,average case.

b)

Given analysis is also correct for given algorithm.

2.

a) Given algorithm for binary search is correct, as it compares given item with middle element.

If it is equal to middle element it returns index ,.

if middle item is less than given element, then search in right half of array otherwise search in left half of array.

b)

Given analysis is correct, as after each recursive step problem size is reduced by 1/2, so algorithm takes O(log n) time.

Add a comment
Know the answer?
Add Answer to:
I posted the question for this and I want to make sure that this answers the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • 32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text search(p,m,t,n) f For i=...

    32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text search(p,m,t,n) f For i= 1 to n-m+1( while (ti+/-1-= pj){ jj+1 If (j> m) Return i return 0 32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text...

  • Please write and draw the recursive trace, and please explain, thank you! Problem 1. [26 pts]...

    Please write and draw the recursive trace, and please explain, thank you! Problem 1. [26 pts] Given the Fibonacci numbers, defined as: Fo 0, F1-1, F k Fk2 write or draw the recursive trace of the calculation of the 5th Fibonacci number (Fs 5) with the following Algorithm (which is based on linear recursion) Algorithm Fibonacci Linear (k) Input: a positive integer value k Output: a pair of Fibonacci numbers (F., F..) if k < 2 then R-1 return (R,...

  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

    need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...

  • Modify Algorithm 3.2 (Binomial Coefficient Using Dynamic Programming) so that it uses only a one-dimensional array...

    Modify Algorithm 3.2 (Binomial Coefficient Using Dynamic Programming) so that it uses only a one-dimensional array indexed from 0 to k. Algorithm 3.2 Binomial Coefficient Using Dynamic Programming Problem: Compute the binomial coefficient. Inputs: nonnegative integers n and k, where ks n. Outputs: bin2, the binomial coefficient (2) int bin2 (int n, int k) index i, j; int B[0..n][0..k]; for (i = 0; i <= n; i++) for (i = 0; j <= minimum( i,); ++) if (j == 0...

  • 3. The indegree of a vertex u is the number of incoming edges into u, .e, edges of the form (v,u)...

    3. The indegree of a vertex u is the number of incoming edges into u, .e, edges of the form (v,u) for some vertex v Consider the following algorithm that takes the adjacency list Alvi, v2, n] of a directed graph G as input and outputs an array containing all indegrees. An adjacency list Alvi, v.. /n] is an array indexed by the vertices in the graph. Each entry Alv, contains the list of neighbors of v) procedure Indegree(Alvi, v2,......

  • PLEASE READ CAREFULLY AND ALL THE WAY THROUGH!!! I already asked this question but unfortunately the...

    PLEASE READ CAREFULLY AND ALL THE WAY THROUGH!!! I already asked this question but unfortunately the person who answered it did not read it. There is a sorting algorithm, “Stooge-Sort” which is named after the comedy team, "The Three Stooges." if the input size, n, is 1or 2, then the algorithm sorts the input immediately. Otherwise, it recursively sorts the first 2n/3 elements, then the last 2n/3 elements, and then the first 2n/ 3 elements again. The details are shown...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • When asked to describe an algorithm you are expected to give a clear pseudo-code description of...

    When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT