Adjacency matrix
An n × n table for a graph of n vertices: cell (i, j) is 1 if there's an edge between i and j, and 0 if not. Checking an edge is O(1), but it takes O(n²).
Example
A 100-vertex graph takes 10,000 cells, even if it has only 150 edges.
Test yourself: Adjacency matrix
Graphs
An adjacency matrix for a graph with 100 vertices has ___ cells.
100 × 100 = 10,000, even if the graph has only 150 edges. That's why it takes O(n²).
Graph operations
In an undirected graph with a matrix, adding edge 2-5 changes ___ cells.
M[2][5] and M[5][2] become 1. Still O(1): 2 changes, whether the graph has 10 or 10,000 vertices.
Graph operations
Removing the first vertex of a 100-vertex matrix shifts about ___ cells.
(100 − 1)² = 9,801: everything to the right and below shifts 1 spot. That's why it's O(n²).
0/3
▶ Play