datarekha

Graph Theory & Representations

A graph is just vertices joined by edges. Learn the vocabulary, the handshake fact, and the two ways to store a graph — the bedrock for every BFS/DFS question.

7 min read Intermediate GATE DA Lesson 60 of 122

What you'll learn

  • A graph G = (V, E); directed vs undirected; degree, in-degree and out-degree
  • The handshake fact: the sum of all degrees equals twice the number of edges
  • Paths, cycles, connectivity, DAGs, and a tree as a connected acyclic graph with V - 1 edges
  • Adjacency matrix (O(V squared) space, O(1) edge check) vs adjacency list (O(V + E) space, best for sparse graphs)

Before you start

The last lesson ended by tearing up the tree’s rule book: no single root, no ban on cycles, no one-parent-per-node. What you are left with is the most general relationship structure there is — a graph. A graph is simply a set of vertices (nodes) joined by edges (connections). Friendships, road networks, task dependencies, web links — all graphs; so are the dependency DAG behind every Spark or Airflow pipeline and the data feeding a graph neural network. Before any traversal algorithm makes sense, you need the vocabulary and the two standard ways to store a graph in memory. That is this lesson.

Vertices, edges, and direction

Formally a graph is a pair G = (V, E), where V is the set of vertices and E is the set of edges. Two flavours:

  • Undirected — an edge {u, v} is symmetric: if u connects to v, then v connects to u. Facebook friendships.
  • Directed (a digraph) — an edge (u, v) is an arrow from u to v and says nothing about the reverse direction. Twitter follows.

Degree counts a vertex’s edges. In an undirected graph the degree of a vertex is the number of edges touching it. In a directed graph you split this into in-degree (arrows arriving) and out-degree (arrows leaving).

Paths, cycles, connectivity, DAGs, trees

  • A path is a sequence of vertices where consecutive ones are joined by an edge.
  • A cycle is a path that returns to its start (and repeats no edge).
  • A graph is connected if there is a path between every pair of vertices.
  • A DAG (directed acyclic graph) is a directed graph with no directed cycle — the structure behind task scheduling and the only graph that can be topologically sorted (next lesson).
  • A tree is a connected acyclic graph — exactly the restricted shape from last lesson. A key fact: a tree on V vertices has exactly V − 1 edges — add one more edge and you create a cycle; remove one and it disconnects.

Two ways to store a graph

How you store a graph decides which operations are cheap. The two standard choices:

The graph1234Adjacency matrix.123412340111100110001100symmetric; O(V²) cellsAdjacency list1 → 2, 3, 42 → 1, 43 → 14 → 1, 2store only real edges; O(V + E)
The same 4-vertex graph in both representations. A 1 in cell (i, j) means an edge i to j.
  • Adjacency matrix — a V × V grid where cell (i, j) = 1 if an edge joins i and j, else 0. O(V²) space, but checking “is there an edge i-j?” is O(1). For an undirected graph the matrix is symmetric.
  • Adjacency list — for each vertex, store a list of its neighbours. O(V + E) space, which is far smaller when the graph is sparse (few edges). Listing a vertex’s neighbours is direct; an edge-existence check costs up to O(degree).

Explore the trade-off live — click an edge and watch it light up in both the list and the matrix, and toggle directed/undirected to see the matrix symmetry change:

TryGraph representations

Adjacency list vs adjacency matrix — click any edge to highlight it everywhere

Click an edge in the graph, a cell in the matrix, or a neighbour in the list. The same connection lights up in all three views at once.

Graph
ABCDE
Adjacency list
A
B
C
D
E
Adjacency matrix
ABCDE
A01100
B10011
C10010
D01101
E01010
Adjacency listO(V + E)= 5 + 12 = 17 entriesGreat for sparse graphs
vs
Adjacency matrixO(V²)= 5² = 25 cellsO(1) edge check; good for dense

How GATE asks this

Expect a NAT or MCQ. Common patterns: “given the degree sequence, how many edges?” (use the handshake fact), “how many edges in a complete graph on n vertices?” (n(n−1)/2), “how much space does an adjacency matrix need?” (O(V²)), and terminology checks on DAGs and trees. These are fast marks if the definitions are reflexes.

Worked example

Take an undirected graph on 4 vertices with edge set { (1,2), (1,3), (1,4), (2,4) } — that is 4 edges (the graph drawn above).

Step 1 — degrees. Count edges at each vertex:

deg(1) = 3   (joins 2, 3, 4)
deg(2) = 2   (joins 1, 4)
deg(3) = 1   (joins 1)
deg(4) = 2   (joins 1, 2)

Step 2 — verify the handshake fact. Sum of degrees = 3 + 2 + 1 + 2 = 8, and 2 × |E| = 2 × 4 = 8. They match.

Step 3 — representations. The adjacency matrix is the symmetric grid above; the adjacency list is 1 → 2,3,4, 2 → 1,4, 3 → 1, 4 → 1,2.

Step 4 — a complete graph. If these 4 vertices were all mutually connected, the edge count would be C(4, 2) = 4·3/2 = 6. Our graph has only 4 of those 6 possible edges, so it is not complete.

A question to carry forward

You can now store a graph and count its edges — but a graph earns its keep only once you can walk it: find a path from here to there, visit every reachable city, or order a pile of tasks so that prerequisites always come first. And walking a graph is genuinely harder than walking a tree, for one reason the last lesson forbade and this one allows: cycles. With no root to start from and loops that can send you round and round forever, exploration needs real discipline. Here is the thread into the final lesson of this chapter: how do you systematically visit a graph’s vertices — breadth-first and depth-first — without getting lost or circling endlessly, and how does that same machinery order tasks and find shortest paths?

In one breath

  • A graph G = (V, E): vertices joined by edges. Undirected (symmetric) vs directed (arrows); degree = edges touching a vertex (split into in/out for digraphs).
  • Handshake fact: sum of degrees = 2 × |E| (each edge counted at both ends).
  • Complete graph on n vertices: n(n−1)/2 edges. Tree = connected + acyclic = exactly V − 1 edges. DAG = directed + no cycle.
  • Adjacency matrix: O(V²) space, O(1) edge check (symmetric if undirected). Adjacency list: O(V + E) space, best for sparse graphs.
  • Traps: degree sum is 2|E| not |E|; a matrix is O(V²) even when sparse; a tree has V − 1 edges, not V.

Practice

Quick check

0/6
Q1Recall: which statements about adjacency representations are TRUE? (select all that apply)select all that apply
Q2Recall: which of the following are properties of a DAG (directed acyclic graph)? (select all that apply)select all that apply
Q3Trace: in a DIRECTED graph, vertex v has 2 edges pointing into it and 3 edges pointing out. What is its in-degree? (integer)numerical answer — type a number
Q4Trace: a tree has 15 vertices. How many edges does it have? (integer)numerical answer — type a number
Q5Apply: an undirected graph has 6 vertices and the degree of every vertex is 3. How many edges does it have? (integer)numerical answer — type a number
Q6Create: how many edges are in a complete undirected graph on 8 vertices? (integer)numerical answer — type a number

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Practice this in an interview

All questions

Related lessons

Explore further

Skip to content