Hopcroft–Karp algorithm in Python

后端 未结 2 1193
一个人的身影
一个人的身影 2020-12-28 23:11

I am trying to implement the Hopcroft Karp algorithm in Python using networkx as graph representation.

Currently I am as far as this:

#Algorithms for         


        
相关标签:
2条回答
  • 2020-12-28 23:43

    In python there is a package for this algorithm. HopcroftKarp, you can directly use that package for your implementation.

    0 讨论(0)
  • 2020-12-28 23:50

    The line

    if self.pair[v] and self.dfs(v):
    

    should be

    if self.pair[v] is None and self.dfs(v):
    

    as per the pseudo-code on the Wikipedia page. The only other problem I see is that you are using the deque as a stack and you want to use it as a queue. To remedy that, you just need to popleft rather than pop (which pops right). So the line

    v = self.q.pop()
    

    should be

    v = self.q.popleft()
    

    Hopefully everything else works. I was just checking that your Python code works in the same manner as the pseudocode on Wikipedia so hopefully that pseudocode is correct.

    0 讨论(0)
提交回复
热议问题