简体   繁体   English

计算长度为K的DAG中的路径数

[英]Count the number of paths in DAG with length K

I have a DAG with 2^N nodes, with values from 0 to 2^N-1. 我有一个具有2 ^ N个节点的DAG,其值从0到2 ^ N-1。 There is edge from x to y if x < y and x (xor) y = 2^p, x and y being the node values and pa non-negative integer. 如果x <y并且x(xor)y = 2 ^ p,则x到y之间有边,x和y是节点值,并且pa是非负整数。 Since N can be as large as 100000, generating the graph and than proceeding with the counting would take much computational time. 由于N可以高达100000,因此生成图形并进行计数将需要大量的计算时间。 Is there any way to count the paths with certain length K (K being the number of edges between two nodes), differently stated, is there an equation of some sort for this kind of counting? 有什么方法可以计算具有一定长度K(K是两个节点之间的边数)的路径,换句话说,是否有某种方程式可以用于这种计数? Thanks in advance 提前致谢

Michael's got some good insights, but I'm not sure I follow his entire argument. 迈克尔(Michael)有一些很好的见解,但我不确定我是否会遵循他的全部论点。 Here's my solution. 这是我的解决方案。

Let's say N=4, K=2. 假设N = 4,K = 2。 So the nodes range from 0 (0000 2 ) to 15 (1111 2 ). 因此,节点的范围从0(0000 2 )到15(1111 2 )。

Now let's consider node 2 (0010 2 ). 现在让我们考虑节点2(0010 2 )。 There's an edge from 2 to 3 (0011 2 ) because 2 < 3 and xor(2,3) = 1 = 2 0 . 因为2 <3且xor(2,3)= 1 = 2 0,所以存在2到3(0011 2 )的边。 There's also an edge from 2 to 6 because 2 < 6 and xor(2,6) = 4 = 2 2 . 还有一个从2到6的边,因为2 <6并且xor(2,6)= 4 = 2 2 And there's an edge from 2 to 10 because 2 < 10 and xor(2,10) = 8 = 2 3 . 从2到10有一条边,因为2 <10且xor(2,10)= 8 = 2 3

To generalize: for any x, consider all of the 0 bits in x. 概括地说:对于任何x,请考虑x中的所有0位。 By flipping any of the 0 bits to 1, you get a number y that's larger than x and differs from x by one bit. 通过将0位中的任何一位翻转为1,您将得到一个大于y且与x相差一位的数字y。 So there's an edge from x to that y. 所以从x到y有一条边。

The number of 1 bits in x is typically called the population count of x. x中的1位数通常称为x的填充计数 I'll use pop(x) to mean the population count of x. 我将使用pop(x)表示x的总数。

We're dealing with N-bit numbers (when we include leading zeroes), so the number of 0 bits in x is N - pop(x). 我们正在处理N位数字(当我们包含前导零时),因此x中的0位数字是N-pop(x)。

Let's use the term “ j -path” to mean a path of length j . 让我们使用术语“ j -path”来表示长度为j的路径。 We want to count the number of K-paths. 我们要计算K路径的数量。

Every node x has N - pop(x) outgoing edges. 每个节点x都有N-pop(x)个输出边缘。 Each of these edges is a 1-path. 这些边缘中的每一个都是1路径。

Let's consider node 5 (0101 2 ). 让我们考虑节点5(0101 2 )。 Node 5 has an edge to 7 (0111 2 ), and node 7 has an edge to 15 (1111 2 ). 节点5的边缘为7(0111 2 ),节点7的边缘为15(1111 2 )。 Node 5 also has an edge to 13 (1101 2 ), and node 13 has an edge to 15 (1111 2 ). 节点5的边缘为13(1101 2 ),节点13的边缘为15(1111 2 )。 So there are two 2-paths out of node 5: 5-7-15 and 5-13-15. 因此,节点5外有两个2路径:5-7-15和5-13-15。

