GATE CSE Algorithms formula sheet
Complexities, counts and recurrences GATE CSE asks about in Algorithms: searching, sorting and hashing, asymptotic analysis and the master theorem, greedy, divide-and-conquer and dynamic programming, and graph algorithms.
52 formulas · updated 2026-11-14 · reviewed 2026-11-14 · Spotted an error? Tell us
Searching and sorting
Binary search
Needs a sorted array with random access; on a linked list it is no better than linear search. Linear search: n comparisons worst case.
Comparison-sort lower bound
Holds for every comparison-based sort (worst and average case). Counting, radix and bucket sort escape it by not comparing.
Comparison sorts
All entries are Θ(·) time. Merge sort uses Θ(n) extra space; quicksort uses O(log n) stack on average, O(n) worst. Selection sort does at most n − 1 swaps.
Non-comparison sorts
k = key range (or base), d = number of digits. Radix sort needs a stable sort per digit. Bucket sort assumes uniform input; worst case .
Inversions and insertion sort
I = number of inversions (pairs i < j with a[i] > a[j]). Sorted input: n − 1 comparisons; reverse sorted: n(n−1)/2.
Quicksort cases
Worst case with first/last element pivot on already sorted or reverse-sorted input, n(n−1)/2 comparisons. Any constant-ratio split (e.g. 1:9) is still Θ(n log n).
Binary heap facts
1-indexed array. Build-heap is O(n); insert, extract-min and decrease-key are O(log n); finding the max in a min-heap is O(n) (scan the leaves).
Comparison counts
Standard merge of sorted lists of sizes m and n uses at most m + n − 1; the min-and-max and second-largest counts are optimal worst-case values. Max alone needs n − 1.
Selection (k-th smallest)
Worst-case linear. Randomised quickselect is O(n) expected but worst case.
Hashing
Load factor
n keys, m slots. With chaining α may exceed 1; with open addressing α ≤ 1.
Chaining
Under simple uniform hashing. Worst case Θ(n) when all keys land in one chain.
Open addressing probes
Expected probes under uniform hashing, α < 1. E.g. α = 0.5 gives at most 2 probes for a miss.
Probe sequences
i = 0, 1, 2, … Linear probing suffers primary clustering, quadratic secondary clustering. Double hashing needs coprime to m to visit every slot.
Collisions
Assumes each key hashes independently and uniformly. Expected empty slots after n insertions: .
Division method
Pick m prime and not close to a power of 2; just keeps the low p bits of k.
Asymptotic notation
Big-O, Omega, Theta
O is an upper bound, not necessarily tight. Worst-case time Θ(g) means the worst input takes Θ(g).
Limit test
If the limit does not exist the test says nothing (e.g. f = n(1 + sin n)).
Growth order
Any polylog is for every ε > 0; any polynomial is for c > 1.
Useful identities
Traps: but ; but is not.
Standard sums
Geometric sum (r ≠ 1) is Θ(1) for r < 1 and for r > 1. .
Loop counting
Nested loop where the inner loop runs n/i times gives the harmonic sum. Inner loop with j = j² runs Θ(log log n) times.
Recurrences and the master theorem
Master theorem
Requires a ≥ 1, b > 1, some ε > 0, and c < 1 in the regularity condition. Gap example: is not covered by the basic form (answer ).
Decreasing recurrences
b > 0, k ≥ 0. The often-quoted for a > 1 is only a loose upper bound. E.g. Tower of Hanoi T(n) = 2T(n−1) + 1 gives (exactly moves).
Common recurrences
For forms substitute . For uneven splits use the recursion tree: depth , Θ(n) work per level.
Divide and conquer
Merge sort
Same in best, average and worst case; Θ(n) auxiliary array.
Integer multiplication
Schoolbook multiplication of two n-digit numbers is .
Matrix multiplication
Plain divide-and-conquer with 8 sub-products: , no gain over the triple loop.
Fast exponentiation
Computing twice instead of squaring once gives T(n) = 2T(n/2) + 1 = Θ(n).
Closest pair and max subarray
Closest pair of points (with presorting) and divide-and-conquer max subarray. Kadane's algorithm solves max subarray in Θ(n).
Greedy algorithms
Fractional knapsack
Greedy is optimal for fractional knapsack but not for 0/1 knapsack (use DP).
Activity selection
Greedy by earliest start or shortest duration is not optimal.
Huffman coding
= depth (code length) of symbol i. Repeatedly merge the two smallest frequencies. Optimal merge pattern of sorted files uses the same rule.
Job sequencing with deadlines
Unit-time jobs. O(n log n) with a disjoint-set structure for free slots.
Dynamic programming
0/1 knapsack
Θ(nW) time: pseudo-polynomial (W is the capacity value, not its bit length). Space can be cut to Θ(W).
Longest common subsequence
Θ(mn) time and space. A subsequence need not be contiguous (unlike a substring).
Matrix chain multiplication
Matrix is . Θ(n³) time, Θ(n²) space. Multiplying p×q by q×r costs pqr scalar multiplications.
Parenthesisations
Catalan number: 1, 1, 2, 5, 14 for n = 1..5. Brute force is exponential, which is why DP is used.
Subset sum
Θ(n·S) time for target S; pseudo-polynomial. The decision problem is NP-complete.
Edit distance
, . Θ(mn). Unit costs for insert, delete, substitute.
Graph traversals
BFS and DFS cost
BFS uses a queue and finds shortest paths by edge count in unweighted graphs. DFS uses a stack (or recursion).
BFS tree properties
An undirected graph is bipartite iff no edge joins two vertices on the same BFS level.
DFS edge classification
d = discovery time, f = finish time (parenthesis theorem). Undirected DFS has only tree and back edges. A directed graph is acyclic iff DFS finds no back edge.
Topological sort
Exists iff the graph is a DAG. Not unique in general.
Strongly connected components
The component graph of SCCs is always a DAG. Tarjan's algorithm does it in one DFS.
Articulation points and bridges
Non-root u is a cut vertex iff some child c has low[c] ≥ d[u]; the DFS root iff it has ≥ 2 children. Tree edge (u, c) is a bridge iff low[c] > d[u].
Minimum spanning trees and shortest paths
MST properties
Cycle property: a strictly heaviest edge on a cycle is in no MST. Distinct edge weights ⇒ MST is unique.
Kruskal and Prim
Kruskal sorts edges and uses union–find; it builds a forest. Prim grows one tree. Both work with negative weights.
Relaxation
Every single-source shortest-path algorithm here is a schedule of relaxations; initialise d[s] = 0, others ∞.
Dijkstra
Correct only when all edge weights are non-negative. Array version is better for dense graphs.
Bellman–Ford
Handles negative edges. If a V-th pass still relaxes some edge, a negative cycle is reachable from the source.
DAG shortest paths
Works with negative weights (no cycles exist). Negate weights to get longest paths in a DAG.
Floyd–Warshall
All pairs; negative edges allowed. A negative cycle exists iff some at the end. With ∨/∧ it gives transitive closure (Warshall).