Oxford Mathematics Problem Sheet 1
Oxford Mathematics Problem Sheet 1
Oxford Mathematics Problem Sheet 1
Assignment 1,CS19B039,CS19B036,CS19B006
Page 1 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
In the loop:
children of node i are numbered higher than i. So, it preserves.
Termination:
i becomes 0, node is the root of the heap and it’s true.
Termination:
i = -1 for the loop,
n
ak x k
P
eval =
k=0
derivative ends at i = 0,
n
(k)ak xk−1
P
So deriveval =
k=1
Page 2 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
For the number 1 in permutation, let it be at position k then (k-1) values that
are greater than 1 are on its left. Hence a1=k-1 and position of is (k)th empty
space.i.e., visited [k] = false for 0 to k.
Intial Condition:
visited[i]=0 for all i in range(0,n+1)
Loop invariants:
{visited[i]∈{true,false} for all i} and {1<=j<=n}
Final Condition:
visited[i]=1 for all i in range pssbl
Proof:-
Initial case the permutation is same as inversion vector for single element
Page 3 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
Assume {j1,j2,...jk} be the inversion vector add 1 to the set of elements. The per-
mutation given by inversion matrix {i1,...ik+1} we take ia+1=ja for all a∈[1,k]· To
finish the construction, we need to add 1 into the modified permutation of length
k. This can be done by shifting all elements whose index to greater than i1, to the
right by one position and filling ik+1 by 1. visited[i] for all [1,n] is true and thus,
a permutation can be constructed using inversion Matrix.
Page 4 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
i=1,j=1,k=1
minW1 = 0
while(i,j,k <= n)
W1 = max(abs(a[i]-b[j]),abs(b[j]-c[k]),abs(c[k]-a[i]))
minW1 = min(minW1,W1)
min = min(a[i],b[j],c[k])
if(a[i] == min) i++
else if(b[j] == min) j++
else if(c[k] == min) k++
end while
M2 = minW1
Page 5 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
Type-2:
min = c[1]
max = a[n]
while(abs(c[1]-b[i]),abs(a[n]-b[i]))
i++
end while
tempMin2 = min(abs(c[1]-b[i]),abs(b[i]-a[n]))
tempM3(c,a) = max(tempMin1,tempMin2);
M3 = max(tempM3(a,b),tempM3(b,c),tempM3(c,a))
Page 6 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
M4 = minW2
Initial condition:{(i,j,k = 1)∪(minW 2 = 0)}
LoopInvariants : {(i, j, k <= n) ∪ (minW 2{W2(i,j,k)})}
Final condition :{(minW2 = min(W2(i,j,k)))∪(max(i, j, k) = n + 1)}
Page 7 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
CommonFinder2(arr1,arr2)
{
temp[m];
k=0;
while(i < arrsize and j < m) //O(m) time complexity
Page 8 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
if(arr1[i]==arr2[j])
temp[k]=arr1[i]
i++;j++;k++
else if(arr1[i] < arr2[j])
i++;
else
j++;
endwhile
arrsize=k;
return temp;
}
arrsize=m;
int∗ common=CommonFinder2(A[0], A[1]);
for(i=0;i < n − 2;i++) //O(nm) time complexity
common=CommonFinder2(common, A[i+2]);
endfor
x=common[0];
for(i=0,i < n;i++)
ind=BinarySearch(A[i],x);
Loop Invariant:
{i <= n − 2} and {Array ”common” contains all common elements in the arrays
A[0]..A[i+1]} and {arrsize >=1}
is common for both O(nm) and O(nm2 ) algorithms.
Program correctness:
From loop invariant we can see that Array ”common” contains all common ele-
ments in the arrays A[0]..A[i+1].At the end of the loop i will be n-2 which means
all common elements btw A[0] to A[n-1] are present in ”common” array.
Since atleast one common element is there,size of ”common” array cant be zero.Hence
it will be one of the common element.This guarentees the solution.// In case of
EXTN2 it is not given that common element is present or not.
So, the for loop will be changed slightly to check whether common elements have
come to zero.
At this point we stop execution.
Page 9 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
endfor
Everything other than this remains same as in the case of O(nm) algorithm before.
Loop Invariant:
{i <= n − 2} and {Array ”common” contains all common elements in the arrays
A[0]..A[i+1]}
The difference between two loop invariants is the condition {arrsize >=1}.
Page 10 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
Q x−xj
8. (a) Let Si (x) = xi −xj
i!=j,1<=j<=n
Si (xj ) is 0, when i!=j and 1 when i=j.
Because numerater is 0 when i!=j and when i=j factors of numerator
cancels with denominator results 1.
Now our polynomial A(x) is linear combination of Si (x) for 1 <= i <= n
P
A(x) = yi Si (x)
1<=i<=n
So for any xi (1 <= i <= n),A(xi ) = yi Si (xi ).But Si (xi ) = 1,so A(xi ) = yi
Hence it satisfies all n points.
Page 11 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
9. Let f (n) be the minimum number of calls be done for n people. we can say simply
for f (1) = 0, f (2) = 1, f (3) = 3, f (4) = 4
we can work on 5 and 6 just for induction.
min calls will be 6 and 8 for both 5 and 6 of the value n.
we can assume by this mincalls will be 2n − 4,if n >= 4.
lets prove by induction,
for f (k) = 2k − 4 .
lets introduce a person A into the group.
Lets assume he calls B who is already in the group and let the calling be 2k − 4
for passing A information.
Another calling between them makes A gets all information
f (k + 1) = 2k − 4 + 2
Extra two calls for AtoB and BtoA
f (k + 1) = 2(k + 1) − 4
Hence min calling will be 2n − 4 for n >= 4.
Here sum denotes the no.of paths from u to v and the algorithm is linear which is
O(n)
Page 12 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
Page 13 of 14
CS2800: Assignment 1,CS19B039,CS19B036,CS19B006
(b) In a directed graph,if two nodes u,v are in the same strongly connected compo-
nents, then the nodes reachable from u are identical to those reachable from v, so
cost[u] = cost[v]. We donot need to distiguish between nodes in the same SCC,
and we can work with a DAG
Find the strongly connected component of G
For each SCC C: let the price p∗c for component C be the smallest price of
it’s nodes, p∗c = minuc pu
Run the DAG of algorithm(a) on the metagraph, using these metaprices
p∗c .This returns component metacosts cost∗ [C] .
For each SCC C; and each node uC: cost[u] = cost∗ [C]
This takes linear time because the algorithm for SCC and for part(a) are both
linear time
Page 14 of 14