Saltar al contenido principal
Interactive Algorithm Education

Visualize & Master Algorithms & Data Structures

Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.

About A graph is a collection of vertices (nodes) connected by edges

A graph is a collection of vertices (nodes) connected by edges.

Graphs model real-world networks: social connections, road maps, dependency trees, and the internet.

Unlike trees, graphs can have cycles and multiple paths between nodes.

How It Works

Graphs can be represented in two main ways.

An **Adjacency Matrix** uses an N×N 2D array where `matrix[u][v] = weight` if an edge exists.

An **Adjacency List** maps each vertex to a list of its neighbors — more memory-efficient for sparse graphs.

Key properties: directed vs.

undirected edges, weighted vs.

unweighted edges, cyclic vs.

acyclic structure.

Time & Space Complexities

Operation Time Space
Add Vertex O(V²) matrix / O(1) list O(V²) / O(1)
Add Edge O(1) O(1)
Remove Edge O(1) matrix / O(deg) list O(1)
Check Edge Exists O(1) matrix / O(deg) list O(1)
Get Neighbors O(V) matrix / O(deg) list O(1)
Space Usage — O(V²) matrix / O(V+E) list

Best Use Cases

  • Social networks (users = vertices, friendships = edges)
  • Road/transit maps (cities = vertices, roads = edges with distance weights)
  • Internet routing (routers = vertices, links = edges)
  • Dependency graphs (build systems, package managers)
  • Knowledge graphs and recommendation engines

Worked Example

Represent the undirected graph A–B, A–C, B–D, C–D

Input: 4 vertices (A, B, C, D); edges A–B, A–C, B–D, C–D
  1. 1 Adjacency matrix: A row is [0, 1, 1, 0], B row [1, 0, 0, 1], C row [1, 0, 0, 1], D row [0, 1, 1, 0] — 16 entries.
  2. 2 Adjacency list: A → [B, C]; B → [A, D]; C → [A, D]; D → [B, C] — 8 entries plus 4 headers.
  3. 3 Check edge A–B: matrix reads matrix[A][B] = 1 instantly; the list scans A's degree.
  4. 4 Because the graph is undirected, each edge appears twice in the list and symmetrically in the matrix.
  5. 5 Space: matrix O(V²)=16 cells; list O(V + E)=4+4 entries — the list wins as the graph gets sparser.
Result: Both representations are valid; the matrix gives O(1) edge checks at O(V²) space, the list uses O(V + E) space.

Pseudocode

Adjacency Matrix — add edge
function addEdge(matrix, u, v, weight = 1):
    matrix[u][v] = weight
    if undirected:
        matrix[v][u] = weight
Adjacency List — add edge
function addEdge(adj, u, v, weight = 1):
    adj[u].append({ to: v, weight })
    if undirected:
        adj[v].append({ to: u, weight })

Adjacency Matrix vs Adjacency List

Adjacency Matrix

# 4-node graph A,B,C,D
# A B C D
A [0, 1, 1, 0]
B [1, 0, 0, 1]
C [1, 0, 0, 1]
D [0, 1, 1, 0]
Space: O(V²) = O(16)
Edge check: O(1)
Get neighbors: O(V)

Adjacency List

# 4-node graph A,B,C,D
A → [B, C]
B → [A, D]
C → [A, D]
D → [B, C]
Space: O(V+E) = O(12)
Edge check: O(degree)
Get neighbors: O(degree)
Dense graph?
Use Matrix
Sparse graph?
Use List
Fast edge lookup?
Use Matrix
Low memory?
Use List