Graph Theory | Rooting a Tree

栏目: IT技术 · 发布时间: 4年前

内容简介:Today, we are going to see how we can root a tree. This is the 8th post of my ongoing seriesThis is one of those very basic and fundamental transformations we need to have if we want to work with rooted trees. The motivation of rooting a tree is that often

Graph Theory | Rooting a Tree

Today, we are going to see how we can root a tree. This is the 8th post of my ongoing series Graph Theory : Go Hero . You should definitely check out the index page to deep dive into Graphs and related problems. I mostly try to come up with new posts of this series in every weekends. Let’s see how rooting is done.

This is one of those very basic and fundamental transformations we need to have if we want to work with rooted trees. The motivation of rooting a tree is that often it can help to add a structure and simplify the problem. A rooted tree can convert an undirected tree into a directed one which lot more easier to work with. Conceptually, rooting a tree is like picking up the tree by a specific node and having all the edges point downwards.

Photo by author

We can root a tree by using any of it’s nodes, however be cautious about the node we are choosing because not all nodes would not generate well balanced trees. So we need to be a bit selective.

In some situations, it’s always a good idea to have a route back to the parent node so that we can walk back. I have illustrated routes to parent nodes with red lines below.

Photo by author

Let’s see how we can root a tree.

Rooting Solution

Rooting a tree is easily done with a Depth First Search ( DFS ). I have created an animated version of the resultant DFS below. You would definitely understand it, for sure.

GIF created by author

and that’s rooting a tree in a nutshell.

Pseudo Code

class Treenode:

 int id;
 Treenode parent;
 Treenode [] children;function rootTree(g, rootId = 0):
 root = Treenode(rootId, null, [])
 return buildTree(g, root, null)function buildTree(g, node, parent):
 for child in g[node.id]:
 if parent != null and childId == parent.id:
 continue
 child = Treenode(childId, node, [])
 node.children.add(child)
 buildTree(g, child, node)
 return node

We have a class defined with name Treenode. Every node in the tree would have an unique id, that’s what we are storing in the id placeholder. As we discussed earlier, it’s always a best practice to save the parent node because it would help us to travel back. Also, we save some references to the children of the current node.

Then we define a function called rootTree which takes two parameters into it – a graph and the id of the node to get start. The graph g would be represented as an adjacency list with undirected edges. The first line of rootTree method creates a Treenode object with given rootId , parent reference and list of children. The rootTree function invokes another function named buildTree with paramters graph g, root node and reference to the parent node.

The buildTree method takes the exact three parameters we just talked about. As we enter into the function, we end up landing in a for loop which travels all over the children of the current node. We know the edges are undirected, so we absolutely need to manage the situation where we add a directed edge pointing towards the same node. If the above condition is not met, we are sure that we have a confirmed child in our hand. Then we create an object to the Treenode class and add the child to the list of children of the current node. Afterwards, it does DFS more into the tree using the newly created node. We return the current node as we visit all the neighbors of the node.

So, that’s how we root a tree. We will discuss about Tree center(s) in the coming post. Let’s keep learning, together.

Cheers all.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法学

算法学

哈雷尔 / 霍红卫 / 高等教育 / 2007-6 / 39.00元

本书主要论述计算机科学的基本概念、思想、方法和结果。全书内容由 5个部分组成。“预备知识”部分包括算法学中的基本概念、算法结构、算法所操纵的数据以及描述算法所用的程序设计语言。“方法和分析”部分包括算法设计的方法、算法的正确性和效率、评价算法的方法。“局限性和健壮性”部分包括可执行算法的固有局限性以及实现这些算法的计算机的固有局限性、不可计算性和不可判定性、算法学的通用性及其健壮性。此外,还讨论了......一起来看看 《算法学》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具