Proof of correctness: Algorithm for diameter of a tree in graph theory
Asked Answered
A

4

43

In order to find the diameter of a tree I can take any node from the tree, perform BFS to find a node which is farthest away from it and then perform BFS on that node. The greatest distance from the second BFS will yield the diameter.

I am not sure how to prove this, though? I have tried using induction on the number of nodes, but there are too many cases.

Any ideas would be much appreciated...

Alnico answered 15/11, 2013 at 20:58 Comment(1)
Can you tell me what you mean by too many cases? Theoretically all the sub cases should have be proved by induction as well.Balkin
K
26

Let's call the endpoint found by the first BFS x. The crucial step is proving that the x found in this first step always "works" -- that is, that it is always at one end of some longest path. (Note that in general there can be more than one equally-longest path.) If we can establish this, it's straightforward to see that a BFS rooted at x will find some node as far as possible from x, which must therefore be an overall longest path.

Hint: Suppose (to the contrary) that there is a longer path between two vertices u and v, neither of which is x.

Observe that, on the unique path between u and v, there must be some highest (closest to the root) vertex h. There are two possibilities: either h is on the path from the root of the BFS to x, or it is not. Show a contradiction by showing that in both cases, the u-v path can be made at least as long by replacing some path segment in it with a path to x.

[EDIT] Actually, it may not be necessary to treat the 2 cases separately after all. But I often find it easier to break a configuration into several (or even many) cases, and treat each one separately. Here, the case where h is on the path from the BFS root to x is easier to handle, and gives a clue for the other case.

[EDIT 2] Coming back to this later, it now seems to me that the two cases that need to be considered are (i) the u-v path intersects the path from the root to x (at some vertex y, not necessarily at the u-v path's highest point h); and (ii) it doesn't. We still need h to prove each case.

Kravits answered 16/11, 2013 at 3:49 Comment(1)
If someone looking for proof by contradiction refer this video youtube.com/watch?v=2PFl93WM_aoKef
A
17

I'm going to work out j_random_hacker's hint. Let s, t be a maximally distant pair. Let u be the arbitrary vertex. We have a schematic like

    u
    |
    |
    |
    x
   / \
  /   \
 /     \
s       t ,

where x is the junction of s, t, u (i.e. the unique vertex that lies on each of the three paths between these vertices).

Suppose that v is a vertex maximally distant from u. If the schematic now looks like

    u
    |
    |
    |
    x   v
   / \ /
  /   *
 /     \
s       t ,

then

d(s, t) = d(s, x) + d(x, t) <= d(s, x) + d(x, v) = d(s, v),

where the inequality holds because d(u, t) = d(u, x) + d(x, t) and d(u, v) = d(u, x) + d(x, v). There is a symmetric case where v attaches between s and x instead of between x and t.

The other case looks like

    u
    |
    *---v
    |
    x
   / \
  /   \
 /     \
s       t .

Now,

d(u, s) <= d(u, v) <= d(u, x) + d(x, v)
d(u, t) <= d(u, v) <= d(u, x) + d(x, v)

d(s, t)  = d(s, x) + d(x, t)
         = d(u, s) + d(u, t) - 2 d(u, x)
        <= 2 d(x, v)

2 d(s, t) <= d(s, t) + 2 d(x, v)
           = d(s, x) + d(x, v) + d(v, x) + d(x, t)
           = d(v, s) + d(v, t),

so max(d(v, s), d(v, t)) >= d(s, t) by an averaging argument, and v belongs to a maximally distant pair.

Argive answered 23/3, 2015 at 20:4 Comment(0)
A
1

Here's an alternative way to look at it:

Suppose G = ( V, E ) is a nonempty, finite tree with vertex set V and edge set E.

Consider the following algorithm:

  1. Let count = 0. Let all edges in E initially be uncolored. Let C initially be equal to V.
  2. Consider the subset V' of V containing all vertices with exactly one uncolored edge:
    • if V' is empty then let d = count * 2, and stop.
    • if V' contains exactly two elements then color their mutual (uncolored) edge green, let d = count * 2 + 1, and stop.
    • otherwise, V' contains at least three vertices; proceed as follows:
  3. Increment count by one.
  4. Remove all vertices from C that have no uncolored edges.
  5. For each vertex in V with two or more uncolored edges, re-color each of its green edges red (some vertices may have zero such edges).
  6. For each vertex in V', color its uncolored edge green.
  7. Return to step (2).

That basically colors the graph from the leaves inward, marking paths with maximal distance to a leaf in green and marking those with only shorter distances in red. Meanwhile, the nodes of C, the center, with shorter maximal distance to a leaf are pared away until C contains only the one or two nodes with the largest maximum distance to a leaf.

By construction, all simple paths from leaf vertices to their nearest center vertex that traverse only green edges are the same length (count), and all other simple paths from a leaf vertex to its nearest center vertex (traversing at least one red edge) are shorter. It can furthermore be proven that

  • this algorithm always terminates under the conditions given, leaving every edge of G colored either red or green, and leaving C with either one or two elements.
  • at algorithm termination, d is the diameter of G, measured in edges.
  • Given a vertex v in V, the maximum-length simple paths in G starting at v are exactly those that contain contain all vertices of the center, terminate at a leaf, and traverse only green edges between center and the far endpoint. These go from v, across the center, to one of the leaves farthest from the center.

Now consider your algorithm, which might be more practical, in light of the above. Starting from any vertex v, there is exactly one simple path p from that vertex, ending at a center vertex, and containing all vertices of the center (because G is a tree, and if there are two vertices in C then they share an edge). It can be shown that the maximal simple paths in G having v as one endpoint all have the form of the union of p with a simple path from center to leaf traversing only green edges.

The key point for our purposes is that the incoming edge of the other endpoint is necessarily green. Therefore, when we perform a search for the longest paths starting there, we have access to those traversing only green edges from leaf across (all vertices of) the center to another leaf. Those are exactly the maximal-length simple paths in G, so we can be confident that the second search will indeed reveal the graph diameter.

Atmosphere answered 6/2, 2017 at 21:44 Comment(0)
C
-4

1:procedureTreeDiameter(T)

2:pick an arbitrary vertex v where v∈V

3:u = BFS ( T, v )

4:t = BFS ( T, u )

5:return distance ( u, t )

Result: Complexity = O(|V|)

Conjunct answered 12/4, 2019 at 2:43 Comment(1)
The question asks why the algorithm works, not the steps of the algorithm.Isochronous

© 2022 - 2024 — McMap. All rights reserved.