Next let's look at node 2 (0010 2 ) again. 接下来,让我们再次查看节点2(0010 2 )。 Node 2 has an edge to 3 (0011 2 ), which has edges to 7 (0111 2 ) and 11 (1011 2 ). 节点2的边为3(0011 2 ),节点的边为7(0111 2 )和11(1011 2 )。 Node 2 also has an edge to node 6 (0110 2 ), which has edges to 7 (0111 2 ) and 14 (1110 2 ). 节点2还具有节点6(0110 2 )的边缘,节点6具有7(0111 2 )和14(1110 2 )的边缘。 Finally, node 2 has an edge to node 10 (1010 2 ), which has edges to 11 (1011 2 ) and 14 (1110 2 ). 最后,节点2的边缘为节点10(1010 2 ),节点10的边缘为11(1011 2 )和14(1110 2 )。 In all, there are six 2-paths out of node 2: 2-3-7, 2-3-11, 2-6-7, 2-6-14, 2-10-11, and 2-10-14. 节点2总共有6条2路径:2-3-7、2-3-11、2-6-7、2-6-14、2-10-11和2-10-14 。

The pattern is that, for any node x with z bits set to zero, where z ≥ K, there are some K-paths out of x. 模式是,对于z位设置为零的任何节点x,其中z≥K,x中都有一些K路径。 To find a K-path out of x, you pick any K of the zero bits. 要从x中找到K路径,请选择零位中的任何K。 Flipping those bits to 1, one by one, gives you the path. 将这些位一一翻转为您提供路径。 You can flip the bits in any order you want; 您可以按任意顺序翻转位。 each order gives a different path. 每个订单给出了不同的路径。

When you want to pick k items, in a specific order , from a set of n items, that's called an ordered sample without replacement, and there are n! 当您要从一组n个项目中以特定顺序选择k个项目时,称为无替换的有序样本,并且有n个! / (nk)! /(nk)! ways to do it. 方法。 This is often written n P k , but it's easier to type P(n,k) here. 通常将其写为n P k ,但在此处键入P(n,k)会更容易。

