Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. β€Ί
  3. posts
  4. β€Ί
  5. …

  6. β€Ί
  7. 3 Graph Data Structures

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

🍯 Honey never spoils β€” archaeologists found 3,000-year-old jars still edible.

πŸͺ This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

🍯 Honey never spoils β€” archaeologists found 3,000-year-old jars still edible.
Programming

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    kubernetes

    Management

    Programming
    • 🧱 Data Structures: Arrays, Stacks, Queues, Heaps, Hash Tables, Tries & Graphs

    • 🌲 Trees Deep Dive: BST, AVL Rotations, Red-Black Trees, B-Trees & B+ Trees

    • πŸ•ΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS

    • πŸ”’ Algorithmic Complexity: Big O From First Principles

    • πŸ”Ž Searching Algorithm Complexity πŸ“–

    • ⚑ Sorting Algorithm Complexity πŸ“–

    • πŸ—„οΈ Database Comparison πŸ“–

    • Ansible: Agentless Configuration Management

    • CI/CD Pipelines: From Commit to Production

    • Unix Internals: Processes, File Descriptors, and Syscalls

    • Programming Index


    Terraform

    Z_Appendix

Cover Image for πŸ•ΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS
Programming

πŸ•ΈοΈ Graph Data Structures: Adjacency List vs Matrix, BFS & DFS

Graphs from the ground up β€” vertices and edges, why there's no single Big O for a graph, adjacency list vs adjacency matrix tradeoffs, and why BFS and DFS traversal cost is the foundation every graph algorithm builds on.

Data Structures
Graphs
BFS
DFS
Big O
Study Notes
← Previous

🌲 Trees Deep Dive: BST, AVL Rotations, Red-Black Trees, B-Trees & B+ Trees

Next β†’

πŸ”’ Algorithmic Complexity: Big O From First Principles

πŸ•ΈοΈ Graph

A graph is a set of vertices (VVV) connected by edges (EEE) β€” the general case, with no hierarchy or root. Edges may be directed or undirected, weighted or not.

flowchart LR
    A --- B
    A --- C
    B --- D
    C --- D
    D --- E

Every other non-linear structure in this series β€” trees included β€” is really a graph with an extra constraint bolted on (a tree is a connected, acyclic graph with a designated root). A plain graph drops all of those constraints, which is exactly why it's the most general and the most common shape for representing real-world relationships: social networks, road maps, dependency graphs, call graphs.


Adjacency List vs Matrix

There's no single Big O for a graph because it isn't one structure β€” the representation is the design decision:

Representation Space Edge lookup Best when
Adjacency list O(V+E)O(V+E)O(V+E) O(deg⁑v)O(\deg v)O(degv) Sparse graphs (most real graphs)
Adjacency matrix O(V2)O(V^2)O(V2) O(1)O(1)O(1) Dense graphs, frequent edge checks
flowchart LR
    subgraph Matrix["Adjacency Matrix β€” O(V^2) space"]
        direction TB
        M["    A B C<br/>A [ 0 1 1 ]<br/>B [ 1 0 0 ]<br/>C [ 1 0 0 ]"]
    end

    subgraph List["Adjacency List β€” O(V+E) space"]
        direction TB
        LA["A β†’ [B, C]"]
        LB["B β†’ [A]"]
        LC["C β†’ [A]"]
    end

An adjacency matrix answers "are A and B connected?" in O(1)O(1)O(1) β€” just index into the grid β€” at the cost of O(V2)O(V^2)O(V2) space even if the graph barely has any edges. An adjacency list only stores edges that actually exist, so it's O(V+E)O(V+E)O(V+E) space, but checking a specific edge means scanning that node's neighbor list. This is the exact same space-vs-lookup tradeoff a hash table's chaining makes at the bucket level, just applied at the whole-graph level: dense connectivity favors the matrix, sparse connectivity favors the list β€” and most real-world graphs are sparse, which is why adjacency lists dominate in practice.


Traversal: BFS and DFS

Traversal is where the actual work lives β€” BFS and DFS both visit every vertex and edge once, so they run in O(V+E)O(V+E)O(V+E) on a list and O(V2)O(V^2)O(V2) on a matrix.

Breadth-First Search (BFS) Depth-First Search (DFS)
Explores Level by level, all neighbors before going deeper One path fully before backtracking
Data structure Queue Stack (or recursion)
Finds Shortest path in an unweighted graph Any path; good for exhaustive exploration
Typical use Shortest path, level-order processing Cycle detection, topological sort, connected components

Everything downstream builds on these two walks: shortest paths, connectivity, topological sort, and cycle detection are all BFS or DFS with extra bookkeeping layered on top β€” there is no graph algorithm that avoids visiting vertices and edges at least once, so O(V+E)O(V+E)O(V+E) (or O(V2)O(V^2)O(V2) on a matrix) is the practical floor for anything that needs to reason about the whole graph.


Key Takeaways

Concept Summary
No single Big O A graph's complexity depends entirely on its representation, not the abstract structure
Adjacency list O(V+E)O(V+E)O(V+E) space, O(deg⁑v)O(\deg v)O(degv) edge lookup β€” wins for sparse graphs (most real ones)
Adjacency matrix O(V2)O(V^2)O(V2) space, O(1)O(1)O(1) edge lookup β€” wins for dense graphs or frequent edge checks
BFS / DFS Both visit every vertex and edge once β€” O(V+E)O(V+E)O(V+E) on a list, O(V2)O(V^2)O(V2) on a matrix β€” the floor every graph algorithm builds on
Trees are graphs A tree is just a connected, acyclic graph with a root β€” every tree structure in this series is a constrained special case of a graph

The representation choice isn't a detail β€” it's the entire performance profile of every algorithm that runs on the graph afterward. Picking adjacency list vs matrix before writing a single traversal is the graph equivalent of picking the right data structure before writing the rest of the program.


Related Posts

  • 🧱 Data Structures β€” linear structures and the Heap/Hash Table/Trie that round out the non-tree, non-graph structures
  • 🌲 Non-Linear Data Structures β€” trees are graphs with a root and no cycles; see how BST/AVL/Red-Black build on that constraint
  • πŸ”’ Algorithmic Complexity β€” the Big O notation used throughout this post
Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Fri Feb 20 2026

Share This on

← Previous

🌲 Trees Deep Dive: BST, AVL Rotations, Red-Black Trees, B-Trees & B+ Trees

Next β†’

πŸ”’ Algorithmic Complexity: Big O From First Principles

Programming/3-Graph-Data-Structures
Let's work together
+49 176-2019-2523
hiteshkrsahu@gmail.com
WhatsApp
Skype
Munich πŸ₯¨, Germany πŸ‡©πŸ‡ͺ, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
Β  Home/About
Β  Skills
Β  Work/Projects
Β  Lab/Experiments
Β  Contribution
Β  Awards
Β  Art/Sketches
Β  Thoughts
Β  Contact
Links
Β  Sitemap
Β  Legal Notice
Β  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| Β© 2026 All rights reserved.