Graph Algorithms
📖 Introduction
🛤️ Shortest Path Algorithms
BFS for Unweighted Graphs
from collections import deque
from typing import Dict, List, Optional
def bfs_shortest_path(
graph: Dict[str, List[str]],
start: str,
end: str
) -> Optional[List[str]]:
"""
Find shortest path in unweighted graph.
Time: O(V + E)
"""
if start == end:
return [start]
visited = {start}
queue = deque([(start, [start])])
while queue:
node, path = queue.popleft()
for neighbor in graph[node]:
if neighbor == end:
return path + [neighbor]
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, path + [neighbor]))
return NoneDijkstra's Algorithm
Bellman-Ford Algorithm
Floyd-Warshall Algorithm
Algorithm Comparison
Algorithm
Time
Space
Negative Weights
Use Case
🌲 Minimum Spanning Tree
Prim's Algorithm
Kruskal's Algorithm
🔗 Union-Find (Disjoint Set Union)
Union-Find Applications
Number of Connected Components
Redundant Connection
Accounts Merge
📊 Topological Sort
Kahn's Algorithm (BFS)
DFS-Based Topological Sort
Course Schedule II
🎯 Advanced Graph Problems
Cheapest Flights Within K Stops
Alien Dictionary
Critical Connections (Bridges)
📝 Practice Exercises
Exercise 1: Path with Maximum Probability
Exercise 2: Reconstruct Itinerary
Exercise 3: Evaluate Division
🔑 Key Takeaways
🚀 What's Next?
📚 References
Last updated