So, the nodes that have exactly 2 zero bits have P(2,2) = 2! 因此,正好具有2个零位的节点的P(2,2)= 2! / (2-2)! /(2-2)! = 2 2-paths out of them. = 2条2条路径。 (Note that 0! = 1.) The nodes that have exactly 3 zero bits have P(3,2) = 3! (请注意,0!=1。)正好具有3个零位的节点的P(3,2)= 3 !! / 1! / 1! = 6 2-paths out of them. = 6条2条路径。 The node that has exactly 4 zero bits has P(4,2)= 4! 正好有4个零位的节点的P(4,2)= 4! / 2! / 2! = 12 2-paths out of it. = 12条2条路径。 (Since I'm using N=4 for the example, there is only one node with exactly 4 zero bits, which is node 0.) (由于我在示例中使用N = 4,因此只有一个节点具有正好4个零位,即节点0。)

But then we need to know, how many nodes have exactly 2 zero bits? 但是然后我们需要知道,有多少个节点正好有2个零位? Well, when there are n items to choose from, and we want to choose k of them, and we don't care about the order of the chosen items, that's called an unordered sample without replacement, and there are n! 好吧,当有n个商品可供选择时,我们想选择k个商品,而我们并不关心所选商品的顺序,那就是无替换的无序样本,而有n个商品! / (k! (nk)!) ways to do it. /(k!(nk)!)的方法。 This is called “n choose k”, and it's usually written in a way that I can't reproduce on stack overflow, so I'll write it as C(n,k). 这称为“ n select k”,通常以无法在堆栈溢出时重现的方式编写,因此将其写为C(n,k)。

For our example with N=4, there are C(4,2) = 6 nodes with exactly 2 bits set to zero. 对于我们的N = 4的示例,有C(4,2)= 6个节点,其中2位恰好设置为零。 These nodes are 3 (0011 2 ), 5 (0101 2 ), 6 (0110 2 ), 9 (1001 2 ), 10 (1010 2 ), and 12 (1100 2 ). 这些节点是3(0011 2 ),5(0101 2 ),6(0110 2 ),9(1001 2 ),10(1010 2 )和12(1100 2 )。 Each of these nodes has P(2,2) 2-paths out of it, so that means there are C(4,2) * P(2,2) = 6 * 2 = 12 2-paths out of nodes with exactly two 0 bits. 这些节点中的每个节点都具有P(2,2)2条路径,因此这意味着C(4,2)* P(2,2)= 6 * 2 = 12个节点中的2条路径完全相同两个0位。

Then there are C(4,3) = 4 nodes with exactly 3 bits set to zero. 然后有C(4,3)= 4个节点,其中恰好3位设置为零。 These nodes are 1 (0001 2 ), 2 (0010 2 ), 4 (0100 2 ), and 8 (1000 2 ). 这些节点是1(0001 2 ),2(0010 2 ),4(0100 2 )和8(1000 2 )。 Each of these nodes has P(3,2) 2-paths out of it, so there are C(4,3) * P(3,2) = 4 * 6 = 24 2-paths out of nodes with exactly three 0 bits. 这些节点中的每个节点都具有P(3,2)2条路径,因此,在正好三个0的节点中有C(4,3)* P(3,2)= 4 * 6 = 24条2条路径位。

Finally, there is C(4,4) = 1 node with exactly 4 bits set to zero. 最后,存在C(4,4)= 1个节点,其中恰好4位设置为零。 This node has P(4,2) = 12 2-paths out of it. 该节点具有P(4,2)= 12个2路径。

So the total number of 2-paths when N=4 is C(4,2)*P(2,2) + C(4,3)*P(3,2) + C(4,4)*P(4,2) = 12 + 24 + 12 = 48. 因此,当N = 4时2路径的总数为C(4,2)* P(2,2)+ C(4,3)* P(3,2)+ C(4,4)* P( 4,2)= 12 + 24 + 12 = 48。

For general N and K (where K ≤ N), the number of K-paths is the sum of C(N,z) * P(z,K) for K ≤ z ≤ N. 对于一般的N和K(其中K≤N),K路径的数目是K≤z≤N时C(N,z)* P(z,K)的总和。

I can type that into Wolfram Alpha (or Mathematica) like this: 我可以这样输入Wolfram Alpha(或Mathematica):

Sum[n!/(z! (n - z)!) z!/(z - k)!, {z, k, n}]

And it simplifies it to this : 并将其简化为

2^(n-k) n! / (n-k)!

The stated problem seems to be equivalent to this one: 陈述的问题似乎与此等效:

Consider the set of all possible binary strings of length N. Consider operation Fi that flips i-th bit from 0 to 1. For strings x & y denote |x| 考虑所有可能的长度为N的二进制字符串的集合。考虑将第i位从0翻转到1的操作Fi。对于字符串x&y表示| x |。 the number of set bits, x 设置位数x

It's easy to see that one can obtain y from x by a series of exactly K operations Fi if and only if (x,y) is K-admissible. 很容易看出,只要且仅当(x,y)是K可容许的,一个人才能通过一系列完全K的操作Fi从x获得y。 Moreover, if we fix x and sum up over all y such that (x,y) is K-admissible we get (N-|x|)! 而且,如果我们固定x并对所有y求和,使得(x,y)是K可容许的,则得到(N- | x |)!

Finally, we need to sum up over all x with |x|<=(NK). 最后,我们需要用| x | <=(NK)对所有x求和。 For a given choice of |x| 对于| x |的给定选择 we have N!/(N-|x|)!|x|! 我们有N!/(N- | x |)!| x |! possible choices of x. x的可能选择。 Combine with the above and you get that for the given |x| 结合以上内容,即可得到给定的| x |。 there are N!/|x|! 有N!/ | x |! possible paths. 可能的路径。

Denote |x|=M, with M from 0 to NK, and your answer is the sum over all M of N!/M! 表示| x | = M,M从0到NK,而您的答案是N!/ M!的所有M之和。

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

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