2021 Algorithm
2021 Algorithm
Name of the Course : B. Sc. (Hons.) Computer Science – CBCS (old course)
Name of the Paper : Design and Analysis of Algorithms
Semester : IV
Year of admission : 2015,2016,2017 and 2018
Duration : 3 Hours
Maximum Marks : 75
1. A student named Rohan wishes to enhance his knowledge by studying a few online
courses. Every course has a fee that one has to pay to register. Also, each course gives
some credit on its successful completion. He has some amount of money saved with him.
He needs to choose among the available courses such that he gets maximum sum of
credits by spending the money he has. Write an algorithm to find the optimal solution for
Rohan’s problem.
Using the algorithm given by you, find the courses he should study for the given instance,
illustrating each step clearly.
2. A person named Albert (denoted by A) starts a food delivery service. The people who
wish to avail this service register themselves with him by providing their address. He
hires delivery boys to deliver food and assigns a different delivery boy to each registered
person. To save time, he asks the delivery boys to use Scooters to deliver the food. The
following diagram shows the time that a delivery boy takes to reach from one place to
another. Consider that B, C, X, Y and Z are the registered people. The number on each
edge represents the time (in minutes). Compute the minimum time to reach them all
individually.
Now, suppose all Scooters need to be serviced and are unavailable. The delivery boys
need to use bicycles to reach the registered people. The amount of time taken to reach the
destination using a bicycle is the square of the time taken to reach the destination using a
Scooter. Will the delivery boys be reaching the respective destination using same route as
computed above? Does this imply all such routes will never change in any such problem
instance where the new edge weights are square of original edge weights?
3. Suppose there are n bottles and each bottle i has some capacity ci. Write the most
efficient algorithm to arrange the bottles in the order of their capacities. Also, explain the
running time of your algorithm.
Next, consider that every bottle has an expiry date before which its content should be
consumed. Each bottle is assigned a priority such that a higher value indicates a higher
priority. The bottle having a closer expiry date has a higher priority assigned to it. Give an
efficient algorithm to arrange the bottles in non-increasing order of their priorities. Does
your algorithm involve any comparisons? If yes, count them. Explain the running time of
your algorithm.
4. Consider a set of jobs to be scheduled on a processor. Each job has an execution time
associated with it. A job-scheduler allocates the processor to one of the given jobs at a
time. The scheduler has fixed a maximum execution time: the maximum duration for
which a job can continue processing once scheduled. If the execution time of any job is
greater than this value then it is pre-empted and scheduled again later with the remaining
execution time.
Consider the following variant of shortest-job-first scheduling algorithm which
schedules the jobs in the ascending order of their execution times. Suppose the data
structure used for this scheduling algorithm is a red-black tree (called as scheduling tree)
where the jobs are the nodes in the tree.
Algorithm:
• The job having least execution time will be chosen and sent for execution and is
removed from the tree.
• If the job reaches its maximum execution time while being processed, it is pre-
empted and needs to be scheduled later to complete its processing. This job is
reinserted into the scheduling tree based on its remaining execution time.
• Again, the job with the least execution time is selected, repeating the above steps
until all the jobs are processed completely.
Consider a set of jobs whose execution times (in nanoseconds) are as follows:
4,10,7,23,5,30. Construct the red-black tree corresponding to the given input and then
schedule the jobs using the above stated algorithm thereby showing the red-black tree
after every step. Assume the maximum execution time is 20 nanoseconds.
5. A party is going on in a hotel. When the dinner starts, a waiter is assigned the task of
distributing plates to the guests. He creates a pile of plates on a table by adding one plate
at a time. The waiter can pick up one or more plates at a time from the pile to distribute.
The waiter may add more plates to the pile one by one at any time. Suppose he performs a
total of n operations wherein an operation can be putting one plate onto the pile or
removing one or more plates from the pile. Analyse the average cost per operation over a
sequence of n operations.
Later, after the dinner gets over, a game is played to find out the most social person in the
party. Suppose there are m guests in the party. Each guest considers a set of guests as his
friends and gives a flower to each of his friends. The guest who receives the maximum
number of flowers wins the game. Suggest an efficient divide-and-conquer algorithm,
having least possible worst-case running time, to find a winner. Also, find all the winners,
if more than one winner exists. Explain the running of the algorithm given by you.
6. A chemical laboratory has n chemicals stored in it. There is a risk that some chemicals
may react with some others. For safety purpose, the lab assistant decides that for each pair
of reactive chemicals, one is kept in a yellow-coloured bottle and the other is kept in a
green-coloured bottle. Give an efficient algorithm that he can use to find out whether such
a colouring is possible or not. If yes, the algorithm should determine the bottle colour for
each of the n chemicals. If not, the algorithm should report it. Give an instance having at
least 6 chemicals in which there are a minimum of 7 reactive pairs for which such a
colouring is possible.