CO34563 Assignment 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Shri G. S.

Institute of Technology and Science, Indore


¯
Department of Computer Engineering
CO34563 - Assignment - 4 (Greedy Algorithms)

Course Instructor: Surendra Gupta ([email protected])


Due Date: 3 March 2024

1. For the following problems, write pseudocode solutions and state the worse case running time
(in terms of Θ or O where appropriate). Also give the correctness of the yours algorithms. You
will be graded on the efficiency of your solutions. After that implement the algorithms in any
programming language and verified the complexity of the yours algorithm.

Problem 1: Assume that we have N workers and N jobs that should be done. For each pair
(worker, job) we know salary that should be paid to worker for him to perform the job. Our
goal is to complete all jobs minimizing total inputs, while assigning each worker to exactly
one job and vice versa. Converting this problem to a formal mathematical definition we can
form the following equations:
C is a cost matrix of NxN where cij is cost of worker i to perform job j.
X is resulting binary matrix, where xij = 1 if and only if ith worker is assigned to j th job.
PN
j=1 xij = 1, ∀i ∈ [1, N ] one worker to one job assignment.
PN
xij = 1, ∀j ∈ [1, N ] one job to one worker assignment.
i=1 P
PN N
i=1 j=1 cij xij is a total cost function and it should be minimize.
We can also rephrase this problem in terms of graph theory. Let’s look at the job and workers
as if they were a bipartite graph, where each edge between the ith worker and j th job has
weight of cij . Write a program to find minimum-weight matching in the graph (the matching
will consists of N edges, because our bipartite graph is complete).
Problem 2: Suppose you are given an array A[1 .. n] of integers, each of which may be positive,
negative, or zero. A contiguous subarray A[i .. j] is called a positive interval if the sum of
its entries is greater than zero. Describe and analyze an algorithm to compute the minimum
number of positive intervals that cover every positive entry in A. For example, given the
following array as input, your algorithm should output the number 3.

Problem 3: Consider the following bridge crossing problem where n people with speeds s1; : : :
; sn wish to cross the bridge as quickly as possible. The rules remain:
(a) It is nighttime and you only have one ash-light.
(b) A maximum of two people can cross at any one time
(c) Any party who crosses, either 1 or 2 people must have the ash-light with them.
(d) The ash-light must be walked back and forth, it cannot be thrown, etc.
(e) A pair must walk together at the rate of the slower person’s pace.
Give an efficient algorithm to find the fastest way to get a group of people across the bridge.
You must have a proof of correctness for your method.
Problem 4: The setting for this problem is storage system with a fast memory consisting of
k pages and a slow memory consisting of n pages. At any time, the fast memory can hold
copies of up to k of the pages in slow memory. The input consists of a sequence of pages from
slow memory, think of these as being accesses to memory. If an accessed page is not in fast

1
memory, then it must be swapped into fast memory, and if the fast memory was full, some
page must selected to be evicted from fast memory. The goal is to determine the pages to
evict so as to minimize the total number of evictions. Consider for example that k = 2, n =
4 pages are named A, B, C and D, and the access sequence is A B C A. Then after the first
two pages, the fast memory contains A and B. When C is accessed then either A or B must
be evicted. If B is evicted then no further evictions are necessary, and the total number of
evictions is 1. If A was evicted, then either B or C must be evicted when A is accessed again,
and the total number of evictions would be 2. Give a greedy for this problem and prove that
it is correct using an exchange argument.
Problem 5: You wish to drive from point A to point B along a highway minimizing the time
that you are stopped for gas. You are told beforehand the capacity C of you gas tank in liters,
your rate F of fuel consumption in liters/kilometer, the rate r in liters/minute at which you
can fill your tank at a gas station, and the locations A=x1 , x2 , . . . , B=xn of the gas stations
along the highway. So if you stop to fill your tank from 2 liters to 8 liters, you would have to
stop for 6/r minutes. Consider the following two algorithms:
(a) Stop at every gas station, and fill the tank with just enough gas to make it to the next
gas station.
(b) Stop if and only if you don’t have enough gas to make it to the next gas station, and if
you stop, fill the tank up all the way.
For each algorithm write a program and also either prove or disprove that this algorithm
correctly solves the problem. Your proof of correctness must use an exchange argument.
Problem 6: Write a program(s) for the following problem and compare the results:
INPUT: A set S = (xi ; yi ) 1 ≤ i ≤ n of intervals over the real line. Think of interval (xi ; yi )
as being a request for a room for a class that meets from time xi to time yi .
OUTPUT: Find an assignment of classes to rooms that uses the fewest number of rooms.
Note that every room request must be honored and that no two classes can use a room at
the same time.
(a) Consider the following iterative algorithm. Assign as many classes as possible to the
first room (we can do this using the greedy algorithm discussed in class, and in the class
notes), then assign as many classes as possible to the second room, then assign as many
classes as possible to the third room, etc. Does this algorithm solve the Problem? Justify
your answer.
(b) Consider the following algorithm. Process the classes in increasing order of start times.
Assume that you are processing class C. If there is a room R such that R has been assigned
to an earlier class, and C can be assigned to R without overlapping previously assigned
classes, then assign C to R. Otherwise, put C in a new room. Does this algorithm solve
the Problem?
Problem 7: Write a program for the following problem:
INPUT: Positive integers r1 ; r2 ; :::; rn and c1 ; :::; cn .
OUTPUT: An n by n matrix A with 0/1 entries such that for all i the sum of the ith row in
A is ri and the sum of the ith column in A is ci , if such a matrix exists.
Think of the problem this way. You want to put pawns on an n by n chessboard so that the
ith row has ri pawns and the ith column has ci pawns.
Problem 8: Write a program for the following problem:
INPUT: A set S = (xi ; yi ) 1 ≤ i ≤ n of intervals over the real line.
OUTPUT: A maximum cardinality subset S’ of S such that no pair of intervals in S’ overlap.
Consider the following algorithm:
Repeat until S is empty 1. Select the interval I that overlaps the least number of other
intervals. 2. Add I to final solution set S’. 3. Remove all intervals from S that overlap with I.
Prove or disprove that this algorithm solves the problem.
Problem 9: We wish to compute the laziest way to dial given n-digit number on a standard
pushbutton telephone using two fingers. We assume that the two fingers start out on the
star and hash keys, and that the effort required to move a finger from one button to another

2
is proportional to the Euclidean distance between them. Design and analyze an efficient
algorithm that computes in time of dialing that involves moving your fingers the smallest
amount of total distance.
Problem 10: There are N floors and N persons each one is tagged with some random unique
number between 1 to N (represents floor number). We have a lift which can accommodate
one person at a time. Every person is in some random floor. Initially lift is at floor 1 and
all floors have single person. Design an algorithm to move the persons to their corresponding
floor with minimum number of lift movements. [Restriction: Lift can have at most one person
at a time. While moving persons, at some point of time, we can keep more than one person
at one floor. ]

You might also like