0% found this document useful (0 votes)
37 views6 pages

Running Time:: T (P) O (1) + T (P - 1)

Sir Akbar Mehdi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Running Time:: T (P) O (1) + T (P - 1)

Sir Akbar Mehdi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q_01:

Sol: (a)

To implement Extract_min on a non-empty Young tableau, we extract the element A[1,1]


from tableau and then applying MAKE-YOUNG-TABLEAU to restore the property of Young
tableau.(Based on the Heapify algo).

Algo:

EXTRACT-MIN(A)

1. k A[1,1]
2. A[1.1] ∞
3. MAKE-YOUNG-TABLEAU(A,1,1)
4. Return k

MAKE-YOUNG-TABLEAU(A, x, y)

1. Small_x x
2. Small_y y
3. If x+1≤m and A[x,y] > A[x+1,y]
4. Then small_x x+1
5. Small_y y
6. If y+1 ≤ n and A[small_x,small_y] > A[x,y+1]
7. Then small_x x
8. Small_y y+1
9. If small_x ≠ x OR small_y ≠ y
10. Then swap A[x,y] A[small_x,small_y]
11. MAKE-YOUNG-TABLEAU(A, small_x, small_y)

Running Time:
The running time of MAKE-YOUNG-TABLEAU is O(m + n) because MAKE-YOUNG-
TABLEAU swaps A [x ,y] with either A [x+ 1, y] or Y [x, y + 1] and recursively MAKE-
YOUNG-TABLEAU the tableau at the corresponding entry. Therefore, with every recursive
step, x+y is incremented by 1. However, x +y cannot be more than m + n and this gives the
time bound of O(m+ n). More precisely, we can say that YOUNGIFYing a tableau of size m x n
will recursively MAKE-YOUNG-TABLEAU a tableau of size (m- 1)xn or of size mx(n-1).

Therefore, if we let p = m + n to be the size of our problem:


T(p) = O(1) + T(p - 1)
The solution of the above recurrence is T(p) = O(p) = O(m + n).
b).
To insert a new element into young tableau we first insert new element at
A[m, n] and then call MAKE-TABLEAU on the element to restore its property.

Algo:

INSERT (A, val)


1. A[m, n] val
2. MAKE-TABLEAU(A, m, n, val)

MAKE-TABLEAU (A, x, y , val)


1. While (x>1 and y>1) and (A[x-1,y]> val or A[x, y-1]>val)
2. If A[x-1 , y]> A[ x, y-1]
3. Swap A[x-1,y] val
4. x x-1
5. Else Swap A[x,y-1] val
6. y y-1

This Algorithm inserts a new element in tableau on O(m+n) time because


there is one call to MAKE-TABLEAU and in worst case the number of
comparisons does not exceed m+n, and does a constant amount of work at
each comparison.
Therefore the running time is O(m+n).

c)

2 3 5 14
4 6 9 16
7 8 12 ∞
∞ ∞ ∞ ∞

Inserting value 10
2 3 5 14 2 3 5 14
4 6 9 16 4 6 9 16
7 8 12 ∞ 7 8 12 10
∞ ∞ ∞ 10 ∞ ∞ ∞ ∞

2 3 5 14 2 3 5 10
4 6 9 10 4 6 9 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

EXTARCT-MIN

2 3 5 10 ∞ 3 5 10
4 6 9 14 4 6 9 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

∞ 3 5 10 3 ∞ 5 10
4 6 9 14 4 6 9 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

3 5 ∞ 10 3 5 9 10
4 6 9 14 4 6 ∞ 14
7 8 12 16 7 8 12 16
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
3 5 9 10 3 5 9 10
4 6 12 14 4 6 12 14
7 8 ∞ 16 7 8 16 ∞
∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

Q_02:

Iterative version of RANDOMIZED-SELECT Algorithm:

RANDOMIZED-SELECT (A, p, r, i)

1. For j 1 to length[A]
2. If p = r
3. Then return A[p]
4. q RANDOMIZED-PARTITION(A,p,r)
5. If i= q
6. Then return A[q]
7. Else if i> q
8. Then p q+1
9. Else
10. r q-1

Q_03:

Sol:
a).
n
Maximum Number of inversions: C2 =n(n-1)/2
Order of list: Descending ordered list
Because in the descending ordered list for every i<k ai > ak
Therefore every pair is an inversion.
b).
1.Insertion Sort:
In K-sorted array the maximum number of inversions are nk/2.
Therefore using insertion sort the outer loop will run n-times and the inner loop
will run k-times at maximum for each iteration of outer loop.
Therefore the Running time of insertion sort on k-sorted array is
T=O(nk).

ii. Merge-Sort:

In case of Merge-sort the Running time will be O(nlog n) irrespective of the order
of array(whether k-sorted or not).Because it divides the input array into two parts
recursively and then merges in a sorted order.

iii. Quick-Sort:

In case of Quick sort the worst case of k-sorted array is that it divides the array
into two parts containing n-k and k elements recursively considering the n-kth as
pivot. Therefore the recursion of quick sort in this case becomes
T(n)=T(n-k)+Ө(n)

Which results in the running time= Ө(n2). {K is a constant}

Bonus Part:

This Algorithm is based on heap concepts.We have a separate area of length k to hold the heap.
We arrange copies of the first k elements in the heap. The idea is for each successive index,
i=0,...,n-k-1, we write the top of the heap into array[i] and add array[i+k] to the heap, all of this
being done in such a manner that the heap property is preserved.

The way to do this is to remove the top element, sift up, put the new entry into the newly vacated
bottom row position, and then sift it up into place. When there are no more new elements we sift
out the remaining elements using the standard heapsort. This also is a n*log(k) algorithm.
NUST-SEECS
ASSIGNMENT : 02

SUBJECT: Introduction to Computing Algorithm

SUBMITTED TO: Sir Akbar Mehdi

Submitted By:

Group members: Reg #

Muhammad Idris 132

M.Shoaib khan 126

Asif Ali 05

Section: BIT-10B

Date: 05/04/2010

You might also like