---
date: '2024-03-22'
description: assignment on tree distance proofs, path uniqueness in undirected trees, triangle inequality, and maximum distance algorithm using bfs.
id: A8
modified: 2026-06-05 15:08:41 GMT-04:00
tags:
  - sfwr2c03
title: Trees path
created: '2024-03-22'
published: '2024-03-22'
pageLayout: default
slug: thoughts/university/twenty-three-twenty-four/sfwr-2c03/a8/A8
permalink: https://aarnphm.xyz/thoughts/university/twenty-three-twenty-four/sfwr-2c03/a8/A8.md
generator:
  quartz: v4.6.0
  hostedProvider: Cloudflare
  baseUrl: aarnphm.xyz
full: https://aarnphm.xyz/llms-full.txt
---
## Problème 1.

Let $\mathcal{G}  = (\mathcal{N}, \mathcal{E})$ be an _undirected tree_ (graph $G$ is _undirected, connected_, and has $|\mathcal{N}|=|\mathcal{E}| + 1$ if we count edges $(v, w)$ and $(w, v)$ as the same edge). Let $m, n \in \mathcal{N}$. We say that the _distance_ between $m$ and $n$, denoted by `dist(m,n)`, is the length of the shortest path between $m$ and $n$.

> \[!question\] P1.1
>
> Show that $\text{dist}(m, n) = \text{dist}(n, m)$

An undirected tree $\mathcal{G}$ has the following properties:

1. $\mathcal{G}$ is connected and acyclic
2. A tree with $n$ nodes has $n-1$ edges
3. In a tree, there is a unique path between any two vertices.

Let $m$ and $n$ be two arbitrary vertices in the $\mathcal{G}$. There exists a unique path $P$ from $m$ to $n$.

Let the vertices along the path $P$ be: $m, v_{1}, v_{2}, \dots, v_k, n$. The length of path is $k+1$, which is the number of edges

Let the vertices along the path $P^{'}$ be: $n, v_k, \dots, v_2, v_1, m$. Since $\mathcal{G}$ is an undirected graph, each edge $(v_i, v_{i+1})$ in $P$ corresponds to the same edge $(v_{i+1}, v_i)$ in $P^{'}$. Therefore, length of path $P^{'}$ is also $k+1$

`dist(m, n)` denotes the shortest path from m to n, and `dist(n, m)` denotes the shortest path from n to m. Since there is one unique path between m and n in the tree, both $P$ and $P^{'}$ are shortest paths between m and n, with length $k+1$

Therefore $\text{dist}(m, n) = \text{dist}(n, m)$ $\square$

> \[!question\] P1.2
>
> Prove that there is a _unique_ path without repeated nodes and edges from node $m$ to node $n$ with length `dist(m,n)`

Since $\mathcal{G}$ is an undirected tree, there is a unique path between any two vertices. Let $m$ and $n$ be two arbitrary vertices in the $\mathcal{G}$. There exists a simple path $P$ from $m$ to $n$.

Suppose there are two different simple paths connecting $m$ and $n$:

$$
\begin{align*}
P_1 & : m, u_1, u_2, \dots, u_k, n \\
P_2 & : m, v_1, v_2, \dots, v_j, n
\end{align*}
$$

where $j \neq k$. Since the paths are different and since $P_2$ is a simple path, $P_1$ must contain an edge that isn’t in $P_2$

Let $j \ge 1$ the first index for which the edge $\{ u_{j-1}, u_j \}$ of $P_1$ is not in $P_2$. Then $u_{j-1} = v_{j-1}$.

Let $u_k$ be the first vertex in path $P_1$ after $u_{j-1}$ (that is $k \geq j$) that is in the path $P_2$. Then $u_k = v_l$ for some $l \geq j$

We now have two simple path, $Q_1: u_{j-1}, \dots, u_k$ using edges from $P_1$ and $Q_2 : v_{j-1}, \dots, v_l$ using edges from $P_2$, between $u_{j-1} = v_{j-1}$ and $u_k = v_l$.

The path $Q_1$ and $Q_2$ have no vertices, edges in common, thus the path from $u_{j-1}$ to $u_k$ along $Q_1$ followed by the path from $v_l$ to $v_{j-1}$ along the reverse of $Q_2$ is a cyclic in $T$, which contradicts the assumption that $T$ is a tree.

Thus, the path from $m$ to $n$ is unique simple path $\square$

> \[!question\] P1.3
>
> Prove the triangle inequality $\text{dist}(m, n) \leq \text{dist}(m, x) + \text{dist}(x, n)$

Let $m,n,x$ be three arbitrary vertices in the undirected tree $\mathcal{G}$a

There exists a simple unique path $P_1$ from $m$ to $x$ (length is $\text{dist}(m,x)$), a simple unique path $P_2$ from $x$ to $n$ (length is ).

Consider $P$ formed by concatenating $P_1$ and $P_2$. This is a path from $m$ to $n$ that passes through $x$ (length is $\text{dist}(m,x)+\text{dist}(x,n)$)

since $P$ denotes path from $m$ to $n$, and $\text{dist}(m,n)$ denotes the shortest path between $m$ and $n$, we have

$$
\text{dist}(m,n) \leq \text{length}(P) = \text{dist}(m,x) + \text{dist}(x,n)
$$

> \[!question\] P1.4
>
> Provide an algorithm that computes the distance $d=\text{max}_{m,n \in N} \text{dist}(m,n)$ that is the maximum distance between any pair of nodes in $\mathcal{G}$ in $\mathcal{O}(|\mathcal{N}| + \mathcal{E})$

<div class="ps-root" data-inline-macros=""><span type="button" class="clipboard-button ps-clipboard" aria-label="Copy pseudocode to clipboard"><svg width="16" height="16" viewBox="0 0 16 16" class="copy-icon"><use href="#github-copy"></use></svg><svg width="16" height="16" viewBox="0 0 16 16" class="check-icon"><use href="#github-check" fill-rule="evenodd" fill="rgb(63, 185, 80)"></use></svg></span><span class="ps-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><annotation encoding="application/x-tex">"\\begin{algorithm}\n\\caption{Maximum Distance in Tree}\n\\begin{algorithmic}\n\\Procedure{MaxDistance}{$\\mathcal{G}$}\n\\State $u \\gets$ \\Call{BFS}{$\\mathcal{G}, v$} \\Comment{$v$ is any arbitrary node in $\\mathcal{N}$}\n\\State $d \\gets$ \\Call{BFS}{$\\mathcal{G}, u$}\n\\State \\Return $d$\n\\EndProcedure\\Procedure{BFS}{$\\mathcal{G}, s$}\n\\State $Q \\gets$ empty queue\n\\State $\\text{dist}[v] \\gets \\infty$ for all $v \\in \\mathcal{N}$\n\\State $\\text{dist}[s] \\gets 0$\n\\State $Q.\\text{Enqueue}(s)$\n\\State $f \\gets s$\n\\While{$Q$ is not empty}\n\\State $u \\gets Q.\\text{Dequeue}()$\n\\State $f \\gets u$\n\\ForAll{$v \\in \\mathcal{N}$ such that $(u, v) \\in \\mathcal{E}$}\n\\If{$\\text{dist}[v] = \\infty$}\n\\State $\\text{dist}[v] \\gets \\text{dist}[u] + 1$\n\\State $Q.\\text{Enqueue}(v)$\n\\EndIf\n\\EndFor\n\\EndWhile\n\\State \\Return $f$\n\\EndProcedure\n\\end{algorithmic}\n\\end{algorithm}"</annotation></semantics></math></span>
<div class="ps-algorithm with-caption">
<p class="ps-line" style="text-indent:-0.6em;padding-left:0.6em;">
<span class="ps-keyword">Algorithm 34 </span>Maximum Distance in Tree</p>
<div class="ps-algorithmic with-linenum">
<div class="ps-block" style="margin-left:1.2em;">
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:0em;">1:</span><span class="ps-keyword">procedure </span><span class="ps-funcname">MaxDistance</span>(<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">G</mi></mrow><annotation encoding="application/x-tex">\mathcal{G}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7805em;vertical-align:-0.0972em;"></span><span class="mord mathcal" style="margin-right:0.0593em;">G</span></span></span></span>)</p>
<div class="ps-block" style="margin-left:0.6em;">
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">2:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>←</mo></mrow><annotation encoding="application/x-tex">u \gets</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span></span></span></span> <span class="ps-funcname">BFS</span>(<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">G</mi><mo separator="true">,</mo><mi>v</mi></mrow><annotation encoding="application/x-tex">\mathcal{G}, v</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathcal" style="margin-right:0.0593em;">G</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span></span></span></span>)<span class="ps-comment">  ▷<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi></mrow><annotation encoding="application/x-tex">v</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span></span></span></span> is any arbitrary node in <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">N</mi></mrow><annotation encoding="application/x-tex">\mathcal{N}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathcal" style="margin-right:0.1474em;">N</span></span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">3:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mo>←</mo></mrow><annotation encoding="application/x-tex">d \gets</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span></span></span></span> <span class="ps-funcname">BFS</span>(<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">G</mi><mo separator="true">,</mo><mi>u</mi></mrow><annotation encoding="application/x-tex">\mathcal{G}, u</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathcal" style="margin-right:0.0593em;">G</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">u</span></span></span></span>)</p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">4:</span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">5:</span><span class="ps-keyword">return </span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span></span></span></span></p>
</div>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:0em;">6:</span><span class="ps-keyword">end procedure</span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:0em;">7:</span><span class="ps-keyword">procedure </span><span class="ps-funcname">BFS</span>(<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="script">G</mi><mo separator="true">,</mo><mi>s</mi></mrow><annotation encoding="application/x-tex">\mathcal{G}, s</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathcal" style="margin-right:0.0593em;">G</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span></span></span></span>)</p>
<div class="ps-block" style="margin-left:0.6em;">
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">8:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Q</mi><mo>←</mo></mrow><annotation encoding="application/x-tex">Q \gets</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">Q</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span></span></span></span> empty queue</p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">9:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>dist</mtext><mo stretchy="false">[</mo><mi>v</mi><mo stretchy="false">]</mo><mo>←</mo><mi mathvariant="normal">∞</mi></mrow><annotation encoding="application/x-tex">\text{dist}[v] \gets \infty</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">dist</span></span><span class="mopen">[</span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord">∞</span></span></span></span> for all <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mo>∈</mo><mi mathvariant="script">N</mi></mrow><annotation encoding="application/x-tex">v \in \mathcal{N}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathcal" style="margin-right:0.1474em;">N</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">10:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>dist</mtext><mo stretchy="false">[</mo><mi>s</mi><mo stretchy="false">]</mo><mo>←</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">\text{dist}[s] \gets 0</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">dist</span></span><span class="mopen">[</span><span class="mord mathnormal">s</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">11:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Q</mi><mi mathvariant="normal">.</mi><mtext>Enqueue</mtext><mo stretchy="false">(</mo><mi>s</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">Q.\text{Enqueue}(s)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">Q</span><span class="mord">.</span><span class="mord text"><span class="mord">Enqueue</span></span><span class="mopen">(</span><span class="mord mathnormal">s</span><span class="mclose">)</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">12:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mo>←</mo><mi>s</mi></mrow><annotation encoding="application/x-tex">f \gets s</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.1076em;">f</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">s</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">13:</span><span class="ps-keyword">while </span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Q</mi></mrow><annotation encoding="application/x-tex">Q</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">Q</span></span></span></span> is not empty<span class="ps-keyword"> do</span></p>
<div class="ps-block" style="margin-left:0.6em;">
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-1.5em;">14:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>←</mo><mi>Q</mi><mi mathvariant="normal">.</mi><mtext>Dequeue</mtext><mo stretchy="false">(</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">u \gets Q.\text{Dequeue}()</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">Q</span><span class="mord">.</span><span class="mord text"><span class="mord">Dequeue</span></span><span class="mopen">(</span><span class="mclose">)</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-1.5em;">15:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mo>←</mo><mi>u</mi></mrow><annotation encoding="application/x-tex">f \gets u</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.1076em;">f</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">u</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-1.5em;">16:</span><span class="ps-keyword">for all </span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mo>∈</mo><mi mathvariant="script">N</mi></mrow><annotation encoding="application/x-tex">v \in \mathcal{N}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathcal" style="margin-right:0.1474em;">N</span></span></span></span> such that <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>u</mi><mo separator="true">,</mo><mi>v</mi><mo stretchy="false">)</mo><mo>∈</mo><mi mathvariant="script">E</mi></mrow><annotation encoding="application/x-tex">(u, v) \in \mathcal{E}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">u</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathcal" style="margin-right:0.0894em;">E</span></span></span></span><span class="ps-keyword"> do</span></p>
<div class="ps-block" style="margin-left:0.6em;">
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-2.25em;">17:</span><span class="ps-keyword">if </span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>dist</mtext><mo stretchy="false">[</mo><mi>v</mi><mo stretchy="false">]</mo><mo>=</mo><mi mathvariant="normal">∞</mi></mrow><annotation encoding="application/x-tex">\text{dist}[v] = \infty</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">dist</span></span><span class="mopen">[</span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord">∞</span></span></span></span><span class="ps-keyword"> then</span></p>
<div class="ps-block" style="margin-left:0.6em;">
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-3em;">18:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>dist</mtext><mo stretchy="false">[</mo><mi>v</mi><mo stretchy="false">]</mo><mo>←</mo><mtext>dist</mtext><mo stretchy="false">[</mo><mi>u</mi><mo stretchy="false">]</mo><mo>+</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">\text{dist}[v] \gets \text{dist}[u] + 1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">dist</span></span><span class="mopen">[</span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">dist</span></span><span class="mopen">[</span><span class="mord mathnormal">u</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1</span></span></span></span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-3em;">19:</span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Q</mi><mi mathvariant="normal">.</mi><mtext>Enqueue</mtext><mo stretchy="false">(</mo><mi>v</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">Q.\text{Enqueue}(v)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">Q</span><span class="mord">.</span><span class="mord text"><span class="mord">Enqueue</span></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.0359em;">v</span><span class="mclose">)</span></span></span></span></p>
</div>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-2.25em;">20:</span><span class="ps-keyword">end if</span></p>
</div>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-1.5em;">21:</span><span class="ps-keyword">end for</span></p>
</div>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">22:</span><span class="ps-keyword">end while</span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">23:</span></p>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:-0.75em;">24:</span><span class="ps-keyword">return </span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi></mrow><annotation encoding="application/x-tex">f</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.1076em;">f</span></span></span></span></p>
</div>
<p class="ps-line ps-code">
<span class="ps-linenum" style="left:0em;">25:</span><span class="ps-keyword">end procedure</span></p>
</div>
</div>
</div>
</div>

