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.
A graph has a set of vertices () and a set of edges (). In an undirected graph an edge is an unordered pair ; in a directed graph it is an ordered pair . A simple graph has no self-loops and no repeated edges. Unless stated otherwise, GATE graphs are simple.
The degree is the number of edges at . In a directed graph each vertex has an in-degree and an out-degree.
Handshake lemma: , 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, .
A -regular graph (every degree ) on vertices has edges, so must be even.
Important graphs:
Graph
Edges
Note
Complete
every pair joined; -regular
Cycle
2-regular
Path ( vertices)
Complete bipartite
A simple graph on vertices has at most edges; there are simple graphs on labelled vertices.
A list of numbers is a degree sequence of a simple graph only if its sum is even, no entry exceeds , and it passes the Havel–Hakimi test: remove the largest entry , subtract 1 from the next 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 vertices and components has at least edges. So a connected graph needs at least .
With components, the most edges is : one big complete graph on vertices plus isolated vertices.
A simple graph with more than 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 has exactly the edges missing from , so .
3. Trees, forests and bipartite graphs
A tree is a connected graph with no cycle. For a graph on vertices, any two of these imply the third, and together they mean it is a tree:
connected; 2. acyclic; 3. has exactly 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 vertices has at least two leaves (degree 1). Using the handshake lemma, lets you find the number of leaves.
A forest (acyclic graph) with trees has edges.
A spanning tree of a connected graph is a subgraph that is a tree on all vertices. Cayley's formula: has spanning trees, which is also the number of labelled trees on 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, is bipartite exactly when is even, and is bipartite only for . Bipartiteness is tested by 2-colouring during BFS.
A bipartite graph with parts of size and has at most edges, so a bipartite graph on vertices has at most edges.
4. Representing a graph
Adjacency matrix: an matrix with if there is an edge from to , else 0. For an undirected graph is symmetric with a zero diagonal (no loops), and row sums to .
Adjacency list: for each vertex, a list of its neighbours. Total length is (undirected) or (directed).
Matrix
List
Space
Is an edge?
List neighbours of
Visit all edges
Best for
dense graphs
sparse graphs
Walks from matrix powers: is the number of walks of length from to . In particular:
for a simple undirected graph.
The number of triangles is , 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'], ...}.
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: has an Euler circuit exactly when is odd (every degree is even). has one when and 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: is Hamiltonian for ; is; a tree never is. A sufficient condition (Dirac): if and every vertex has degree at least , the graph is Hamiltonian.
Keep the two apart: Euler is about edges, Hamilton about vertices.
Formulas to remember
Handshake lemma
Edges of K_n
Edges of a forest with k trees
Most edges with k components
Cayley's formulaLabelled trees on vertices, or spanning trees of
Walks of length k
Triangles in an undirected graph
Common mistakes
Forgetting the factor 2 in the handshake lemma: the degree sum is , not .
Assuming any 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 ; each one contributes 6.
Using the adjacency matrix size for sparse graphs when the question asks for .
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?
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 and , find the number of walks of length 2 from b to d, and the number of triangles.
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?
Step 1: Where should the edges go?
Try it on paper first.
Example 4: Euler circuit or Euler path?
conceptual
Decide whether , and have an Euler circuit, only an Euler path, or neither.
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.