Skip to content

Chapter 4 of 7

Graph Algorithms

Graph traversal forms the basis of countless algorithms. Breadth-First Search (BFS) explores level by level using a queue, making it ideal for shortest path in unweighted graphs, level-order traversals, and bipartite checks. Depth-First Search (DFS) goes as deep as possible before backtracking, expressed elegantly either recursively or iteratively with a stack. DFS underlies cycle detection, topological sorting, strongly connected components, and solving maze problems. Both traversals run in O(V + E) time.

Shortest path algorithms address weighted graphs. Dijkstra's algorithm with a min-heap finds shortest paths from a source in O((V + E) log V) for graphs with non-negative weights. Bellman-Ford handles negative weights in O(V × E) and detects negative cycles. Floyd-Warshall computes all-pairs shortest paths in O(V³). A* search extends Dijkstra with a heuristic for faster goal-directed search, optimal when the heuristic is admissible. SPFA offers practical speedups over Bellman-Ford by only relaxing edges from recently updated nodes.

Minimum Spanning Trees (MST) connect all vertices with minimum total weight. Kruskal's algorithm sorts edges and greedily adds them if they don't form a cycle, using Union-Find for cycle detection. Prim's algorithm grows the MST from a starting vertex, adding the cheapest edge to an unvisited vertex via a min-heap. Advanced graph topics include finding articulation points and bridges (critical nodes/edges whose removal disconnects the graph) using DFS with discovery times and low-link values. Tarjan's and Kosaraju's algorithms find strongly connected components in O(V + E), while topological sort using Kahn's algorithm processes nodes with in-degree zero to detect cycles. Maximum flow algorithms like Edmonds-Karp and Dinic's solve network flow problems with applications in bipartite matching and scheduling.

All chapters
  1. 1Searching and Sorting Foundations
  2. 2Array Techniques and Problem Patterns
  3. 3Trees and Hierarchical Structures
  4. 4Graph Algorithms
  5. 5Dynamic Programming
  6. 6Advanced Data Structures
  7. 7String Algorithms and Specialized Techniques

Drill it

Reading is not remembering. These come from the Algorithms Code deck:

Q

What is Binary Search and what is its time complexity?<br><br>Write the implementation in Python.

Binary Search finds a target in a sorted array by repeatedly halving the search space.Time: O(log n) &nbsp;|&nbsp; Space: O(1)def binary_search(arr, target):...

Q

What is Bubble Sort and what is its time complexity?<br><br>Write the implementation in Python.

Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.Time: O(n²) &nbsp;|&nbsp; Space: O(1)def bubble_sort(arr): n = len(arr) for...

Q

What is Merge Sort and what is its time complexity?<br><br>Write the implementation in Python.

Merge Sort is a divide-and-conquer algorithm that splits the array in half, recursively sorts each half, then merges them.Time: O(n log n) &nbsp;|&nbsp; Space:...

Q

What is Quick Sort and what is its time complexity?<br><br>Write the implementation in Python.

Quick Sort picks a pivot, partitions elements around it, then recursively sorts each partition.Time: O(n log n) avg, O(n²) worst &nbsp;|&nbsp; Space: O(log n)de...