Data Structures & AlgorithmsVery hardProduct-company placement

A tree on vertices 1–8 has edges 2–1, 3–2, 4–1, 5–2, 6–5, 7–2, 8–1. What is the sum of the distances (in edges) over all unordered pairs of vertices?

  1. A. 124
  2. B. 62
  3. C. 55
  4. D. 69

Correct answer: B. 62

Each edge splitting the tree into parts of sizes s and n − s lies on s·(n − s) of the paths; summing over the 7 edges gives 62. Counting ordered pairs would double it.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic