简体   繁体   English

如果保持节点之间的关系,则两个 K-Way 树是等价的

[英]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},...

  1. Go over each node in T2 and check if they are in T1. Go 在 T2 中的每个节点上检查它们是否在 T1 中。 Store these nodes.存储这些节点。
  2. Create all the possible pairs from these nodes.从这些节点创建所有可能的对。
  3. Go over each pair, start from one node in T1 and do dfs to see if it reaches the second node. Go 在每一对上,从 T1 中的一个节点开始,做 dfs 看它是否到达第二个节点。

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 :最差空间

  • Assume num_nodes in T1 = num_x假设 T1 中的 num_nodes = num_x
  • Assume num_nodes in T2 = num_y假设 T2 中的 num_nodes = num_y
  • Worst case space: O(num_x + num_y + (num_y*(num_y-1) ) // last term is for the pairs最坏情况空间: O(num_x + num_y + (num_y*(num_y-1) ) // 最后一项是对的

Worst time :最糟糕的时间

  • Time to create all the pairs: O(num_y * (num_y-1))创建所有对的时间:O(num_y * (num_y-1))
  • Time to determine if two nodes are reachable in T1 linked in T1: O(height of T1) = O(h)确定在 T1 中链接的 T1 中两个节点是否可达的时间:O(T1 的高度) = O(h)
  • Total time = O(h * num_y * (num_y-1))总时间 = O(h * num_y * (num_y-1))

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 = T1T2的交集。 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是否是T1D的祖先,以及使用我们之前在其欧拉之旅中为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.

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