Courses / Computer Science I
Data Structures

Graph Representations

Computer Science I 202 words Free to read

Graph Basics

A graph models things and their connections: vertices (nodes) joined by edges. They represent road maps, social networks, and dependency chains.

Edges can be directed (one-way) or undirected (mutual), and may carry a weight representing distance, cost, or capacity.

PropertyDescription
Vertices (VV)The nodes being connected
Edges (EE)The connections between nodes
WeightCost, distance, or capacity

Common Pitfall: Using an adjacency matrix for a large sparse graph. A million-node graph with few edges wastes O(V2)O(V^2) space on empty cells.

Memory Trade-offs

Choosing a graph representation is a classic space-time trade-off between two standard structures.

An adjacency matrix is a V×VV \times V grid where entry (i,j)(i, j) records the connection. It allows O(1)O(1) edge checking, suiting dense graphs.

An adjacency list stores each vertex's neighbors. It uses O(V+E)O(V + E) space, suiting sparse graphs.

RepresentationSpaceCheck edgeBest for
Adjacency matrixO(V2)O(V^2)O(1)O(1)Dense graphs
Adjacency listO(V+E)O(V + E)O(degu)O(\deg u)Sparse graphs
Graph Representations

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes

Data Structures