IAE Academy

GATE Data Structures and Algorithms formula sheet

75 formulas in 7 topics, each with when to use it. Each is explained in the topic notes.

Print the whole sheet

1-D ARRAY ELEMENT ADDRESS

w = size of one element in bytes; L = lower bound of the index (L = 0 for a C array, giving Base + i*w); Base = address of the first element A[L].

2-D ARRAY ADDRESS (ROW-MAJOR, 0-INDEXED)

cols = number of columns. C stores 2-D arrays row-major, and these notes assume row-major unless a question says column-major. For column-major, whole columns are stored one after another: Base + (j*rows + i)*w, where rows = number of rows.

MAXIMUM NODES AT A GIVEN LEVEL

Root at level 0. An upper bound for any binary tree, reached exactly when level is completely filled.

MAXIMUM NODES IN A TREE OF GIVEN HEIGHT

Reached exactly by a perfect binary tree of height (every level completely filled).

BIG-O (UPPER BOUND)

grows no faster than , up to a constant factor.

BIG-OMEGA (LOWER BOUND)

grows at least as fast as , up to a constant factor.

LINEAR SEARCH: NUMBER OF KEY COMPARISONS

The successful-search average assumes the key is present and equally likely to be at each of the positions. Both the worst case and the average case are .

BINARY SEARCH COMPLEXITY

Convention: each probe of a[mid] counts as one (three-way) comparison. The worst case holds for both successful and unsuccessful search. If a question counts the == test and the < test as separate comparisons, recount under its rule. Valid only for a sorted array with random access.

FRACTIONAL KNAPSACK

Applies only when any fraction of an item may be taken. Solved by sorting items on in descending order and filling greedily until capacity is used up; only the last item taken can be partial. Time .

EXPECTED (AVERAGE) CODE LENGTH

is the probability of symbol . If frequencies are given, divide each by the total frequency. is its codeword length (the depth of its leaf). Huffman's construction minimises over all prefix-free codes for the given distribution. When weights tie, different trees are possible, but they all have the same minimum .

SPACE COST OF THE TWO REPRESENTATIONS

Use the matrix when the graph is dense (E close to V^2) or constant-time edge checks are needed; use the list when the graph is sparse (E much smaller than V^2). An undirected graph's adjacency lists hold 2E entries in total (each edge appears in both endpoints' lists); a directed graph's hold E.

HANDSHAKE LEMMA AND EDGE BOUNDS (SIMPLE GRAPHS)

The degree sum counts every undirected edge once from each end. Consequence: every undirected graph has an even number of odd-degree vertices. In a directed graph, the sum of in-degrees = the sum of out-degrees = E.

FLOOR DIVISION AND REMAINDER IN PYTHON

Holds for integers with b nonzero; a // b is the floor of the true quotient, so a nonzero remainder takes the sign of b.

NUMBER OF VALUES IN A RANGE

Holds for integers a, b and any nonzero step k, positive or negative; for example len(range(3, 20, 4)) = ceil(17/4) = 5 and len(range(5, 0, -2)) = ceil(2.5) = 3.