∑GATE DA Atlas

Introduction to Graph Theory

Graphs and their vocabulary (directed, undirected, weighted, simple), degrees and the handshaking lemma, walks, paths, cycles and connectivity, special graphs (complete, bipartite, trees and forests, DAGs), adjacency matrix and adjacency list representations with their costs, counting walks with matrix powers, Euler and Hamiltonian paths, planarity and Euler's formula, and colouring.

Section 410 practice questions

Graphs

A graph G=(V,E)G = (V, E) is a set of vertices (nodes) VV and edges EE connecting pairs of vertices. Graphs model networks of every kind: roads, social links, web pages, dependencies, state spaces in AI search.

KindEdges are
Undirectedunordered pairs {u,v}\{u, v\}
Directed (digraph)ordered pairs (u,v)(u, v), "arcs"
Weightedlabelled with a cost/length w(u,v)w(u, v)
Simpleno self-loops, no parallel edges
Multigraphparallel edges allowed

A simple undirected graph has at most (n2)=n(n−1)2\binom n2 = \frac{n(n-1)}{2} edges; a simple digraph at most n(n−1)n(n-1). Dense graphs have E=Θ(V2)E = \Theta(V^2); sparse ones have E=O(V)E = O(V).

Degrees

  • Undirected: deg⁡(v)\deg(v) = number of incident edges. Handshaking lemma: ∑vdeg⁡(v)=2∣E∣\sum_v\deg(v) = 2\lvert E\rvert, so the number of odd-degree vertices is even.
  • Directed: in-degree and out-degree, with ∑deg⁡−(v)=∑deg⁡+(v)=∣E∣\sum\deg^-(v) = \sum\deg^+(v) = \lvert E\rvert.
  • kk-regular: every vertex has degree kk (then ∣E∣=nk/2\lvert E\rvert = nk/2).

Walks, paths, cycles, connectivity

TermDefinition
Walksequence of vertices joined by edges (repeats allowed)
Trailwalk with no repeated edge
Pathwalk with no repeated vertex
Cycleclosed path (length ≥ 3 in simple graphs)
Connecteda path between every pair of vertices
Connected componentmaximal connected subgraph
Strongly connected (digraph)directed path both ways between every pair
Distance d(u,v)d(u, v)length of a shortest path
Diameterlargest distance between two vertices

A graph with nn vertices and cc components has at least n−cn - c edges.

Special graphs

K₄ K₂,₃ (bipartite) tree DAG
Complete, bipartite, tree and directed acyclic graphs.
GraphKey facts
Complete KnK_n(n2)\binom n2 edges, every vertex degree n−1n - 1
Bipartitevertices split into two sides, edges only across; ⇔ no odd cycle ⇔ 2-colourable; Km,nK_{m,n} has mnmn edges
Treeconnected, acyclic, n−1n - 1 edges, a unique path between any two vertices; adding any edge creates exactly one cycle
Forestacyclic; n−cn - c edges for cc trees
DAGdirected, no directed cycle; has a topological order
Cycle CnC_n, path PnP_nnn and n−1n - 1 edges

A spanning tree of a connected graph is a subgraph that is a tree containing all vertices (n−1n - 1 edges). By Cayley's formula, KnK_n has nn−2n^{n-2} spanning trees.

Representations

Python
# vertices 0..3, undirected edges 0-1, 0-2, 1-2, 2-3
adj_list = {0: [1, 2], 1: [0, 2], 2: [0, 1, 3], 3: [2]}

adj_matrix = [
    [0, 1, 1, 0],
    [1, 0, 1, 0],
    [1, 1, 0, 1],
    [0, 0, 1, 0],
]
OperationAdjacency matrixAdjacency list
SpaceΘ(V2)\Theta(V^2)Θ(V+E)\Theta(V + E)
Is (u,v)(u, v) an edge?O(1)O(1)O(deg⁡u)O(\deg u)
Iterate over neighbours of uuΘ(V)\Theta(V)Θ(deg⁡u)\Theta(\deg u)
Visit all edgesΘ(V2)\Theta(V^2)Θ(V+E)\Theta(V + E)
Best fordense graphs, matrix algebrasparse graphs, traversals

For undirected graphs the adjacency matrix is symmetric; for weighted graphs store w(u,v)w(u, v) (∞ or None for no edge).

Counting walks: (Ak)ij(A^k)_{ij} is the number of walks of length exactly kk from ii to jj. trace⁡(A3)/6\operatorname{trace}(A^3)/6 counts the triangles of an undirected graph.

Euler and Hamiltonian

Euler (every edge once)Hamiltonian (every vertex once)
Circuit/cycle existsconnected (ignoring isolated vertices) and all degrees evenno simple test — NP-complete
Path existsexactly 0 or 2 vertices of odd degreeNP-complete
AlgorithmHierholzer, O(E)O(E)exhaustive search / DP O(2nn2)O(2^n n^2)

Planarity and colouring

  • A graph is planar if it can be drawn without crossing edges. For connected planar graphs, Euler's formula V−E+F=2V - E + F = 2 holds, and if V≥3V \ge 3, E≤3V−6E \le 3V - 6 (E≤2V−4E \le 2V - 4 if there are no triangles). Hence K5K_5 (E=10>9E = 10 > 9) and K3,3K_{3,3} (E=9>8E = 9 > 8) are not planar.
  • A proper vertex colouring gives adjacent vertices different colours; the minimum number of colours is the chromatic number χ(G)\chi(G). χ(Kn)=n\chi(K_n) = n; χ=2\chi = 2 iff bipartite (with at least one edge); every planar graph is 4-colourable. Colouring models scheduling and register allocation.

Quick revision

Practice questions

GATE-style questions: 4 one-mark and 6 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.

Q1NAT1 mark

An undirected graph has 5 vertices with degrees 3, 3, 2, 2, 2. The number of edges is ______.

Q2NAT1 mark

The number of edges in the complete graph K6K_6 is ______.

Q3MCQ1 mark

A tree with nn vertices has exactly

Q4MSQ2 marks

Which statements about finite simple undirected graphs are true?

One or more options are correct.

Q5NAT2 marks

A connected planar graph has 10 vertices and 15 edges. The number of faces in any planar drawing (including the outer face) is ______.

Q6NAT2 marks

The number of distinct simple undirected graphs on 4 labelled vertices is ______.

Q7MCQ2 marks

Which of these graphs has an Euler circuit (a closed walk using every edge exactly once)?

Q8NAT2 marks

For the 4-cycle C4C_4 with vertices 1–2–3–4–1 and adjacency matrix AA, the entry (A2)13(A^2)_{13} is ______.

Q9NAT2 marks

A forest (acyclic undirected graph) has 10 vertices and 7 edges. The number of connected components (trees) is ______.

Q10MCQ1 mark

The space needed by an adjacency-list representation of a graph with VV vertices and EE edges is