相关疑难解决方法(0)

Python中的Hopcroft-Karp算法

我正在尝试使用networkx作为图形表示在Python中实现Hopcroft Karp算法.

目前我就是这样:

#Algorithms for bipartite graphs

import networkx as nx
import collections

class HopcroftKarp(object):
    INFINITY = -1

    def __init__(self, G):
        self.G = G

    def match(self):
        self.N1, self.N2 = self.partition()
        self.pair = {}
        self.dist = {}
        self.q = collections.deque()

        #init
        for v in self.G:
            self.pair[v] = None
            self.dist[v] = HopcroftKarp.INFINITY

        matching = 0

        while self.bfs():
            for v in self.N1:
                if self.pair[v] and self.dfs(v):
                    matching = matching + 1

        return matching

    def dfs(self, v):
        if v != None:
            for …
Run Code Online (Sandbox Code Playgroud)

python algorithm graph bipartite graph-algorithm

7
推荐指数
1
解决办法
5325
查看次数

标签 统计

algorithm ×1

bipartite ×1

graph ×1

graph-algorithm ×1

python ×1