Search code examples
graphtreetraversalminimum-spanning-treedepth-first-search

Find parent node of a tree to create the shortest possible tree height


I have an undirected graph represented as an adjacency matrix of Euclidean weights. I'm using this to represent the minimum spanning tree for a larger complete graph.

What I want to find is the single node within the graph that, when used as the root node, creates the shortest possible tree height. What I've come up with is performing a depth-first traversal using every node as the root, and keeping track of the shortest height seen. Is there a faster way to accomplish this?


Solution

  • This is a classic algorithms question. What you're looking for is called the center of the tree, and it can be found using a simple iterative algorithm. This question has a great answer that explains how to do it.

    Hope this helps!