Data Structures & AlgorithmsHardGATE CS · Advanced

A tree on the vertices A–J has the edges A–B, B–C, C–D, A–E, D–F, B–G, F–H, A–I, C–J. What is the sum of the distances over all unordered pairs of vertices (the Wiener index)?

  1. A. 119
  2. B. 138
  3. C. 128
  4. D. 256

Correct answer: C. 128

Each edge lies on the path of exactly s(n-s) pairs, where s is the size of one side after removing it; summing s(n-s) over the 9 edges gives 128. Counting ordered pairs would double it.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic