简体   繁体   English

给定具有自循环的有向加权图,找到与给定节点x完全相距k dist的节点列表?

[英]Given a directed weighted graph with self loops ,find the list of nodes that are exactly k dist from a given node x?

Each edge in the graph has weight of 1,The graph may have cycles ,if a node has self loop it can be any distance from itself from 0 to infinity , depending on the no. 图中的每个边都具有1的权重。图可以具有周期,如果节点具有自循环,则它可以是从0到无穷大的任何距离,具体取决于编号。 of time we take the self loop. 我们采取自我循环的时间。

I have solved the problem using bfs, but the constraint on distance is in order of 10^9 ,hence bfs is slow. 我已经使用bfs解决了这个问题,但是对距离的约束是10 ^ 9的顺序,因此bfs很慢。

We ll be asked multiple queries on a given graph of the form (distance , source) and the o/p is the list of nodes that are exactly at the given distance starting from the source vertex. 我们将在形式(距离,源)的给定图形上询问多个查询,并且o / p是从源顶点开始精确地在给定距离处的节点列表。

Constraints 约束

1<=Nodes<=500
1<queries<=500
1<=distance<=10^9

I have a feeling ,there would be many repeated computations as the no. 我有一种感觉,会有很多重复的计算作为没有。 of nodes are small,but i am not able to figure out how do i reduce the problem in smaller problems. 节点很小,但我无法弄清楚如何在较小的问题中减少问题。

What is the efficient way to do this? 有效的方法是什么?

Edit : I have tried using matrix exponentiation but its too slow ,for the given constraints. 编辑:我已经尝试过使用矩阵求幂,但对于给定的约束,它太慢了。 The problem has a time limit of 1 sec. 问题的时间限制为1秒。

Let G = (V,E) be your graph, and define the adjacency matrix A as follows: G = (V,E)为图形,并定义邻接矩阵A ,如下所示:

A[i][j] = 1       (V[i],V[j]) is in E
          0       otherwise

In this matrix, for each k : 在这个矩阵中,对于每个k

(A^k)[i][j] > 0 if and only if there is a path from v[i] to v[j] of length exactly k.

This means by creating this matrix and then calculating the exponent, you can easily get your answer. 这意味着通过创建此矩阵然后计算指数,您可以轻松获得答案。

For fast exponent calculation you can use exponent by squaring , which will yield O(M(n)^log(k)) where M(n) is the cost for matrix multiplication for nXn matrix. 对于快速指数计算,您可以通过平方使用指数 ,这将产生O(M(n)^log(k)) ,其中M(n)是nXn矩阵的矩阵乘法成本

This will also save you some calculation when looking for different queries on the same graph. 在同一图表上查找不同的查询时,这也可以节省一些计算。

Appendix - claim proof: 附录 - 索赔证明:

Base: A^1 = A , and indeed in A by definition, A[i][j]=1 if and only if (V[i],V[j]) is in E 基数: A^1 = A ,实际上在A根据定义, A[i][j]=1当且仅当(V[i],V[j])E

Hypothesis: assume the claim is correct for all l<k 假设:假设声明对所有l<k都是正确的

A^k = A^(k-1)*A . A^k = A^(k-1)*A From induction hypothesis, A^(k-1)[i][j] > 0 iff there is a path of length k-1 from V[i] to V[j] . 根据归纳假设, A^(k-1)[i][j] > 0如果存在从V[i]V[j]的长度为k-1的路径。

Let's examine two vertices v1,v2 with indices i and j . 让我们检查具有索引ij两个顶点v1,v2
If there is a path of length k between them, let it be v1->...->u->v2 . 如果它们之间存在长度为k的路径,则将其设为v1->...->u->v2 Let the index of u be m . u的指数为m
From ih A^(k-1)[i][m] > 0 because there is a path. 从ih A^(k-1)[i][m] > 0因为有一条路径。 In addition A[m][j] = 1 , because (u,v2) = (V[m],V[j]) is an edge. 另外A[m][j] = 1 ,因为(u,v2) = (V[m],V[j])是边。

A^k[i][j] = A^(k-1)*A[i][j] = A^(k-1)[i][1]A[1][j] + ... + A^(k-1)[i][m]A[m][j] + ... + A^(k-1)[i][n]A[n][j] 

And since A[m][j] > 0 and A^(k-1)[i][m] > 0 , then A^(k-1)*A[i][j] > 0 由于A[m][j] > 0A^(k-1)[i][m] > 0 ,则A^(k-1)*A[i][j] > 0

