Overview
The graph algorithms module provides implementations for various graph and tree algorithms commonly used in competitive programming. All algorithms are implemented as templates to support different numeric types.Graph Data Structures
Base Graph Structure
Source vertex of the edge
Destination vertex of the edge
Weight/cost of the edge
Number of vertices in the graph
Adjacency list representation
List of all edges in the graph
Shortest Path Algorithms
Dijkstra’s Algorithm
Finds shortest paths from a source vertex to all other vertices in a graph with non-negative edge weights.Input graph (directed or undirected)
Starting vertex (0-indexed)
vector<T> - Distance from start to each vertex. Returns numeric_limits<T>::max() if no path exists.
Complexity:
- Time: O((V + E) log V)
- Space: O(V)
Bellman-Ford Algorithm
Finds shortest paths from a source vertex, works with negative edge weights and detects negative cycles.Input undirected graph
Starting vertex (0-indexed)
Output parameter - set to true if negative cycle detected
vector<T> - Distance from source to each vertex
Complexity:
- Time: O(V·E)
- Space: O(V)
Floyd-Warshall Algorithm
Finds shortest paths between all pairs of vertices.Adjacency matrix (input/output). Initialize with edge weights and infinity.
Number of vertices
- Time: O(V³)
- Space: O(1) additional
Minimum Spanning Tree
Kruskal’s Algorithm
Finds the minimum spanning tree using Disjoint Set Union.Number of vertices
List of edges (will be sorted)
T - Total cost of the minimum spanning tree
Complexity:
- Time: O(E log E)
- Space: O(V)
Disjoint Set Union (DSU)
Also known as Union-Find, efficiently manages disjoint sets with path compression and union by size.Methods
Find the representative of the set containing x
Unite the sets containing left and right. Returns true if they were in different sets.
Check if left and right are in the same set
Get the size of the set containing x
Get all connected components as groups
- Time: O(α(n)) amortized per operation (inverse Ackermann function)
- Space: O(n)
Tree Algorithms
Tree Diameter
Finds the longest path in a tree.- Time: O(V)
- Space: O(V)
Binary Lifting
Supports LCA (Lowest Common Ancestor) and k-th ancestor queries. Complexity:- Preprocessing: O(V log V)
- Query: O(log V)
Euler Tour
Converts a tree into a linear sequence for range queries. Complexity:- Time: O(V)
- Space: O(V)
Graph Traversal
Cycle Detection
Detects cycles in directed and undirected graphs.- Time: O(V + E)
- Space: O(V)
Topological Sort
Orders vertices in a DAG (Directed Acyclic Graph).vector<int> - Topologically sorted vertices, or empty if cycle exists
Complexity:
- Time: O(V + E)
- Space: O(V)
Strongly Connected Components (SCC)
Finds strongly connected components using Kosaraju’s algorithm. Complexity:- Time: O(V + E)
- Space: O(V)
Available Algorithms
| Algorithm | File | Time Complexity |
|---|---|---|
| Dijkstra | graph_dijkstra_std.cpp | O((V+E) log V) |
| Dijkstra (with path) | graph_dijkstra_path.cpp | O((V+E) log V) |
| Bellman-Ford | graph_bellman_ford.cpp | O(VE) |
| Floyd-Warshall | graph_floyd_warshall.cpp | O(V³) |
| Kruskal MST | graph_kruskal.cpp | O(E log E) |
| DSU | graph_dsu.cpp | O(α(n)) |
| Find Cycle | graph_find_cycle.cpp | O(V+E) |
| Topological Sort | graph_toposort_dfs.cpp | O(V+E) |
| SCC (Kosaraju) | graph_scc_kosaraju.cpp | O(V+E) |
| Tree Diameter | tree_diameter.cpp | O(V) |
| Binary Lifting | tree_binary_lifting_std.cpp | O(V log V) |
| Euler Tour | tree_euler_tour.cpp | O(V) |
| Centroid Decomposition | graph_centroid_decomposition.cpp | O(V log V) |
| Tree Distance | tree_distance_i.cpp | O(V) |
| Subtree Queries | tree_subtree_queries.cpp | O(V) |
| Difference Array on Trees | tree_difference_array_technique_on_trees.cpp | O(V) |
| Merge Trick on Trees | tree_merge_trick_on_trees.cpp | O(V log V) |
See Also
Data Structures
Advanced data structures for competitive programming
Range Query
Segment trees and other range query structures