Skip to content
GATEiit

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

    T(n)=T(n/2)+O(1)=O(log⁡n),max comparisons=⌊log⁡2n⌋+1

    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

    height of decision tree ≥ ⌈log⁡2n!⌉=Ω(nlog⁡n)

    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

    Counting: Θ(n+k),Radix: Θ(d (n+k)),Bucket: Θ(n) average

    k = key range (or base), d = number of digits. Radix sort needs a stable sort per digit. Bucket sort assumes uniform input; worst case O(n2).

  • Inversions and insertion sort

    insertion sort time=Θ(n+I),0≤I≤n(n−1)2

    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: T(n)=T(n−1)+Θ(n)=Θ(n2),balanced: T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)

    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

    parent(i)=⌊i/2⌋, children 2i, 2i+1;h=⌊log⁡2n⌋;leaves=⌈n/2⌉

    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

    merge(m,n)≤m+n−1,min and max=⌈3n/2⌉−2,2nd largest=n+⌈log⁡2n⌉−2

    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)

    median of medians: T(n)≤T(n/5)+T(7n/10)+O(n)=O(n)

    Worst-case linear. Randomised quickselect is O(n) expected but O(n2) worst case.

Hashing

  • Load factor

    α=nm

    n keys, m slots. With chaining α may exceed 1; with open addressing α ≤ 1.

  • Chaining

    expected search (hit or miss)=Θ(1+α)

    Under simple uniform hashing. Worst case Θ(n) when all keys land in one chain.

  • Open addressing probes

    unsuccessful≤11−α,successful≤1αln⁡11−α

    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 h2(k) coprime to m to visit every slot.

  • Collisions

    E[#colliding pairs]=(n2)1m,P(no collision)=∏i=1n−1(1−im)

    Assumes each key hashes independently and uniformly. Expected empty slots after n insertions: m(1−1/m)n.

  • Division method

    h(k)=k mod m

    Pick m prime and not close to a power of 2; m=2p 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

    1≺log⁡log⁡n≺log⁡n≺(log⁡n)k≺n≺n≺nlog⁡n≺n2≺nk≺2n≺n!≺nn

    Any polylog is o(nε) for every ε > 0; any polynomial is o(cn) for c > 1.

  • Useful identities

    log⁡(n!)=Θ(nlog⁡n),alog⁡bn=nlog⁡ba,log⁡an=Θ(log⁡bn)

    Traps: 2n+1=Θ(2n) but 22n≠O(2n); log⁡(nk)=Θ(log⁡n) but (log⁡n)k is not.

  • Standard sums

    ∑i=1ni=n(n+1)2,∑i=1ni2=n(n+1)(2n+1)6,∑i=1n1i=Θ(log⁡n),∑i=0nri=rn+1−1r−1

    Geometric sum (r ≠ 1) is Θ(1) for r < 1 and Θ(rn) for r > 1. ∑i=1nlog⁡i=Θ(nlog⁡n).

  • Loop counting

    i=1,2,4,⋯<n → Θ(log⁡n) iterations,∑i=1nni=Θ(nlog⁡n)

    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: T(n)=2T(n/2)+n/log⁡n is not covered by the basic form (answer Θ(nlog⁡log⁡n)).

  • Decreasing recurrences

    b > 0, k ≥ 0. The often-quoted O(nkan/b) for a > 1 is only a loose upper bound. E.g. Tower of Hanoi T(n) = 2T(n−1) + 1 gives Θ(2n) (exactly 2n−1 moves).

  • Common recurrences

    For T(n) forms substitute m=log⁡n. For uneven splits use the recursion tree: depth log⁡3/2n, Θ(n) work per level.

Divide and conquer

  • Merge sort

    T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)

    Same in best, average and worst case; Θ(n) auxiliary array.

  • Integer multiplication

    Karatsuba: T(n)=3T(n/2)+Θ(n)=Θ(nlog⁡23)≈Θ(n1.585)

    Schoolbook multiplication of two n-digit numbers is Θ(n2).

  • Matrix multiplication

    Strassen: T(n)=7T(n/2)+Θ(n2)=Θ(nlog⁡27)≈Θ(n2.81)

    Plain divide-and-conquer with 8 sub-products: 8T(n/2)+Θ(n2)=Θ(n3), no gain over the triple loop.

  • Fast exponentiation

    Computing xn/2 twice instead of squaring once gives T(n) = 2T(n/2) + 1 = Θ(n).

  • Closest pair and max subarray

    T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)

    Closest pair of points (with presorting) and divide-and-conquer max subarray. Kadane's algorithm solves max subarray in Θ(n).

