简体   繁体   English

在加权图中查找从节点 A 到 B 的所有路径,权重为 K 或更低

[英]Finding all paths in weighted graph from node A to B with weight of K or lower

I have a directed graph, with source and target.我有一个有向图,有源和目标。 every edge is weighted on weight of 1. The nodes have no weight.每条边的权重为 1。节点没有权重。 I need to find an efficient way(I write on C#, but I don't think it is relevant), to find ALL the paths from the source to target, that the weight of the path(or the number of edges, which is the same with those details) be less or equal to a certain value, let's call it K.我需要找到一种有效的方法(我写在 C# 上,但我认为它不相关),找到从源到目标的所有路径,即路径的权重(或边数,即与那些细节相同)小于或等于某个值,我们称之为K。

A possible way I thought is to run Dijkstra on every value from 1 to K, but since K can be very big it won't be efficient.我认为一种可能的方法是对从 1 到 K 的每个值运行 Dijkstra,但由于 K 可能非常大,因此效率不高。 I have been looking on some answers, but also there was a very similar question to mine, I didn't find this answer as helpfully.我一直在寻找一些答案,但也有一个与我非常相似的问题,我没有发现这个答案有帮助。

( Find all simple path from node A to node B in direct weighted graph with the sum of weighs less a certain value? ) 在直接加权图中找到从节点 A 到节点 B 的所有简单路径,且权重之和小于某个值?

You can simply run a DFS from the source to the target to analyze all paths.您可以简单地运行从源到目标的 DFS 来分析所有路径。 If you have to find number of paths you stop when you reach depth K returning 0 and when you reach the target returning 1 and if you need to describe the paths as well you can build a tree while exploring with the DFS.如果您必须找到在到达深度 K 时停止的路径数,返回 0,到达目标时返回 1,如果您还需要描述路径,则可以在使用 DFS 探索时构建一棵树。 Complexity will be O(E) where E is the number of edges复杂度将为 O(E),其中 E 是边数

Here a mixture of python and pseudo code:这里是 python 和伪代码的混合:

def DFS(node, depth, target, visited, graph, max_depth):
  visited.add(node)
  if depth >= max_depth:
    return Null
  elif node == target:
    return new_tree_node(node)
  else:
    sub_trees = list()
    for connected_node in graph[node]:
      if connected_node not in visited:
        sub_tree = DFS(connected_node, depth+1, target, visited, graph, max_depth)
        if sub_tree != Null:
          sub_trees.append(sub_tree)

    if length(sub_trees) > 0:
      tree_node = new_tree_node(node)
      for sub_tree in sub_trees:
         tree_node.add_child(sub_tree)
      return tree_node
    else:
      return Null

tree = DFS(source, 0, target, set(), graph, K)

PS: this solution could easily be adapted to weighted graph as well PS:这个解决方案也可以很容易地适应加权图

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

相关问题 在加权定向循环图中寻找从A到B的不同路径的算法 - Algorithm for finding distinct paths from A to B in weighted, directed, cyclic graph 枚举加权图中从A到B的所有路径,其中路径长度在C1和C2之间 - Enumerate all paths in a weighted graph from A to B where path length is between C1 and C2 在直接加权图中找到从节点A到节点B的所有简单路径,权重之和小于某个值? - Find all simple path from node A to node B in direct weighted graph with the sum of weighs less a certain value? 求加权有向图中权重最低的路径权重的算法 - Algorithm for finding weight of path with lowest weight in weighted directed graph 使用堆栈查找加权图的最短路径 - Finding Shortest Paths of weighted graph using stacks 在加权无向图中找到固定成本的路径? - Finding paths of fixed cost in weighted undirected graph? 在加权循环有向图中找到从源到目的地的所有路径 - Find all paths from source to destination in a weighted cyclic directed graph 使用给定的数据结构查找从节点 A 到节点 B 的路径 - Finding paths from node A to node B using the given data structure 使用networkx在图中查找基数k的所有node_cut - Finding all node_cut of cardinality k in a graph with networkx 在加权图中找到最大权重集团 C# 实现 - Finding a maximum weight clique in a weighted graph C# implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM