Utkarsh Garg Roll No. 12771 Collaborated With: Kartik Agrawal (12344)
Utkarsh Garg Roll No. 12771 Collaborated With: Kartik Agrawal (12344)
T(n)<= T(n-1) + cn
<= T(n-2 ) + c(n-1) + cn
<= T(n-3) + c(n-2) + c(n-1) + cn
.
2
<= d + c + 2c + 3c + . nc
= O(n )
2.)
Solution.
In this case the maximum elements in the partition pushed above can be n/2(since it is assumed to be having lesser elements). If similar
method of pushing indices is followed then in the worst case partitions with size n/2, n/4, n/8 and so on will be pushed above the other
partition. Hence, maximum stack depth will be equal to the number of levels which is log(n). Hence the worst case depth of Stack is
O(log(n)).
NR-Qsort(A,n)
{
Stack S
Push(S, (1, n))
while S is not empty
{
(p, r) = Pop(S)
q = Partition(A, p, r)
if(q-1> r-q)
if (q - 1 > p)
Push (p, q - 1)
if (r > q + 1)
Push (q + 1, r)
Else
if (r > q + 1)
Push (q + 1,r)
if (q - 1 > p)
Push (p, q - 1)
}
}
Pivot in partition routine is selected at random with probability 1/n
Cn=k=1()n (Ck-1+Cn-k))/n +n+1
which gives Cn =O(nlogn)
Utkarsh Garg
Roll No. 12771
Collaborated with: Kartik Agrawal(12344)
Problem 2. Kendall tau distance between permutations
1.)
Solution:
For permutations we will be using a variable different which keep on updating when the comparison of elements of the two
sub-arrays generated after partition results in a smaller element residing in the right partition.
Basically, we are calculating the total number of inversions in the permutation. An inversion is - for i<j, P[i]>P[j]
Pseudo Code:
Int different =0;
// global integer
Copy(P, p, q, Q)
// copies the array into another array and adds a at the end
{
For i = 1 to q-p+1
Q[i] =P[p+i-1]
Q[q-p+2] =
}
Kendall_tau_Merge(P, p, q, r)
{
t1= q-p+1
t2= r-q
L[1, 2, ,t1+1 ] and R[1, 2, ,t2+1] are defined to copy the left and right partition into them
Copy(P, p, q, L)
Copy(P, q+1, r, R)
i =1
j=2
for k= p to r
if L[i] R[j]
P[k]=L[i]
i=i+1
else
P[k]=R[j]
different=different+ (t1-i+1)
j=j+1
}
int Kendall_tau_MergeSort(P, p, r)
{
If(p<r)
q= (p+r)/2
Kendall_tau_MergeSort(P, p, q)
Kendall_tau_MergeSort(P, q+1, r)
Kendall_tau_Merge(P, p, q, r)
Return (different)
}
Int main
{
Int Kendall_tau_distance
Take the inputs for permutation other than identity permutation in array P
Kendall_tau_distance= Kendall_tau_MergeSort(P, p, r)
// p and r are first and last indices of P
}
Time Complexity:
Since the algorithm works same as that of Mergesort (only constant no. of computations are more because of the different variable)
Hence, it is a O(n log n) time algorithm
Eg.
A= 2,3,1
and R=1
2.)
Solution:
Given two permutations we have to find Kendall tau distance between them
Description:
Take one of the permutations and label its elements as with labels 1, 2, 3, 4 . n
Now, consider the second permutation and label its elements. For each element give the same label as that element was
given in the first permutation as follows
A=5, 2, 4, 1, 3
B=3, 2, 4, 1, 5
For i=1 to 5
C[A[i]]=i
For i=1 to 5
D[i]=C[B[i]]
Now, we have to compute number of inversions in assigned labels of the second permutation, hence we apply the algorithm
used in part(1) to array D
Time complexity:
The assignment of labels is of order n and the subsequent algorithm on D is (nlogn)
Hence, T(n)= (nlogn)
Eg.
A=5, 2, 4, 1, 3
B=3, 2, 4, 1, 5
C=4, 2, 5, 3, 1
D=5, 2, 3, 4, 1
Now apply algorithm used in part(1) on D which gives Kendall tau distance between A and B as 7
Utkarsh Garg
Roll No. 12771
Collaborated with: Kartik Agrawal(12344)
Problem 3 : Lower bound for sorting with equal keys.
Solution:
Hence, 2 l n!/(f1!*f2!*..fk!)
h
2 n!/(f1!*f2!*..fk!)
h log(n!) i=1() k (log(fi!))