GATE CS Algorithms questions
Practise Algorithms questions for GATE CS. Explore official PYQs and original subject-wise quizzes, check answers and view available solutions. Free, no sign-in.
Compare greedy choices, dynamic-programming states and graph algorithms. Identify the input size before analysing the time and space complexity of each solution.
Subject, topic, year and type filter the full library. Match any choice within a filter, and all selected filter categories. Search, source and sort apply to this page.
20 on this page
Let and be two vertices in a undirected graph having distinct positive edge weights. Let be a partition of such that and . Consider the edge having the minimum weight amongst all those edges that have one vertex in and one vertex in . Let the weight of an edge denote the …
We are given tasks . The execution of each task requires one unit of time. We can execute one task at a time. Each task has a profit and a deadline . Profit is earned if the task is completed before the end of the unit of time. …
Let and be two vertices in a undirected graph having distinct positive edge weights. Let be a partition of such that and . Consider the edge having the minimum weight amongst all those edges that have one vertex in and one vertex in . The edge must definitely belong to:
double foo(int n) { int i; double sum; if(n == 0) { return 1.0; } else { sum = 0.0; for(i = 0; i < n; i++) { sum += foo(i); } return sum; } } The space complexity of the above code is?
Let be an undirected graph with positive edge weights. Dijkstra’s single source shortest path algorithm can be implemented using the binary heap data structure with time complexity:
Suppose , Which one of the following is FALSE?
Consider the following C-program: void foo (int n, int sum) { int k = 0, j = 0; if (n == 0) return; k = n % 10; j = n/10; sum = sum + k; foo (j, sum); printf ("%d,",k); } int main() { int a = 2048, sum = 0; foo(a, sum); printf("%d\n", sum); } What does the above program print?
An undirected graph has nodes. its adjacency matrix is given by an square matrix whose (i) diagonal elements are 0’s and (ii) non-diagonal elements are 1’s. Which one of the following is TRUE?
Suppose there are sorted lists of elements each. The time complexity of producing a sorted list of all these elements is: (Hint:Use a heap data structure)
The recurrence equation evaluates to
Let be an array storing a bit ( or ) at each location, and is a function whose time complexity is . Consider the following program fragment written in a C like language: counter = 0; for (i=1; i<=n; i++) { if (a[i] == 1) counter++; else {f (counter); counter = 0;} } The complexity of this program fragment …
The time complexity of the following C function is (assume ) int recursive (int n) { if(n == 1) return (1); else return (recursive (n-1) + recursive (n-1)); }
Suppose we run Dijkstra’s single source shortest path algorithm on the following edge-weighted directed graph with vertex as the source. In what order do the nodes get included into the set of vertices for which the shortest path distances are finalized?
Consider the following C program main() { int x, y, m, n; scanf("%d %d", &x, &y); /* Assume x>0 and y>0*/ m = x; n = y; while(m != n) { if (m > n) m = m-n; else n = n-m; } printf("%d", n); } The program computes
What does the following algorithm approximate? (Assume ). x = m; y = 1; While (x-y > ϵ) { x = (x+y)/2; y = m/x; } print(x);
Two matrices and are to be stored in arrays and respectively. Each array can be stored either in row-major or column-major order in contiguous memory locations. The time complexity of an algorithm to compute will be
The problem and are
The tightest lower bound on the number of comparisons, in the worst case, for comparison-based sorting is of the order of
In a permutation , of distinct integers, an inversion is a pair such that and What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of with at most inversions?
In the following program fragment, , , and TwoLog_n are integer variables, and is an array of integers. The variable is initialized to an integer , and TwoLog_n is initialized to the value of for (k = 3; k <= n; k++) A[k] = 0; for (k = 2; k <= TwoLog_n; k++) for (j = k+1; j …
