简体   繁体   English

如何找到某个顶点可到达的所有边的总权重?

[英]How to find the sum weight of all edges reachable from some vertex?

Consider a directed graph with no cycles. 考虑没有周期的有向图。 I need to find for each u the total weight of edges reachable from u (by reachable we mean there's a path from u to some v ). 我需要为每个u找到可从u到达的边缘的总权重(通过可到达我们意味着存在从uv的路径)。

Now, what I thought about is running topological sort and then starting to run from the last node to the first node (possible by interchanging the direction of the edges) 现在,我想到的是运行拓扑排序,然后开始从最后一个节点运行到第一个节点(可以通过交换边的方向来进行)

And then we're evaluating f[v] = f[u] + w(u,v) . 然后我们评估f[v] = f[u] + w(u,v)

but there's a problem; 但是有一个问题 for this graph, we will count f[d] twice. 对于此图,我们将计算f[d]两次。 How can I overcome this? 我该如何克服?

在此处输入图片说明

You can use either BFS or DFS to achieve this. 您可以使用BFS或DFS来实现此目的。

total = 0
dfs (node):
  if visited[node] == 1:
      return
  visited[node] = 1
  for all u connected to node:
      total += weight[node][u]
      dfs(u)

Note that we check the visited after total += weight[node][u] . 请注意,我们在total += weight[node][u]之后检查total += weight[node][u]

You can use a bottom up approach. 您可以使用自下而上的方法。 that is firstly calculate the outdegree of each vertex, Now the vertices with 0 outdegree would have F[u] = 0 for them. 首先计算每个顶点的出度,现在出度为0的顶点的F [u] = 0。 Now add all such vertices in a queue Q. 现在,将所有这些顶点添加到队列Q中。
Also you would need to store the transpose of the Graph, suppose it was T. 同样,您需要存储图的转置,假设它是T。

While(!Q.empty){
u=Q.front();
Q.pop();
for all edges E originating from T[u]{
F[v]+=w; (where (u,v) was the edge with w as weight)
//now remove u from the graph
outdegree[v]--;
if(outdegree[v]==0)
Q.push(v);
}
}

暂无
暂无

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

相关问题 找到有向图的顶点在时间O(| E | + | V |)中可到达的所有顶点 - Find all vertices reachable from a vertex of a directed graph in time O(|E| + |V|) 在所有可达顶点中找到最有价值的顶点 - find the most valuable vertex among all reachable vertices 如何找到入射到特定顶点的边列表 - How to find the list of edges incident to a particular vertex 找到使共享顶点颜色的边的权重总和最小化的图形着色 - Find graph colouring that minimises sum of weights of edges that share vertex colours 如何从一个位置查找所有可能的可到达数字? - How to find all possible reachable numbers from a position? 在有向无环图中,找到一条路径的权重是组成该路径的有向边的权重之和 - In Directed Acyclic graph, find the weight of a path is the sum of the weights of the directed edges comprising the path 如何使用Java查找图的中心(顶点,该顶点与其他每个顶点相连,但边指向图的中心) - how to Find the center of graph (vertex, that is connected with every other vertex, but edges are directed to the center of graph) with java 有向图找到从给定节点输入边缘均不能到达的节点 - Directed graph finding the Node whose input edges are not all reachable from Given node 从加权图中选择边,这样每个顶点都是一个边的端点,并且边权重的总和被最小化 - Choose edges from weighted graph, such that each vertex is an endpoint of one edge, and the sum of edge weights is minimized 在有向图中查找所有其他节点均可访问的节点 - Find nodes in directed graph that is reachable from all other nodes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM