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 AVisual: undirected graph
Edges: (A,B), (A,C), (B,D), (C,D), (D,E) — bidirectional
Core vocabulary
A point in the graph. Represents an entity: person, city, webpage, task.
A connection between two vertices. Can carry a weight (cost, distance, capacity).
Number of edges touching a vertex. In-degree = edges coming in. Out-degree = edges going out.
A sequence of vertices where each consecutive pair is connected by an edge.
A path that starts and ends at the same vertex, visiting no vertex twice (except start=end).
An undirected graph where every vertex is reachable from every other vertex.
A maximal connected subgraph. Disconnected graphs have multiple components.
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
GPS navigation — Dijkstra's algorithm on weighted road graph
Finding isolated networks in an infrastructure graph
Build systems — ordering tasks respecting dependencies (DAG)
Deadlock detection in OS process graphs
Laying cable to connect all offices with minimum wire
Network bandwidth capacity, bipartite matching
Size notation
O(V) time
O(E) time
O(V + E) time
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=5Adjacency List
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)?
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.