Greedy algorithms

  • Fractional knapsack

    take items in decreasing viwi,  split the last one;O(nlog⁡n)

    Greedy is optimal for fractional knapsack but not for 0/1 knapsack (use DP).

  • Activity selection

    sort by finish time, pick each activity whose start ≥ last finish;O(nlog⁡n)

    Greedy by earliest start or shortest duration is not optimal.

  • Huffman coding

    cost=∑ifi di,n−1 merges,O(nlog⁡n) with a min-heap

    di = 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

    sort by profit (desc.), place each job in the latest free slot≤di;O(n2)

    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

    m[i][j]=min⁡i≤k<j(m[i][k]+m[k+1][j]+pi−1 pk pj),m[i][i]=0

    Matrix Ai is pi−1×pi. Θ(n³) time, Θ(n²) space. Multiplying p×q by q×r costs pqr scalar multiplications.

  • Parenthesisations

    #ways to parenthesise n matrices=Cn−1=1n(2n−2n−1)

    Catalan number: 1, 1, 2, 5, 14 for n = 1..5. Brute force is exponential, which is why DP is used.

  • Subset sum

    S[i][s]=S[i−1][s] ∨ (ai≤s ∧ S[i−1][s−ai]),S[0][0]=true

    Θ(n·S) time for target S; pseudo-polynomial. The decision problem is NP-complete.

  • Edit distance

    D[i][j]=min⁡(D[i−1][j]+1, D[i][j−1]+1, D[i−1][j−1]+[xi≠yj])

    D[i][0]=i, D[0][j]=j. Θ(mn). Unit costs for insert, delete, substitute.

Graph traversals

  • BFS and DFS cost

    Θ(V+E) (adjacency list),Θ(V2) (adjacency matrix)

    BFS uses a queue and finds shortest paths by edge count in unweighted graphs. DFS uses a stack (or recursion).

  • BFS tree properties

    undirected: only tree and cross edges,∣ level(u)−level(v) ∣≤1  ∀(u,v)∈E

    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

    order vertices by decreasing f[v]  (or Kahn: repeatedly remove in-degree 0);Θ(V+E)

    Exists iff the graph is a DAG. Not unique in general.

  • Strongly connected components

    Kosaraju: DFS on G, then DFS on GT in decreasing f;Θ(V+E)

    The component graph of SCCs is always a DAG. Tarjan's algorithm does it in one DFS.

  • Articulation points and bridges

    low[u]=min⁡(d[u], d[w] (back edge u–w), low[c] (child c))

    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

    ∣E(T)∣=V−1;cut property: a lightest edge across any cut is in some MST

    Cycle property: a strictly heaviest edge on a cycle is in no MST. Distinct edge weights ⇒ MST is unique.

  • Kruskal and Prim

    Kruskal: O(Elog⁡E)=O(Elog⁡V);Prim: O(Elog⁡V) heap, O(E+Vlog⁡V) Fibonacci, O(V2) array

    Kruskal sorts edges and uses union–find; it builds a forest. Prim grows one tree. Both work with negative weights.

  • Relaxation

    if d[v]>d[u]+w(u,v) then d[v]←d[u]+w(u,v), π[v]←u

    Every single-source shortest-path algorithm here is a schedule of relaxations; initialise d[s] = 0, others ∞.

  • Dijkstra

    O((V+E)log⁡V) binary heap,O(E+Vlog⁡V) Fibonacci heap,O(V2) array

    Correct only when all edge weights are non-negative. Array version is better for dense graphs.

  • Bellman–Ford

    V−1 passes relaxing all edges: Θ(VE)

    Handles negative edges. If a V-th pass still relaxes some edge, a negative cycle is reachable from the source.

  • DAG shortest paths

    relax edges in topological order: Θ(V+E)

    Works with negative weights (no cycles exist). Negate weights to get longest paths in a DAG.

  • Floyd–Warshall

    dij(k)=min⁡(dij(k−1), dik(k−1)+dkj(k−1));Θ(V3) time, Θ(V2) space

    All pairs; negative edges allowed. A negative cycle exists iff some dii<0 at the end. With ∨/∧ it gives transitive closure (Warshall).

Where marks come fromTopic-wise PYQ analysis →Use the formulasPractise PYQs on this subject →For numerical answersGATE virtual calculator →