chapter

    Algorithms Notes for Gate Cs

    GATE CS Algorithms: 5 chapters, 47 previous year questions (100% of Algorithms), 1355 practice questions and one solved question from each chapter.

    A question from this chapter

    Question 1
    Level 3: Exam Standard

    Consider the recurrence relation for , with , where is a positive integer. What is the minimum value of for which the Master Theorem yields ?

    Question 2
    Level 3: Exam Standard

    Consider the following problems and the optimal number of element comparisons required to solve them in the worst case.

    List I

    P. Finding the second largest element in an array of 16 distinct elements using a tournament tree.

    Q. Finding both the minimum and maximum elements in an array of 10 distinct elements using the optimal pairwise method.

    R. Finding the minimum element in an array of 8 distinct elements.

    S. Finding the second largest element in an array of 32 distinct elements using a tournament tree.

    List II

    1. 7
    2. 13
    3. 15
    4. 18
    5. 35

    Which of the following options correctly matches the problems in List I with their corresponding comparison counts in List II?

    Question 3
    Level 3: Exam Standard

    Consider the tree with vertex set and edge set . The two-BFS diameter algorithm is executed on : first, BFS is run from vertex $1$ to find the farthest vertex ; then, BFS is run from to find the farthest vertex from . What is the distance between and as output by this algorithm?

    Question 4
    Level 3: Exam Standard

    Consider a connected undirected weighted graph with vertices and the following edges with weights:

    , , , , , .

    Which ONE of the following statements is IMPOSSIBLE?

    Question 5
    Level 3: Exam Standard

    An alphabet consists of five symbols with frequencies 20, 15, 5, 5, 5. A proposed prefix-free code assigns code lengths 1, 2, 4, 4, 4 to these symbols respectively. Let be the total encoded length of a 50-symbol message using this proposed code, and let be the total encoded length using the optimal Huffman code. What is the value of ?

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Algorithms Notes for GATE CS

    GATE CS Algorithms: 5 chapters, 47 previous year questions (100% of Algorithms), 1355 practice questions and one solved question from each chapter.

    About Algorithms Notes

    Full study notes for Algorithms in GATE CS, organised across 5 chapters. Each chapter page explains concepts from the basics with worked examples and the formulas you need.

    Algorithms Weightage in GATE CS

    Algorithms accounts for 47 of 47 Algorithms previous year questions in our bank (100%), about 4.7 per paper across 10 papers.

    Algorithms Chapter Matrix

    ChapterTopicsPYQsShare of unit PYQsPractice questions
    Asymptotic Analysis and Recurrence RelationsSolving Recurrence Relations, Asymptotic Notation and Growth Comparison, Loop Complexity and Function Comparison1226%322
    Searching, Sorting, Selection and HashingSorting Algorithms and Operation Counts, Binary and Bitonic Array Searching, Selection and Comparison Bounds, Linear-time Array Property Verification, Universal Hashing and Adversarial Inputs1021%287
    Graph Traversal, Connectivity and Directed GraphsBreadth-first Search Trees and Applications, Depth-first Search Edges and Timestamps, Connected Components and Articulation Points, Directed Graphs, Cycles and Strong Connectivity, Greedy Graph Coloring1021%287
    Minimum Spanning Trees and Shortest PathsMst Cut-cycle Properties and Uniqueness, Minimum Spanning Tree Construction and Counting, Shortest-path Properties and Algorithms, Effects of Edge-weight Transformations1021%281
    Dynamic Programming, Greedy Algorithms and OptimizationLongest Non-decreasing Subsequences and Array Optimization, Rod Cutting Dynamic Programming, Huffman Coding and Prefix-free Codes, Optimal Path Computation in Directed Acyclic Graphs, Dynamic Programming Table Evaluation511%178

    More from Algorithms

    One Solved Question from Each Algorithms Chapter

    Question 1 · Asymptotic Analysis and Recurrence Relations MCQ

    Consider the recurrence relation for , with , where is a positive integer. What is the minimum value of for which the Master Theorem yields ?

    1. A.

      $3$

    2. B.

      $9$

    3. C.

      $10$

    4. D.

      $27$

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a Master Theorem Case-selection question. The solution equals only when the leaves dominate, i.e., Case 1 applies.

    Exam route: Case 1 requires for some . Here and the critical function is . So we need , giving . The smallest integer satisfying this is .

    Learning route:

    Step 1: Identify , , . Critical exponent .

    Step 2: For , we need Case 1: .

    Step 3: This means , so , i.e., .

    Step 4: . Minimum integer .

    Verification: At , , so — Case 2 applies, giving . Contradiction. At , , and , so Case 1 gives . Confirmed.

    Answer: $10$

    Question 2 · Searching, Sorting, Selection and Hashing MCQ

    Consider the following problems and the optimal number of element comparisons required to solve them in the worst case.

    List I

    P. Finding the second largest element in an array of 16 distinct elements using a tournament tree.

    Q. Finding both the minimum and maximum elements in an array of 10 distinct elements using the optimal pairwise method.

    R. Finding the minimum element in an array of 8 distinct elements.

    S. Finding the second largest element in an array of 32 distinct elements using a tournament tree.

    List II

    1. 7
    2. 13
    3. 15
    4. 18
    5. 35

    Which of the following options correctly matches the problems in List I with their corresponding comparison counts in List II?

    1. A.

      P-4, Q-2, R-1, S-5

    2. B.

      P-3, Q-2, R-1, S-5

    3. C.

      P-4, Q-1, R-2, S-5

    4. D.

      P-4, Q-2, R-1, S-4

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a direct formula application question for comparison bounds. We must match each problem to its exact worst-case comparison count by carefully reading the problem type and array size.

    Step 1: For P, the tournament method for the 2nd largest of elements takes comparisons. For , this is . Matches with 4.

    Step 2: For Q, the pairwise method for min and max takes comparisons. For , this is . Matches with 2.

    Step 3: For R, finding just the minimum takes comparisons. For , this is . Matches with 1.

    Step 4: For S, using the tournament method for , the count is . Matches with 5.

    Answer: P-4, Q-2, R-1, S-5.

    Question 3 · Graph Traversal, Connectivity and Directed Graphs NAT

    Consider the tree with vertex set and edge set . The two-BFS diameter algorithm is executed on : first, BFS is run from vertex $1$ to find the farthest vertex ; then, BFS is run from to find the farthest vertex from . What is the distance between and as output by this algorithm?

    Correct Answer:

    7.00

    Step-by-Step Solution

    Key idea: this is a direct-formula tree diameter question, recognisable because it explicitly invokes the two-BFS algorithm on a tree. The governing fact: in any tree, the farthest vertex from an arbitrary start is guaranteed to be an endpoint of some diameter, and the second BFS finds the other endpoint, yielding the exact diameter.

    Step 1: Run BFS from vertex $1$. Compute distances: $d(1,1)=0$; $d(1,2)=1, d(1,3)=1$; $d(1,4)=2, d(1,5)=2, d(1,6)=2, d(1,7)=2$; $d(1,8)=3, d(1,9)=3, d(1,10)=3, d(1,11)=3$; $d(1,12)=4$. The farthest vertex from $1$ is $12$ at distance $4$. So $u = 12$.

    Step 2: Run BFS from vertex $12$. Compute distances: $d(12,12)=0$; $d(12,10)=1$; $d(12,6)=2$; $d(12,3)=3$; $d(12,1)=4, d(12,7)=4$; $d(12,2)=5, d(12,11)=5$; $d(12,4)=6, d(12,5)=6$; $d(12,8)=7, d(12,9)=7$. The farthest vertices from $12$ are $8$ and $9$, both at distance $7$. So $z \in \{8, 9\}$ and the distance is $7$.

    Step 3: The algorithm outputs $7$, which is the diameter of $T$ (the longest path is $8$-$4$-$2$-$1$-$3$-$6$-$10$-$12$ or $9$-$5$-$2$-$1$-$3$-$6$-$10$-$12$, both of length $7$).

    Answer: 7.00

    Tempting wrong path: computing the height of the tree from vertex $1$ (which is $4$) and reporting that as the diameter, or miscounting the path length by one edge. The break is at Step 2: the diameter is the maximum distance between ANY two vertices, not just from the root. Verification: the path $8$-$4$-$2$-$1$-$3$-$6$-$10$-$12$ has $7$ edges, confirming the diameter is $7$.

    Question 4 · Minimum Spanning Trees and Shortest Paths MCQ

    Consider a connected undirected weighted graph with vertices and the following edges with weights:

    , , , , , .

    Which ONE of the following statements is IMPOSSIBLE?

    1. A.

      The minimum spanning tree of has total weight .

    2. B.

      Edge with weight belongs to every minimum spanning tree of .

    3. C.

      If is changed to , the new minimum spanning tree has total weight .

    4. D.

      There exists a spanning tree of with total weight strictly less than .

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is an impossibility-check question that tests multiple MST properties with negative edge weights. Evaluate each statement by computing the MST and applying MST theorems.

    First, find the MST of .

    Sort edges by weight: , , , , , .

    Run Kruskal's algorithm:

    1. Add with weight . Components: , , .
    2. Add with weight . Components: , .
    3. Add with weight $1$. This connects and . Components: . All vertices connected. Stop.

    MST with total weight .

    Evaluate each statement:

    (A) "The MST has total weight ."

    This matches our computation. This is TRUE, hence POSSIBLE.

    (B) "Edge with weight belongs to every MST."

    Edge has weight , which is the globally minimum weight in . The global minimum edge always belongs to every MST (provable via the cut property). This is TRUE, hence POSSIBLE.

    (C) "If is changed to , the new MST has total weight ."

    New .

    New sorted edges: , , , , , .

    Kruskal's: Add , add , add (connects the two components). MST weight .

    This is TRUE, hence POSSIBLE.

    (D) "There exists a spanning tree with total weight strictly less than ."

    By definition, the MST has the minimum total weight among all spanning trees. Since the MST weight is , no spanning tree can have weight less than . This is IMPOSSIBLE.

    Answer: D

    Question 5 · Dynamic Programming, Greedy Algorithms and Optimization MCQ

    An alphabet consists of five symbols with frequencies 20, 15, 5, 5, 5. A proposed prefix-free code assigns code lengths 1, 2, 4, 4, 4 to these symbols respectively. Let be the total encoded length of a 50-symbol message using this proposed code, and let be the total encoded length using the optimal Huffman code. What is the value of ?

    1. A.

      3

    2. B.

      4

    3. C.

      5

    4. D.

      7

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is an integrated problem requiring validation of a proposed code, construction of the optimal Huffman tree, and comparison of their total costs.

    Step 1: Calculate .

    The proposed lengths are 1, 2, 4, 4, 4 for frequencies 20, 15, 5, 5, 5.

    .

    Step 2: Construct the optimal Huffman tree.

    • Initialize heap: 5, 5, 5, 15, 20.
    • Merge 5 + 5 = 10. Heap: 5, 10, 15, 20.
    • Merge 5 + 10 = 15. Heap: 15, 15, 20.
    • Merge 15 + 15 = 30. Heap: 20, 30.
    • Merge 20 + 30 = 50. Root.

    Tree structure: 50 -> 20, 30. 30 -> 15, 15. 15 -> 5, 10. 10 -> 5, 5.

    Depths: 20 is at depth 1. 15 is at depth 2. 5 is at depth 3. 5 and 5 are at depth 4.

    Lengths: 1, 2, 3, 4, 4.

    Step 3: Calculate .

    .

    Step 4: Compute the difference.

    .

    Answer: C