Sie sind auf Seite 1von 5

Sri Harsha Jujjavarapu

01787250

1. Chapter 2, Exercise 8 (Stress Testing)

Given a ladder with n rungs and k number of jars. We have to perform fewer
drops to find the highest safe rung.

a. k = 2
We have given 2 jars to find the highest safe rung.

For jar = 1:
We will divide ladder into pieces with each some length x (assuming n is
dividable by x) such that first we will try dropping jar from the x rung and if it
is not broken, we will move to the 2*x rung and so on until we reach the point
where ladder is completed, or jar is broken. In worst case total number of
droppings needed is n/x.

For jar = 2:
If the jar is broken at some rung k’ * x, we will have only one jar to find the
highest safe rung. In worst case no of droppings needed at most to find
highest safe rung is x because we have got only one jar, so we have to try
every rung between (k’-1) * x and k’ * x.

In this strategy worst case scenario is when highest safest rung is last but
one. So, when k = 2 in worst case total no of droppings needed is (n/x + x).

Here x is variable, we need to find x which minimizes total no of droppings


i.e. minimizes equation (n/x + x).

According to AM-GM inequality

𝑥1 + 𝑥2 ≥ 2√𝑥1 ∗ 𝑥2

So minimum value of (n/x + x) is 2√𝑛 and x value will be √𝑛. So, when x is √𝑛
we will get minimum number of droppings.

So, when k = 2 we will drop jar from heights multiples of √𝑛 and if jar broke
at k’*√𝒏 rung we find highest safest rung by dropping it from every rung
between (k’-1) * √𝒏 and k’ * √𝒏, if we follow this strategy, we will drop total
jars at most 2√𝒏.
If n is not perfect square, we will drop jar from heights multiples of [√𝒏] and
in this case we will drop jars at most (2√𝒏 + 2) times.

Time complexity is O(√𝒏).

b. k > 2

We have given jars more than two.

For jar = 1.
We will divide ladder into pieces with each some length x (assuming n is
dividable by x) such that first we will try dropping jar from the x rung and if it
is not broken, we will move to the 2*x rung and so on until we reach the point
where ladder is completed, or jar is broken. In worst case total number of
droppings needed is n/x.

For jar = 2.
If jar is broken at some rung k’ * x. We have to check at which rung jar will
break between (k’-1) * x and k’ * x with help of remaining k-1 jars. We will
divide narrowed possible rungs into pieces with each some length y
(assuming x is dividable by y) such that first we will try dropping jar from the
(k’-1) * x + y rung and if it is not broken, we will move to the (k’-1) * x + 2*y
rung and so on until we reach the point where or jar is broken. In worst case
total number of droppings needed is x/y.

For jar = 3.
If jar is broken at some rung (k’ -1) * x + k’’*y. We have to check at which rung
jar will break between (k’ -1) * x + (k’’-1) * y and (k’ -1) * x + k’’ * y with help of
remaining k-2 jars. We will divide narrowed possible rungs into pieces with
each some length z (assuming y is dividable by z) such that first we will try
dropping jar from the (k’-1) * x + (k’’-1) * y + z rung and if it is not broken,
we will move to the (k’-1) * x + (k’’-1) * y + 2*z rung and so on until we reach
the point where or jar is broken. In worst case total number of droppings
needed is y/z.

We will continue on until only one jar is remaining.

For jar = k.
If the jar is broken at some rung (k’-1) * x + (k’’-1) * y + (k’’’-1) * z + ……
+k’’’’*w, we will have only one jar to find the highest safe rung. In worst case
no of droppings needed at most to find highest safe rung is w because we
have got only one jar, so we have to try every rung between (k’-1) * x + (k’’-1)
* y + (k’’’-1) * z + … +(k’’’’-1)*w and (k’-1) * x + (k’’-1) * y + (k’’’-1) * z + …
+k’’’’*w.

In this strategy worst case scenario is when highest safest rung is last but
one. In worst case total number of droppings, we need is n/x + x/y + y/z + …
+ w.

Here we need to tune variables x, y, z, …, w such that total number of


droppings should be minimum.

According to AM-GM inequality

𝑥1 + 𝑥2 + ⋯ + 𝑥𝑛 ≥ 𝑛 𝑛√𝑥1 ∗ 𝑥2 ∗ … ∗ 𝑥𝑛
𝑘 𝑛 𝑥 𝑦
So minimum value of worst-case time is 𝑘 √( 𝑥 ) ∗ (𝑦 ) ∗ ( 𝑧 ) ∗ … ∗ (𝑤) which is

𝑘 𝑘√𝑛.

𝒌−𝟏
𝑛 𝑥 𝑦
Minimum value is possible when ( 𝑥 ) = (𝑦 ) = ( 𝑧 ) = ⋯ = (𝑤), i.e. 𝒙 = 𝒏 𝒌 ,
𝒌−𝟐 𝒌−𝟑 𝟏
𝒚= 𝒏 𝒌 , 𝒛= 𝒏 𝒌 … 𝒘 = 𝒏𝒌 .

𝒌−𝟏
First, we begin dropping first jar from heights multiples of [𝒏 𝒌 ]. We drop jar
𝒏
at most ( 𝒌−𝟏 + c) times and narrow the possible rungs.
𝒏 𝒌
We will apply the same strategy for other jars recursively. Total we drop jar
at most 𝒌 𝒌√𝒏 + c times.

Time complexity is O(𝒌 𝒌√𝒏).


2. Chapter 3, Exercise 4 (Butterfly Studies)

Given n specimens and m judgements, we have to declare whether m judgements


are consistent or not. Each specimen is classified into two classes A and B. For
each pair of specimens i and j if both of them belongs to same class then
judgement is same or different.

We can construct a Graph G = (V, E), vertexes as n specimens and edges as m


judgements. We can use BFS travesal to find the consistency of data.

Pseudocode

Enqueue any one vertex into queue Q and label as A.


For vertex v’ dequeue from queue Q:
Mark v’ as visited
For vertex v adjacent to vertex v’:
If v is not labeled:
If judgement of v and v’ is same:
Label v as same class as of v’
EndIf
If judgement of v and v’ are different:
Label v as class different from v’
EndIf
Enqueue vertex v into Queue Q.
EndIf
EndFor
EndFor

For each judgment (v, u):


If judgment was same:
If u and v have different labels:
Print inconsistency u and v
EndIf
EndIf
If judgment was different:
If u and v have same labels:
Print inconsistency u and v
EndIf
EndIf
EndFor
In the above algorithm we used BFS traversal to visit all nodes. Running time of
BFS is O(m+n). After labeling each node we ran another For loop to find inconsistencies
in the judgement, this takes O(m). Total time complexity of algorithm is O(m+n).