Chapter 19 · Graphs

Graphs

A graph G = (V, E) is a set of vertices V connected by edges E. Trees are a special case of graphs. Graphs model anything with relationships: social networks, road maps, dependency systems, the internet itself. Understanding graphs unlocks a huge class of algorithmic problems.

What is a graph data structure?

A graph is a set of nodes (vertices) connected by edges. Edges can be directed (one-way, like following someone) or undirected (two-way, like friendship), and weighted (with a cost such as distance) or unweighted. Graphs model networks of every kind: maps, social networks, the web, dependencies and recommendations.

// Undirected, unweighted graph as an adjacency list
const graph = new Map([
  ["A", ["B", "C"]],
  ["B", ["A", "D"]],
  ["C", ["A"]],
  ["D", ["B"]],
]);
graph.get("A");   // neighbours of A

Visual: undirected graph

AB
edge(A,B)
CDE

Edges: (A,B), (A,C), (B,D), (C,D), (D,E) — bidirectional

Core vocabulary

Vertex (node)

A point in the graph. Represents an entity: person, city, webpage, task.

Edge

A connection between two vertices. Can carry a weight (cost, distance, capacity).

Degree

Number of edges touching a vertex. In-degree = edges coming in. Out-degree = edges going out.

Path

A sequence of vertices where each consecutive pair is connected by an edge.

Cycle

A path that starts and ends at the same vertex, visiting no vertex twice (except start=end).

Connected

An undirected graph where every vertex is reachable from every other vertex.

Component

A maximal connected subgraph. Disconnected graphs have multiple components.

Strongly connected

Directed graph where every vertex can reach every other vertex via directed paths.

Types of graphs

Undirected

Edges have no direction. (A,B) == (B,A). Examples: friendship graph, road network.

Directed (Digraph)

Edges have direction. (A,B) means A points to B only. Examples: Twitter follows, web links.

Weighted

Each edge has a numeric weight (cost, distance). Used in shortest path algorithms.

Unweighted

All edges are equal. BFS finds shortest path in hop count.

Cyclic

Contains at least one cycle. Most real-world graphs are cyclic.

Acyclic (DAG)

Directed Acyclic Graph. No directed cycles. Used for task dependencies, build systems.

Dense

Number of edges close to V^2. Adjacency matrix is efficient.

Sparse

Few edges relative to V^2. Most real graphs are sparse. Adjacency list is efficient.

Real-world graph problems

Shortest path

GPS navigation — Dijkstra's algorithm on weighted road graph

Connected components

Finding isolated networks in an infrastructure graph

Topological sort

Build systems — ordering tasks respecting dependencies (DAG)

Cycle detection

Deadlock detection in OS process graphs

Minimum spanning tree

Laying cable to connect all offices with minimum wire

Max flow

Network bandwidth capacity, bipartite matching

Size notation

space for vertex data — V = number of verticesO(V)

O(V) time

space for edge data — E = number of edgesO(E)

O(E) time

BFS / DFS traversal — visit every vertex and edge onceO(V + E)

O(V + E) time

adjacency matrix space — V rows × V columnsO(V^2)

O(V^2) time

Live demo — directed graph builder

Add and remove directed edges between nodes A–E. Watch how the adjacency list representation updates in real time.

Directed Graph — adjacency list

V=5, E=5
ABCDE

Adjacency List

A→BC
B→D
C→D
D→E
E→∅
fromto

Interactive sandbox — code a Graph

Establish vertices and insert directional links programmatically inside this interactive graph representation workspace. Compile and run live!

Code Editor

<pre id="out"></pre>

Preview

🎯 Quick Check

Which type of graph is best modeled by a Directed Acyclic Graph (DAG)?

🔍Trees are special graphs
Every tree is a connected acyclic undirected graph with exactly V-1 edges. Binary trees, BSTs, heaps — all are trees and thus graphs. Graph algorithms like DFS and BFS work on trees too. But most graph algorithms handle cases trees do not: cycles, disconnected components, directed edges, and weights.

Graphs: summary

Key points

  • Degree is the number of edges at a node; directed graphs have in-degree and out-degree.
  • A cycle is a path that returns to its start; a DAG is a directed acyclic graph.
  • BFS finds the shortest paths in unweighted graphs; Dijkstra's algorithm handles weighted ones.

Frequently asked questions

›Is a tree a graph?

Yes: a tree is a connected, acyclic, undirected graph.