|
| 1 | + |
| 2 | +# Shortest Path Problem: Theory, Mathematical Formulation, and Dijkstra's Algorithm |
| 3 | + |
| 4 | +## 1. Theoretical Explanation |
| 5 | + |
| 6 | +The **Shortest Path Problem** (also called the minimum path problem) seeks to find the path between two nodes in a network that minimizes the total distance, cost, or travel time. |
| 7 | +- The network consists of a single supply node (origin) and a single demand node (destination), with all other nodes being transshipment nodes (zero supply and demand). |
| 8 | +- The problem can be solved using mathematical programming or graph algorithms such as Dijkstra's algorithm. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## 2. Mathematical Formulation |
| 13 | + |
| 14 | +Let: |
| 15 | + |
| 16 | +- **Parameters:** |
| 17 | + \( c_{ij} \) = distance, cost, or time from node \( i \) to node \( j \). |
| 18 | + |
| 19 | +- **Decision Variables:** |
| 20 | + \( x_{ij} = \begin{cases} |
| 21 | + 1 & \text{if arc } (i, j) \text{ is included in the shortest path} \\ |
| 22 | + 0 & \text{otherwise} |
| 23 | + \end{cases} \) |
| 24 | + |
| 25 | +- **Objective Function:** |
| 26 | + Minimize the total cost of the path from the origin to the destination: |
| 27 | + \[ |
| 28 | + \min z = \sum_{(i,j)} c_{ij} x_{ij} |
| 29 | + \] |
| 30 | + |
| 31 | +- **Constraints:** |
| 32 | + - The total flow out of the origin is 1. |
| 33 | + - The total flow into the destination is 1. |
| 34 | + - For all intermediate nodes, the flow in equals the flow out. |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## 3. Dijkstra's Algorithm: Step-by-Step Procedure |
| 39 | + |
| 40 | +**Initialization:** |
| 41 | +- Let \( R \) be the set of labeled (closed) nodes, initially empty. |
| 42 | +- Let \( NR \) be the set of unlabeled (open) nodes, initially all nodes. |
| 43 | +- Assign distance 0 to the source node and \( \infty \) to all other nodes. |
| 44 | + |
| 45 | +**Algorithm Steps:** |
| 46 | +1. While \( NR \) is not empty: |
| 47 | + - Select the node \( k \) in \( NR \) with the smallest tentative distance. |
| 48 | + - Move \( k \) from \( NR \) to \( R \). |
| 49 | + - For each unlabeled successor \( j \) of \( k \): |
| 50 | + - If \( \text{distance}(k) + c_{kj} < \text{distance}(j) \), update \( \text{distance}(j) \) and set \( k \) as the predecessor of \( j \). |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 4. Example 3: Oil Company Logistics Network |
| 55 | + |
| 56 | +**Problem:** |
| 57 | +An oil company analyzes the flow of its products in a logistics network from node **A** (origin) to node **E** (destination), passing through intermediate nodes **B, C, D**. Arc weights represent flow time in seconds. |
| 58 | + |
| 59 | +### **Network Structure** |
| 60 | + |
| 61 | +- **A → B:** 25 |
| 62 | +- **A → C:** 28 |
| 63 | +- **B → D:** 22 |
| 64 | +- **C:** No outgoing arcs |
| 65 | +- **D → E:** 18 |
| 66 | + |
| 67 | +### **Dijkstra's Algorithm Tableaus** |
| 68 | + |
| 69 | +#### **Tableau 1: Initialization** |
| 70 | + |
| 71 | +| Node | Distance | Predecessor | Status | |
| 72 | +|------|----------|-------------|------------| |
| 73 | +| A | 0 | - | Permanent | |
| 74 | +| B | ∞ | - | Temporary | |
| 75 | +| C | ∞ | - | Temporary | |
| 76 | +| D | ∞ | - | Temporary | |
| 77 | +| E | ∞ | - | Temporary | |
| 78 | + |
| 79 | +#### **Tableau 2: After Visiting A** |
| 80 | + |
| 81 | +| Node | Distance | Predecessor | Status | |
| 82 | +|------|----------|-------------|------------| |
| 83 | +| A | 0 | - | Permanent | |
| 84 | +| B | 25 | A | Temporary | |
| 85 | +| C | 28 | A | Temporary | |
| 86 | +| D | ∞ | - | Temporary | |
| 87 | +| E | ∞ | - | Temporary | |
| 88 | + |
| 89 | +#### **Tableau 3: After Visiting B** |
| 90 | + |
| 91 | +| Node | Distance | Predecessor | Status | |
| 92 | +|------|----------|-------------|------------| |
| 93 | +| A | 0 | - | Permanent | |
| 94 | +| B | 25 | A | Permanent | |
| 95 | +| C | 28 | A | Temporary | |
| 96 | +| D | 47 | B | Temporary | |
| 97 | +| E | ∞ | - | Temporary | |
| 98 | + |
| 99 | +#### **Tableau 4: After Visiting C** |
| 100 | + |
| 101 | +| Node | Distance | Predecessor | Status | |
| 102 | +|------|----------|-------------|------------| |
| 103 | +| A | 0 | - | Permanent | |
| 104 | +| B | 25 | A | Permanent | |
| 105 | +| C | 28 | A | Permanent | |
| 106 | +| D | 47 | B | Temporary | |
| 107 | +| E | ∞ | - | Temporary | |
| 108 | + |
| 109 | +#### **Tableau 5: After Visiting D** |
| 110 | + |
| 111 | +| Node | Distance | Predecessor | Status | |
| 112 | +|------|----------|-------------|------------| |
| 113 | +| A | 0 | - | Permanent | |
| 114 | +| B | 25 | A | Permanent | |
| 115 | +| C | 28 | A | Permanent | |
| 116 | +| D | 47 | B | Permanent | |
| 117 | +| E | 65 | D | Temporary | |
| 118 | + |
| 119 | +#### **Tableau 6: After Visiting E (Final)** |
| 120 | + |
| 121 | +| Node | Distance | Predecessor | Status | |
| 122 | +|------|----------|-------------|------------| |
| 123 | +| A | 0 | - | Permanent | |
| 124 | +| B | 25 | A | Permanent | |
| 125 | +| C | 28 | A | Permanent | |
| 126 | +| D | 47 | B | Permanent | |
| 127 | +| E | 65 | D | Permanent | |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +### **Optimal Path and Total Time** |
| 132 | + |
| 133 | +- **Path:** A → B → D → E |
| 134 | +- **Total Time:** 25 + 22 + 18 = **65 seconds** |
| 135 | + |
| 136 | +``` |
| 137 | +
|
| 138 | +graph LR |
| 139 | +A --25--> B --22--> D --18--> E |
| 140 | +
|
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 5. Python Implementation Example |
| 146 | + |
| 147 | +``` |
| 148 | +
|
| 149 | +import heapq |
| 150 | +
|
| 151 | +def dijkstra(graph, start): |
| 152 | +distances = {node: float('inf') for node in graph} |
| 153 | +predecessors = {node: None for node in graph} |
| 154 | +distances[start] = 0 |
| 155 | +queue = [(0, start)] |
| 156 | +while queue: |
| 157 | +curr_dist, curr_node = heapq.heappop(queue) |
| 158 | +for neighbor, weight in graph[curr_node].items(): |
| 159 | +distance = curr_dist + weight |
| 160 | +if distance < distances[neighbor]: |
| 161 | +distances[neighbor] = distance |
| 162 | +predecessors[neighbor] = curr_node |
| 163 | +heapq.heappush(queue, (distance, neighbor)) |
| 164 | +return distances, predecessors |
| 165 | +
|
| 166 | +# Example 3 Graph |
| 167 | +
|
| 168 | +graph = { |
| 169 | +'A': {'B': 25, 'C': 28}, |
| 170 | +'B': {'D': 22}, |
| 171 | +'C': {}, |
| 172 | +'D': {'E': 18}, |
| 173 | +'E': {} |
| 174 | +} |
| 175 | +
|
| 176 | +distances, predecessors = dijkstra(graph, 'A') |
| 177 | +print("Distances:", distances) |
| 178 | +print("Predecessors:", predecessors) |
| 179 | +
|
| 180 | +``` |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## 6. References |
| 185 | + |
| 186 | +- [Class Material PDF: Caminho Mínimo e LP - PUC-SP](https://ppl-ai-file-upload.s3.amazonaws.com/web/direct-files/attachments/27709701/1f6878e6-1eb2-41b6-835a-7bb02c00cc10/class_12-Caminho-Minimoe-LP.pdf) |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +**This README provides a comprehensive overview, mathematical formulation, algorithmic steps, tableaus, and a worked example for the shortest path problem as presented in your course material.** |
| 191 | + |
| 192 | + |
0 commit comments