简体   繁体   English

图算法计算不同起始顶点和一个结束顶点之间的所有可能路径

[英]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 and E as ending point then I have to find all possible paths between A and E whose path length is at most 4 in a time efficient manner. 现在,如果我选择A作为起点,选择E作为终点,那么我必须以省时的方式找到AE之间A所有可能路径,路径长度最多为4。 So all possible paths between A and E with at most path length 4 are 因此, AE之间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]表示从ij的总走法,则由于乘法,我们有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.

相关问题 求N个任意顶点之间所有路径的图算法 - Graph Algorithm To Find All Paths Between N Arbitrary Vertices 图顶点之间的所有最小路径 - All minimum paths between the vertices of a graph 在未知大小的加权有向图上,一个人如何遍历两个顶点之间从最短到最长的所有可能的非循环路径? - On a weighted directed graph of unknown size, how can one iterate over all possible acyclic paths between two vertices from shortest to longest? 如何计算两个顶点之间的所有最短路径? - How to count all of the shortest paths between 2 vertices? 如何找到有向图中2个特定顶点之间的所有可能路径中存在的顶点? - How to find vertices exist in all possible route between 2 specific vertex in a directed graph? 在给定起始顶点和深度限制的情况下查找循环有向图中的所有可能路径 - Finding all possible paths in a cyclic directed graph given a starting vertex and a depth limitation 使用Floyd-Warshall算法计算2个顶点之间的路径数 - Using Floyd-Warshall algorithm to count number of paths between 2 vertices C#算法搜索两个顶点之间的所有路径 - C# algorithm search for all paths between two vertices 计算图中2个顶点之间的路径 - Counting Paths Between 2 Vertices In A Graph 在无向图中的两个顶点之间的所有简单路径上查找所有*顶点* - Find all *vertices* on all simple paths between two vertices in an undirected graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM