You are on page 1of 5

ADA Assignment 1

Anubhav Chaudhary
2016013

1 Group Members
1. Anubhav Chaudhary: 2016013
2. Deepak Magesh Srivatsav: 2016030
3. Yashit Maheshwary: 2016123

2 Question 1
2.1 Algorithm
This question can be solved using a greedy algorithm. We first sort the given
set of input points in the increasing order. Now for each element xi {0 ≤ i ≤ n}
in the sorted set X = {x1 , x2 , . . . , xn } we do the following. We keep a global
list L.
1. yi = xi . Add a tuple {yi , yi + 1} in the list L.
2. Consider the range {yi , yi + 1}. Remove all the x0i s such that xi <= yi + 1.

2.2 Proof
Claim: that there is an optimal solution which has a interval say {x0 , x0 + 1}.
Suppose that there exists an in which the element xp is covered by the following
interval {y0 , y0 + 1}. Now since we know that the elements were sorted therefore
x0 is the smallest element in the set {y0 , y0 + 1} so we can simply shift the range
to {x0 , x0 + 1} as all there was no element before x0 . Therefore the claim is
proved and greedy property is also satisfied.

2.3 Complexity
The running time is simply the time to sort O(n log n) plus the time to iterate
over the list O(n) therefore Complexity is

O(n log n)

1
3 Question 2
3.1 Algorithm
This can be done very easily by always selecting the highest possible amount of
coin until we can no longer select it. We can solve it by taking cases
1. Case 1: If n < 5 we can simply use n pennies.
2. Case 2: If 5 ≤ n < 10 we can use 1 nickle and the remaining n − 5
pennies.

3. Case 3: If 10 ≤ n < 25. For this case the solution will depend on the
above two cases also. Now first we will select the maximum number of
dimes which will be bn/10c. The remaining amount left will be n − 10 ×
bn/10c and this much can be generated using the above two cases.
4. Case 4: If 25 ≤ n we select the maximum number of quarters that we
can which will be bn/25c. Now the remaining amount n − 25 × bn/25c can
be generated using one or more of the above 3 cases.

3.2 Proof
A thing to notice here is that except pennies, all other three (dimes, nickels,
quarters) have a GCD of 5. Meaning that a better optimal solution (with
lesser number of coins) can be obtained by grouping multiple coins of smaller
denominations into a some coins of higher denominations. We can also do this
case by case

1. Consider only pennies and nickels. So if requirement is < 4 then we use


pennies. If requirement ≥ 5 then we have to use one nickels which would
reduce the number of coins at least by 5 (by selecting one or more nickels)
therefore the number of coins will be less than 5 for which we can use
pennies.

2. Now along with the above two consider that we have dimes also. In this
case, if we have more than 10 cents as the requirement then we can use
dimes instead of two nickels.
3. Now if we have the above three coins along with quarters then we can
replace 2 or more dimes with one or more quarters.

So we can replace any two or more smaller denomination coins by a one or more
larger denomination coin.

3.3 Complexity
Since only division and subtraction is required therefore it can be done in con-
stant time (4 operations) Complexity i.e. O(1).

2
4 Question 3
4.1 Finding the path
Now simple going through each edge of the graph one by one and visiting vertex
only once. We can repeat the above for all the edges. This algorithm is clearly
polynomial time as it is simply a graph traversal multiple times. Therefore for
sure this problem belongs to NP class.
Second step is to show that Hamiltonian Cycle is NP-Complete, so we can try
and reduce this problem to finding Hamiltonian Path.
Proof: Lets start with a graph G(V, E) and another graph G ∗ (V ∗, E∗).
The graph G∗ is constructed on the fact that G contains a Hamiltonian cycle
if and only if the graph G∗ has a Hamiltonian path. We can show this easily
be starting with some random vertex (v = 4) ∈ V and adding a copy of this
say (v 0 = 5) along with all other edges of v = 4 along with v 0 = 5 to our other
Graph G∗. Moreover we add two more edges 6 and 7 connected to 4 and 5
respectively as can be seen below.

