
- Graph Theory - Home
- Graph Theory - Introduction
- Graph Theory - History
- Graph Theory - Fundamentals
- Graph Theory - Applications
- Types of Graphs
- Graph Theory - Types of Graphs
- Graph Theory - Simple Graphs
- Graph Theory - Multi-graphs
- Graph Theory - Directed Graphs
- Graph Theory - Weighted Graphs
- Graph Theory - Bipartite Graphs
- Graph Theory - Complete Graphs
- Graph Theory - Subgraphs
- Graph Theory - Trees
- Graph Theory - Forests
- Graph Theory - Planar Graphs
- Graph Theory - Hypergraphs
- Graph Theory - Infinite Graphs
- Graph Theory - Random Graphs
- Graph Representation
- Graph Theory - Graph Representation
- Graph Theory - Adjacency Matrix
- Graph Theory - Adjacency List
- Graph Theory - Incidence Matrix
- Graph Theory - Edge List
- Graph Theory - Compact Representation
- Graph Theory - Incidence Structure
- Graph Theory - Matrix-Tree Theorem
- Graph Properties
- Graph Theory - Basic Properties
- Graph Theory - Coverings
- Graph Theory - Matchings
- Graph Theory - Independent Sets
- Graph Theory - Traversability
- Graph Theory Connectivity
- Graph Theory - Connectivity
- Graph Theory - Vertex Connectivity
- Graph Theory - Edge Connectivity
- Graph Theory - k-Connected Graphs
- Graph Theory - 2-Vertex-Connected Graphs
- Graph Theory - 2-Edge-Connected Graphs
- Graph Theory - Strongly Connected Graphs
- Graph Theory - Weakly Connected Graphs
- Graph Theory - Connectivity in Planar Graphs
- Graph Theory - Connectivity in Dynamic Graphs
- Special Graphs
- Graph Theory - Regular Graphs
- Graph Theory - Complete Bipartite Graphs
- Graph Theory - Chordal Graphs
- Graph Theory - Line Graphs
- Graph Theory - Complement Graphs
- Graph Theory - Graph Products
- Graph Theory - Petersen Graph
- Graph Theory - Cayley Graphs
- Graph Theory - De Bruijn Graphs
- Graph Algorithms
- Graph Theory - Graph Algorithms
- Graph Theory - Breadth-First Search
- Graph Theory - Depth-First Search (DFS)
- Graph Theory - Dijkstra's Algorithm
- Graph Theory - Bellman-Ford Algorithm
- Graph Theory - Floyd-Warshall Algorithm
- Graph Theory - Johnson's Algorithm
- Graph Theory - A* Search Algorithm
- Graph Theory - Kruskal's Algorithm
- Graph Theory - Prim's Algorithm
- Graph Theory - Borůvka's Algorithm
- Graph Theory - Ford-Fulkerson Algorithm
- Graph Theory - Edmonds-Karp Algorithm
- Graph Theory - Push-Relabel Algorithm
- Graph Theory - Dinic's Algorithm
- Graph Theory - Hopcroft-Karp Algorithm
- Graph Theory - Tarjan's Algorithm
- Graph Theory - Kosaraju's Algorithm
- Graph Theory - Karger's Algorithm
- Graph Coloring
- Graph Theory - Coloring
- Graph Theory - Edge Coloring
- Graph Theory - Total Coloring
- Graph Theory - Greedy Coloring
- Graph Theory - Four Color Theorem
- Graph Theory - Coloring Bipartite Graphs
- Graph Theory - List Coloring
- Advanced Topics of Graph Theory
- Graph Theory - Chromatic Number
- Graph Theory - Chromatic Polynomial
- Graph Theory - Graph Labeling
- Graph Theory - Planarity & Kuratowski's Theorem
- Graph Theory - Planarity Testing Algorithms
- Graph Theory - Graph Embedding
- Graph Theory - Graph Minors
- Graph Theory - Isomorphism
- Spectral Graph Theory
- Graph Theory - Graph Laplacians
- Graph Theory - Cheeger's Inequality
- Graph Theory - Graph Clustering
- Graph Theory - Graph Partitioning
- Graph Theory - Tree Decomposition
- Graph Theory - Treewidth
- Graph Theory - Branchwidth
- Graph Theory - Graph Drawings
- Graph Theory - Force-Directed Methods
- Graph Theory - Layered Graph Drawing
- Graph Theory - Orthogonal Graph Drawing
- Graph Theory - Examples
- Computational Complexity of Graph
- Graph Theory - Time Complexity
- Graph Theory - Space Complexity
- Graph Theory - NP-Complete Problems
- Graph Theory - Approximation Algorithms
- Graph Theory - Parallel & Distributed Algorithms
- Graph Theory - Algorithm Optimization
- Graphs in Computer Science
- Graph Theory - Data Structures for Graphs
- Graph Theory - Graph Implementations
- Graph Theory - Graph Databases
- Graph Theory - Query Languages
- Graph Algorithms in Machine Learning
- Graph Neural Networks
- Graph Theory - Link Prediction
- Graph-Based Clustering
- Graph Theory - PageRank Algorithm
- Graph Theory - HITS Algorithm
- Graph Theory - Social Network Analysis
- Graph Theory - Centrality Measures
- Graph Theory - Community Detection
- Graph Theory - Influence Maximization
- Graph Theory - Graph Compression
- Graph Theory Real-World Applications
- Graph Theory - Network Routing
- Graph Theory - Traffic Flow
- Graph Theory - Web Crawling Data Structures
- Graph Theory - Computer Vision
- Graph Theory - Recommendation Systems
- Graph Theory - Biological Networks
- Graph Theory - Social Networks
- Graph Theory - Smart Grids
- Graph Theory - Telecommunications
- Graph Theory - Knowledge Graphs
- Graph Theory - Game Theory
- Graph Theory - Urban Planning
- Graph Theory Useful Resources
- Graph Theory - Quick Guide
- Graph Theory - Useful Resources
- Graph Theory - Discussion
Graph Theory - Time Complexity
Time Complexity
Time complexity in graph theory measures how fast or slow an algorithm works when solving problems with graphs. It shows how the algorithm's performance changes as the graph grows in size, which is usually measured by the number of nodes (V) and edges (E) in the graph.
In this tutorial, we will explore the time complexity of common graph algorithms and analyze how they work on different types of graphs.
Time Complexity Notation
Time complexity is expressed using the Big-O notation. This shows the maximum amount of time an algorithm might take to finish as the size of the input grows. For graph algorithms, the input size is often measured in terms of −
- V: Number of vertices in the graph.
- E: Number of edges in the graph.
The time complexity of graph algorithms depends on how the graph is stored. Two common ways to store graphs are adjacency matrices and adjacency lists. These methods affect how quickly the algorithm can find and work with edges and nodes, which impacts how efficient the algorithm is.
Time Complexity of Common Graph Algorithms
Different graph algorithms have different time complexities depending on their approach and graph representation. Time complexity helps us understand how efficient these algorithms are for tasks like traversing or analyzing graphs.
Here, we will explore the time complexity of commonly used algorithms like BFS, DFS, and Dijkstra's algorithm.
Breadth-First Search (BFS)
The Breadth-First Search (BFS) algorithm traverses a graph level by level, starting from a source vertex. It explores all neighboring vertices before moving to the next level of neighbors.
It continues in this manner, level by level, ensuring that each node is visited in the shortest path order from the source.
- Adjacency List: O(V + E)
- Adjacency Matrix: O(V2)
In the adjacency list representation, BFS visits each vertex and explores all its adjacent edges, resulting in a time complexity of O(V + E). In the adjacency matrix representation, it must iterate through all V2 elements.
The following image displays a graph where nodes are represented by circles, and edges by lines. The BFS tree will be highlighted in orange, showing the order of the BFS traversal starting from node 0.