If there is no such path, then for each vertex u such that (u,v2) is an edge, there is no path of length k-1 from v to u (otherweise v1->..->u->v2 is a path of length k ). 如果没有这样的路径,那么对于每个顶点u使得(u,v2)是边缘,没有从vu的长度k-1路径(其他的v1->..->u->v2是长度为k )的路径。

Then, using induction hypothesis we know that if A^(k-1)[i][m] > 0 then A[m][j] = 0 , for all m . 然后,使用归纳假设我们知道如果A^(k-1)[i][m] > 0A[m][j] = 0 ,对于所有m
If we assign that in the sum defining A^k[i][j] , we get that A^k[i][j] = 0 如果我们在定义A^k[i][j]的和中分配它,我们得到A^k[i][j] = 0

QED QED

Small note: Technically, A^k[i][j] is the number of paths between i and j of length exactly k . 小注释:从技术上讲, A^k[i][j]ij之间的路径数,长度恰好为k This can be proven similar to above but with a bit more attention to details. 这可以证明与上面类似,但更多地关注细节。
To avoid the numbers growing too fast (which will increase M(n) because you might need big integers to store that value), and since you don't care for the value other than 0/1 - you can treat the matrix as booleans - using only 0/1 values and trimming anything else. 为了避免数字增长太快(这会增加M(n)因为你可能需要大整数来存储该值),并且因为你不关心0/1以外的值 - 你可以将矩阵视为布尔值 - 仅使用0/1值并修剪其他任何值。

if there are cycles in your graph, then you can infer that there is a link between each adjacent nodes in cycle * N + 1 , because you can iterate through as much as you wish. 如果图表中有周期,那么您可以推断cycle * N + 1每个相邻节点之间存在链接,因为您可以根据需要进行迭代。

That bring me to the idea, we can use the cycles to our advantage! 这让我想到了这个想法,我们可以利用这些循环来获得优势!
using BFS while detecting a cycle, we calculate offset + cycle*N and then we get as close to our goal(K) 在检测周期时使用BFS,我们计算offset + cycle*N然后我们得到接近我们的目标(K)

and search for the K pretty easily. 很容易搜索K.

eg 例如

A -> B -> C -> D -> BK = 1000; A - > B - > C - > D - > BK = 1000; S = A; S = A;

A - 0 A - 0
B - 1 B - 1
C - 2 C - 2
D - 3 D - 3
B - 1 (+ 4N) B - 1(+ 4N)
here you can check floor() of k - (1+4N) = 0 > 1000 - 1 - 4N = 0 > 999 = 4N > N=249 => best B is 249*4 + 1 = 997 在这里你可以检查k - (1+4N) = 0 > 1000 - 1 - 4N = 0 > 999 = 4N > N=249 =>最佳B是249*4 + 1 = 997 floor()
simpler way would be to calculate: round(k - offset, cycle) 更简单的方法是计算: round(k - offset, cycle)

from here you can count only few more steps. 从这里你可以只计算几个步骤。
in this example (as a REGEX): A(BCD){249}BCD 在这个例子中(作为REGEX): A(BCD){249}BCD

暂无
暂无

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

相关问题 有向图中的深度:在距离给定节点 k 处查找节点 - Depth in Directed Graph: Finding nodes at k distance from a given node 给定一个加权图和自然数 k,如何找到可以除以 k 的从节点 s 到 t 的最便宜的路径? - Given a weighted graph and natural number k how to find the cheapest path from node s to t that can be divided by k? 利用至少K个节点和给定起始节点的贪婪方法在有向图中寻找路径 - Finding Paths in Directed Graph with Greedy Approach With At Least K Nodes and a Given Starting Node 在有向图中找到具有给定长度的路径,允许循环和负长度 - Find a path in a directed graph with a given length, allowing for loops and negative lengths 有向图中两个给定节点的最近相遇节点 - nearest meeting node of two given nodes in a Directed Graph 给定一个有向图,找出两个节点之间是否存在路由 - Given a directed graph, find out whether there is a route between two nodes 给定有一个负边(u,v)的有向加权图,找到最短路径(s,t) - Given directed weighted graph that has one negeative edge (u,v), find shortest path (s,t) 如何在加权无向图中找到包含两个给定节点的最小加权循环? - How to find the minimum weighted cycle containing two given nodes in a weighted undirected graph? 直径为k &lt;| V |的连通加权有向图,找到最短路径 - connected weighted directed graph with diameter k< |V|, find the shortest path 在有向加权图中找到两个节点之间的最短路径 - Find shortest path between two nodes in directed weighted graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM