🐐
LeetGoat
Interview Prep
Competitive Programming
Discord
Graph templates
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Connected Components
Connected components, undirected O(V + E)
Undirected [node1, node2] edge list plus numNodes and oneIndexed (a node with no edges is still its own component). Returns each component's nodes and its edge list
Cycle Checks
Has a cycle, directed O(V + E)
Directed [a, b] edge list (a -> b), sized from the largest label. True if you can follow edges from some node back to itself
Has a cycle, undirected O(V + E)
Undirected [node1, node2] edge list, sized from the largest label. True if there's a loop anywhere (a self-loop or the same edge twice counts)
Bipartite
Bipartite split O(V + E)
Undirected [node1, node2] edge list plus numNodes and oneIndexed. The two sides with every edge going between them, or None (C++: possible = false) if there's an odd loop
Bridges & Articulation Points
Bridges O(V + E)
Undirected [node1, node2] edge list, sized from the largest label. Every edge whose removal splits its component in two (a repeated edge never is)
Articulation points O(V + E)
Undirected [node1, node2] edge list, sized from the largest label. Every node whose removal splits its component into two or more pieces
Minimum Spanning Tree
Multiple minimum spanning trees O(E log E)
Undirected weighted [node1, node2, weight] edge list, sized from the largest label. One minimum spanning tree per separate piece of the graph (a connected graph gives one): minimum total weight, and the tree path between any two nodes has the smallest possible biggest edge. Does NOT minimize distances
Shortest Paths
Dijkstra, directed O((V + E) log E)
Shortest distances from a source over a weighted [from, to, weight] edge list (from -> to, weight >= 0), plus the previous node on a shortest path. numNodes and oneIndexed are required: a node with no edges never shows up in the edge list
Dijkstra, undirected O((V + E) log E)
Shortest distances from a source over a weighted [node1, node2, weight] edge list (both ways, weight >= 0), plus the previous node on a shortest path. numNodes and oneIndexed are required: a node with no edges never shows up in the edge list
All-Pairs Shortest Paths
Floyd-Warshall, directed O(V^3)
Shortest distance between every pair of nodes over a weighted [from, to, weight] edge list (a -> b, weight >= 0; negative weights are an error). numNodes is required: a node with no edges never shows up in the edge list. Nodes 0..numNodes-1
1-indexed
Floyd-Warshall, undirected O(V^3)
Shortest distance between every pair of nodes over a weighted [node1, node2, weight] edge list (both ways, weight >= 0; negative weights are an error). numNodes is required: a node with no edges never shows up in the edge list. Nodes 0..numNodes-1
1-indexed
Strongly Connected Components
Strongly connected components O(V + E)
Directed [a, b] edge list (a -> b), plus numNodes and whether labels start at 1 (both required: a node with no edges never shows up in the edge list but is still its own component). Returns each node's component, each component's nodes, and the condensation DAG, ids in reverse topological order
Functional Graphs
Functional graph breakdown O(V)
Every node has exactly one outgoing edge, so each component is one cycle with tails feeding in. Directed [a, b] edge list (a -> b), one edge per node, so no node count. Nodes 0..len(edges)-1
1-indexed
Topological Sort
Topological sort + DAG check O(V + E)
Directed [a, b] edge list (a -> b, a must come before b), plus numNodes and whether labels start at 1 (both required: a node with no edges still has to be in the order). Returns None (C++: {}) if there's a cycle, else every node once with a before b for every edge a -> b: nodes with no incoming edges first
Euler Paths & Circuits
Euler circuit, directed O(V + E)
A walk over an [a, b] edge list (a -> b) using every edge once and ending where it started, or None / {} if there isn't one. Optional start; labels 0..n-1 or 1..n
Euler path, directed O(V + E)
A walk over an [a, b] edge list (a -> b) using every edge once, or None / {} if there isn't one. Optional start and end, picked from the degrees if left out; a circuit counts. Labels 0..n-1 or 1..n
Euler circuit, undirected O(V + E)
A walk over an undirected [node1, node2] edge list using every edge once and ending where it started, or None / {} if there isn't one. Optional start; labels 0..n-1 or 1..n
Euler path, undirected O(V + E)
A walk over an undirected [node1, node2] edge list using every edge once, or None / {} if there isn't one. Optional start and end, picked from the odd-degree nodes if left out; a circuit counts. Labels 0..n-1 or 1..n
Graph Templates — LeetGoat