0
0
Data Structures Theoryknowledge~6 mins

Graph representations (adjacency matrix vs list) in Data Structures Theory - Key Differences Explained

Choose your learning style9 modes available
Introduction
Imagine you want to keep track of connections between places or friends. Choosing the right way to store these connections can make finding and using them much easier and faster.
Explanation
Adjacency Matrix
An adjacency matrix uses a square grid to show connections between nodes. Each row and column represents a node, and the cell where they meet shows if there is a connection. This makes it quick to check if two nodes are connected, but it uses a lot of space, especially if there are few connections.
Adjacency matrix is a grid that quickly shows if two nodes are connected but can waste space for sparse graphs.
Adjacency List
An adjacency list keeps a list for each node showing only the nodes it connects to. This saves space when there are fewer connections because it only stores existing links. However, checking if two nodes are connected can take longer since you may need to search through a list.
Adjacency list saves space by listing only existing connections but can be slower to check specific links.
When to Use Each
Use an adjacency matrix when the graph is dense, meaning many connections exist, because it allows fast lookups. Use an adjacency list when the graph is sparse, with fewer connections, to save memory and handle large graphs efficiently.
Choose adjacency matrix for dense graphs and adjacency list for sparse graphs to balance speed and memory.
Real World Analogy

Think of a city map where every street intersection is a node. An adjacency matrix is like a big table listing every possible pair of intersections and marking if a direct road connects them. An adjacency list is like having a list at each intersection showing only the streets that lead out from there.

Adjacency Matrix → A big table showing every possible pair of intersections and whether a direct road connects them
Adjacency List → A list at each intersection showing only the streets that lead out from there
When to Use Each → Choosing between a full city map table or small lists at intersections depending on how many roads exist
Diagram
Diagram
    Nodes
      0   1   2   3
  0 [0] [1] [0] [0]
  1 [1] [0] [1] [1]
  2 [0] [1] [0] [0]
  3 [0] [1] [0] [0]

Adjacency List:
0: 1
1: 0, 2, 3
2: 1
3: 1
This diagram shows a graph with four nodes represented as both an adjacency matrix and an adjacency list.
Key Facts
Adjacency MatrixA square grid where each cell shows if a pair of nodes is connected.
Adjacency ListA list for each node showing only its connected nodes.
Dense GraphA graph with many connections between nodes.
Sparse GraphA graph with few connections compared to the number of nodes.
Space ComplexityHow much memory a data structure uses to store information.
Code Example
Data Structures Theory
graph_matrix = [
    [0, 1, 0, 0],
    [1, 0, 1, 1],
    [0, 1, 0, 0],
    [0, 1, 0, 0]
]

graph_list = {
    0: [1],
    1: [0, 2, 3],
    2: [1],
    3: [1]
}

# Check if node 0 is connected to node 1 using matrix
print('Matrix:', graph_matrix[0][1] == 1)

# Check if node 0 is connected to node 1 using list
print('List:', 1 in graph_list[0])
OutputSuccess
Common Confusions
Believing adjacency matrix always uses less memory.
Believing adjacency matrix always uses less memory. Adjacency matrix uses more memory for sparse graphs because it stores all possible connections, even if many don't exist.
Thinking adjacency list is always slower to check connections.
Thinking adjacency list is always slower to check connections. Adjacency list can be fast for checking neighbors but slower than matrix for direct connection checks because it may require searching a list.
Summary
Graphs can be stored using adjacency matrices or adjacency lists, each with different strengths.
Adjacency matrices use more memory but allow quick connection checks, best for dense graphs.
Adjacency lists save memory by storing only existing connections, ideal for sparse graphs.