2. **Visit Neighboring Nodes:** For the current node, consi…

ruthheasman ·

2. **Visit Neighboring Nodes:** For the current node, consider all of its unvisited neighbors and calculate their tentative distances through the current node. Compare the newly calculated tentative distance with the current assigned value and assign the smaller one.

Replies

metamitya ·

how are tentative distances calculated

metamitya ·

Tentative distances are a key concept in algorithms like Dijkstra's algorithm, which is used to find the shortest path from a starting node to all other nodes in a weighted graph. The term "tentative distance" refers to the current known shortest distance from the starting node to a particular node, which may be updated as the algorithm proceeds.

metamitya ·

Here's how tentative distances are calculated:

metamitya ·

---

metamitya ·

### **Step-by-Step Explanation:**

metamitya ·

1. **Initialization:**

metamitya ·

- **Starting Node:**
- Set the tentative distance to the starting node as **0** because the distance from the starting node to itself is zero.
- **All Other Nodes:**
- Set the tentative distances to all other nodes as **infinity** (∞) to represent that they are initially unreachable.

metamitya ·

2. **Visit Neighboring Nodes:**

metamitya ·

When the algorithm selects the current node (starting with the starting node), it examines all of its unvisited neighbors.

metamitya ·

3. **Calculating Tentative Distances:**

metamitya ·

For each unvisited neighbor of the current node:

metamitya ·

- **Retrieve Current Tentative Distance:**
- Get the tentative distance to the **current node** (let's denote this as **D_current**).
- **Edge Weight:**
- Determine the **weight of the edge** from the current node to the neighbor node (denote this as **W_edge**).
- **Calculate New Tentative Distance:**
- **New Tentative Distance = D_current + W_edge**
- This represents the distance from the starting node to the neighbor node **via the current node**.
- **Compare and Update:**
- If the **new tentative distance** is **less than** the neighbor node's previously recorded tentative distance, update it with the new, smaller value.

metamitya ·

---

metamitya ·

### **Example:**

metamitya ·

Let's illustrate this with a simple example.

metamitya ·

#### **Graph Structure:**

metamitya ·

- **Nodes:** A, B, C
- **Edges and Weights:**
- A → B (weight = 4)
- A → C (weight = 2)
- B → C (weight = 5)

metamitya ·

#### **Algorithm Steps:**

metamitya ·

1. **Initialization:**

metamitya ·

- Tentative distances:
- **A:** 0 (starting node)
- **B:** ∞
- **C:** ∞

metamitya ·

2. **First Iteration (Current Node: A):**

metamitya ·

- **Neighbors of A:** B and C

metamitya ·

**Calculating Tentative Distances to Neighbors:**

metamitya ·

- **Neighbor B:**

metamitya ·

- **D_current (A):** 0
- **W_edge (A → B):** 4
- **New Tentative Distance to B:** 0 + 4 = 4
- **Previous Tentative Distance to B:** ∞
- **Update:** Since 4 < ∞, update tentative distance to B as 4.

metamitya ·

- **Neighbor C:**

metamitya ·

- **D_current (A):** 0
- **W_edge (A → C):** 2
- **New Tentative Distance to C:** 0 + 2 = 2
- **Previous Tentative Distance to C:** ∞
- **Update:** Since 2 < ∞, update tentative distance to C as 2.

metamitya ·

- **Updated Tentative Distances:**

metamitya ·

- **A:** 0 (visited)
- **B:** 4
- **C:** 2

metamitya ·

3. **Second Iteration (Current Node: C):**

metamitya ·

- Choose the unvisited node with the smallest tentative distance, which is **C** (tentative distance = 2).

metamitya ·

- **Neighbors of C:** B

metamitya ·

**Calculating Tentative Distances to Neighbors:**

metamitya ·

- **Neighbor B:**

metamitya ·

- **D_current (C):** 2
- **W_edge (C → B):** 5
- **New Tentative Distance to B:** 2 + 5 = 7
- **Previous Tentative Distance to B:** 4
- **Update:** Since 7 > 4, do not update tentative distance to B.

metamitya ·

- **Updated Tentative Distances:**

metamitya ·

- **A:** 0 (visited)
- **B:** 4
- **C:** 2 (visited)

metamitya ·

4. **Third Iteration (Current Node: B):**

metamitya ·

- **B** is the next unvisited node with the smallest tentative distance.

metamitya ·

- No unvisited neighbors to update.

metamitya ·

- **All nodes have been visited.**

metamitya ·

---

metamitya ·

### **Key Points:**

metamitya ·

- **Tentative Distance Calculation:**

metamitya ·

\[
\text{Tentative Distance to Neighbor} = \text{Tentative Distance to Current Node} + \text{Weight of Edge to Neighbor}
\]

metamitya ·

- **Purpose:**

metamitya ·

- This calculation helps determine if there's a shorter path to the neighbor node through the current node.

metamitya ·

- **Updating Tentative Distances:**

metamitya ·

- If the new calculated tentative distance is **smaller** than the previously recorded tentative distance for that neighbor, **update** it.
- This ensures that the algorithm always has the shortest known distance from the starting node to each node at every step.

metamitya ·

- **Why It Works:**