IAE Academy

Graph algorithms formulas

Graphs: traversals, minimum spanning trees and shortest paths. 9 formulas for GATE CS, DA Data Structures and Algorithms, each with when to use it. Each is explained in the topic notes.

All 9 Graph algorithms formulas

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.

PARENTHESIS THEOREM (FOR TWO VERTICES WITH D[U] < D[V])

Exactly one of the two holds. The first means v is a descendant of u in the DFS forest (the intervals are nested). The second means neither is a descendant of the other (the intervals are disjoint). Partial overlap, d[u] < d[v] < f[u] < f[v], is impossible.

NUMBER OF EDGES IN A SPANNING TREE OR FOREST

The first holds for any spanning tree of a connected graph on V vertices, whatever the weights. The second is for a graph with k connected components, where Kruskal run to completion produces a minimum spanning forest.

NUMBER OF LABELLED SPANNING TREES OF THE COMPLETE GRAPH K_N

For example, K_4 has 4^2 = 16 spanning trees. This counts spanning trees, not MSTs; with distinct weights, exactly one of them is the MST.

DIJKSTRA'S TIME COMPLEXITY

Correctness is guaranteed only when all edge weights are non-negative. The array version suits dense graphs (E close to V^2); the binary-heap version suits sparse graphs (E much smaller than V^2).

BELLMAN-FORD'S TIME COMPLEXITY

Each of the V−1 rounds scans all E edges once; the extra negative-cycle check costs another O(E).

FLOYD-WARSHALL RECURRENCE

d_k[i][j] is the length of the shortest path from i to j whose intermediate vertices all lie in {1,...,k}. d_0 is the input: d_0[i][i] = 0, d_0[i][j] = w(i,j) where a direct edge exists, and infinity otherwise. The answer is d_V.

FLOYD-WARSHALL COMPLEXITY

Three nested loops over all V vertices. It is correct with negative edge weights as long as there is no negative-weight cycle, and it detects one if present. Replacing min with OR and + with AND gives the transitive-closure (reachability) algorithm, also O(V^3).

Where this was asked: 2018 (2), 2020 (3), 2022 (1), 2023 (1), 2024 (6), 2025 (8), 2026 (7). See the 28 previous-year questions on Graph algorithms. This shows what was asked, not what will be asked.