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.
A graph has a set of vertices and a set of edges. In an undirected graph an edge has no direction; in a directed graph an edge goes from to . A simple graph has no self-loops and no parallel edges. Below, and .
Handshake rule: , so the number of odd-degree vertices is even. In a directed graph, in-degrees out-degrees .
A simple undirected graph has at most edges; a simple directed graph at most .
The number of simple undirected graphs on labelled vertices is (each possible edge is in or out).
A tree on vertices is connected and acyclic and has exactly edges. A forest with trees has edges.
A graph with connected components has at least 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 is close to and dense when is close to . This decides which storage to use.
2. Storing a graph: matrix vs list
Adjacency matrix: an array with if edge exists (or its weight). For an undirected graph it is symmetric.
Adjacency list: an array of lists; list holds the neighbours of . An undirected edge appears in two lists, so there are list nodes; a directed graph has .
Task
Matrix
List
Space
Is an edge?
List neighbours of
Add an edge
at the head
All in-degrees (directed)
BFS or DFS
Use a list for sparse graphs, a matrix for dense graphs or many edge queries. An incidence matrix is with a 1 where a vertex meets an edge; each column of an undirected one has exactly two 1s.
Matrix powers. is the number of walks of length from to . For a simple undirected graph, , so .
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 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 , go to the first unvisited neighbour and continue from there; backtrack when stuck.
Both visit every vertex once and scan every adjacency list once: with lists, 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 .
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 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
Maximum edges (simple, undirected)
Edges in a forest = number of trees; a tree has
Labelled simple graphs on n vertices
Walks of length k
Trace of A squaredSimple undirected graph
BFS / DFS time
Common mistakes
Counting an undirected edge once in the adjacency list; it appears in both endpoints' lists ( nodes).
Quoting for BFS or DFS on an adjacency matrix; it is .
Confusing walks (vertices may repeat) with paths when reading .
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?
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.
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 , and .
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?
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.