Representations Ready
vertices 0
edges 0
cells used 0
  • Current
  • Looking at
  • Done
  • Edge / neighbour

Ready

Comparisons 0 Shifts 0
Ready

Edges are written A-B (letters A–H, up to 12 edges) · Keyboard: ← → step · Space play / pause · R reset

How it works

Every operation, in plain steps

These are exactly the steps the visualizer animates.

Three ways to store a graph

An ADJACENCY LIST keeps, for every vertex, the list of its neighbours — small when there are few edges. An ADJACENCY MATRIX is a V × V table with a 1 where an edge exists — one look answers "is there an edge u → v?", but it always uses V² cells. An INCIDENCE MATRIX is a V × E table with one COLUMN per edge — every edge points to its two ends. Undirected edges put 1 at both ends; directed edges put −1 where the edge leaves and +1 where it arrives.

// Adjacency list: adj[u] = the neighbours of u
int adj[V][V];  int deg[V];     // adj[u][0 .. deg[u]-1]

// Adjacency matrix: m[u][v] = 1 if there is an edge u -> v
int m[V][V];

// Incidence matrix: one column per edge
int inc[V][E];     // undirected: 1 at both ends
                   // directed:  -1 at the tail (leaves), +1 at the head (arrives)

// An undirected edge A - B is stored twice in the list and the matrix
// (A -> B and B -> A) but only once in the incidence matrix.

Adjacency list

  1. One list per vertex, holding its neighbours.
  2. Adding an edge appends to a list (twice if undirected).
  3. Memory: V + E entries (V + 2E if undirected).
  4. The neighbours of u are just the list of u.

Adjacency matrix

  1. A V × V table, every cell starts at 0.
  2. Edge u → v sets m[u][v] = 1 (undirected: m[v][u] too).
  3. Is there an edge? One look at one cell: O(1).
  4. Memory: always V², however few edges.

Incidence matrix

  1. One ROW per vertex, one COLUMN per edge.
  2. Undirected: put 1 in the rows of the two ends.
  3. Directed: −1 where the edge leaves, +1 where it arrives.
  4. Memory: V × E cells. Finding an edge means scanning columns.

Directed vs undirected

  1. Undirected adjacency matrix is symmetric: m[u][v] = m[v][u].
  2. Directed: row u shows where u points, column v shows who points at v.
  3. Every incidence column of an undirected graph has exactly two 1s.
  4. Every directed column has one −1 and one +1.

Weights

  1. List: store (neighbour, weight) pairs.
  2. Matrix: store the weight instead of 1 (∞ or 0 for "no edge").
  3. Incidence: store the weight in the end cells.
  4. Floyd–Warshall starts from exactly such a weight matrix.

Which one?

  1. Few edges (sparse), most real graphs: adjacency list.
  2. Many edges or constant edge checks: adjacency matrix.
  3. Algorithms that work edge by edge (cuts, flows): incidence matrix.
  4. Search and shortest paths on big graphs: adjacency list.
Algorithm

Pseudocode

Language-independent steps. The visualizer shows the same logic in C, Python and Java.

1 ADD-EDGE-LIST(u, v)
2 append v to adj[u]
3 if the graph is undirected then append u to adj[v]
Complexity

How fast is each operation?

V = vertices, E = edges, degree = the neighbours of one vertex. The list is the smallest for sparse graphs; the matrix answers edge questions instantly.

Operation Best Average Worst Speed
Add an edge (list or matrix) O(1) O(1) O(1) Instant
Add an edge (incidence matrix) O(V) O(V) O(V) Grows with n
Is there an edge u–v? (list) O(1) O(degree) O(V) Grows with n
Is there an edge u–v? (matrix) O(1) O(1) O(1) Instant
Is there an edge u–v? (incidence) O(1) O(E) O(E) Grows with n
Neighbours of u (list) O(1) O(degree) O(V) Grows with n
Neighbours of u (matrix) O(V) O(V) O(V) Grows with n
Neighbours of u (incidence) O(E) O(E) O(E) Grows with n

The three representations

Adjacency list Adjacency matrix Incidence matrix
Shape A list for every vertex V × V table V × E table
Memory O(V + E) O(V²) O(V × E)
Is u joined to v? Scan u's list: O(degree) One cell: O(1) Scan the columns: O(E)
All neighbours of u O(degree) — the list O(V) — a whole row O(E) — every column
Add an edge O(1) O(1) O(V) — a new column
Undirected edge stored Twice (u in v, v in u) Twice (m[u][v] and m[v][u]) Once (one column, two 1s)
Best for Sparse graphs — most real graphs Dense graphs, quick edge checks Edge-centred algorithms

Directed incidence matrix: reading a column

Entry in the column of edge u → v Meaning
−1 in the row of u The edge LEAVES u (u is the tail)
+1 in the row of v The edge ARRIVES at v (v is the head)
0 everywhere else The edge does not touch that vertex

Some books use the opposite signs — what matters is that the two ends get opposite signs, so the column adds up to 0.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 8 correct
Question 1

An undirected graph has 5 edges. How many entries does its adjacency list have in total?

Question 2

A graph has 1,000 vertices and only 2,000 edges. Which representation uses the least memory?

Question 3

Which representation answers "is there an edge from u to v?" in O(1)?

Question 4

The adjacency matrix of an undirected graph is always…

Question 5

In the incidence matrix of an undirected graph, how many non-zero entries does each column have?

Question 6

In a directed incidence matrix, the column of the arrow A → B has…

Question 7

What is the size of the incidence matrix of a graph with 6 vertices and 9 edges?

Question 8

To find ALL the neighbours of a vertex u in an adjacency matrix, you must…

Assessment

Graphs quiz

Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.