[英]Two K-Way Trees are equivalent if relationship between nodes are maintained
Two k-way trees are given, T1 and T2.给出了两个 k 路树,T1 和 T2。 If any two nodes (A, B) from T1 has the property that node B is the descendant of A and that relationship is maintained in the second tree T2 then T1 is equivalent to T2.
如果来自 T1 的任意两个节点 (A, B) 具有节点 B 是 A 的后代的属性,并且该关系在第二棵树 T2 中保持,则 T1 等价于 T2。 A k-way tree is defined to be one where a node can have up to up to k branches at a node.
一棵k路树被定义为一个节点在一个节点上最多可以有k个分支。 Determine if two k-way trees are equivalent.
确定两个 k-way 树是否等价。
Any ideas on a very efficient algorithm in terms of both time and space and beat this one would be appreciated.任何关于时间和空间方面非常有效的算法的想法并击败这个算法都将不胜感激。
For example, T1 and T2 are equivalent.例如,T1 和 T2 是等价的。
T1: T1:
n1
/ | \
n2 n3 n4
/ \
n5 n6
/ | \
n7 n8 n9
\
n12
T2: T2:
n10
/ | \
n3 n6 n2
/ | | \
n15 n9 n0 n7
My attempt:我的尝试:
Make an adjacency list of T1:n1: {n2, n3, n4}, n2: {n5, n6}, n3: {}, n4: {}, n5: {}, n6: {n7, n8, n9}, n9: {n12}, n12: {}制作T1的邻接表:n1:{n2,n3,n4},n2:{n5,n6},n3:{},n4:{},n5:{},n6:{n7,n8,n9}, n9:{n12},n12:{}
Make and adjacency list of T2: n10: {n3, n6, n2}, n6: {n9}, n2: {n0, n7}, ... T2的制作和邻接表:n10:{n3,n6,n2},n6:{n9},n2:{n0,n7},...
For example ,例如,
Pick (n9, n6).选择 (n9, n6)。 Use the T1 adjacency list to see if n6 is reachable from n9.
使用 T1 邻接表查看 n6 是否可以从 n9 到达。 It is not possible.
这不可能。 At some point we will get to pair (n6, n9) and will start from n6.
在某个时候,我们将配对 (n6, n9) 并将从 n6 开始。 Is n9 reachable from n6?
从 n6 可以到达 n9 吗? Yes.
是的。
Worst Space :最差空间:
Worst time :最糟糕的时间:
Maybe to determine if two nodes are reachable in T1 an euler tour and rmq could achieve it in O(1)也许要确定在 T1 中是否可以访问两个节点,欧拉之旅和 rmq 可以在 O(1) 中实现它
Can be solved efficiently using Euler's tour of given trees.可以使用 Euler 对给定树的遍历有效地解决。
First observe that you only need to consider the nodes set S
= intersection of T1
and T2
.首先观察您只需要考虑节点集
S
= T1
和T2
的交集。 Create euler's tour of the T1
and ignore any node which is not is S
(but don't avoid doing DFS for subtree of S
).创建
T1
的欧拉之旅并忽略任何不是S
的节点(但不要避免对S
的子树进行 DFS)。
Put the nodes as they appear in the euler's tour and for each node stores two integers - the starting index in the euler tour array and ending index in the array.将出现在欧拉环中的节点放置在欧拉环中,并为每个节点存储两个整数——欧拉环数组中的起始索引和数组中的结束索引。 Ending index is the index where you leave the DFS of that node at return to it's parent.
结束索引是您将该节点的 DFS 返回到其父节点的索引。 Let's denote these indexes array by
st[]
and en[]
.让我们用
st[]
和en[]
来表示这些索引数组。 Also store it's own index so that we can query later at what index is the node itself.还要存储它自己的索引,以便我们稍后可以查询节点本身的索引。 Let's call this array as
idx[]
我们称这个数组为
idx[]
Now, performing a DFS in T2
and avoiding any nodes which appear in S
we can form pairs for the relation of ancestor and descendant.现在,在
T2
中执行 DFS 并避免出现在S
中的任何节点,我们可以为祖先和后代的关系形成对。 There could be total O(m. m)
such pairs in worst case for m
= number of total nodes in T2
在最坏的情况下,可能有总共
O(m. m)
个这样的对, m
= T2
中的总节点数
For any pair A, D
to check whether A
is ancestor of D
in T1
as well use the indexes we created earlier for each node of T1
in its Euler tour.对于任何对
A, D
来检查A
是否是T1
中D
的祖先,以及使用我们之前在其欧拉之旅中为T1
的每个节点创建的索引。
st[A] < idx[D] < en[A]
then we have that relationship, otherwise not. st[A] < idx[D] < en[A]
那么我们就有这种关系,否则就没有。
Total time complexity is O(m. m)
总时间复杂度为
O(m. m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.