|
| 1 | +# Graph Algorithms in Python |
| 2 | + |
| 3 | +This directory contains implementations of fundamental graph algorithms in Python, showcasing various graph traversal, shortest path, and spanning tree algorithms. |
| 4 | + |
| 5 | +## 📁 Available Implementations |
| 6 | + |
| 7 | +### 1. Dijkstra's Algorithm - `dijkstra_algorithm.py` |
| 8 | +**Description**: Finds shortest paths from a source vertex to all other vertices in a weighted graph with non-negative edge weights. |
| 9 | + |
| 10 | +**Key Features**: |
| 11 | +- Single-source shortest path algorithm |
| 12 | +- Handles weighted graphs with non-negative weights |
| 13 | +- Uses priority queue for efficient vertex selection |
| 14 | +- Provides path reconstruction |
| 15 | + |
| 16 | +**Time Complexity**: O((V + E) log V) using priority queue |
| 17 | +**Space Complexity**: O(V) |
| 18 | + |
| 19 | +**Applications**: |
| 20 | +- GPS navigation systems |
| 21 | +- Network routing protocols |
| 22 | +- Social network analysis |
| 23 | +- Game pathfinding |
| 24 | + |
| 25 | +### 2. Kruskal's Algorithm - `kruskals_algorithm.py` |
| 26 | +**Description**: Finds the Minimum Spanning Tree (MST) of a weighted undirected graph using greedy approach and Union-Find data structure. |
| 27 | + |
| 28 | +**Key Features**: |
| 29 | +- Greedy algorithm for MST construction |
| 30 | +- Union-Find data structure for cycle detection |
| 31 | +- Step-by-step execution tracking |
| 32 | +- Interactive and demonstration modes |
| 33 | +- Comprehensive error handling and validation |
| 34 | + |
| 35 | +**Time Complexity**: O(E log E) where E is the number of edges |
| 36 | +**Space Complexity**: O(V + E) where V is the number of vertices |
| 37 | + |
| 38 | +**Applications**: |
| 39 | +- Network design (connecting cities with minimum cost) |
| 40 | +- Circuit design and VLSI layout |
| 41 | +- Clustering algorithms |
| 42 | +- Image segmentation |
| 43 | +- Approximation algorithms for TSP |
| 44 | + |
| 45 | +**Code Highlights**: |
| 46 | +```python |
| 47 | +# Create graph and add edges |
| 48 | +graph = Graph(6) |
| 49 | +graph.add_edge(0, 1, 4) |
| 50 | +graph.add_edge(1, 2, 1) |
| 51 | +graph.add_edge(2, 3, 4) |
| 52 | + |
| 53 | +# Find MST using Kruskal's algorithm |
| 54 | +mst_edges, total_weight = kruskals_mst(graph) |
| 55 | +print(f"MST total weight: {total_weight}") |
| 56 | + |
| 57 | +# Get step-by-step execution |
| 58 | +mst_edges, weight, steps = kruskals_with_steps(graph) |
| 59 | +for step in steps: |
| 60 | + print(step) |
| 61 | +``` |
| 62 | + |
| 63 | +**Union-Find Data Structure**: |
| 64 | +- **Path Compression**: Optimizes find operations |
| 65 | +- **Union by Rank**: Balances tree height during union operations |
| 66 | +- **Cycle Detection**: Efficiently detects if adding an edge creates a cycle |
| 67 | + |
| 68 | +## 🚀 How to Run |
| 69 | + |
| 70 | +### Run Dijkstra's Algorithm |
| 71 | +```bash |
| 72 | +python dijkstra_algorithm.py |
| 73 | +``` |
| 74 | + |
| 75 | +### Run Kruskal's Algorithm |
| 76 | +```bash |
| 77 | +python kruskals_algorithm.py |
| 78 | +``` |
| 79 | + |
| 80 | +### Program Modes |
| 81 | +Both implementations offer: |
| 82 | +1. **Interactive Mode**: Create custom graphs and see results |
| 83 | +2. **Demonstration Mode**: Pre-built examples with detailed explanations |
| 84 | + |
| 85 | +## 📊 Algorithm Comparison |
| 86 | + |
| 87 | +| Algorithm | Type | Time Complexity | Space Complexity | Use Case | |
| 88 | +|-----------|------|----------------|------------------|----------| |
| 89 | +| Dijkstra | Shortest Path | O((V+E) log V) | O(V) | Single-source shortest paths | |
| 90 | +| Kruskal | MST | O(E log E) | O(V+E) | Minimum spanning tree | |
| 91 | + |
| 92 | +## 🎯 When to Use Each Algorithm |
| 93 | + |
| 94 | +### Use Dijkstra When: |
| 95 | +- ✅ Finding shortest paths from one vertex to all others |
| 96 | +- ✅ All edge weights are non-negative |
| 97 | +- ✅ Need actual shortest distances |
| 98 | +- ✅ Working with road networks, communication networks |
| 99 | + |
| 100 | +### Use Kruskal When: |
| 101 | +- ✅ Need to connect all vertices with minimum total cost |
| 102 | +- ✅ Working with undirected, weighted graphs |
| 103 | +- ✅ Designing network infrastructure |
| 104 | +- ✅ Finding minimum cost to connect components |
| 105 | + |
| 106 | +## 🔧 Graph Representations |
| 107 | + |
| 108 | +Both algorithms support different graph input formats: |
| 109 | + |
| 110 | +**Adjacency List Format**: |
| 111 | +```python |
| 112 | +graph = { |
| 113 | + 0: [(1, 4), (2, 3)], # vertex: [(neighbor, weight), ...] |
| 114 | + 1: [(0, 4), (2, 1)], |
| 115 | + 2: [(0, 3), (1, 1)] |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +**Edge List Format**: |
| 120 | +```python |
| 121 | +edges = [ |
| 122 | + (0, 1, 4), # (u, v, weight) |
| 123 | + (0, 2, 3), |
| 124 | + (1, 2, 1) |
| 125 | +] |
| 126 | +``` |
| 127 | + |
| 128 | +## 📚 Educational Value |
| 129 | + |
| 130 | +These implementations demonstrate: |
| 131 | + |
| 132 | +**Data Structures**: |
| 133 | +- Priority Queues (heapq) |
| 134 | +- Union-Find with optimizations |
| 135 | +- Graph representations |
| 136 | + |
| 137 | +**Algorithm Techniques**: |
| 138 | +- Greedy algorithms |
| 139 | +- Dynamic programming concepts |
| 140 | +- Path compression and union by rank |
| 141 | + |
| 142 | +**Problem-Solving Patterns**: |
| 143 | +- Graph traversal strategies |
| 144 | +- Optimization problems |
| 145 | +- Cycle detection techniques |
| 146 | + |
| 147 | +## 🤝 Contributing |
| 148 | + |
| 149 | +Feel free to contribute additional graph algorithms: |
| 150 | + |
| 151 | +**Shortest Path Algorithms**: |
| 152 | +- Bellman-Ford Algorithm |
| 153 | +- Floyd-Warshall Algorithm |
| 154 | +- A* Search Algorithm |
| 155 | +- Johnson's Algorithm |
| 156 | + |
| 157 | +**Spanning Tree Algorithms**: |
| 158 | +- Prim's Algorithm |
| 159 | +- Borůvka's Algorithm |
| 160 | + |
| 161 | +**Traversal Algorithms**: |
| 162 | +- Breadth-First Search (BFS) |
| 163 | +- Depth-First Search (DFS) |
| 164 | +- Topological Sort |
| 165 | + |
| 166 | +**Flow Algorithms**: |
| 167 | +- Ford-Fulkerson Method |
| 168 | +- Edmonds-Karp Algorithm |
| 169 | +- Dinic's Algorithm |
| 170 | + |
| 171 | +**Advanced Algorithms**: |
| 172 | +- Strongly Connected Components |
| 173 | +- Articulation Points and Bridges |
| 174 | +- Minimum Cut algorithms |
| 175 | + |
| 176 | +## 📖 References |
| 177 | + |
| 178 | +- [Introduction to Algorithms - CLRS](https://mitpress.mit.edu/books/introduction-algorithms) |
| 179 | +- [Graph Theory - Diestel](https://www.springer.com/gp/book/9783662575604) |
| 180 | +- [Algorithms - Sedgewick](https://algs4.cs.princeton.edu/home/) |
| 181 | +- [GeeksforGeeks Graph Algorithms](https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/) |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +**Contributors**: Multiple authors (see individual files) |
| 186 | +**Hacktoberfest 2025**: ✅ |
| 187 | +**Language**: Python |
| 188 | +**Category**: Graph Algorithms |
0 commit comments