Step-by-Step BFS Traversal:
Start at Node 0 (Source):
- Add Node 0 to the queue.
- Queue: [0]
- Visited: {0}
Explore Node 0:
- The neighbors of Node 0 are Node 1 and Node 2.
- Add Node 1 and Node 2 to the queue.
- Queue: [1, 2]
- Visited: {0, 1, 2}
Explore Node 1:
- The neighbors of Node 1 are Node 0 (already visited) and Node 3.
- Add Node 3 to the queue.
- Queue: [2, 3]
- Visited: {0, 1, 2, 3}
Explore Node 2:
- The neighbors of Node 2 are Node 0 (already visited) and Node 3 (already visited).
- No new nodes are added to the queue.
- Queue: [3]
- Visited: {0, 1, 2, 3}
Explore Node 3:
- The neighbors of Node 3 are Node 1 (already visited) and Node 4.
- Add Node 4 to the queue.
- Queue: [4]
- Visited: {0, 1, 2, 3, 4}
Explore Node 4:
- The neighbors of Node 4 are Node 3 (already visited).
- No new nodes are added to the queue.
- Queue: [] (empty)
At this point, the queue is empty, and all reachable nodes have been visited. The traversal order is: 0, 1, 2, 3, 4
Depth-First Search (DFS)
The Depth-First Search (DFS) algorithm explores as deeply as possible into the graph, visiting one neighbor of each node before moving to the next unvisited neighbor. If no unvisited neighbors are found, it backtracks to the previous node.
The DFS traversal begins at the source node (Node 0) and explores each branch before backtracking.
- Adjacency List: O(V + E)
- Adjacency Matrix: O(V2)
Similar to BFS, the time complexity of DFS is O(V + E) for adjacency lists, as it explores every vertex and edge once.
In the following image, the edges in the DFS traversal path are colored red −