Now lets prove the first half of the if and only if statement, that is, Given that
G has a Hamiltonian cycle (4-0-1-2-3-4) then G∗ has a Hamiltonian path. Now
start with vertex 6 in G∗, if we follow the cycle 6 − 4 − 0 − 1 − 2 − 3 − 5 − 7.
For the second part of the if and only if statement, lets start with the fact
that G∗ has a Hamiltonian path then G will have a Hamiltonian cycle. Now
this path has to start from 6 and end at 7. This Hamiltonian path can be easily
converted to a Hamiltonian cycle by removing the vertices 6 and 7. Now the
path must go from 4 to 5, now if we remove 5 also we are left with a cycle which
was a Hamiltonian cycle in G.
Therefore finding Hamiltonian path is also a NP-Hard problem along with it
being NP complete.

5 Question 4
For this question we can use the well known Minimum spanning tree (MST)
algorithm for finding the clusters. Lets start with a graph G(V, E) where the set
V represents all the nodes/objects and the set E represents all the edges which
is basically all the distance between the objects (can be modelled very easily).
Now lets start by creating a Minimum spanning Tree (MST) using the well know

3
Kruksals algorithm. Now as the name suggests that it is a M̈inimums̈panning
Tree. Therefore if we delete one edge it will result in having 2 disconnected
components. So if we delete say k edges then we will have k + 1 disconnecting
edges. Now by doing so we will inturn get the remaining edges [k − 1 in number]
will be the most edges with the maximum weight/distance(in our case) if we
delete these k − 1 edges we would get k disconnected components. As we said
in the beginning that we started with a minimum spanning tree, therefore when
we delete the edges with maximum distance, the minimum distance will be
maximum.
Creating the mst would be the most expensive step therefore it will take about
O(n2 log n).
Alternatively we can also use prim’s algorithm since our graph is a complete
graph therefore working on an adjacency matrix will will give a complexity of
O(V 2 ) where V is the number of vertices.

6 Question 5
Minimum Cut: A set of vertices whose removal will disconnect a connected
graph.
Cut Property: So if have N which is a set of nodes and we let e to be the
minimum weight edge with exactly one endpoint in N . Then the edge e will be
present in all the minimum spanning tree (MST) of the graph.
Say that there is a graph G(V, E) which has a MST M . We have edge e ∈ E in
which is the minimum weight edge with only one endpoint in N , where N is a
subset of all the nodes of the graph G(V, E).
Proof: Lets start with the opposite. Say the e does not belong to M , in
other words e ∈ / M . If we try to add e to M then it will create a cycle in M
as the property of MST says. [If we add an edge (u, v) between u and v to a
MST T of a Graph G (assuming that it won’t create a cycle) then it means that
u and v were not connected before therefore adding this, but this violates the
property of MST]. Therefore there would two edges e and some other e0 which
would be crossing the cut twice. Now if we remove the edge e0 we would get M 0
where, cost(M 0 ) < cost(M ), which violates the property of MST, contradicting
the fact that we initially started with MST.
Cycle Property: Lets start with a cycle C in a graph G(V, E). Let there
be an edge e which has the maximum weight in the cycle C. Then the edge e
is not in the MST M of graph G(V, E)
Proof: Lets start with the assumption that edge e belongs to the MST, or
e ∈ M . Now if we try to remove the edge e from M this would result in creating
a cut say K in M . Which would end up disconnecting the graph. Since the
original graph had a cycle therefore there would exist an edge e0 which we can
use to replace e, the resulting MST M 0 would have cost less than that of M ,
contradicting the fact the M was a MST.

4
7 Question 6
This we would do this the standard way, start by defining a function which
can define loss. Now this function should have some certain properties which
is that we it should have a less number of minimas (preferably only one global
minima should be there). The loss function will use the mean squared error.
The minima function and the derivative is:
500
X 2
F (a, b, c) = Y ∗i − Y i
i=1

In the above two equations Y ∗i is the predicted value and Y i is the actual
value. For each iteration i we will update the values of the variables which we
are predicting, that is, a, b, c using the following equations

∂G(a, b, c)
a=a−α
∂a
∂G(a, b, c)
b=b−α
∂b
∂G(a, b, c)
c=c−α
∂c
In the above equation α is defined as the learning rate, which is a pretty low
value usually of the order of 10−3 . The value is low so as to avoid jumps and
reach the final minima in certain steps (finite steps).

You might also like