Data Structures · Graphs

Representing graphs: adjacency lists and matrices

Before a program can sort tasks or search a graph, it has to store the graph. This page reviews what a directed acyclic graph is, shows the two standard ways to store any graph, and then lets you build graphs and watch each structure change. New to graphs? Start with Introduction to graphs ↗.

What is a DAG?

A graph is a set of vertices (nodes) joined by edges. In an undirected graph an edge is a two-way connection, like a friendship. In a directed graph each edge points one way, like "A must happen before B."

A directed acyclic graph (DAG) is a directed graph with no cycles: there is no way to start at a node, follow the arrows, and come back to where you started.

A DAG. A data pipeline from the case study: Ingestion → Cleaning → Transformation, which feeds both Analysis and Visualization. Every arrow points forward, so the tasks can be put in order.
Not a DAG. Following B → C → D leads back to B. If these were tasks, B would have to finish before itself, so no valid order exists.

Two ways to store a graph

Number the vertices 0, 1, 2, … so they can be used as array indexes. Then there are two standard choices:

Undirected graph

The graph. 5 vertices, 7 edges.
Adjacency list. Each edge appears twice, once in each endpoint's list, so there are 14 entries for 7 edges.
Adjacency matrix. Symmetric across the diagonal: adj[u][v] == adj[v][u].

Directed graph

The graph. The example DAG from the case study: A → B, A → C, B → D, C → D.
Adjacency list. Each edge appears once, in the list of the vertex it leaves. This is the Graph = {A: [B, C], B: [D], C: [D], D: []} from the case study.
Adjacency matrix. Row = from, column = to. It is not symmetric: adj[A][B] is 1 but adj[B][A] is 0.

Which one should you use?

OperationAdjacency listAdjacency matrix
MemoryO(V + E)O(V²)
Is there an edge u → v?O(degree of u)O(1)
Visit every neighbor of uO(degree of u)O(V)
Add an edgeO(1)O(1)
Add a vertexO(1)O(V²) to copy into a bigger grid
Good fitSparse graphs, where most pairs are not connected: task lists, road maps, social networksDense graphs, or when you check "is u connected to v?" very often
How would you choose which representation to use?Count the edges. Most real graphs, including every task graph on the case-study page, are sparse: each vertex connects to only a few others. A list stores just those edges. A matrix stores all V² pairs, and most of them are 0. DFS and topological sort visit each vertex's neighbors, which is exactly what a list is fast at.
Do you need a linked list for an adjacency list?No. "List" means any sequence. In C++ the usual choice is vector<vector<int>>, or map<string, vector<string>> when vertices have names, as in the topological sort program. The drawings below use boxes and arrows because that is how the idea is usually drawn.

Build a graph and watch the structure change

Each tab has its own graph. Click empty space to add a vertex. Click one vertex and then another to add an edge. Drag a vertex to move it. Switch to Delete to remove vertices or edges. In the matrix tabs you can also click a cell to add or remove that edge.

Edge →


        
Changes to the graph are explained here.