Question

In: Computer Science

Your developing a set of algorithms for a librarian robot that can put newly arrived books...

Your developing a set of algorithms for a librarian robot that can put newly arrived books to the main book shelf (of a sufficient capacity) while maintaining an alphabetical order.

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 are n books on the main shelf, which have already 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 ascend- ing alphabetical order and you are allowed to use a temporary shelf of an infinite capacity.

i) Design an algorithm (description + pseudo code) for the robot to arrange the new books onto the main shelf using Ccmp = Θ(n + m) label comparisons and Cmov = Θ(n + m) book movements in the worst case.

ii) 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 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 to n, for instance, m = 10.

i) Design an algorithm (description + pseudo code) for the robot to arrange the new books onto the main shelf using Ccmp = Θ(log(n)) label comparisons in the worst case.

ii) 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

//PSEUDO CODE HERE

Solutions

Expert Solution

1.) Given that both of A and B are sorted we can using following standard approach.

i) we take i=0 and j=0 , k=0 pointing to stating index of array A ,B and C respectively we will use a while loop and keep inserting minimum element among A[i] and B[j] in array C until we reach end of any of the array.

if(A[i]<=B[i]){

C[k] = A[i];

i++;

k++;
}

else{

C[k] = B[j];

j++;

k++;

}

As per the algo we can see that for each element at max 1 time we can change position so  Cmov = Θ(n + m).

and similiarly at max n+m time we can compare content of array A with content of B array so .Ccmp = Θ(n + m).

finally we will put content(books ) of c to main shelf(A).

void ArrangeBookScenario(A,B,C,n,m) 
{ 
    int i = 0, j = 0, k = 0; 
  
    // Traverse both array 
    while (i<n && j <m) 
    { 
 
        if (A[i] <= B[j]) 
            C[k++] = A[i++]; 
        else
            C[k++] = B[j++]; 
    } 
   append(A,C); //append remaining elements of array A to C
   append(B,C); //append remaining elements of array B to C
 A = C //putting books of temporary cell in main shelf
} 

2.) In this problem we are given that Array A is sored and the array B is not. and size of array B is small comparision to array A so we can using binary search based approach here. we will be finding appropriate index for insertion of each element of B aray and shifting remaining content of array .

upper bound function can help us to find proper index .

upper bound(a,x) tells us the first index in array a where A[i] > x. we will insert x at position returned by upper bound and will push array to right by 1 unit.

Algo for above problem

As upper bound is an algo based on binary search so in worst case comparision taken will be logn.

So Ccmp = m*logn = O(logn)

and

Cmov at worse case can be upto O(n*m)

Take scenario when Array B is also sorted but in reverse order that will give us max possible value of Movements in Array,


Algorithm ArrangingBooksScenario2(A,B,n,m)
{
for(every element  x in B )
{
int index = upper_bound(A,x); // upperbound will tell index of first element  in a where A[i]> x 
  shiftarray(index);//shift content of array by by 1unit from index till last 
  A[index] = B[i]; 
}
}

Related Solutions

You are building a library robot that fetches/places books for a librarian. list all the important...
You are building a library robot that fetches/places books for a librarian. list all the important parts (physical) for the robot to properly function (2 points) List at least 4 functionalities of the robot. (2 points) What type of wheels/legs are appropriate and why? (1 point)
Your company is looking at developing one of two potential kitchen products – either a toaster or a robot. The discount rate is 4%.
Question Description ToasterRobotInvestment$650,000$675,000Year 1$150,000$400,000Year 2$500,000$350,000Year 3$225,000$250,000Year 4$125,000  Your company is looking at developing one of two potential kitchen products – either a toaster or a robot. The discount rate is 4%.Calculate the NPV for Toaster and RobotWhich project is better? Why?
What are devices in your home that appear to use computers or algorithms? Can you name...
What are devices in your home that appear to use computers or algorithms? Can you name at least one device for every room in your house? Describe one algorithm each device performs
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal (assuming N is a...
Discussion Question 250 words As a leader, you can put ethical values into action and set...
Discussion Question 250 words As a leader, you can put ethical values into action and set the example you want followers to live by. You can resist pressures to act unethically just to avoid criticism or achieve short-term gains. Discuss.
Maslows Theory: your ability to set my own needs and how i can set goals to...
Maslows Theory: your ability to set my own needs and how i can set goals to acheive them
A run for your money Developing countries in Latin America and Asia can borrow for longer...
A run for your money Developing countries in Latin America and Asia can borrow for longer Aug 26th 2010 PERU is not an obvious investment darling. For much of its existence, the country has been in a state of default. As recently as 1990 the inflation rate was 7,500%. Yet in the past few years Peru has persuaded creditors to lend it money for ever-longer periods in its own currency. It issued its first 20-year local-currency bond in 2006; its...
can you put this in your own words? Our class was divided into three groups and...
can you put this in your own words? Our class was divided into three groups and everyone followed the same procedure and steps. Each group had to keep track of their daphnia’s rate of growth based on how many lives and reproduce and how many die. To start off with this experiment, we had to come up a group name and then filled the “Daphnia life history data sheet” on which we recorded all our group member’s phone numbers and...
After all the time, effort and care you put into your work search, it can be...
After all the time, effort and care you put into your work search, it can be tempting to leap at the first job you’re offered without negotiating salary, benefits and other terms of employment. With the exception of some entry-level positions and jobs where you’re automatically placed on a grid determined by education and experience, many employers expect you to make a counter- offer and negotiate terms. To effectively negotiate a job offer, you need to understand and evaluate the...
What was your biggest challenge in developing a communication plan? How can you use communication planning...
What was your biggest challenge in developing a communication plan? How can you use communication planning in your life?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT