简体   繁体   English

通过选择根节点减少广度优先树的深度

[英]Reducing depth of breadth first tree by choice of root node

If the root node can be picked randomly in a graph, is there an existing algorithm that picks the root node such that the resulted breadth first tree has the smallest depth or height? 如果可以在图形中随机选择根节点,是否存在现有算法可以选择根节点,以使生成的广度优先树的深度或高度最小?


I have a hunch that I should pick the node with the largest fan out as the root node. 我有一种预感,我应该选择扇出最大的节点作为根节点。


Let me give one example. 让我举一个例子。

There is a cyclic directed graph {(0,1),(1,2),(1,5),(1,6),(2,3),(3,4),(4,2),(5,2),(6,0)} 有一个循环的有向图{{(0,1),(1,2),(1,5),(1,6),(2,3),(3,4),(4,2),( 5,2),(6,0)}

If node 0 is chosen as root, breadth first tree is {(0,1),(1,2),(1,5),(1,6),(2,3),(3,4)} The depth is 5 如果选择节点0作为根,则广度优先树为{{0,1),(1,2,2,(1,5),(1,6),(2,3),(3,4)}深度是5

If node 6 is chosen as root, breadth first tree is {(6,0),(0,1),(1,2),(1,5),(2,3),(3,4)} The depth is 6 如果选择节点6作为根,则广度优先的树是{{6,0),(0,1),(1,2),(1,5),(2,3),(3,4)}深度是6

I am assuming you are talking about a weighted graph otherwise it does not make much difference doing BFS from different nodes as root. 我假设您正在谈论的是加权图,否则从不同的节点作为根执行BFS不会有太大的区别。

One naive brute force approach is to consider each node of the graph as root and construct the BFS tree. 一种幼稚的蛮力方法是将图的每个节点视为根,并构造BFS树。 Measure the height each time and after covering all nodes as roots we get the node from which the BFS Tree produces the minimum height. 每次测量高度,在将所有节点作为根覆盖后,我们得到BFS树从其产生最小高度的节点。 Done just this you might end up taking exponential time. 只是这样做,您可能会花费大量时间。 Time: O(n * (v + e) + logxn) for each node n we do BFS + for each tree we calculate height x levels in tree. 时间:每个节点n的O(n * (v + e) + logxn) n我们为每棵树做BFS +,我们计算树中的高度x水平。 But I suspect dynamic programming we can bring this time to a much more manageable level. 但是我怀疑动态编程可以使这段时间更易于管理。 Since we store results at every stage we can reuse it for later computations. 由于我们在每个阶段存储结果,因此我们可以将其重新用于以后的计算。

Another method that comes to mind is optimal binary search tree. 想到的另一种方法是最佳二进制搜索树。 What you do is process your graph & collate the weights of each nodes into an array. 您要做的是处理图形并将每个节点的权重整理到一个数组中。 Using this weights we would construct a BST such that the nodes with maximum weight would fall near the root of the BST and nodes with less weight would fall lower in the BST (some probably as leafs). 使用此权重,我们将构造一个BST,以使最大权重的节点落在BST的根附近,而权重较小的节点在BST中落入较低(有些可能是叶子)。 This way upon searching in BST your likelihood of finding a node is better. 这样,在BST中搜索时,找到节点的可能性会更好。

update: to expand on the above approach - 更新:对上述方法进行扩展- 在此处输入图片说明

The above recursion is simple, we one by one try each node as root r . 上面的递归很简单,我们逐个尝试将每个节点作为根r r varies from i to j . ri to j变化。 We recursively calculate optimal cost from i to r-1 and r+1 to j with r as root. 我们以r为根递归计算从i到r-1和从r + 1到j的最优成本。 We add sum of weights (or frequencies) from i to j (see first term in the above formula), this is added because every search will go through root and one comparison will be done for every search. 我们将i到j的权重(或频率)总和相加(请参见上式中的第一项),这是因为每次搜索都将经过根,并且每次搜索都将进行一次比较。

Hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM