GATE Overflow - Recent questions in Algorithms
https://gateoverflow.in/questions/algorithms
Powered by Question2AnswerCormen Edition 3 Exercise 12.1 Question 5 (Page No. 289)
https://gateoverflow.in/326595/cormen-edition-3-exercise-12-1-question-5-page-no-289
Argue that since sorting $n$ elements takes $\Omega (n\ lgn)$ time in the worst case in the comparison model, any comparison-based algorithm for constructing a $BST$ from an arbitrary list of n elements takes $\Omega (n\ lgn)$ time in the worst case.Algorithmshttps://gateoverflow.in/326595/cormen-edition-3-exercise-12-1-question-5-page-no-289Wed, 20 Nov 2019 06:26:19 +0000Cormen Edition 3 Exercise 22.2 Question 8 (Page No. 539)
https://gateoverflow.in/326019/cormen-edition-3-exercise-22-2-question-8-page-no-539
<p><span style="font-size: larger;">The diameter of a tree $T= (V, E)$ is defined as $max_{u,v\ \epsilon\ V}\ \delta(u,v)$, that is, the largest of all shortest-path distances in the tree. Give an efficient algorithm to compute the diameter of a tree, and analyze the running time of your algorithm.</span></p>Algorithmshttps://gateoverflow.in/326019/cormen-edition-3-exercise-22-2-question-8-page-no-539Tue, 12 Nov 2019 15:45:00 +0000Cormen Edition 3 Exercise 22.2 Question 7 (Page No. 539)
https://gateoverflow.in/326018/cormen-edition-3-exercise-22-2-question-7-page-no-539
<p><span style="font-size: larger;">There are two types of professional wrestlers: “babyfaces” (“good guys”) and “heels” (“bad guys”). Between any pair of professional wrestlers, there may or may not be a rivalry. Suppose we have n professional wrestlers and we have a list of r pairs of wrestlers for which there are rivalries. Give an $O(n+r)$ time algorithm that determines whether it is possible to designate some of the wrestlers as babyfaces and the remainder as heels such that each rivalry is between a babyface and a heel. If it is possible to perform such a designation, your algorithm should produce it.</span></p>Algorithmshttps://gateoverflow.in/326018/cormen-edition-3-exercise-22-2-question-7-page-no-539Tue, 12 Nov 2019 15:38:50 +0000Cormen Edition 3 Exercise 22.2 Question 6 (Page No. 539)
https://gateoverflow.in/326017/cormen-edition-3-exercise-22-2-question-6-page-no-539
Give an example of a directed graph $G=(V, E)$, a source vertex $s\ \epsilon\ V$ , and a set of tree edges $E_{\Pi}\subseteq E$ such that for each vertex $v\ \epsilon\ V$ , the unique simple path in the graph $(V, E_{\Pi})$ from s to v is a shortest path in G, yet the set of edges $E_{\Pi}$ cannot be produced by running BFS on G, no matter how the vertices are ordered in each adjacency list.Algorithmshttps://gateoverflow.in/326017/cormen-edition-3-exercise-22-2-question-6-page-no-539Tue, 12 Nov 2019 15:32:39 +0000Understanding Np-hard
https://gateoverflow.in/321555/understanding-np-hard
I am having difficulty in understanding np and np-hard topic in algorithms. If someone can provide some good source to learn about it in easy manner it would be a real help.<br />
<br />
Thank you!Algorithmshttps://gateoverflow.in/321555/understanding-np-hardSun, 22 Sep 2019 00:20:20 +0000By own
https://gateoverflow.in/321547/by-own
<hr>
<p>$if ...F(n)=O(g(n)) Then ...it is true or false 2^{f(n)}=O(2^{g(n)}) Answer with reason$</p>Algorithmshttps://gateoverflow.in/321547/by-ownSat, 21 Sep 2019 21:39:36 +0000From asymptotic notation
https://gateoverflow.in/321544/from-asymptotic-notation
F(n)=O{ [ f(n)]^2}<br />
<br />
This statement is true or false <br />
<br />
With reason..Algorithmshttps://gateoverflow.in/321544/from-asymptotic-notationSat, 21 Sep 2019 21:04:36 +0000CMI2019-B-6
https://gateoverflow.in/320517/cmi2019-b-6
<p>Let $A$ be an $n\times n $ matrix of integers such that each row and each column is arranged in ascending order. We want to check whether a number $k$ appears in $A.$ If $k$ is present, we should report its position - that is, the row $i$ and column $j$ such that $A(i,j)=k.$ Otherwise, we should declare that $k$ is not present in $A.$</p>
<ol start="1" style="list-style-type:upper-alpha">
<li>Describe an algorithm that solves this problem in time $O(n\log n).$ Justify the complexity of your algorithm.</li>
<li>Describe an algorithm that solves this problem by examining at most $2n$ values in $A.$ Justify the complexity of your algorithm.</li>
<li>For both algorithms, describe a worst-case input where $k$ is present in $A.$</li>
</ol>Algorithmshttps://gateoverflow.in/320517/cmi2019-b-6Fri, 13 Sep 2019 14:36:03 +0000CMI2019-B-7
https://gateoverflow.in/320516/cmi2019-b-7
<p>A college professor gives several quizzes during the semester, with negative marking. He has become bored of the usual "Best $M$ out of $N$ quizzes" formula to award marks for internal assessment. Instead, each student will be evaluated based on the sum of the best contiguous segment (i.e., no gaps) of marks in the overall sequence of quizzes. However, the student is allowed to drop upto $K$ quizzes before calculating this sum.</p>
<p>Suppose a student has scored the following marks in $10$ quizzes during the semester.</p>
<p>$$\begin{array}{|c|c|c|c|c|}\hline \text{Quiz} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\\hline \text{Marks } & 6 & -5 & 3 & -7 & 6 & -1 & 10 & -8 & -8 & 8 \\ \hline \end{array}$$</p>
<p>Without dropping any quizzes, the best segment is quiz $5-7$, which yields a total of $15$ marks. If the student is allowed to drop upto $2$ quizzes in a segment, the best segment is quiz $1-7$, which yields a total of $24$ marks after dropping quizzes $2$ and $4$. If the student is allowed to drop upto $6$ quizzes in a segment, the best total is obtained by taking the entire list and dropping all $5$ negative entries, yielding $33$ marks.</p>
<p>For $1\leq i<N, 0\leq j\leq K,$ let $B[i][j]$ denote the maximum sum segment ending at position $i$ with at most $j$ marks dropped.</p>
<ol start="1" style="list-style-type:upper-alpha">
<li>Write a recursive formula for $B[i][j].$</li>
<li>Explain how to calculate, using dynamic programming, the score the professor needs to award each student.</li>
<li>Describe the space and time complexity of your dynamic programming algorithm.</li>
</ol>Algorithmshttps://gateoverflow.in/320516/cmi2019-b-7Fri, 13 Sep 2019 14:36:03 +0000QUICK SORT- SELF DOUBT
https://gateoverflow.in/319659/quick-sort-self-doubt
<p>In quick sort for sorting of n Numbers, the 75th greatest Element is selected as pivot using $O(n^2)$ time complexity algorithm than what is the worst case time complexity of quick sort.</p>
<ol>
<li>O($n^2$)</li>
<li>O($n^3$)</li>
<li>O(nlogn)</li>
<li>O(n)</li>
</ol>Algorithmshttps://gateoverflow.in/319659/quick-sort-self-doubtMon, 02 Sep 2019 17:04:32 +0000IIIT BLR TEST 1 : ALGORITHMS 3
https://gateoverflow.in/319407/iiit-blr-test-1-algorithms-3
Given an array of ( both positive and negative ) integers, $a_0,a_1,….a_{n-1}$ and $l, 1<l<n$.<br />
<br />
Design a linear time algorithm to compute the maximum product subarray, whose length is atmost $l$.Algorithmshttps://gateoverflow.in/319407/iiit-blr-test-1-algorithms-3Tue, 27 Aug 2019 12:25:12 +0000IIIT BLR TEST 1 : ALGORITHMS 2
https://gateoverflow.in/319406/iiit-blr-test-1-algorithms-2
<p>A 3 way (ternary) min heap is a 3 way ( ternary – each node as atmost three children nodes, left, mid, right ) complete tree with min heap property ( value of the parent is less than the value of the children ) satisfied at every node. Given a ternary heap in the array representation.</p>
<p>(a) Write the following functions.</p>
<ol>
<li>Parent(i) // returns the index of the parent of i</li>
<li>Leftchild(i) // returns the index of the left child of i</li>
<li>Middlechild(i) // returns the index of the Middle child of i</li>
<li>Rightchild(i) // returns the index of the right child of i</li>
</ol>
<p>(b) Write the pseudocode for the topdownheapify function.</p>
<p>(c) In Heapsort, binary heap is preferred over ternary heap. State if this statement is true or false, you must justify your answer.</p>Algorithmshttps://gateoverflow.in/319406/iiit-blr-test-1-algorithms-2Tue, 27 Aug 2019 12:22:00 +0000IIIT BLR TEST 1 : ALGORITHMS 1
https://gateoverflow.in/319404/iiit-blr-test-1-algorithms-1
<p>Solve the following recursions ( in terms of Θ ).</p>
<p>T(0) = T(1) = Θ(1) in all of the following.</p>
<ol>
<li>$T(n) = n + \frac{1}{n}\sum_{i=0}^{i=n-1}T(i)$</li>
<li>$T(n) = n + \frac{2}{n}\sum_{i=0}^{i=n-1}T(i)$</li>
<li>$T(n) = n + \frac{4}{n}\sum_{i=0}^{i=n/2}T(i)$</li>
<li>$T(n) = n + \frac{40}{n}\sum_{i=0}^{i=n/5}T(i)$</li>
</ol>Algorithmshttps://gateoverflow.in/319404/iiit-blr-test-1-algorithms-1Tue, 27 Aug 2019 12:14:29 +0000UGCNET-June-2019-II-61
https://gateoverflow.in/316218/ugcnet-june-2019-ii-61
<p>Match List-I with List-II:</p>
<p>$$\begin{array}{|c|c|c|c|} \hline {} & \text{List-I} & {} & \text{List-II} \\ \hline (a) & \text{Prim’s algorithm} & (i) & O(V^3 \log V) \\ \hline (b) & \text{Dijkstra’s algorithm} & (ii) & O(VE^2) \\ \hline (c) & \text{Faster all-pairs shortest path} & (iii) & O(ElgV) \\ \hline (d) & \text{Edmonds-Karp algorithm} & (iv) & O(V^2) \\ \hline \end{array}$$</p>
<p>Choose the correct option from those options given below:</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>(a) – (ii); (b)-(iv); (c)-(i); (d)-(iii)</li>
<li>(a) – (iii); (b)-(iv); (c)-(i); (d)-(ii)</li>
<li>(a) – (ii); (b)-(i); (c)-(iv); (d)-(iii)</li>
<li>(a) – (iii); (b)-(i); (c)-(iv); (d)-(ii)</li>
</ol>Algorithmshttps://gateoverflow.in/316218/ugcnet-june-2019-ii-61Tue, 02 Jul 2019 13:45:36 +0000UGCNET-June-2019-II-62
https://gateoverflow.in/316217/ugcnet-june-2019-ii-62
<p>There are many sorting algorithms based on comparison. The running time of heapsort algorithm is $O(n \text{lg}n)$. Like $P$, but unlike $Q$, heapsort sorts in place where $(P,Q)$ is equal to</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>Merge sort, Quick sort</li>
<li>Quick sort, insertion sort</li>
<li>Insertion sort, Quick sort</li>
<li>Insertion sort, Merge sort</li>
</ol>Algorithmshttps://gateoverflow.in/316217/ugcnet-june-2019-ii-62Tue, 02 Jul 2019 13:45:35 +0000UGCNET-June-2019-II-64
https://gateoverflow.in/316215/ugcnet-june-2019-ii-64
<p>Which of the following is best running time to sort $n$ integers in the range $0$ to $n^2-1$?</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>$O(\text{lg } n)$</li>
<li>$O(n)$</li>
<li>$O(n\text { lg }n)$</li>
<li>$O(n^2)$</li>
</ol>Algorithmshttps://gateoverflow.in/316215/ugcnet-june-2019-ii-64Tue, 02 Jul 2019 13:45:34 +0000UGCNET-June-2019-II-65
https://gateoverflow.in/316214/ugcnet-june-2019-ii-65
<p>Which of the following is application of depth-first search?</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>Only topological sort</li>
<li>Only strongly connected components</li>
<li>Both topological sort and strongly connected components</li>
<li>Neither topological sort nor strongly connected components</li>
</ol>Algorithmshttps://gateoverflow.in/316214/ugcnet-june-2019-ii-65Tue, 02 Jul 2019 13:45:34 +0000UGCNET-June-2019-II-66
https://gateoverflow.in/316213/ugcnet-june-2019-ii-66
<p>Consider double hashing of the form</p>
<p>$h(k,i)=(h_1(k)+ih_2(k)) \text{mod m}$ where $h_{1}(k) = \text{k mod m} \ , \ \ h_{2}(k)=1+(\text{k mod n})$ where $n=m-1$ and $m=701$. For $k=123456$, what is the difference between first and second probes in terms of slots?</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>$255$</li>
<li>$256$</li>
<li>$257$</li>
<li>$258$</li>
</ol>Algorithmshttps://gateoverflow.in/316213/ugcnet-june-2019-ii-66Tue, 02 Jul 2019 13:45:34 +0000UGCNET-June-2019-II-67
https://gateoverflow.in/316212/ugcnet-june-2019-ii-67
<p>Consider the complexity class $CO-NP$ as the set of languages $L$ such that $\overline{L} \in NP$, and the following two statements:</p>
<p>$S_1: \: P \subseteq CO-NP$</p>
<p>$S_2: \: \text{ If } NP \neq CO-NP, \text{ then } P \neq NP$</p>
<p>Which of the following is/are correct?</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>Only $S_1$</li>
<li>Only $S_2$</li>
<li>Both $S_1$ and $S_2$</li>
<li>Neither $S_1$ nor $S_2$</li>
</ol>Algorithmshttps://gateoverflow.in/316212/ugcnet-june-2019-ii-67Tue, 02 Jul 2019 13:45:33 +0000UGCNET-June-2019-II-68
https://gateoverflow.in/316211/ugcnet-june-2019-ii-68
<p>Consider the following steps:</p>
<p>$S_1$: Characterize the structure of an optimal solution</p>
<p>$S_2$: Compute the value of an optimal solution in bottom-up fashion</p>
<p>Which of the following step(s) is/are common to both dynamic programming and greedy algorithms?</p>
<ol style="list-style-type:upper-alpha" type="A">
<li>Only $S_1$</li>
<li>Only $S_2$</li>
<li>Both $S_1$ and $S_2$</li>
<li>Neither $S_1$ nor $S_2$</li>
</ol>Algorithmshttps://gateoverflow.in/316211/ugcnet-june-2019-ii-68Tue, 02 Jul 2019 13:45:33 +0000Cormen Edition 3 Exercise 10.2 Question 8 (Page No. 241)
https://gateoverflow.in/315987/cormen-edition-3-exercise-10-2-question-8-page-no-241
Explain how to implement doubly linked lists using only one pointer value $x.np$ per item instead of the usual two (next and prev). Assume that all pointer values can be interpreted as $k$-bit integers, and define $x.np$ to be $x.np=x.next$ $XOR$ $x.prev$, the $k$-bit “exclusive-or” of $x.next$ and $x.prev$.(The value $NIL$ is represented by $0$.)Be sure to describe what information you need to access the head of the list. Show how to implement the $SEARCH$, $INSERT$, and $DELETE$ operations on such a list. Also, show how to reverse such a list in $O(1)$ time.Algorithmshttps://gateoverflow.in/315987/cormen-edition-3-exercise-10-2-question-8-page-no-241Sun, 30 Jun 2019 03:35:49 +0000Cormen Edition 3 Exercise 10.2 Question 7 (Page No. 241)
https://gateoverflow.in/315986/cormen-edition-3-exercise-10-2-question-7-page-no-241
Give a $\Theta(n)$ time nonrecursive procedure that reverses a singly linked list of $n$ elements. The procedure should use no more than constant storage beyond that needed for the list itself.Algorithmshttps://gateoverflow.in/315986/cormen-edition-3-exercise-10-2-question-7-page-no-241Sun, 30 Jun 2019 03:26:24 +0000Cormen Edition 3 Exercise 10.2 Question 6 (Page No. 241)
https://gateoverflow.in/315985/cormen-edition-3-exercise-10-2-question-6-page-no-241
The dynamic-set operation $UNION$ takes two disjoint sets $S_1$ and $S_2$ as input, and it returns a set $S=S_1 \cup S_2$ consisting of all the elements of $S_1$ and $S_2$.The sets $S_1$ and $S_2$ are usually destroyed by the operation. Show how to support $UNION$ in $O(1)$ time using a suitable list data structure.Algorithmshttps://gateoverflow.in/315985/cormen-edition-3-exercise-10-2-question-6-page-no-241Sun, 30 Jun 2019 03:23:34 +0000Cormen Edition 3 Exercise 10.2 Question 5 (Page No. 240)
https://gateoverflow.in/315984/cormen-edition-3-exercise-10-2-question-5-page-no-240
Implement the dictionary operations $INSERT$, $DELETE$, and $SEARCH$ using singly linked, circular lists. What are the running times of your procedures?Algorithmshttps://gateoverflow.in/315984/cormen-edition-3-exercise-10-2-question-5-page-no-240Sun, 30 Jun 2019 03:18:53 +0000Cormen Edition 3 Exercise 10.2 Question 4 (Page No. 240)
https://gateoverflow.in/315983/cormen-edition-3-exercise-10-2-question-4-page-no-240
<pre>
LIST-SEARCH’(L, k)
1 x = L.nil.next
2 while x != L.nil and x.key != k
3 x = x.next
4 return x</pre>
<p>As written, each loop iteration in the LIST-SEARCH’ procedure requires two tests: one for $x\neq L.nil$ and one for $x.key\neq k$. Show how to eliminate the test for $x\neq L.nil$ in each iteration.</p>Algorithmshttps://gateoverflow.in/315983/cormen-edition-3-exercise-10-2-question-4-page-no-240Sun, 30 Jun 2019 03:17:24 +0000Cormen Edition 3 Exercise 10.2 Question 3 (Page No. 240)
https://gateoverflow.in/315981/cormen-edition-3-exercise-10-2-question-3-page-no-240
Implement a queue by a singly linked list $L$. The operations of $ENQUEUE$ and $DEQUEUE$ should still take $O(1)$ time.Algorithmshttps://gateoverflow.in/315981/cormen-edition-3-exercise-10-2-question-3-page-no-240Sun, 30 Jun 2019 03:09:27 +0000Cormen Edition 3 Exercise 10.2 Question 2 (Page No. 240)
https://gateoverflow.in/315980/cormen-edition-3-exercise-10-2-question-2-page-no-240
Implement a stack using a singly linked list $L$. The operations $PUSH$ and $POP$ should still take $O(1)$ time.Algorithmshttps://gateoverflow.in/315980/cormen-edition-3-exercise-10-2-question-2-page-no-240Sun, 30 Jun 2019 03:02:18 +0000Cormen Edition 3 Exercise 10.2 Question 1 (Page No. 240)
https://gateoverflow.in/315979/cormen-edition-3-exercise-10-2-question-1-page-no-240
Can you implement the dynamic-set operation $INSERT$ on a singly linked list in $O(1)$ time? How about $DELETE$?Algorithmshttps://gateoverflow.in/315979/cormen-edition-3-exercise-10-2-question-1-page-no-240Sun, 30 Jun 2019 03:00:03 +0000Cormen Edition 3 Exercise 10.1 Question 7 (Page No. 236)
https://gateoverflow.in/315862/cormen-edition-3-exercise-10-1-question-7-page-no-236
Show how to implement a stack using two queues. Analyze the running time of the stack operations.Algorithmshttps://gateoverflow.in/315862/cormen-edition-3-exercise-10-1-question-7-page-no-236Fri, 28 Jun 2019 10:58:11 +0000Cormen Edition 3 Exercise 10.1 Question 6 (Page No. 236)
https://gateoverflow.in/315861/cormen-edition-3-exercise-10-1-question-6-page-no-236
Show how to implement a queue using two stacks. Analyze the running time of the queue operations.Algorithmshttps://gateoverflow.in/315861/cormen-edition-3-exercise-10-1-question-6-page-no-236Fri, 28 Jun 2019 10:57:02 +0000Cormen Edition 3 Exercise 10.1 Question 5 (Page No. 236)
https://gateoverflow.in/315860/cormen-edition-3-exercise-10-1-question-5-page-no-236
Whereas a stack allows insertion and deletion of elements at only one end, and a queue allows insertion at one end and deletion at the other end, a deque (double ended queue) allows insertion and deletion at both ends. Write four $O(1)$ time procedures to insert elements into and delete elements from both ends of a deque implemented by an array.Algorithmshttps://gateoverflow.in/315860/cormen-edition-3-exercise-10-1-question-5-page-no-236Fri, 28 Jun 2019 10:55:50 +0000Cormen Edition 3 Exercise 10.1 Question 4 (Page No. 235)
https://gateoverflow.in/315859/cormen-edition-3-exercise-10-1-question-4-page-no-235
Rewrite ENQUEUE and DEQUEUE to detect underflow and overflow of a queue.Algorithmshttps://gateoverflow.in/315859/cormen-edition-3-exercise-10-1-question-4-page-no-235Fri, 28 Jun 2019 10:52:53 +0000Cormen Edition 3 Exercise 10.1 Question 3 (Page No. 235)
https://gateoverflow.in/315854/cormen-edition-3-exercise-10-1-question-3-page-no-235
<pre>
ENQUEUE(Q, x)
1 Q[Q.tail] = x
2 if Q.tail == Q.length
3 Q.tail = 1
4 else Q.tail = Q.tail + 1
DEQUEUE(Q)
1 x = Q[Q.head]
2 if Q.head == Q.length
3 Q.head = 1
4 else Q.head = Q.head + 1
5 return x</pre>
<p>illustrate the result of each operation in the sequence ENQUEUE(Q,4),ENQUEUE(Q,1),ENQUEUE(Q,3),DEQUEUE(Q),ENQUEUE(Q,8),DEQUEUE(Q) on an initially empty queue $Q$ stored in array $Q[1...6]$.</p>Algorithmshttps://gateoverflow.in/315854/cormen-edition-3-exercise-10-1-question-3-page-no-235Fri, 28 Jun 2019 10:06:39 +0000Cormen Edition 3 Exercise 10.1 Question 2 (Page No. 235)
https://gateoverflow.in/315850/cormen-edition-3-exercise-10-1-question-2-page-no-235
Explain how to implement two stacks in one array $A[1...n]$ in such a way that neither stack overflows unless the total number of elements in both stacks together is $n$.The $PUSH$ and $POP$ operations should run in $O(1)$ time.Algorithmshttps://gateoverflow.in/315850/cormen-edition-3-exercise-10-1-question-2-page-no-235Fri, 28 Jun 2019 09:51:25 +0000Cormen Edition 3 Exercise 10.1 Question 1 (Page No. 235)
https://gateoverflow.in/315848/cormen-edition-3-exercise-10-1-question-1-page-no-235
<pre>
STACK-EMPTY(S)
1 if S.top == 0
2 return TRUE
3 else return FALSE
PUSH(S , x)
1 S.top = S.top + 1
2 S[S.top] = x
POP(S)
1 if STACK-EMPTY(S)
2 error “underflow”
3 else S.top = S.top – 1
4 return S[S.top + 1]</pre>
<p>illustrate the result of each operation in the sequence $PUSH(S,4), PUSH(S,1),PUSH(S,3),POP(S),PUSH(S,8),POP(S)$ on an initially empty stack $S$ stored in array $S[1...6]$</p>Algorithmshttps://gateoverflow.in/315848/cormen-edition-3-exercise-10-1-question-1-page-no-235Fri, 28 Jun 2019 09:47:59 +0000Cormen Edition 3 Exercise 9.1 Question 2 (Page No. 215)
https://gateoverflow.in/315847/cormen-edition-3-exercise-9-1-question-2-page-no-215
Prove the lower bound of $\lceil 3n/2\rceil – 2$ comparisons in the worst case to find both the maximum and minimum of $n$ numbers. (Hint: Consider how many numbers are potentially either the maximum or minimum and investigate how a comparison affects these counts.)Algorithmshttps://gateoverflow.in/315847/cormen-edition-3-exercise-9-1-question-2-page-no-215Fri, 28 Jun 2019 09:30:15 +0000Cormen Edition 3 Exercise 9.1 Question 1 (Page No. 215)
https://gateoverflow.in/315846/cormen-edition-3-exercise-9-1-question-1-page-no-215
Show that the second smallest of $n$ elements can be found with $n+\lceil lg\ n \rceil -2$ comparisons in the worst case. (Hint: Also find the smallest element.)Algorithmshttps://gateoverflow.in/315846/cormen-edition-3-exercise-9-1-question-1-page-no-215Fri, 28 Jun 2019 09:27:27 +0000Cormen Edition 3 Exercise 8.4 Question 5 (Page No. 204)
https://gateoverflow.in/315843/cormen-edition-3-exercise-8-4-question-5-page-no-204
A probability distribution function $P(x)$ for a random variable $X$ is defined by $P(x) =Pr\{X\leq x\}$.Suppose that we draw a list of $n$ random variables $X_1,X_2,…,X_n$ from a continuous probability distribution function $P$ that is computable in $O(1)$ time. Give an algorithm that sorts these numbers in linear averagecase time.Algorithmshttps://gateoverflow.in/315843/cormen-edition-3-exercise-8-4-question-5-page-no-204Fri, 28 Jun 2019 08:05:15 +0000Cormen Edition 3 Exercise 8.4 Question 4 (Page No. 204)
https://gateoverflow.in/315841/cormen-edition-3-exercise-8-4-question-4-page-no-204
<p>We are given $n$ points in the unit circle, $P_i=(x_i,y_i)$, such that $0<x_i^2+y_i^2<1$ for $i=1,2,….,n$.Suppose that the points are uniformly distributed; that is, the probability of finding a point in any region of the circle is proportional to the area of that region. Design an algorithm with an average-case running time of $\Theta(n)$ to sort the $n$ points by their distances $d_i=\sqrt{x_i^2+y_i^2}$ from the origin. (Hint: Design the bucket sizes in <a href="https://gateoverflow.in/315837/cormen-edition-3-exercise-8-4-question-1-page-no-204" rel="nofollow">BUCKET-SORT</a> to reflect the uniform distribution of the points in the unit circle.)</p>Algorithmshttps://gateoverflow.in/315841/cormen-edition-3-exercise-8-4-question-4-page-no-204Fri, 28 Jun 2019 07:59:49 +0000Cormen Edition 3 Exercise 8.4 Question 3 (Page No. 204)
https://gateoverflow.in/315839/cormen-edition-3-exercise-8-4-question-3-page-no-204
Let $X$ be a random variable that is equal to the number of heads in two flips of a fair coin. What is $E[X^2]$? What is $E^2[X]$?Algorithmshttps://gateoverflow.in/315839/cormen-edition-3-exercise-8-4-question-3-page-no-204Fri, 28 Jun 2019 07:53:23 +0000Cormen Edition 3 Exercise 8.4 Question 2 (Page No. 204)
https://gateoverflow.in/315838/cormen-edition-3-exercise-8-4-question-2-page-no-204
Explain why the worst-case running time for bucket sort is $\Theta(n^2)$. What simple change to the algorithm preserves its linear average-case running time and makes its worst-case running time $O(n\ lg\ n)$?Algorithmshttps://gateoverflow.in/315838/cormen-edition-3-exercise-8-4-question-2-page-no-204Fri, 28 Jun 2019 07:47:31 +0000Cormen Edition 3 Exercise 8.4 Question 1 (Page No. 204)
https://gateoverflow.in/315837/cormen-edition-3-exercise-8-4-question-1-page-no-204
<pre>
BUCKET-SORT(A)
1 let B[0...n-1] be a new array
2 n = A.length
3 for i – 0 to n – 1
4 make B[i] an empty list
5 for i = 1 to n
6 insert A[i] into list B[nA[i]]
7 for i = 0 to n – 1
8 sort list B[i] with insertion sort
9 concatenate the lists B[0] , B[1] , ….,B[n-1] together in order</pre>
<p>illustrate the operation of BUCKET-SORT on the array $A=\langle .79,.13,.16,.64,.39,.20,.89,.53,.71,.42\rangle$</p>Algorithmshttps://gateoverflow.in/315837/cormen-edition-3-exercise-8-4-question-1-page-no-204Fri, 28 Jun 2019 07:44:52 +0000Cormen Edition 3 Exercise 8.3 Question 4 (Page No. 200)
https://gateoverflow.in/315833/cormen-edition-3-exercise-8-3-question-4-page-no-200
Show how to sort $n$ integers in the range $0$ to $n^3-1$ in $O(n)$ time.Algorithmshttps://gateoverflow.in/315833/cormen-edition-3-exercise-8-3-question-4-page-no-200Fri, 28 Jun 2019 07:10:52 +0000Cormen Edition 3 Exercise 8.3 Question 3 (Page No. 200)
https://gateoverflow.in/315832/cormen-edition-3-exercise-8-3-question-3-page-no-200
Use induction to prove that radix sort works. Where does your proof need the assumption that the intermediate sort is stable?Algorithmshttps://gateoverflow.in/315832/cormen-edition-3-exercise-8-3-question-3-page-no-200Fri, 28 Jun 2019 07:06:40 +0000Cormen Edition 3 Exercise 8.3 Question 2 (Page No. 200)
https://gateoverflow.in/315831/cormen-edition-3-exercise-8-3-question-2-page-no-200
Which of the following sorting algorithms are stable: insertion sort, merge sort, heapsort, and quicksort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?Algorithmshttps://gateoverflow.in/315831/cormen-edition-3-exercise-8-3-question-2-page-no-200Fri, 28 Jun 2019 07:05:52 +0000Cormen Edition 3 Exercise 8.3 Question 1 (Page No. 199)
https://gateoverflow.in/315829/cormen-edition-3-exercise-8-3-question-1-page-no-199
<pre>
RADIX-SORT(A, d)
1 for i = 1 to d
2 use a stable sort to sort array A on digit i</pre>
<p> </p>
<p>illustrate the operation of RADIX-SORT on the following list of English words: COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX.</p>Algorithmshttps://gateoverflow.in/315829/cormen-edition-3-exercise-8-3-question-1-page-no-199Fri, 28 Jun 2019 06:59:59 +0000Cormen Edition 3 Exercise 8.2 Question 4 (Page No. 197)
https://gateoverflow.in/315823/cormen-edition-3-exercise-8-2-question-4-page-no-197
Describe an algorithm that, given $n$ integers in the range $0$ to $k$ preprocesses its input and then answers any query about how many of the $n$ integers fall into the range $[a..b]$ in $O(1)$ time.Your algorithm should use $\Theta(n+k)$ preprocessing time.Algorithmshttps://gateoverflow.in/315823/cormen-edition-3-exercise-8-2-question-4-page-no-197Fri, 28 Jun 2019 03:38:49 +0000Cormen Edition 3 Exercise 8.2 Question 3 (Page No. 196)
https://gateoverflow.in/315822/cormen-edition-3-exercise-8-2-question-3-page-no-196
<p>Suppose that we were to rewrite the for loop header in line $10$ of the <a href="https://gateoverflow.in/315819/cormen-edition-3-exercise-8-2-question-1-page-no-196" rel="nofollow">COUNTINGSORT</a> as </p>
<pre>
10 for j = 1 to A.length</pre>
<p>Show that the algorithm still works properly. Is the modified algorithm stable?</p>Algorithmshttps://gateoverflow.in/315822/cormen-edition-3-exercise-8-2-question-3-page-no-196Fri, 28 Jun 2019 03:34:07 +0000Cormen Edition 3 Exercise 8.2 Question 2 (Page No. 196)
https://gateoverflow.in/315820/cormen-edition-3-exercise-8-2-question-2-page-no-196
<p>Prove that <a rel="nofollow" href="https://gateoverflow.in/315819/cormen-edition-3-exercise-8-2-question-1-page-no-196">COUNTING-SORT</a> is stable.</p>Algorithmshttps://gateoverflow.in/315820/cormen-edition-3-exercise-8-2-question-2-page-no-196Fri, 28 Jun 2019 03:30:23 +0000Cormen Edition 3 Exercise 8.2 Question 1 (Page No. 196)
https://gateoverflow.in/315819/cormen-edition-3-exercise-8-2-question-1-page-no-196
<pre>
COUNTING-SORT(A, B, k)
1 let C[0,…,k] be a new array
2 for i = 0 to k
3 C[i] = 0
4 for j = 1 to A.length
5 C[A[j]] = C[A[j]] + 1
6 // C[i] now contains the number of elements equal to i .
7 for i =1 to k
8 C[i] = C[i] + C[i-1]
9 // C[i] now contains the number of elements less than or equal to i .
10 for j = A.length downto 1
11 B[C[A[j]]] = A[j]
12 C[A[j]] = C[A[j]] – 1</pre>
<p>illustrate the operation of COUNTING-SORT on the array $A=\langle 6,0,2,0,1,3,4,6,1,3,2 \rangle $</p>Algorithmshttps://gateoverflow.in/315819/cormen-edition-3-exercise-8-2-question-1-page-no-196Fri, 28 Jun 2019 03:28:17 +0000