Graphs and Traversals

Graphs model relationships as vertices and edges, stored as an adjacency matrix (fast lookup, O(V²) space) or list (compact for sparse graphs); breadth-first and depth-first traversals both run in O(V+E) and answer different questions.

Key formulas & points

Skim these first — then read the full notes below.

  • Directed versus undirected changes edge counts and traversal
  • BFS finds shortest paths in unweighted graphs
  • DFS detects cycles and produces topological order

Topic details

Introduction

This topic covers graph representation and traversal. You choose between adjacency matrix and list based on density, implement BFS with a queue and DFS with a stack or recursion, and apply them to shortest paths, cycle detection and topological sorting.

Key relations & formulas

Formulas (Indian textbook notation)

  • adjacencymatrix:O(V2)space,O(1)edgelookupadjacency matrix: O(V^{2}) space, O(1) edge lookup

Formulas (Indian textbook notation)

  • adjacencylist:O(V+E)space,O(degree)edgelookupadjacency list: O(V + E) space, O(degree) edge lookup

Formulas (Indian textbook notation)

  • BFSandDFSbothruninO(V+E)withavisitedsetBFS and DFS both run in O(V + E) with a visited set

Notation and sign conventions

Relation 1 —
adjacencymatrix:Oadjacency matrix: O

Formulas (Indian textbook notation)

  • adjacencymatrix:O(V2)space,O(1)edgelookupadjacency matrix: O(V^{2}) space, O(1) edge lookup
Write this relation with symbols exactly as in Schaum Ds — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
adjacencylist:Oadjacency list: O

Formulas (Indian textbook notation)

  • adjacencylist:O(V+E)space,O(degree)edgelookupadjacency list: O(V + E) space, O(degree) edge lookup
Write this relation with symbols exactly as in Schaum Ds — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
BFSandDFSbothruninOBFS and DFS both run in O

Formulas (Indian textbook notation)

  • BFSandDFSbothruninO(V+E)withavisitedsetBFS and DFS both run in O(V + E) with a visited set
Write this relation with symbols exactly as in Schaum Ds — Standard reference before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

A graph’s representation should match its density: matrices give constant-time edge tests but waste space on sparse graphs, while adjacency lists store only real edges and are the usual choice. Traversal order defines capability — BFS explores level by level, so in an unweighted graph the first time it reaches a vertex is along a shortest path; DFS plunges deep first, exposing back-edges that reveal cycles and enabling topological ordering of a DAG. Both visit every vertex and edge once, hence O(V+E), provided a visited set prevents revisiting in cyclic graphs.

Assumptions and validity limits

State assumptions explicitly before using any relation for graphs and traversals — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Data Structures viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Data Structures papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to graphs and traversals.
4. Use equation 1:
adjacencymatrix:Oadjacency matrix: O
.
5. Use equation 2:
adjacencylist:Oadjacency list: O
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Graphs and Traversals appears in interviews and systems programming. In Indian it software curricula this topic is tested because it connects theory to organising data for efficient algorithms.
GATE and semester exams often combine graphs and traversals with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use graphs and traversals?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students use a matrix for a huge sparse graph (wasting O(V²) space), forget the visited set (causing infinite loops on cycles), and claim BFS gives shortest paths in weighted graphs (it does not — use Dijkstra). Confusing the traversal that detects cycles (DFS) with BFS is common.

Quick revision checklist

Before attempting graphs and traversals problems, confirm you can:
1. Directed versus undirected changes edge counts and traversal
2. BFS finds shortest paths in unweighted graphs
3. DFS detects cycles and produces topological order
Revise the solved examples in Schaum Ds — Standard reference and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Traversal complexity

Problem

A graph has 1000 vertices and 5000 edges. What is the time complexity of BFS, and roughly how many operations?

Solution

BFS is O(V + E) = O(1000 + 5000) ≈ 6000 operations. Using an adjacency list keeps it linear; a matrix would force O(V²) = 10⁶.

Conceptual check — Graphs and Traversals

Problem

In a Data Structures semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of graphs and traversals." What should a complete answer include?

📖 Standard books (India)

  • Schaum DsStandard reference

    Read: Syllabus unit

    Referenced in Indian B.Tech syllabus