Skip to content
GATEiit

GATE CSE · Programming and Data Structures

Graphs

Graphs as a data structure: terms and counting rules, adjacency matrix and adjacency list storage, BFS with a queue and DFS with a stack, and the orders, costs and counts GATE asks about them.

Before you start

This topic comes before: Deadlock, Graph traversals, minimum spanning trees and shortest paths, Graphs: connectivity, matching and colouring, Local optimisation, Regular expressions and finite automata

Concept notes

1. Terms and counting rules

A graph G=(V,E) has a set of vertices and a set of edges. In an undirected graph an edge {u,v} has no direction; in a directed graph an edge (u,v) goes from u to v. A simple graph has no self-loops and no parallel edges. Below, n=∣V∣ and m=∣E∣.

  • Handshake rule: ∑vdeg⁡(v)=2m, so the number of odd-degree vertices is even. In a directed graph, ∑ in-degrees =∑ out-degrees =m.
  • A simple undirected graph has at most (n2)=n(n−1)2 edges; a simple directed graph at most n(n−1).
  • The number of simple undirected graphs on n labelled vertices is 2n(n−1)/2 (each possible edge is in or out).
  • A tree on n vertices is connected and acyclic and has exactly n−1 edges. A forest with k trees has n−k edges.
  • A graph with k connected components has at least n−k edges.
  • A DAG (directed acyclic graph) has at least one vertex of in-degree 0 and one of out-degree 0.

A graph is sparse when m is close to n and dense when m is close to n2. This decides which storage to use.

2. Storing a graph: matrix vs list

Adjacency matrix: an n×n array with A[u][v]=1 if edge (u,v) exists (or its weight). For an undirected graph it is symmetric.

Adjacency list: an array of n lists; list u holds the neighbours of u. An undirected edge appears in two lists, so there are 2m list nodes; a directed graph has m.

Task Matrix List
Space Θ(n2) Θ(n+m)
Is (u,v) an edge? O(1) O(deg⁡u)
List neighbours of u Θ(n) O(deg⁡u)
Add an edge O(1) O(1) at the head
All in-degrees (directed) Θ(n2) Θ(n+m)
BFS or DFS Θ(n2) Θ(n+m)

Use a list for sparse graphs, a matrix for dense graphs or many edge queries. An incidence matrix is n×m with a 1 where a vertex meets an edge; each column of an undirected one has exactly two 1s.

Matrix powers. (Ak)[u][v] is the number of walks of length k from u to v. For a simple undirected graph, (A2)[u][u]=deg⁡(u), so trace(A2)=2m.

ABCDEFA: B, CB: A, DC: A, D, ED: B, C, FE: C, FF: D, E
An undirected graph and its adjacency list (neighbours in alphabetical order)

3. BFS and DFS as data-structure algorithms

Breadth-first search (BFS) uses a queue. Mark the source, enqueue it; repeatedly dequeue u and enqueue every unmarked neighbour, marking it when enqueued. Vertices come out level by level, so BFS gives the fewest-edge path from the source in an unweighted graph.

Depth-first search (DFS) uses a stack (or recursion, which uses the call stack). From u, go to the first unvisited neighbour and continue from there; backtrack when stuck.

Both visit every vertex once and scan every adjacency list once: Θ(n+m) with lists, Θ(n2) with a matrix.

Checking a given order. For BFS: after the source, the unvisited neighbours of each dequeued vertex must appear as one block, and blocks must come in the same order as their parents. For DFS: each next vertex must be an unvisited neighbour of the deepest vertex on the current path that still has one.

Edge types in DFS: tree, back (to an ancestor), forward (to a descendant) and cross edges. In an undirected graph only tree and back edges occur. A graph has a cycle exactly when DFS finds a back edge (for undirected graphs, ignore the edge to the parent). In BFS of an undirected graph, the levels at the two ends of any edge differ by at most 1.

BFS with 2-colouring by level parity tests whether a graph is bipartite: it is, unless some edge joins two vertices of the same level.

4. Components and topological order

Connected components. Loop over the vertices; whenever one is unvisited, start a new BFS or DFS from it and count it. The count of starts is the number of components; total time stays Θ(n+m).

Topological order of a DAG lists the vertices so that every edge goes from earlier to later. It exists exactly when the graph has no directed cycle.

  • Kahn's method: compute in-degrees; put all in-degree-0 vertices in a queue; repeatedly remove one, output it, and decrease its neighbours' in-degrees, queueing any that reach 0. If fewer than n vertices are output, there is a cycle.
  • DFS method: output vertices in decreasing order of DFS finishing time.

A DAG can have many topological orders. To count them for a small DAG, fix what must come first and count the ways to place the rest, respecting every edge. The order is unique exactly when the DAG has a path through all vertices (a Hamiltonian path).

Detailed algorithms built on these (shortest paths, spanning trees) belong to the Algorithms subject; here the focus is the storage and the traversal mechanics.

Formulas to remember

Handshake rule
∑v∈Vdeg⁡(v)=2∣E∣
Maximum edges (simple, undirected)
(n2)=n(n−1)2
Edges in a forestk = number of trees; a tree has n−1
∣E∣=n−k
Labelled simple graphs on n vertices
2n(n−1)/2
Walks of length k
(Ak)uv=number of walks of length k from u to v
Trace of A squaredSimple undirected graph
trace⁡(A2)=∑vdeg⁡(v)=2∣E∣
BFS / DFS time
Θ(∣V∣+∣E∣) (list),Θ(∣V∣2) (matrix)

Common mistakes

  • Counting an undirected edge once in the adjacency list; it appears in both endpoints' lists (2∣E∣ nodes).
  • Quoting Θ(∣V∣+∣E∣) for BFS or DFS on an adjacency matrix; it is Θ(∣V∣2).
  • Confusing walks (vertices may repeat) with paths when reading Ak.
  • Marking a vertex in BFS when it is dequeued instead of when it is enqueued, which lets it enter the queue twice.
  • Checking a BFS order level by level only; within a level, the children of earlier vertices must come first.
  • Assuming a DAG has a unique topological order.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Matrix or list: space and time

numerical

An undirected graph has 1000 vertices and 3000 edges. How many entries does its adjacency matrix have, and how many list nodes does its adjacency list have? Which suits BFS better?

  1. Step 1: Size the matrix.

    Try it on paper first.

Example 2: BFS and DFS orders on the same graph

numerical

Use the graph in the figure, with neighbours taken in alphabetical order. Starting at A, give the BFS order, the recursive DFS order, and the fewest number of edges from A to F.

  1. Step 1: Run BFS with a queue.

    Try it on paper first.

Example 3: Counting walks with the adjacency matrix

numerical

For the graph in the figure, find the number of walks of length 2 from A to D, the value of (A2)AA, and trace⁡(A2).

  1. Step 1: Walks of length 2 from A to D go through a common neighbour. List them.

    Try it on paper first.

Example 4: Counting topological orders

numerical

A DAG on vertices 1 to 6 has edges 1→2, 1→3, 2→4, 3→4, 4→5, 3→6. How many topological orders does it have?

  1. Step 1: What must come first, and what is forced after it?

    Try it on paper first.

Previous year questions

No official GATE CSE 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 CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.