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)?
- A. 119
- B. 138
- C. 128
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A directed acyclic graph has the vertices A–G and the arcs D→F, D→C, D→B, G→C, G→A, C→A, C→E, C→B. How many different topological…
- In a bipartite graph the left vertices are L1–L6 and the right vertices are R1–R6, with the edges L1–R3, L1–R5, L1–R6, L2–R3, L3–R2…
- Dijkstra's algorithm (a vertex is settled when removed from the priority queue and its distance is never changed afterwards) is run from A…
- A directed graph on the vertices A–E has the arcs C→B, C→A, B→C, B→E, B→A, D→E, E→B, E→A, A→C, A→B, A→D. Every vertex has equal in-degree…
- Every node of a binary tree has either 0 or 2 children. Its preorder traversal is W S M Q R P K Z T and its postorder traversal is Q R M K…
- A graph has the vertices A–G and the edges A–B, A–C, A–D, B–D, B–F, B–G, C–D, C–F, C–G, D–F, E–F. In how many ways can the vertices be…