Graph Theory | Center of a Tree. Let’s see how we can find the center of... | by Kelvin Jose | Towards Data Science

Welcome back all. We’re here at the 9th post of this series called Graph Theory: Go Hero. Go and check out the index page for previous posts. I try to update it every weekend. Let’s see how we can find the center of a tree.
Finding the center of a tree is a handy algorithm to know because we see it very often in the subroutine of other algorithms and also it’s a useful way of selecting the root node when we are rooting a tree.
One thing to keep in mind is that trees could have more than one center but not more than two.
Note that the center is always the middle vertex or middle two vertices in every longest path along the tree.
For example, the orange-coloured path in the above image is the longest path and the red node is considered to be the center among them. If we repeat the process and choose another possible long path, the center would stay the same.
Another approach to finding the center is to iteratively pick off each leaf node like peel an onion.
So, we start from the outskirt and end up in the center - gradually.
The nodes with a horizontal line in the middle are the leaves in our example. If we calculate the degree of these nodes,
Degree of a node is the number of nodes it is connected to.
it would be 1, for sure. Because each all of these leaf nodes are connected to exactly one node. All other nodes would also get affected as we prune the leaves i.e. nodes’ degree would start to decrease.
As we can see in the above image, our graph would start to loose it’s leaves iteratively until we find the center of it. Remember, we might either find a center or centers.
Let’s now look at some pseudo code.
function treeCenters(g):    n = g.numberOfNodes()    degree = [0] * n    leaves = []    for(i=0; i<n; i++):        if degree[i] == 0 or degree[i] == 1:            leaves.add(i)            degree[i] = 0    count = leaves.size()        while count < n:        new_leaves = []        for node in leaves:            for neighbor in g[node]:                degree[neighbor] = degree[neighbor] - 1                if degree[neighbor] == 1:                    new_leaves.add(neighbor)        count += new_leaves.size()        leaves = new_leaves
The parameter g to the function treeCenters() is an undirected graph. Variable n represents the number of nodes in our tree. We have defined two arrays, degree and leaves. The former has the size of n and stores the degree of each node in the tree, the latter keeps the most recent layer of leaf nodes. Then we are entering into a loop where we calculate the degree of every nodes of the graph, also we check whether we are considering a tree with single node or it’s a leaf node. If either of these conditions is true, we add the entry into the leaves array and mark its value in degrees array as 0 - because we don’t have to visit the node again. The variable count is going to keep track of the number of nodes we have processed so far. We then continuously check for leaves and prune them. We would end up having the center of the tree, at last.
That’s pretty easy, right?
We will discuss identifying isomorphic trees in the next post. Stay tuned until then.