Skip to content
GATEiit

GATE DA · Programming, Data Structures and Algorithms

Introduction to graph theory

Graph vocabulary for GATE DA: degrees and the handshake lemma, paths, cycles and connectivity, trees and forests, bipartite and complete graphs, Euler circuits, and adjacency matrix and list representations.

Before you start

This topic comes before: Basic graph algorithms: traversals and shortest path, Reasoning under uncertainty: conditional independence representation

Concept notes

1. Graphs, degrees and the handshake lemma

A graph G=(V,E) has a set of vertices V (n=∣V∣) and a set of edges E (m=∣E∣). In an undirected graph an edge is an unordered pair {u,v}; in a directed graph it is an ordered pair (u,v). A simple graph has no self-loops and no repeated edges. Unless stated otherwise, GATE graphs are simple.

The degree deg⁡(v) is the number of edges at v. In a directed graph each vertex has an in-degree and an out-degree.

Handshake lemma: ∑vdeg⁡(v)=2m, because each edge adds 1 to the degree of both its ends. Consequences:

  • The number of odd-degree vertices is even.
  • In a directed graph, ∑in-deg=∑out-deg=m.
  • A k-regular graph (every degree k) on n vertices has nk/2 edges, so nk must be even.

Important graphs:

Graph Edges Note
Complete Kn (n2)=n(n−1)2 every pair joined; (n−1)-regular
Cycle Cn n 2-regular
Path Pn (n vertices) n−1
Complete bipartite Kp,q pq

A simple graph on n vertices has at most (n2) edges; there are 2(n2) simple graphs on n labelled vertices.

A list of numbers is a degree sequence of a simple graph only if its sum is even, no entry exceeds n−1, and it passes the Havel–Hakimi test: remove the largest entry d, subtract 1 from the next d largest, and repeat.

2. Walks, paths, cycles and connectivity

A walk is a sequence of vertices joined by edges; a path repeats no vertex; a cycle is a closed path of length at least 3. The length is the number of edges.

An undirected graph is connected if there is a path between every pair of vertices. Its maximal connected pieces are connected components.

  • A graph with n vertices and k components has at least n−k edges. So a connected graph needs at least n−1.
  • With k components, the most edges is (n−k+12): one big complete graph on n−k+1 vertices plus k−1 isolated vertices.
  • A simple graph with more than (n−12) edges must be connected.

A cut vertex (articulation point) is one whose removal increases the number of components; a bridge is such an edge. An edge is a bridge if and only if it lies on no cycle.

For directed graphs: strongly connected means a directed path from every vertex to every other; weakly connected means connected when directions are ignored. A DAG (directed acyclic graph) has no directed cycle, and its vertices can be listed in topological order (every edge goes forward).

A subgraph uses a subset of vertices and edges; an induced subgraph keeps every edge between the chosen vertices. The complement Gˉ has exactly the edges missing from G, so m+mˉ=(n2).

3. Trees, forests and bipartite graphs

A tree is a connected graph with no cycle. For a graph on n vertices, any two of these imply the third, and together they mean it is a tree:

  1. connected; 2. acyclic; 3. has exactly n−1 edges.

Other facts:

  • Between any two vertices of a tree there is exactly one path. Every edge is a bridge; adding any edge creates exactly one cycle.
  • A tree with n≥2 vertices has at least two leaves (degree 1). Using the handshake lemma, ∑deg⁡=2(n−1) lets you find the number of leaves.
  • A forest (acyclic graph) with k trees has n−k edges.
  • A spanning tree of a connected graph is a subgraph that is a tree on all n vertices. Cayley's formula: Kn has nn−2 spanning trees, which is also the number of labelled trees on n vertices.

A graph is bipartite if its vertices split into two sets with every edge going between them. A graph is bipartite if and only if it has no odd-length cycle. So every tree is bipartite, Cn is bipartite exactly when n is even, and Kn is bipartite only for n≤2. Bipartiteness is tested by 2-colouring during BFS.

A bipartite graph with parts of size p and q has at most pq edges, so a bipartite graph on n vertices has at most ⌊n2/4⌋ edges.

4. Representing a graph