Step-by-Step Depth-First Search (DFS) Traversal:
Start at Node 0 (Source):
- Push Node 0 onto the stack.
- Stack: [0]
- Visited: {0}
Explore Node 0:
- The neighbors of Node 0 are Node 1 and Node 2.
- DFS follows the first neighbor, so it goes to Node 1.
- Push Node 1 onto the stack.
- Stack: [0, 1]
- Visited: {0, 1}
Explore Node 1:
- The neighbors of Node 1 are Node 0 (already visited) and Node 3.
- DFS moves to Node 3 as it hasn't been visited yet.
- Push Node 3 onto the stack.
- Stack: [0, 1, 3]
- Visited: {0, 1, 3}
Explore Node 3:
- The neighbors of Node 3 are Node 1 (already visited) and Node 4.
- DFS moves to Node 4 as it hasn't been visited yet.
- Push Node 4 onto the stack.
- Stack: [0, 1, 3, 4]
- Visited: {0, 1, 3, 4}
Explore Node 4:
- The neighbors of Node 4 are Node 3 (already visited).
- No new nodes to visit, so backtrack to Node 3.
- Stack: [0, 1, 3]
- Visited: {0, 1, 3, 4}
Backtrack to Node 3:
- Since all neighbors of Node 3 have been visited, backtrack to Node 1.
- Stack: [0, 1]
- Visited: {0, 1, 3, 4}
Backtrack to Node 1:
- All neighbors of Node 1 have been visited, so backtrack to Node 0.
- Stack: [0]
- Visited: {0, 1, 3, 4}
Explore Node 2:
- The neighbor of Node 2 is Node 0 (already visited).
- No new nodes to visit, and the stack is now empty.
End of DFS Traversal:
- All reachable nodes from Node 0 have been visited.
- The traversal order is: 0, 1, 3, 4, 2
Dijkstra's Algorithm
Dijkstra's algorithm is used to find the shortest path from a source vertex to all other vertices in a weighted graph. It is commonly used in networking, transportation, and routing algorithms.
The algorithm works by maintaining a set of vertices whose shortest distance from the source is known. It repeatedly selects the vertex with the smallest known distance, explores its neighbors, and updates their distances. It uses a priority queue to achieve efficiency.
- Using a Priority Queue: O((V + E) log V)
- Without a Priority Queue: O(V2)

In the above image,
Shortest distances from source node 0: Node 0: 0 Node 1: 3 Node 2: 1 Node 3: 8 Node 4: 11 Node 5: 12 Shortest path tree (previous nodes): Node 0: Previous node None Node 1: Previous node 2 Node 2: Previous node 0 Node 3: Previous node 1 Node 4: Previous node 3 Node 5: Previous node 4
Analysis of Graph Representations
The way we represent a graph affects how fast algorithms can work on it −
- Adjacency List: This method is useful for graphs with fewer edges (sparse graphs). Algorithms that traverse these graphs usually take O(V + E) time, where V is the number of nodes and E is the number of edges.
- Adjacency Matrix: This method works better for graphs with many edges (dense graphs), but algorithms can take O(V2) time to process them, which can be slower for larger graphs.