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

前端 未结 4 675
南笙
南笙 2020-12-12 17:13

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 g

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 18:08

    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.

提交回复
热议问题