Question

In: Computer Science

Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the...

Design an algorithm that minimises Ccmp, the number of comparisons of two books’ labels in the alphabetical order, and Cmov, the number of book movements (one movement is counted whenever a book is moved from one location to another) in the worst case. Suppose there arenbooks on the main shelf, which haveal ready been sorted in an ascending alphabetical order. The m newly arrived books are carried into the library on a portable shelf.

a) Scenario 1: The newly arrived books have also been sorted in an ascending alphabetical order and you are allowed to use a temporary shelf of an infinite capacity. Design an algorithm (description + pseudo code) for the robot to arrange the new books onto themain shelf using Ccmp=Θ(n+m) label comparisons and Cmov=Θ(n+m) book movements in the worst case. Explain why your algorithm has the stated worst case complexity (ignoring the constant and choosing the dominant term in your analysis).

Algorithm ArrangingBooksScenario1(A,B,C,n,m)

//A,B, and C represent the arrays of book labels for the main, the portable, and the temporary shelves

//COMPLETE WITH YOUR PSEUDO CODE HERE

b) Scenario 2: There is no temporary shelf to use in the library and the number of newly arrived books,m, is a small constant compared ton, for instance,m=10. Design an algorithm (description + pseudo code) for the robot to arrange the new books onto the main shelf using Ccmp=Θ(log(n)) label comparisonsin the worst case. What is the number of book movements Cmov incurred in the worst-case of your algorithm and why?

Algorithm ArrangingBooksScenario2(A,B,n,m)

// A and B represent the arrays of book labels for the main and the portable shelves

//COMPLETE WITH YOUR PSEUDO CODE HERE

Solutions

Expert Solution

For Scenario1:

Since all the previous books i.e. "n" are in sorted order and new books i.e. "m" are also sorted. We only need to compare one book of previous book with one book of new book.

So algorithm works as

Suppose n = 3 and books are ['ab','bdf',bgh']

m = 2 and books are ['ac','ef']

So our shelve C will be of size n+m = 5

Start iterating from starting of both Shelves and compare book names and the one which is smaller alphabetically will be added to Shelve C till we finish of all the books on both the Shelves

1st comparision 'ab' -> 'ac'            ------>        C = ['ab']

2nd comparision 'bdf' -> 'ac'          ------>        C = ['ab' , 'ac']

3rd comparision 'bdf' -> 'ef'           ------>         C = ['ab', 'ac' , 'bdf']

4th comparision 'bgh' -> 'ef'          ------>         C = ['ab', 'ac', 'bdf', 'bgh']

                                                      ------>          C = ['ab', 'ac' , 'bdf', 'bgh', 'ef']

Now the resulted shelve is also sorted and we did comparision approx (n+m)

Algorithm ArrangingBooksScenario1(A,B,C,n,m):

         first = 0, second = 0;

while(1):

       if(A[first]<B[second]):

                C.insert(A[first])

                first = first +1              # increase value by 1 if book in A is smaller

     

       if(A[first]>=B[second]):

                C.insert(B[second])

                second = second +1   # increase value by 1 if book in B is smaller

For Scenario2:

We can use a very famous technique known as binary search to find the best postion in Shelve A(with "n" books) for each book out of "m" books

Algorithm ArrangingBooksScenario2(A,B,n,m):

         x = B[i]

def insort_right(A, x, lo=0, hi=n): """Insert book x in Shelve A, and keep it sorted. If x is already in A, insert it to the right of the rightmost x. Optional args lo (default 0) and hi (default len(a)) bound the slice of a to be searched. """  while lo < hi: mid = (lo+hi)/2 if x < a[mid]: hi = mid else: lo = mid+1 A.insert(lo, x)

Since we are using binary search so the number of comparisions will be "log(n)" for 1 book. Because every time we are dividing the value by 2 so it is like 16->8->4->2->1 so we can find the best location in "log(n)" comparision


Related Solutions

What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy 1 the regular expression ((0 + 11 + 101)∗ (1101))∗ .
(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ......
(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ... , n} by guessing random numbers within the given range until the secret number has been guessed. Numbers should be replaced after being guessed such that ​it is possible to guess 2 and then 2 again​, assuming 2 is in the given range. The algorithm should return both the secret number as well as the number of guesses taken. (b) If possible, calculate the...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
How many character comparisons will the BMH algorithm perform to solve the pattern search problem shown...
How many character comparisons will the BMH algorithm perform to solve the pattern search problem shown below? text: my next door neighbor is a witch pattern: is explain c language
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons...
Please PROVE the following statement: The time complexity of any sorting algorithm that uses only comparisons between elements is the following: Ωn log n in the worst case. you need to prove this , if you can please type answer.
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT