[英]Graph Algorithm to count all possible paths between different starting vertices and one end vertex
I am trying to find the time efficient algorithm for the following task 我正在尝试为以下任务找到省时的算法
Given a set of pages A, B, C, D, E where A,B,C,D can be starting points but E will always be a ending point. 给定一组页面A,B,C,D,E,其中A,B,C,D可以作为起点,但E始终是终点。 The following representing connections between pages (A,B), (A,B), (A,C), (A,E), (B,A), (B,C), (C,A), (C,E), (C,D), (D,E), (D,A). 以下代表页面(A,B),(A,B),(A,C),(A,E),(B,A),(B,C),(C,A),(C ,(E),(C,D),(D,E),(D,A)。 Now if I choose
A
as starting point andE
as ending point then I have to find all possible paths betweenA
andE
whose path length is at most 4 in a time efficient manner. 现在,如果我选择A
作为起点,选择E
作为终点,那么我必须以省时的方式找到A
和E
之间A
所有可能路径,路径长度最多为4。 So all possible paths betweenA
andE
with at most path length 4 are 因此,A
和E
之间A
所有路径长度最大为4的所有路径都是A->E A→E
A->D->E A-> D-> E
A->C->D->E A-> C-> D-> E
A->B->C->E A-> B-> C-> E
A->B->C->E A-> B-> C-> E
A->B->A->E and many more A-> B-> A-> E等
Note 1: Two edges between two same vertices are treated as different and also order of vertices also important. 注1:两个相同顶点之间的两个边被视为不同,并且顶点的顺序也很重要。 A cycle can exist in Graph. 图中可以存在一个循环。
Note 2: Only way to break the infinite loop search is by constraint of maximum limit of path length. 注2:打破无限循环搜索的唯一方法是限制路径长度的最大限制。
This problem can be solved by dynamic programming. 这个问题可以通过动态编程来解决。
We can represent each step by two parameter, the current node and the number of step which are already taken (node, step) 我们可以用两个参数表示每个步骤,即当前节点和已经采取的步骤数(节点,步骤)
Pseudo code: 伪代码:
int[][]dp;
int getNumberOfWay(int node, int step){
if(step == 4)
if(node == end node)
return 1;
return 0;
if(already visit this step)
return dp[node][step];
int result = 0;
if(node == end node)
result++;
for(node that has edges coming from this node)
result += getNumberOfWay(next node, step + 1);
return dp[node][step] = result;
}
Time complexity for this is O(4*E*N) with E is number of edge and N is number of node. 为此的时间复杂度为O(4 * E * N),其中E为边数,N为节点数。
Note: the problem can also be improved if we traverse the graph from the end node, rather than 4 start nodes. 注意:如果我们从末端节点而不是从4个起始节点遍历图形,也可以改善该问题。
It can be solved using matrix multiplication. 可以使用矩阵乘法来解决。
Let's say A[i][j]
denotes the total way to walk from i
to j
, then we have A'[i][j] = A[i][k] * A[k][j]
because of multiplication rule of combination. 假设A[i][j]
表示从i
到j
的总走法,则由于乘法,我们有A'[i][j] = A[i][k] * A[k][j]
合并规则。 And we can see that this formula is in a form of matrix multiplication. 我们可以看到该公式采用矩阵乘法的形式。
Therefore, we can construct a matrix A
with N
rows and columns, where N
is the totals number of pages. 因此,我们可以构造一个具有N
行和列的矩阵A
,其中N
是总页数。 Then A[i][j]
denotes the initial ways to go to j
starting from i
. 然后A[i][j]
表示从i
开始进入j
的初始方式。 For example, initial A
for your problem will be: 例如,您的问题的首字母A
为:
A B C D E
A 0 2 1 0 1
B 1 0 1 0 0
C 1 0 0 1 1
D 1 0 0 0 1
E 0 0 0 0 0
Next, we just need to calculate A[0..3][4] + A^2[0..3][4] + A^3[0..3][4] + A^4[0..3][4]
, where A^k
means the A to the power of k
. 接下来,我们只需要计算A[0..3][4] + A^2[0..3][4] + A^3[0..3][4] + A^4[0..3][4]
其中A^k
意味着A到的功率k
。
The good thing about using matrix multiplication is it could be extended to a more general question: how many paths from i to j could be formed using exactly K steps? 使用矩阵乘法的好处是,它可以扩展到一个更普遍的问题: 使用正好K个步长可以形成从i到j的多少条路径? . 。
In this case, we only need to calculate A^k
, which could be sped up using exponentiation by squaring and solved in O(log k) * O(N^3)
, where O(N^3)
is the complexity of matrix multiplication. 在这种情况下,我们只需要计算A^k
,可以通过平方取幂来加速,并在O(log k) * O(N^3)
求解,其中O(N^3)
是矩阵的复杂度乘法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.