GATE CSE Algorithms Formula Sheet: Recurrences, Sorting, Graphs, DP
Algorithms questions in GATE CSE keep reusing a short list of results, from the Master theorem to Catalan numbers. This sheet collects them group by group. Each group has a table, one worked example of my own with the arithmetic shown, and the trap that costs marks.
In this guide
- Key takeaways
- The terms this sheet uses
- Asymptotic notation: five symbols and how they combine
- Recurrences: the Master theorem in one table
- Recursion trees and the recurrences worth memorising
- Sorting: complexity and properties in one table
- Searching and hashing: load factor and expected probes
- Heaps: height, index arithmetic and linear build-heap
- Graph algorithms: complexity depends on the data structure
- Dynamic programming: three recurrences and their tables
- Greedy: Huffman coding and activity selection
- Counting formulas: Catalan numbers and spanning trees
- Quick revision
For which topics past papers have asked most, read the Algorithms important topics guide; for the other subjects, use the GATE CSE formula sheet.
Key takeaways
- The Master theorem compares with ; Case 3 also needs the regularity condition.
- Merge sort is stable but not in place; quicksort and heapsort are in place but not stable.
- Linear probing degrades much faster than chaining as the load factor approaches 1.
- Build-heap is , not , because most nodes sit near the bottom.
- Graph algorithm complexity depends on the data structure: an array suits dense graphs, a heap suits sparse ones.
- Catalan numbers count BSTs, binary tree shapes and stack permutations; has spanning trees.
The terms this sheet uses
- is the input size. For graphs, is the number of vertices and the number of edges.
- The load factor is the number of keys stored in a hash table of slots.
- A probe is one slot examined during a hash search or insertion.
- A sort is stable if equal keys keep their input order, and in place if it uses only extra space beyond the input.
- Heap height counts edges, so a single node has height 0.
Logs are base 2 unless a base is written.
Asymptotic notation: five symbols and how they combine
Each symbol is a comparison between two growth rates, valid from some onwards.
| Symbol | Meaning | Useful rule |
|---|---|---|
| for some | Transitive: , give | |
| for some | exactly when | |
| both and | ||
| for any | ||
The growth order to carry into the hall is:
Worked example. Order , and . Take the log of each: , and . At these are 200, 400 and 1024, and each gap keeps widening. So and .
The trap: does not give . Take and ; the ratio grows without bound.
Recurrences: the Master theorem in one table
A divide-and-conquer algorithm makes calls on inputs of size and spends splitting and combining:
Here , , and is the work at the leaves.
| Case | Condition on | Solution |
|---|---|---|
| 1 | for some | |
| 2 | with | |
| 3 | and , |
The regularity condition in Case 3 says the work shrinks by a fixed factor at each level down, so the root's cost dominates the whole tree.
Worked example. One Case 2 and one Case 3, with :
Trap: The gap must be polynomial. is Case 2 with , giving . sits at and gives , not .
Recursion trees and the recurrences worth memorising
When the split is unequal, add the cost level by level instead. If the level sums shrink geometrically, the top level dominates. If they stay equal, multiply one level by the depth. If they grow, the leaves dominate.
Worked example. For , the level sums are , , , and so on:
These solutions come up often enough to learn by heart:
| Recurrence | Solution | Recurrence | Solution |
|---|---|---|---|
The trap: two calls on double the work at every level, so is exponential, not linear. The recurrence relations guide covers substitution and change of variable in full.
Sorting: complexity and properties in one table
| Algorithm | Best | Average | Worst | Stable | In place |
|---|---|---|---|---|---|
| Insertion | Yes | Yes | |||
| Bubble (early exit) | Yes | Yes | |||
| Selection | No | Yes | |||
| Merge | Yes | No, extra | |||
| Quick | No | Yes, stack only | |||
| Heap | No | Yes | |||
| Counting, keys in | Yes | No | |||
| Radix, digits | Yes | No |
Insertion sort makes inversions comparisons, minus one for each key that moves to the front. Selection sort always makes comparisons and at most swaps. Any comparison sort needs comparisons in the worst case.
Worked example. Insertion sort on . The inversions are , and , so there are 3. Only the key 1 moves to the front. The count is comparisons. The comparison lower bound, , applies to the worst input, not this one.
The trap: quicksort with a first-element pivot hits its worst case on already sorted input.
Searching and hashing: load factor and expected probes
Binary search on a sorted array makes at most comparisons.
| Scheme | Unsuccessful search | Successful search |
|---|---|---|
| Chaining | about | |
| Linear probing | ||
| Uniform (double) hashing |
Worked example. At , chaining needs 1.75 probes for an unsuccessful search and 1.375 for a successful one. Linear probing needs 8.5 and 2.5. Uniform hashing needs 4 and about 1.85. Primary clustering explains the gap.
Remember: GATE counts the empty slot where a search stops as a probe. Binary search on a linked list is , because reaching the middle element is itself a walk.
Heaps: height, index arithmetic and linear build-heap
| Quantity | Formula |
|---|---|
| Parent and children, 1-based | ; and |
| Parent and children, 0-based | ; and |
| Height of an -node heap | |
| Leaves and internal nodes | leaves at to ; internal |
| Insert, extract-max | each |
| Build-heap | sift-downs, at most comparisons, |
| Heaps on distinct keys | , the left-subtree size |
Build-heap is linear because at most nodes have height , and each costs :
Worked example. A heap of 20 nodes has height . Nodes 11 to 20 are its 10 leaves, so build-heap starts sifting down at node 10. For 5 distinct keys the left subtree holds 3 nodes and the right 1, so max-heaps.
The trap: applying the 1-based parent formula to a 0-based array.
The book's last-minute revision sheet adds step-by-step procedures for Dijkstra, Kruskal, Prim, BFS, DFS, build-heap and partition, with what GATE asks about each, plus a traps table. It is part of the GATE CSE 2027 book.
Graph algorithms: complexity depends on the data structure
| Algorithm | Structure | Time | Note |
|---|---|---|---|
| BFS, DFS | Adjacency list | with a matrix | |
| Topological sort, DAG shortest path | Adjacency list | Negative edges allowed in a DAG | |
| Dijkstra | Array | Non-negative weights | |
| Dijkstra | Binary heap | Best for sparse graphs | |
| Dijkstra | Fibonacci heap | Fastest of the three | |
| Bellman–Ford | Edge list | passes; pass finds a negative cycle | |
| Floyd–Warshall | Matrix | time, space | All pairs |
| Prim | Binary heap | with an array | |
| Kruskal | Sort and union–find | Since |
Worked example. Take , so . With , the array version of Dijkstra costs about steps, and the heap version about . In the complete graph, , and the heap version costs about , so the array wins.
Trap: Adding a constant to every edge weight keeps the MST but can change shortest paths, because a path with more edges gains more. Multiplying every weight by a positive constant changes neither.
Dynamic programming: three recurrences and their tables
| Problem | Recurrence | Table | Time |
|---|---|---|---|
| LCS of lengths , | if , else | ||
| 0/1 knapsack, capacity | |||
| Matrix chain, matrices |
In the matrix chain row, matrix has dimensions . The knapsack's second option applies only when .
Worked example. Take as , as and as , so :
The minimum is 264 scalar multiplications.
The trap: matrices have parenthesisations, not , so three matrices have .
Greedy: Huffman coding and activity selection
| Algorithm | Greedy rule | Time | Key fact |
|---|---|---|---|
| Huffman | Merge the two lowest frequencies | with a heap | Total bits ; internal nodes |
| Activity selection | Pick the earliest finish time that fits | to sort | One linear pass once sorted |
| Fractional knapsack | Highest value per unit weight first | 0/1 knapsack needs DP |
Worked example. Frequencies a: 2, b: 3, c: 5, d: 7, e: 13, total 30. The merges are , , and . Code lengths are e 1, d 2, c 3, a 4, b 4, so the total is:
The merge weights give the same 62. A fixed 3-bit code needs 90.
The trap: tie-breaking can change one symbol's code length but never the total bits.
Counting formulas: Catalan numbers and spanning trees
| Count | Formula | Value at |
|---|---|---|
| BSTs on distinct keys | 14 | |
| Binary tree shapes, stack permutations | 14 | |
| Labelled binary trees | 336 | |
| Parenthesisations of matrices | 5 | |
| Edges in | 6 | |
| Spanning trees of | 16 | |
| Spanning trees of a cycle on vertices | 4 | |
| Spanning trees of | 4096 when |
Worked example. Check for by hand. It has 6 edges, so three-edge subsets. Three edges on four vertices fail to be a tree only when they form a triangle, and has triangles. That leaves .
In one line: counts labelled spanning trees, distinct edge sets on named vertices, not tree shapes.
The graph theory questions guide covers the rest of the counting family, and the time complexity questions guide turns these bounds into loop-counting practice.
Quick revision
- Master theorem: compare with ; Case 2 adds one log; Case 3 needs , .
- ; ; .
- Stable: insertion, bubble, merge, counting, radix. Not in place: merge, counting, radix.
- Chaining ; linear probing unsuccessful; count the empty slot.
- Heap height , leaves, build-heap .
- Dijkstra array or heap; Bellman–Ford ; Floyd–Warshall .
- LCS , knapsack , matrix chain with parenthesisations.
- Huffman total bits sum of merge weights; has spanning trees.
Frequently asked questions
What are the three cases of the Master theorem?
For , let . If is polynomially smaller than , the answer is . If with , the answer is . If is polynomially larger and for some , the answer is .
Which sorting algorithms are stable and which are in place?
Insertion, bubble, merge, counting and radix sort are stable. Insertion, bubble, selection, heapsort and quicksort are in place, with quicksort using only its recursion stack. Merge sort on arrays is stable but needs linear extra space, so it is not in place. Quicksort and heapsort are in place but not stable. Counting sort is stable but not in place.
What is the expected number of probes for linear probing?
With load factor , an unsuccessful search or insertion with linear probing takes about probes and a successful search about . At these are 8.5 and 2.5. Chaining needs only probes for an unsuccessful search. GATE counts the empty slot where a search stops as a probe.
What is the time complexity of Dijkstra's algorithm?
It depends on the priority queue. With a plain array Dijkstra runs in , with a binary heap in and with a Fibonacci heap in . The array version suits dense graphs and the heap version suits sparse ones. All three need non-negative edge weights for the guarantee to hold.
How many binary search trees can be formed with n distinct keys?
The number is the Catalan number . The first values, for n from 0 to 10, are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862 and 16796, so 4 keys give 14 trees. The same number counts unlabelled binary tree shapes and stack permutations. Labelled binary trees number .
Sources
Dates, fees and the syllabus are set by the GATE 2027 organising institute and can change. Always confirm at gate2027.iitm.ac.in.