Data Structures · Visualization

Graph: Introduction

Graph

A graph is a collection of nodes (called vertices) connected by links (called edges). Time: Varies widely by operation and representation (adjacency list vs matrix). Space: O(V + E) with an adjacency list, O(V^2) with an adjacency matrix.

Big-O: BFS/DFS visit each vertex and edge once: O(V + E).

Graph: Introduction

Graph

A graph is a collection of nodes (called vertices) connected by links (called edges). Unlike a tree, a graph has no strict hierarchy — any node can connect to any number of other nodes, connections can form cycles, and edges can be one-directional (directed) or two-directional (undirected), and may carry a weight (a cost or distance). Graphs are the most general-purpose structure for modeling relationships and networks.

How Graph: Introduction Works

  1. Define a set of vertices (nodes), each representing an entity.
  2. Define a set of edges, each connecting two vertices.
  3. Decide if edges are directed (one-way) or undirected (two-way).
  4. Optionally assign weights to edges to represent cost, distance, or capacity.

Graph: Introduction Worked Example

Input: An undirected graph with vertices {A, B, C} and edges A-B, B-C

  1. Vertices: A, B, C
  2. Edge A-B means A and B are directly connected
  3. Edge B-C means B and C are directly connected
  4. A and C are not directly connected, but reachable through B

Output: A -- B -- C

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Graph: Introduction
A set of nodes connected by edges — edges can be one-way or two-way, and optionally carry a weight (cost/distance).
A
B
C
D
E
Unvisited Visited / discovered Current Start
Graph: Introduction
Press ▶ Run to watch it step by step.
Python · Graphs in Code
# Adjacency list: vertex -> neighbours
graph = {
    "A": ["B", "C"],
    "B": ["A", "D"],
    "C": ["A", "E"],
    "D": ["B"],
    "E": ["C"],
}

# Add an edge:
graph["A"].append("D")
# example: intro

Graph: Introduction Time & Space Complexity

TimeVaries widely by operation and representation (adjacency list vs matrix)
SpaceO(V + E) with an adjacency list, O(V^2) with an adjacency matrix

When to Use Graph: Introduction

  • Social networks (people as nodes, friendships as edges)
  • Road/transportation networks and GPS navigation
  • The internet and computer networks
  • Recommendation systems and dependency graphs (e.g. build systems)

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.