|
| 1 | + |
| 2 | +# Exercise 1: Dijkstra's Algorithm – Step-by-Step Solution |
| 3 | + |
| 4 | +## Problem Statement |
| 5 | + |
| 6 | +A US transportation company delivers packages daily in New York City from origin node 1 (Queens) to destination node 6 (Manhattan), using different possible routes as shown in the graph below. The flow on the arcs represents the cost to transport the required demand between neighborhoods. |
| 7 | +**Task:** Determine the best route using Dijkstra's algorithm. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +<br> |
| 12 | + |
| 13 | +## Step-by-Step Solution with Tableaus |
| 14 | + |
| 15 | +### Initialization |
| 16 | + |
| 17 | +Assign value 0 to the source node (1) and ∞ (infinity) to all other nodes. |
| 18 | + |
| 19 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 20 | +|----|----|----|----|----|----| |
| 21 | +| 0 | ∞ | ∞ | ∞ | ∞ | ∞ | |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +### Iteration 1: Visit Node 1 (current minimum: 0) |
| 26 | + |
| 27 | +- Update 2: 0 + 6 = 6, predecessor 1 |
| 28 | +- Update 3: 0 + 9 = 9, predecessor 1 |
| 29 | + |
| 30 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 31 | +|----|-------|-------|----|----|----| |
| 32 | +| 0 | (1,6) | (1,9) | ∞ | ∞ | ∞ | |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +### Iteration 2: Visit Node 2 (current minimum: 6) |
| 37 | + |
| 38 | +- Update 4: 6 + 4 = 10, predecessor 2 |
| 39 | +- Update 5: 6 + 7 = 13, predecessor 2 |
| 40 | + |
| 41 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 42 | +|----|-------|-------|--------|--------|----| |
| 43 | +| 0 | 6 | (1,9) | (2,10) | (2,13) | ∞ | |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +### Iteration 3: Visit Node 3 (current minimum: 9) |
| 48 | + |
| 49 | +(No better paths found from 3.) |
| 50 | + |
| 51 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 52 | +|----|-------|-------|--------|--------|----| |
| 53 | +| 0 | 6 | 9 | (2,10) | (2,13) | ∞ | |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +### Iteration 4: Visit Node 4 (current minimum: 10) |
| 58 | + |
| 59 | +- Update 5: 10 + 2 = 12, predecessor 4 (improves from 13 to 12) |
| 60 | +- Update 6: 10 + 7 = 17, predecessor 4 |
| 61 | + |
| 62 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 63 | +|----|-------|-------|----|--------|--------| |
| 64 | +| 0 | 6 | 9 | 10 | (4,12) | (4,17) | |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +### Iteration 5: Visit Node 5 (current minimum: 12) |
| 69 | + |
| 70 | +- Update 6: 12 + 3 = 15, predecessor 5 (improves from 17 to 15) |
| 71 | + |
| 72 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 73 | +|----|-------|-------|----|----|--------| |
| 74 | +| 0 | 6 | 9 | 10 | 12 | (5,15) | |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +### Iteration 6: Visit Node 6 (current minimum: 15) |
| 79 | + |
| 80 | +| 1* | 2* | 3* | 4* | 5* | 6* | |
| 81 | +|----|-------|-------|----|----|----| |
| 82 | +| 0 | 6 | 9 | 10 | 12 | 15 | |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Minimum Path and Cost |
| 87 | + |
| 88 | +- **Path:** 1 → 2 → 4 → 5 → 6 |
| 89 | +- **Total Cost:** 15 |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Explanation of Tableaus |
| 94 | + |
| 95 | +- Each cell (X, Y) indicates the predecessor node (X) and the cumulative cost to reach the node (Y). |
| 96 | +- At each step, only the best (lowest cost) paths are kept and updated. |
| 97 | +- The algorithm stops when the destination node (6) receives a permanent label. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Python Implementation |
| 102 | + |
| 103 | +``` |
| 104 | +
|
| 105 | +import heapq |
| 106 | +
|
| 107 | +def dijkstra(graph, start, end): |
| 108 | +distances = {node: float('inf') for node in graph} |
| 109 | +predecessors = {node: None for node in graph} |
| 110 | +distances[start] = 0 |
| 111 | +queue = [(0, start)] |
| 112 | +while queue: |
| 113 | +curr_dist, curr_node = heapq.heappop(queue) |
| 114 | +if curr_node == end: |
| 115 | +break |
| 116 | +for neighbor, weight in graph[curr_node].items(): |
| 117 | +distance = curr_dist + weight |
| 118 | +if distance < distances[neighbor]: |
| 119 | +distances[neighbor] = distance |
| 120 | +predecessors[neighbor] = curr_node |
| 121 | +heapq.heappush(queue, (distance, neighbor)) |
| 122 | +\# Reconstruct path |
| 123 | +path = [] |
| 124 | +node = end |
| 125 | +while node is not None: |
| 126 | +path.append(node) |
| 127 | +node = predecessors[node] |
| 128 | +path.reverse() |
| 129 | +return distances[end], path |
| 130 | +
|
| 131 | +# Graph from the example |
| 132 | +
|
| 133 | +graph = { |
| 134 | +1: {2: 6, 3: 9}, |
| 135 | +2: {4: 4, 5: 7}, |
| 136 | +3: {}, |
| 137 | +4: {5: 2, 6: 7}, |
| 138 | +5: {6: 3}, |
| 139 | +6: {} |
| 140 | +} |
| 141 | +
|
| 142 | +cost, path = dijkstra(graph, 1, 6) |
| 143 | +print(f"Minimum cost: {cost}, Path: {path}") |
| 144 | +
|
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## Excel Solver Step-by-Step |
| 150 | + |
| 151 | +1. **Model the Network:** |
| 152 | + - Create a table with nodes as rows and columns, filling in arc costs (use a large number or blank for non-existent arcs). |
| 153 | + |
| 154 | +2. **Define Decision Variables:** |
| 155 | + - For each arc (i, j), create a binary variable \( x_{ij} \) (1 if the arc is used, 0 otherwise). |
| 156 | + |
| 157 | +3. **Objective Function:** |
| 158 | + - Minimize the total cost: |
| 159 | + \[ |
| 160 | + \text{Minimize} \quad Z = \sum_{(i,j)} c_{ij} x_{ij} |
| 161 | + \] |
| 162 | + |
| 163 | +4. **Constraints:** |
| 164 | + - **Flow conservation:** |
| 165 | + - For the source node: Outflow - Inflow = 1 |
| 166 | + - For the destination node: Inflow - Outflow = 1 |
| 167 | + - For all other nodes: Inflow - Outflow = 0 |
| 168 | + |
| 169 | +5. **Set Up Solver:** |
| 170 | + - Set the objective cell to the sum of selected arc costs. |
| 171 | + - Add constraints for flow conservation and binary variables. |
| 172 | + - Run Solver to find the minimum cost path. |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## References |
| 177 | + |
| 178 | +- [PUC-SP Class Material](https://pplx-res.cloudinary.com/image/private/user_uploads/27709701/a31aabec-aeef-42c0-b4cd-9024927871af/Screenshot-2025-05-14-at-13.24.26.jpg) |
| 179 | +- Dijkstra, E. W. (1959). A note on two problems in connexion with graphs. |
| 180 | + |
| 181 | +# |
| 182 | + |
| 183 | +This README provides a complete, clear, and actionable guide for solving Example 1 with Dijkstra’s algorithm, including all tableaus, explanations, and step-by-step solutions in both Python and Excel Solver. |
| 184 | + |
| 185 | + |
| 186 | + |
0 commit comments