Q1 Solve the recurrence relation iteratively(without using Master`s

advertisement

COT 5405 Analysis of Algorithms Spring 2004

On-Campus Exam #2

Name: __________________________________________

UFID: ____________ - ____________

E-mail: _________________________________________

Instructions:

1. Write neatly and legibly

2. While grading, not only your final answer but also your approach to the problem will be evaluated

3. You have to attempt only THREE problems. If you attempt more than three problems, we will grade any two of them of OUR choice.

4. Each problem carries 10 points

5. Total time for the exam is 120 minutes

6. You are not allowed to use a calculator for this exam

I have read carefully, and have understood the above instructions. On my honor, I have neither given nor received unauthorized aid on this examination.

Signature: _____________________________________

Date: ____ (MM) / ____ (DD) / ___________ (YYYY)

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 1 of 9

Question 1:

Consider the following algorithm for finding min-cost spanning tree:

Suppose the graph has vertices numbered 1 … n . Start with a forest containing n trees, each consisting of a single vertex.

Repeat the following until a single tree remains. For each vertex v = 1 … n in turn, consider the cheapest edge with at least one end point in the tree that contains v . If it has both end points in the same tree, then discard it. If not, then add it to the spanning forest.

Does this algorithm find the minimum spanning tree? Justify your answer. If you think it does not work, give a counterexample and a step-by-step spanning tree construction showing that the resulting spanning tree is not minimum.

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 2 of 9

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 3 of 9

Question 2:

Suppose you are the waiter in a restaurant. There are n plates of different weights (say the weights are s

1

through s n

). The probability that a customer would ask for the plate i is given by p i

(obviously ∑ p i

= 1), There is a single stack for storing the plates, in which the plates are kept in the Last-In-First-Out order. Which means, if you want to access a plate in the middle of the stack, you will have to first lift all the plates on top of it.

Suppose i

1

, i

2

, …, i n

denote the final order of placement of the plates in the stack (from the top to the bottom).

Show that the expected weight you will be lifting while accessing a plate is given by:

W exp ected

 j n  

1

 p i j k j

1 s i k

Present an efficient algorithm that determines the optimal ordering of the plates in that stack that minimizes the value of W expected

. Give a formal proof of its optimality and derive its running time.

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 4 of 9

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 5 of 9

Question 3:

You are given n dogs of different weights (not necessarily distinct) ascendingly sorted by their ages (distinct). The dogs have their weights (between 1 & M pounds) and Ages (in days) expressed in integers. They need to be transported to a dog show in a long tractor-trailer with several compartments (C

1

, C

2

, ….) The rules of putting the dogs in different compartments are:

Every compartment (except the last) should carry a weight of exactly M pounds.

No dog travels in a lower-indexed compartment than an younger one (i.e, if a dog is traveling in C

5

, all dogs younger than him must travel in C

1

through C

5

too)

Once the dogs are loaded, every compartment must also be loaded with a bag of cotton to make up for the unused weight. The cotton bag’s cost is proportional to its weight.

Design an efficient loading algorithm to minimize the total cost of the cotton bags. Prove its correctness and justify its complexity.

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 6 of 9

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 7 of 9

Question 4:

You are given G = ( V , E ) a Directed Acyclic Graph, and weight of edge ( i , j ), w ( i , j ) = 1 if ( i , j )

E and w ( i , j ) = 0 otherwise. Two paths are said to be distinct if they differ in any way. For each pair of vertices i , j

V , compute the total number of distinct paths from i to j , denoted C ij .

(Hint: Try to modify the Floyd-Warshall algorithm for computing all-pair shortest path. Pay special attention to the base case here, because it is easy to get it wrong.)

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 8 of 9

Analysis of Algorithms Spring 2005 On-Campus Exam #2 Page 9 of 9

Download