📜  GATE CS 2016 Sec 8(1)

📅  最后修改于: 2023-12-03 14:41:21.815000             🧑  作者: Mango

GATE CS 2016 Sec 8

GATE CS (Graduate Aptitude Test in Engineering - Computer Science) is an entrance exam for students who wish to pursue a Master's or Doctoral degree in Computer Science and Information Technology. This exam is conducted by Indian Institute of Technology (IIT) on behalf of Ministry of Human Resource Development (MHRD), Government of India.

In this article, we will discuss the GATE CS 2016 Sec 8 question paper and its solutions. Sec 8 of the paper consisted of questions related to programming, data structures, and algorithms.

Question 1

The first question in Sec 8 was related to dynamic programming. The question was as follows:

Q. Given a matrix $A[1\dots n][1\dots m]$ of integers, find the length of the longest path in the matrix with consecutive integers. We can move from any cell to any of its 8 neighbors.

The solution to this question involves using dynamic programming to find the length of the longest path in the matrix. The time complexity of the algorithm is $O(nm)$.

def longest_path(A):
    n, m = len(A), len(A[0])
    dp = [[0] * m for _ in range(n)]
    ans = 0
    
    for i in range(n):
        for j in range(m):
            ans = max(ans, dfs(i, j, A, dp))
    return ans

def dfs(i, j, A, dp):
    if dp[i][j] != 0:
        return dp[i][j]
    
    ans = 0
    for ii in range(i-1, i+2):
        for jj in range(j-1, j+2):
            if ii >= 0 and ii < len(A) and jj >= 0 and jj < len(A[0]) and abs(A[ii][jj] - A[i][j]) == 1:
                ans = max(ans, dfs(ii, jj, A, dp))
    dp[i][j] = ans + 1
    return dp[i][j]
Question 2

The second question in Sec 8 was related to hashing. The question was as follows:

Q. Given an array $A[1\dots n]$ of integers and a sum $S$, find if there exists a subarray whose sum is equal to $S$. Show how to do it in $O(n)$.

The solution to this problem involves using hashing to store the prefix sum at each index. The time complexity of the algorithm is $O(n)$.

def subarray_sum(A, S):
    prefix_sum = {0: -1}  # stores prefix sum and index
    s = 0
    
    for i, a in enumerate(A):
        s += a
        if s - S in prefix_sum:
            return (prefix_sum[s-S]+1, i)
        prefix_sum[s] = i
    
    return -1
Question 3

The third question in Sec 8 was related to graph algorithms. The question was as follows:

Q. Given a directed graph $G=(V,E)$ and a source vertex $s$, find if there exists a negative weight cycle reachable from $s$.

The solution to this problem involves using Bellman-Ford algorithm. We run the Bellman-Ford algorithm on the graph and if during any relaxation step, the distance of any vertex is decreased, then that means there is a negative weight cycle in the graph. The time complexity of the algorithm is $O(VE)$.

def negative_weight_cycle(G, s):
    d = {v:float('inf') for v in G}
    d[s] = 0
    
    for _ in range(len(G) - 1):
        for u in G:
            for v, w in G[u]:
                if d[u] + w < d[v]:
                    d[v] = d[u] + w
    
    for u in G:
        for v, w in G[u]:
            if d[u] + w < d[v]:
                return True
    
    return False
Conclusion

In this article, we discussed the GATE CS 2016 Sec 8 question paper and provided solutions to some of the questions. The questions covered topics such as dynamic programming, hashing, and graph algorithms. If you are preparing for GATE CS, we recommend practicing similar questions to gain better understanding of the exam format and the topics covered.