简体   繁体   English

二分图中计数路径数(长度N)

[英]Counting Number of Paths (Length N) in Bipartite Graph

I am currently counting the number of paths of length $n$ in a bipartite graph by doing a depth first search (up to 10 levels). 我目前正在通过进行深度优先搜索(最多10个级别)来计算二分图中长度为$ n $的路径数。 However, my implementation of this takes 5+ minutes to count 7 million paths of length 5 from a bipartite graph with 3000+ elements. 但是,我的实现需要5分钟以上的时间,才能从具有3000+个元素的二部图中计算700万条长度为5的路径。 I am looking for a more efficient way to do this counting problem, and I am wondering if there is any such algorithm in the literature. 我正在寻找一种更有效的方法来解决此计数问题,并且我想知道文献中是否有这样的算法。

These are undirected bipartite graphs, so there can be cycles in the paths. 这些是无向二部图,因此路径中可能存在循环。

My goal here is to count the number of paths of length $n$ in a bipartite graph of 1 million elements under a minute. 我的目标是在一分钟内计算100万个元素的二部图中长度为$ n $的路径数。

Thank you in advance for any suggested answers. 预先感谢您提出的任何建议答案。

I agree with the first idea but it's not quite a BFS. 我同意第一个想法,但它不是一个BFS。 In a BFS you go through each node once, here you can go a large number of times. 在BFS中,您需要遍历每个节点一次,这里可以进行很多次。
You have to keep 2 arrays (let's call it Cnt1, and Cnt2, Cnt1 is the number of times you have reached an element and you have a path of length i, and Cnt2 is the same but for length i + 1). 您必须保留2个数组(我们将其称为Cnt1和Cnt2,Cnt1是到达元素的次数,并且路径的长度为i,而Cnt2相同,但长度为i +1)。 First time all the elements are 0 in Cnt2 and 1 in Cnt1( because you have one path of length zero starting at each node). 第一次,所有元素在Cnt2中均为0,在Cnt1中为1(因为从每个节点开始都有一条长度为零的路径)。

Repeat N times: 重复N次:
Go through all the nodes 遍历所有节点
For the current node you go through all his connected nodes and for each you add at there position on Cnt2 the number of times you reached the current node in Cnt1. 对于当前节点,您要遍历其所有连接的节点,并为每个节点在Cnt2上的该位置添加到达Cnt1中当前节点的次数。
When you finished all the nodes you just Copy Cnt2 in Cnt1 and make Cnt2 zero. 完成所有节点后,只需将Cnt2复制到Cnt1中并将Cnt2设为零即可。
At the end you just add all the numbers of Cnt1 and that is the answer. 最后,您只需添加Cnt1的所有数字即可,这就是答案。

Convert to a breadth-first search, and whenever you have 2 paths that lead to the same node at the same length, just keep track of how many such ways there are and not how you got there. 转换为广度优先搜索,每当您有2条路径以相同的长度通向同一节点时,只需跟踪有多少种这样的方式,而不是如何到达那里。

This will avoid a lot of repeated work and should provide a significant speedup. 这样可以避免大量重复工作,并可以大大提高速度。 (If n is not small, there are better speedups, read on.) (如果n不小,则可以提高速度,请继续阅读。)

My goal here is to count the number of paths of length n in a bipartite graph of 1 million elements under a minute. 我的目标是在一分钟内计算一百万个元素的二部图中长度为n的路径数。

Um, good luck? 嗯,祝你好运?

An alternate approach to look into is if you take the adjacency matrix of the graph, and raise it to the n'th power, all of the entries of the matrix you get are the number of paths of length end starting in one place, ending in another. 要研究的另一种方法是,如果采用图的邻接矩阵并将其提高到第n次幂,则矩阵的所有条目都是长度结束的路径数(从一个位置开始)在另一个。 So you can take shortcuts like repeated squaring. 因此,您可以采用快捷方式,例如重复平方。 Convenient, isn't that? 方便,不是吗?

Unfortunately a million element graph gives rise to an adjacency matrix with 10^12 entries. 不幸的是,一百万个元素图产生了一个具有10 ^ 12项的邻接矩阵。 Multiplying two such matrices with a naive algorithm should require 10^18 operations. 将两个这样的矩阵与朴素算法相乘应该需要10 ^ 18的运算。 Of course we have better matrix multiplication algorithms, but you're still not getting below, say, 10^15 operations. 当然,我们有更好的矩阵乘法算法,但是您仍然没有低于10 ^ 15的运算量。 Which will most assuredly not complete in 1 minute. 肯定会在1分钟内完成。 (If your matrix is sparse enough you might have a chance, but you should do some researching on the topic.) (如果矩阵足够稀疏,则可能会有机会,但是您应该对此主题进行一些研究。)

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

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