Let G=(N,E) be an undirected tree (graph G is undirected, connected, and has ∣N∣=∣E∣+1 if we count edges (v,w) and (w,v) as the same edge). Let m,n∈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.
P1.1
Show that dist(m,n)=dist(n,m)
An undirected tree G has the following properties:
G is connected and acyclic
A tree with n nodes has n−1 edges
In a tree, there is a unique path between any two vertices.
Let m and n be two arbitrary vertices in the G. There exists a unique path P from m to n.
Let the vertices along the path P be: m,v1,v2,…,vk,n. The length of path is k+1, which is the number of edges
Let the vertices along the path P′ be: n,vk,…,v2,v1,m. Since G is an undirected graph, each edge (vi,vi+1) in P corresponds to the same edge (vi+1,vi) 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 dist(m,n)=dist(n,m)□
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 G is an undirected tree, there is a unique path between any two vertices. Let m and n be two arbitrary vertices in the G. There exists a simple path P from m to n.
Suppose there are two different simple paths connecting m and n:
P1P2:m,u1,u2,…,uk,n:m,v1,v2,…,vj,n
where j=k. Since the paths are different and since P2 is a simple path, P1 must contain an edge that isn’t in P2
Let j≥1 the first index for which the edge {uj−1,uj} of P1 is not in P2. Then uj−1=vj−1.
Let uk be the first vertex in path P1 after uj−1 (that is k≥j) that is in the path P2. Then uk=vl for some l≥j
We now have two simple path, Q1:uj−1,…,uk using edges from P1 and Q2:vj−1,…,vl using edges from P2, between uj−1=vj−1 and uk=vl.
The path Q1 and Q2 have no vertices, edges in common, thus the path from uj−1 to uk along Q1 followed by the path from vl to vj−1 along the reverse of Q2 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 □
P1.3
Prove the triangle inequality dist(m,n)≤dist(m,x)+dist(x,n)
Let m,n,x be three arbitrary vertices in the undirected tree Ga
There exists a simple unique path P1 from m to x (length is dist(m,x)), a simple unique path P2 from x to n (length is ).
Consider P formed by concatenating P1 and P2. This is a path from m to n that passes through x (length is dist(m,x)+dist(x,n))
since P denotes path from m to n, and dist(m,n) denotes the shortest path between m and n, we have
dist(m,n)≤length(P)=dist(m,x)+dist(x,n)
P1.4
Provide an algorithm that computes the distance d=maxm,n∈Ndist(m,n) that is the maximum distance between any pair of nodes in G in O(∣N∣+E)