Adjacency matrix: an n×n matrix A with Auv=1 if there is an edge from u to v, else 0. For an undirected graph A is symmetric with a zero diagonal (no loops), and row u sums to deg⁡(u).

Adjacency list: for each vertex, a list of its neighbours. Total length is 2m (undirected) or m (directed).

Matrix List
Space Θ(n2) Θ(n+m)
Is (u,v) an edge? O(1) O(deg⁡u)
List neighbours of u Θ(n) Θ(deg⁡u)
Visit all edges Θ(n2) Θ(n+m)
Best for dense graphs sparse graphs

Walks from matrix powers: (Ak)uv is the number of walks of length k from u to v. In particular:

  • (A2)uu=deg⁡(u) for a simple undirected graph.
  • The number of triangles is trace⁡(A3)6, since each triangle gives 6 closed walks of length 3 (3 starting points × 2 directions).

In Python a graph is usually a dict of lists: g = {'a': ['b', 'c'], 'b': ['a'], ...}.

abcdabcda0111b1010c1101d1010Row sums = degrees 3, 2, 3, 2 (total 10 = 2 × 5 edges)
A graph on 4 vertices and its adjacency matrix

5. Euler and Hamiltonian graphs

An Euler circuit is a closed walk that uses every edge exactly once; an Euler path does the same but may end at a different vertex.

For a connected undirected graph (ignoring isolated vertices):

Odd-degree vertices Result
0 Euler circuit exists
2 Euler path exists (from one odd vertex to the other), no circuit
more than 2 Neither

Examples: Kn has an Euler circuit exactly when n is odd (every degree n−1 is even). Kp,q has one when p and q are both even.

For a connected directed graph, an Euler circuit exists if and only if every vertex has in-degree equal to out-degree.

A Hamiltonian cycle visits every vertex exactly once and returns to the start. Unlike the Euler case there is no simple degree test, and deciding it is NP-complete. Known cases: Kn is Hamiltonian for n≥3; Cn is; a tree never is. A sufficient condition (Dirac): if n≥3 and every vertex has degree at least n/2, the graph is Hamiltonian.

Keep the two apart: Euler is about edges, Hamilton about vertices.

Formulas to remember

Handshake lemma
∑v∈Vdeg⁡(v)=2m
Edges of K_n
(n2)=n(n−1)2
Edges of a forest with k trees
m=n−k
Most edges with k components
(n−k+12)=(n−k)(n−k+1)2
Cayley's formulaLabelled trees on n vertices, or spanning trees of Kn
n n−2
Walks of length k
(Ak)uv
Triangles in an undirected graph
trace⁡(A3)6

Common mistakes

  • Forgetting the factor 2 in the handshake lemma: the degree sum is 2m, not m.
  • Assuming any n−1 edges make a tree; the graph must also be connected (or acyclic).
  • Mixing up Euler (every edge once) with Hamiltonian (every vertex once).
  • Counting a triangle once in trace⁡(A3); each one contributes 6.
  • Using the adjacency matrix size n2 for sparse graphs when the question asks for Θ(n+m).

Worked examples

Try each step yourself, then reveal the working.

Example 1: Using the handshake lemma

numerical

A simple graph has 10 edges. Six vertices have degree 3 and every other vertex has degree 2. How many vertices does it have?

  1. Step 1: Write the degree sum.

    Try it on paper first.

Example 2: Counting walks with the adjacency matrix

numerical

The graph has edges a–b, b–c, c–d, d–a and a–c (see the figure). Using A2 and A3, find the number of walks of length 2 from b to d, and the number of triangles.

  1. Step 1: Compute the (b, d) entry of A².

    Try it on paper first.

Example 3: Most edges in a disconnected graph

numerical

A simple graph has 8 vertices and exactly 3 connected components. What is the largest possible number of edges?

  1. Step 1: Where should the edges go?

    Try it on paper first.

Example 4: Euler circuit or Euler path?

conceptual

Decide whether K4, K5 and K2,3 have an Euler circuit, only an Euler path, or neither.

  1. Step 1: List the degrees.

    Try it on paper first.

Previous year questions

No official GATE DA question is tagged with this topic yet. The topic test below uses practice questions.

Topic test

8 questions: original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.