Skip to main content
CSData StructuresDiagramBeginner

Graph Representation

KO: 그래프 표현 (인접 리스트 vs 인접 행렬)·EN: Graph Representation·JA: グラフ表現(隣接リスト vs 隣接行列)

Comparing Adjacency Lists and Adjacency Matrices for storing graph vertices and edges, balancing memory efficiency against O(1) edge checks

Table of Contents

Structure Diagram

Graph Representation: Adjacency List vs Adjacency Matrix
LISTAdjacency List

Standard for sparse graphs · O(V + E) space

  • SpaceO(V + E) — stores only existing edges
  • Edge LookupO(deg(V)) — scan neighbor list
  • Iterate NeighborsO(deg(V)) — optimal for BFS/DFS/Dijkstra
  • Best ForSocial graphs, road networks, web links
MATRIXAdjacency Matrix

Dense graphs · O(1) edge check

  • SpaceO(V²) — fixed V×V 2D grid
  • Edge LookupO(1) — direct matrix[u][v] lookup
  • Iterate NeighborsO(V) — must scan entire row
  • Best ForSmall dense graphs, Floyd-Warshall, linear algebra
Quick Answer

What is Graph Representation?

Comparing Adjacency Lists and Adjacency Matrices for storing graph vertices and edges, balancing memory efficiency against O(1) edge checks. It is a core concept in Data Structures within CS.

Core Principles

Choosing how to store a graph G = (V, E) in memory directly determines both memory usage and algorithm speed.

  • Adjacency List: Stores only each vertex's outgoing neighbors in O(V + E) space. Ideal for BFS, DFS, and Dijkstra where iterating over actual neighbors dominates.
  • Adjacency Matrix: A V × V grid giving O(1) edge existence checks, at the cost of O(V²) memory—impractical once V reaches tens of thousands.
  • Edge List: A flat list of (u, v, weight) tuples, handy for edge-centric algorithms like Kruskal's MST or Bellman-Ford.

Practical Takeaways

  • Default to Adjacency Lists: Real-world graphs (social networks, roads, service meshes) are sparse (E ≪ V²).
  • Use CSR for high-performance analytics: Graph engines and GNNs flatten adjacency lists into Compressed Sparse Row (CSR) arrays for contiguous memory and cache locality.
  • Model edge attributes explicitly: Store (to, weight) pairs for weighted graphs and mirror entries for undirected graphs.

Notable Examples

  • Map Routing Engines: Sparse road networks stored as adjacency lists or CSR.
  • Floyd-Warshall & Network Latency Tables: Small dense graphs represented as matrices.
  • Graph Databases & PageRank: Large-scale sparse link structures in web search and recommendations.