简体   繁体   English

如何找到树的任意两个顶点之间的边或顶点数量?

[英]How to find the number of edges or vertices between any two vertices of a tree?

Its a general tree (acyclic graph), so there can be only one such path. 它是一棵普通树(无环图),因此只能有一个这样的路径。 What algorithm can I use for this? 我可以使用哪种算法?

EDIT: 编辑:

Need to find the paths for all pairs of vertices in the tree 需要找到树中所有顶点对的路径

I want to extend @templatetypedef's answer here 1 . 我想在这里扩展@templatetypedef的答案1

Note that in your problem, you need to do at least one write per each pair of nodes in the tree. 请注意,在您的问题中,您需要对树中的每对节点至少执行一次写入操作。 There are n*(n-1)/2 of these. 其中有n*(n-1)/2个。
Thus, in terms of big O notation, you cannot find an algorithm that runs better than O(n^2) . 因此,就O表示法而言,您找不到比O(n^2)更好的算法。

Now, use DFS or BFS to find the path per node. 现在,使用DFSBFS查找每个节点的路径。 It will run in O(n+m) ( n vertices, m edges). 它将在O(n+m)n个顶点, m边)中运行。 But since it is a tree, we know that m=n-1 , giving us O(n) for BFS/DFS. 但是由于它是一棵树,我们知道m=n-1 ,对于BFS / DFS给我们O(n) Note that in a single BFS/DFS from some node v - you get d(v,u) for EVERY node u . 请注意,在某个节点v的单个BFS / DFS中,每个节点u都得到d(v,u)

If you repeat it per each node, it will get you O(n^2) - which is optimal in terms of big O notation. 如果对每个节点重复此操作,它将得到O(n^2) -就大O表示法而言,这是最佳选择。 I do agree you might get better constants with some optimizations, but that's about it. 我同意您可能会通过一些优化获得更好的常量,但是仅此而已。


(1) Started it as a comment, but it got too long and I figured it worth an answer. (1)以评论开头,但是它太长了,我认为它值得回答。

One simple option is to do a depth-first search starting at one of the nodes until you reach the other node. 一个简单的选择是从一个节点开始进行深度优先搜索,直到到达另一个节点为止。 You can then look at the path that was taken from the first node to the second, which must be the unique path between those nodes, and then count how many edges are in that path. 然后,您可以查看从第一个节点到第二个节点的路径,该路径必须是这些节点之间的唯一路径,然后计算该路径中有多少条边。 This runs in linear time because a DFS on a tree only takes linear time. 这以线性时间运行,因为树上的DFS仅占用线性时间。

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

You need to find the Lowest Common Ancestor (LCA). 您需要找到最低公共祖先 (LCA)。 There are different approaches as you can study here: http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html 您可以在此处研究不同的方法: http : //leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html

I have used the fallowing solution since I'm not fan to recursivity, that should work better for your problem since you need to find all the pairs in an efficient manner. 由于我不喜欢递归,因此我使用了休闲解决方案,因为您需要以有效的方式查找所有对,所以该方法更适合您的问题。

在此处输入图片说明

1-) Finding path between A -> B : -Iterate starting from node A going up each parent node flagging each one with A Flag, until parent root fund. 1-)在A-> B之间寻找路径:-从节点A开始迭代,沿着每个父节点向上标记每个A标记,直到父根基金为止。 -Iterate starting from node B going up until A Flag found. -从节点B开始迭代,直到找到A标志。 You have found LCA node. 您已找到LCA节点。 - Result path is a list from A to LCA node, plus a reversed list from B to LCA node. -结果路径是从A到LCA节点的列表,以及从B到LCA节点的反向列表。

2-) Improvement finding A -> B : - Iterate both nodes simultaneously, flagging with A Flag, and B Flag each ancestor node. 2-)改进发现A-> B:-同时迭代两个节点,分别用A标志和B标志标记每个祖先节点。 Until A iterations find B Flag or B iterations find A Flag. 直到A迭代找到B标志或B迭代找到A标志。 Then the first to find another node flag has found the LCA node. 然后第一个找到另一个节点标志的人找到了LCA节点。

3-) Finding all pairs paths: You can simply use solution above for each pair. 3-)查找所有线对路径:您可以简单地对每个线对使用上述解决方案。 Or try to consider making a first pass iterating all the tree creating lists of flags, then a second pass to identify all LCA nodes for each pair, that would be inconvenient when the tree number of nodes grows too big. 或者尝试考虑进行第一遍遍历所有树以创建标志列表的过程,然后进行第二遍遍以识别每对树的所有LCA节点,这在节点的树数太大时会很不方便。

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

相关问题 在树中查找 k 个顶点以覆盖最大边数 - Find k vertices in a tree to cover the maximum number of edges 树图 - 找到多少对顶点,它们之间路径上的边总和为 C - tree graph - find how many pairs of vertices, for which the sum of edges on a path between them is C 在两个顶点之间具有平行边的Dijkstra - Dijkstra with parallel edges between two Vertices 通过使用顶点数和边数之间的关系在可能格式错误的二叉树中查找循环 - Finding a loop in a possibly malformed binary tree by using the relation between the number of vertices and number of edges 查找加权有向图中两个指定顶点之间是否存在任何路径 - Find if there exists any path between two specified vertices in a weighted digraph 如何找到手绘多边形的边和顶点 - How to find Edges and Vertices of a hand drawn polygon 生成树最小化连接到多个边的顶点数? - Spanning tree which minimizes the number of vertices connected to multiple edges? 如何在有向图和线性时间中找到两个顶点之间不同的最短路径的数量? - How to find the number of different shortest paths between two vertices, in directed graph and with linear-time? 如何在DAG中找到两个顶点之间的最大权重路径? - How to find the maximum-weight path between two vertices in a DAG? 图的任意两个顶点之间存在循环 - Existence of cycle between any two vertices of graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM