profile pic
⌘ '
raccourcis clavier

Problème 1.

Let G=(N,E)\mathcal{G} = (\mathcal{N}, \mathcal{E}) be an undirected tree (graph GG is undirected, connected, and has N=E+1|\mathcal{N}|=|\mathcal{E}| + 1 if we count edges (v,w)(v, w) and (w,v)(w, v) as the same edge). Let m,nNm, n \in \mathcal{N}. We say that the distance between mm and nn, denoted by dist(m,n), is the length of the shortest path between mm and nn.

P1.1

Show that dist(m,n)=dist(n,m)\text{dist}(m, n) = \text{dist}(n, m)

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

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

Let mm and nn be two arbitrary vertices in the G\mathcal{G}. There exists a unique path PP from mm to nn.

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

Let the vertices along the path PP^{'} be: n,vk,,v2,v1,mn, v_k, \dots, v_2, v_1, m. Since G\mathcal{G} is an undirected graph, each edge (vi,vi+1)(v_i, v_{i+1}) in PP corresponds to the same edge (vi+1,vi)(v_{i+1}, v_i) in PP^{'}. Therefore, length of path PP^{'} is also k+1k+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 PP and PP^{'} are shortest paths between m and n, with length k+1k+1

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

P1.2

Prove that there is a unique path without repeated nodes and edges from node mm to node nn with length dist(m,n)

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

Suppose there are two different simple paths connecting mm and nn:

P1:m,u1,u2,,uk,nP2:m,v1,v2,,vj,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 jkj \neq k. Since the paths are different and since P2P_2 is a simple path, P1P_1 must contain an edge that isn’t in P2P_2

Let j1j \ge 1 the first index for which the edge {uj1,uj}\{ u_{j-1}, u_j \} of P1P_1 is not in P2P_2. Then uj1=vj1u_{j-1} = v_{j-1}.

Let uku_k be the first vertex in path P1P_1 after uj1u_{j-1} (that is kjk \geq j) that is in the path P2P_2. Then uk=vlu_k = v_l for some ljl \geq j

We now have two simple path, Q1:uj1,,ukQ_1: u_{j-1}, \dots, u_k using edges from P1P_1 and Q2:vj1,,vlQ_2 : v_{j-1}, \dots, v_l using edges from P2P_2, between uj1=vj1u_{j-1} = v_{j-1} and uk=vlu_k = v_l.

The path Q1Q_1 and Q2Q_2 have no vertices, edges in common, thus the path from uj1u_{j-1} to uku_k along Q1Q_1 followed by the path from vlv_l to vj1v_{j-1} along the reverse of Q2Q_2 is a cyclic in TT, which contradicts the assumption that TT is a tree.

Thus, the path from mm to nn is unique simple path \square

P1.3

Prove the triangle inequality dist(m,n)dist(m,x)+dist(x,n)\text{dist}(m, n) \leq \text{dist}(m, x) + \text{dist}(x, n)

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

There exists a simple unique path P1P_1 from mm to xx (length is dist(m,x)\text{dist}(m,x)), a simple unique path P2P_2 from xx to nn (length is ).

Consider PP formed by concatenating P1P_1 and P2P_2. This is a path from mm to nn that passes through xx (length is dist(m,x)+dist(x,n)\text{dist}(m,x)+\text{dist}(x,n))

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

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

P1.4

Provide an algorithm that computes the distance d=maxm,nNdist(m,n)d=\text{max}_{m,n \in N} \text{dist}(m,n) that is the maximum distance between any pair of nodes in G\mathcal{G} in O(N+E)\mathcal{O}(|\mathcal{N}| + \mathcal{E})

"\\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}"

Algorithm 39 Maximum Distance in Tree

1:procedure MaxDistance(G\mathcal{G})

2:uu \gets BFS(G,v\mathcal{G}, v)vv is any arbitrary node in N\mathcal{N}

3:dd \gets BFS(G,u\mathcal{G}, u)

4:

5:return dd

6:end procedure

7:procedure BFS(G,s\mathcal{G}, s)

8:QQ \gets empty queue

9:dist[v]\text{dist}[v] \gets \infty for all vNv \in \mathcal{N}

10:dist[s]0\text{dist}[s] \gets 0

11:Q.Enqueue(s)Q.\text{Enqueue}(s)

12:fsf \gets s

13:while QQ is not empty do

14:uQ.Dequeue()u \gets Q.\text{Dequeue}()

15:fuf \gets u

16:for all vNv \in \mathcal{N} such that (u,v)E(u, v) \in \mathcal{E} do

17:if dist[v]=\text{dist}[v] = \infty then

18:dist[v]dist[u]+1\text{dist}[v] \gets \text{dist}[u] + 1

19:Q.Enqueue(v)Q.\text{Enqueue}(v)

20:end if

21:end for

22:end while

23:

24:return ff

25:end procedure