Graph Algorithms

Comprehensive guide to 23+ essential graph algorithms with complexity analysis, use cases, and interactive visualizations.

Try Interactive Platform

Graph Traversal

Breadth-First Search (BFS)

Explores graph level by level, visiting all neighbors before moving deeper. Uses a queue data structure to maintain the order of exploration.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Shortest path in unweighted graphs, level-order traversal, finding connected components

Depth-First Search (DFS)

Explores as far as possible along each branch before backtracking. Uses a stack (or recursion) to track the exploration path.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Topological sorting, cycle detection, pathfinding, maze solving

Topological Sort

Linear ordering of vertices in a directed acyclic graph (DAG) where each vertex appears before all vertices it points to.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Task scheduling, dependency resolution, build systems, course prerequisites

Eulerian Path

Finds a path that visits every edge exactly once in an undirected graph. Exists when graph has exactly 0 or 2 vertices of odd degree.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Route planning, puzzle solving, circuit design, DNA sequencing

Shortest Path Algorithms

Dijkstra's Algorithm

Finds shortest paths from a source vertex to all other vertices in a weighted graph with non-negative edge weights.

Time Complexity: O((V + E) log V)
Space Complexity: O(V)
Use Cases: GPS navigation, network routing, social networks

Bellman-Ford Algorithm

Finds shortest paths from a source vertex and detects negative weight cycles. Works with negative edge weights.

Time Complexity: O(VE)
Space Complexity: O(V)
Use Cases: Currency arbitrage detection, network protocols

Floyd-Warshall Algorithm

Finds shortest paths between all pairs of vertices using dynamic programming with intermediate vertices.

Time Complexity: O(V³)
Space Complexity: O(V²)
Use Cases: All-pairs shortest paths, transitive closure

Minimum Spanning Tree

Prim's Algorithm

Builds minimum spanning tree by growing from a single vertex, always adding the minimum weight edge that connects to a new vertex.

Time Complexity: O((V + E) log V)
Space Complexity: O(V)
Use Cases: Network design, clustering algorithms

Kruskal's Algorithm

Builds minimum spanning tree by sorting all edges and using Union-Find to avoid cycles while adding minimum weight edges.

Time Complexity: O(E log E)
Space Complexity: O(V)
Use Cases: Network design, circuit design, clustering

Borůvka's Algorithm

Builds minimum spanning tree using a parallel component-based approach, suitable for distributed computing environments.

Time Complexity: O(E log V)
Space Complexity: O(V)
Use Cases: Parallel MST computation, distributed algorithms

Graph Connectivity

Tarjan's SCC Algorithm

Finds strongly connected components in directed graphs using DFS with low-link values and a stack.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Dependency analysis, social network analysis

Kosaraju's SCC Algorithm

Finds strongly connected components using two DFS passes: one on the original graph and one on the transpose graph.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Web crawling, dependency resolution

Articulation Points

Finds vertices whose removal would increase the number of connected components in an undirected graph.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Network reliability, critical infrastructure analysis

Bridge Finding

Finds edges whose removal would increase the number of connected components in an undirected graph.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Network reliability, critical path analysis

Bipartite Check

Determines if a graph can be colored with exactly two colors such that no adjacent vertices share the same color.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Matching problems, scheduling, resource allocation

Special Algorithms

Cycle Detection

Detects the presence of cycles in both directed and undirected graphs using various techniques like DFS and Union-Find.

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Deadlock detection, dependency analysis

Hamiltonian Path

Finds a path that visits every vertex exactly once using backtracking and dynamic programming techniques.

Time Complexity: O(2ⁿ × n²)
Space Complexity: O(2ⁿ × n)
Use Cases: Tour planning, optimization problems

Traveling Salesman Problem

Finds the shortest possible route that visits every city exactly once and returns to the starting point.

Time Complexity: O(n² × 2ⁿ)
Space Complexity: O(n × 2ⁿ)
Use Cases: Route optimization, logistics, circuit board drilling

Graph Coloring

Assigns colors to vertices such that no two adjacent vertices share the same color, using the minimum number of colors.

Time Complexity: O(V × 2ⱽ)
Space Complexity: O(2ⱽ)
Use Cases: Scheduling, register allocation, frequency assignment

Maximal Clique

Finds the largest complete subgraph where every pair of vertices is connected by an edge.

Time Complexity: O(3ⁿ/³)
Space Complexity: O(V)
Use Cases: Social network analysis, bioinformatics, data mining

Chordality Check

Determines if a graph is chordal (every cycle of length 4 or more has a chord - an edge connecting two non-adjacent vertices in the cycle).

Time Complexity: O(V + E)
Space Complexity: O(V)
Use Cases: Perfect graph recognition, optimization problems

Assignment Problem

Solves optimal assignment of workers to tasks using the Hungarian algorithm to minimize total cost.

Time Complexity: O(n³)
Space Complexity: O(n²)
Use Cases: Resource allocation, job scheduling, matching problems

Network Flow

Maximum Flow

Finds the maximum flow from a source to a sink in a flow network using algorithms like Ford-Fulkerson with augmenting paths.

Time Complexity: O(V²E)
Space Complexity: O(V²)
Use Cases: Network capacity planning, resource allocation, bipartite matching

Minimum Cut

Finds the minimum capacity cut that separates the source from the sink, closely related to maximum flow by the max-flow min-cut theorem.

Time Complexity: O(V²E)
Space Complexity: O(V²)
Use Cases: Network reliability, image segmentation, clustering

Ready to Explore These Algorithms?

Experience interactive visualizations and step-by-step execution of all these algorithms.

Launch Interactive Platform