Dijkstra's Shortest Path
Find the shortest path from a source node to all others in a weighted graph with non-negative edges. The quintessential greedy graph algorithm.
O((V+E) log V)O(V+E)Loading algorithms…
A curated, interactive reference for software engineers. Study graph traversal, sorting, dynamic programming, and more — with step-by-step visualizations, complexity analysis, and pseudocode.
Showing all 30 algorithms
Find the shortest path from a source node to all others in a weighted graph with non-negative edges. The quintessential greedy graph algorithm.
O((V+E) log V)O(V+E)Explore a graph level by level from a source node. Guarantees the shortest path in unweighted graphs.
O(V+E)O(V)Explore as deep as possible before backtracking. Foundation for cycle detection, topological sort, and more.
O(V+E)O(V)Shortest paths from a source that handles negative edge weights and detects negative cycles.
O(V·E)O(V)All-pairs shortest path algorithm using dynamic programming. Works with negative weights (no negative cycles).
O(V³)O(V²)Heuristic-guided shortest path algorithm. Faster than Dijkstra's when a good heuristic is available.
O(E log V)O(V)Build a Minimum Spanning Tree by greedily adding the cheapest edges that don't form cycles.
O(E log E)O(V)Grow a Minimum Spanning Tree one vertex at a time by always picking the cheapest reachable edge.
O(E log V)O(V)Order the vertices of a directed acyclic graph so every edge points forward. Kahn's in-degree peeling and DFS-based post-order, side by side.
O(V+E)O(V)Detect cycles in a directed graph with three-color DFS: white unvisited, gray on the recursion stack, black fully explored. A back edge to a gray node means cycle.
O(V+E)O(V)Divide-and-conquer sorting that guarantees O(n log n) time. Stable, predictable, and widely used in production.
O(n log n)O(n)Partition-based sort that is extremely fast in practice despite O(n²) worst case. Pick a pivot, partition the array, recurse — average O(n log n) with great cache locality.
O(n log n) avgO(log n)Sort in-place using a max-heap. Phase 1 builds the heap; phase 2 repeatedly extracts the max. Guaranteed O(n log n) with no extra memory.
O(n log n)O(1)Repeatedly swap adjacent elements that are out of order. The simplest comparison sort — O(n²) worst case, but with an early-termination optimisation that makes it linear on already-sorted input. Stable and in-place.
O(n²)O(1)Sort integers digit by digit. Achieves linear time by exploiting digit structure instead of comparisons.
O(d·(n+k))O(n+k)Halve the search space at each step in a sorted array. The canonical O(log n) search algorithm.
O(log n)O(1)Check every element until a match is found. Works on unsorted data; baseline for search algorithm comparisons.
O(n)O(1)Find a pattern in text in linear time by never re-examining text characters — the failure (LPS) table remembers how much of the pattern already matched.
O(n+m)O(m)Cache overlapping subproblems to compute Fibonacci numbers in linear time instead of exponential.
O(n)O(n)Maximize value in a capacity-limited knapsack. A classic NP-hard problem solved efficiently with DP.
O(n·W)O(n·W)Find the longest subsequence common to two strings. Used in diff tools and bioinformatics.
O(m·n)O(m·n)Find the minimum number of coins that make up an amount. Classic unbounded knapsack variant.
O(n·amount)O(amount)Minimum insertions, deletions, and substitutions to transform one string into another. The classic 2D DP with traceback for the exact edit script.
O(m·n)O(m·n)A BST maintains the ordering property: left < root < right. Enables O(log n) search, insert, and delete.
O(log n) avgO(n)A self-balancing BST that maintains O(log n) height using rotations. Guarantees worst-case log n operations.
O(log n)O(n)Build an optimal prefix-free code by repeatedly merging the two least-frequent nodes. Watch the greedy tree take shape and the codes fall out.
O(n log n)O(n)Disjoint Set Union with path compression and union by rank. Near-constant time connectivity queries.
O(α(n)) amortizedO(n)Insert, search, and collect prefix matches character by character. The backbone of autocomplete and spell checkers.
O(L) per opO(A·L·n)Range queries and point updates in O(log n) via a binary decomposition of the array. Watch queries split, recurse, and merge.
O(log n) per opO(n)Place N queens on an N×N board so none attack each other. The canonical backtracking example — try, conflict, undo, retry.
O(N!)O(N)Big-O time & space for every algorithm in this catalog
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| Dijkstra's Shortest Path | Medium | O((V+E) log V) | O(V+E) | Greedy · Shortest Path |
| Breadth-First Search | Easy | O(V+E) | O(V) | Traversal · Shortest Path |
| Depth-First Search | Easy | O(V+E) | O(V) | Traversal · Recursion |
| Bellman-Ford | Medium | O(V·E) | O(V) | Shortest Path · Dynamic Programming |
| Floyd-Warshall | Medium | O(V³) | O(V²) | All-Pairs · Dynamic Programming |
| A* Search | Hard | O(E log V) | O(V) | Heuristic · Shortest Path |
| Kruskal's MST | Medium | O(E log E) | O(V) | MST · Greedy |
| Prim's MST | Medium | O(E log V) | O(V) | MST · Greedy |
| Topological Sort | Medium | O(V+E) | O(V) | DAG · Ordering |
| Cycle Detection (Directed) | Medium | O(V+E) | O(V) | DFS · Colored DFS |
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| Merge Sort | Medium | O(n log n) | O(n) | Divide & Conquer · Stable |
| Quick Sort | Medium | O(n log n) avg | O(log n) | Divide & Conquer · In-Place |
| Heap Sort | Medium | O(n log n) | O(1) | Heap · In-Place |
| Bubble Sort | Easy | O(n²) | O(1) | Comparison · Stable |
| Radix Sort | Medium | O(d·(n+k)) | O(n+k) | Non-Comparison · Linear Time |
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| Binary Search | Easy | O(log n) | O(1) | Divide & Conquer · Sorted Array |
| Linear Search | Easy | O(n) | O(1) | Brute Force · Unsorted |
| KMP String Matching | Medium | O(n+m) | O(m) | Strings · LPS Table |
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| Fibonacci (Memoization) | Easy | O(n) | O(n) | Memoization · Overlapping Subproblems |
| 0/1 Knapsack | Hard | O(n·W) | O(n·W) | Optimization · Bottom-Up |
| Longest Common Subsequence | Medium | O(m·n) | O(m·n) | Strings · 2D DP |
| Coin Change | Medium | O(n·amount) | O(amount) | Optimization · Bottom-Up |
| Edit Distance (Levenshtein) | Medium | O(m·n) | O(m·n) | Strings · 2D DP |
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| Binary Search Tree | Medium | O(log n) avg | O(n) | BST · Ordered |
| AVL Tree | Hard | O(log n) | O(n) | Self-Balancing · Rotations |
| Huffman Coding | Medium | O(n log n) | O(n) | Greedy · Prefix Code |
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| Union-Find (DSU) | Medium | O(α(n)) amortized | O(n) | Disjoint Sets · Path Compression |
| Trie (Prefix Tree) | Easy | O(L) per op | O(A·L·n) | Prefix Tree · Strings |
| Segment Tree | Hard | O(log n) per op | O(n) | Range Query · Point Update |
| Algorithm | Difficulty | Time Complexity | Space Complexity | Notes |
|---|---|---|---|---|
| N-Queens | Medium | O(N!) | O(N) | Backtracking · Constraint Satisfaction |