DINIC的算法,用于多个水槽,单源最大流量问题

发布于 2025-02-04 07:17:21 字数 870 浏览 2 评论 0原文

我正在尝试实现一种算法,该算法可以找到两个接收器,以便从给定来源的总流量最大。我正在使用dinic的算法,并在

def maxflow( G,s ):
    g = Graph( G )
    maximum=0
    for i in range( g.V ):
        g.addEdge( ( i, g.V-1, float('inf') ) )
        g2 = copy.deepcopy( g )
        for j in range(g2.V):
            if j == i or j == s or i==s:
                continue
            g2.addEdge( ( j, g.V-1, float('inf') ) )
            maximum = max( g2.DinicMaxflow( s, g.V-1 ), maximum )
            g2 = copy.deepcopy( g )
        g.removeVirtualSink(i)
    return maximum

据我所知,除了检查所有可能的顶点之外,实际上没有更好的方法。我遇到的问题是,由于算法留下残留矩阵,我需要一种方法来存储图形的“默认”状态,以便我可以在每个dinicmaxflow()调用后返回它,但是deepcopy()非常慢。 有没有一种方法可以更快地复制数据或优选的数据,以便在每个函数调用后都不必复制数据?

I'm trying to implement an algorithm that would find two sinks such that the total flow from a given source is largest. I'm using Dinic's algorithm and largerly basing my implementation on this version with appropriate driver function:

def maxflow( G,s ):
    g = Graph( G )
    maximum=0
    for i in range( g.V ):
        g.addEdge( ( i, g.V-1, float('inf') ) )
        g2 = copy.deepcopy( g )
        for j in range(g2.V):
            if j == i or j == s or i==s:
                continue
            g2.addEdge( ( j, g.V-1, float('inf') ) )
            maximum = max( g2.DinicMaxflow( s, g.V-1 ), maximum )
            g2 = copy.deepcopy( g )
        g.removeVirtualSink(i)
    return maximum

As far as I know there really isn't a better way of doing this other than checking every possible pair of vertices. The problem I have is that due to the algorithm leaving a residual matrix I need a way to store a "default" state of the graph so that I can return to it after every DinicMaxflow() call, however deepcopy() is terribly slow.
Is there a way to either copy the data faster or preferably make it so that I don't have to copy the data after every function call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-02-11 07:17:21

我也遇到了同样的问题,我想到了一个解决方案:在其中您可以额外的节点:将其连接到无限流量范围的所有开始节点,并将其视为启动节点。
在水槽中,同一情况下,连接到一个额外的节点,将其连接到具有INF范围的所有先前的水槽节点,并将此新节点视为最终水槽。
Dinic的算法已经是一种复杂的算法,而多启动和水槽则增加了更复杂的功能。因此,这个技巧可能会有所帮助。我测试了实现,并一直工作。
祝你今天过得愉快。

I also encounter the same problem, I came up with a solution: where u make an extra node: connected it to all the start node with infinite flow range and consider it as starting node.
In the sink, same case, connect to an extra node, connect it to all previous sink nodes with inf range and consider this new node as an final sink.
dinic's algorithm is already an complex one, while multiple start and sink add more complexity. so this trick might help. I tested the implementation and always worked.
Have a nice day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文