Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in definition of "minimum-weight perfect matching" function #4

Open
Shriyanshagro opened this issue Nov 27, 2019 · 5 comments
Open

Comments

@Shriyanshagro
Copy link

def minimum_weight_matching(MST, G, odd_vert):

The current approach doesn't ensure minimum weighted edges from odd_vert with no two edge-sharing same vertices.

the function should be instead:

def minimum_weight_matching(MST, G, odd_vert):
    vertices = []
    for W, u, v in sorted((G[u][v], u, v)
                          for u in G
                          for v in G[u]
                          if v in odd_vert and u in odd_vert):
        if u not in vertices and v not in vertices:
            length = G[v][u]
            MST.append((u, v, length))
            vertices.append(u)
            vertices.append(v)
@Retsediv
Copy link
Owner

@Shriyanshagro Thank you so much! Great catch!

Could you, please, make PR to resolve this issue + test which shows that it will actually solve this problem?

@Shriyanshagro
Copy link
Author

Shriyanshagro commented Nov 29, 2019 via email

@PhanLeSon03
Copy link

This code is not running correctly. It is not written well (difficult to understand) and having a lot of mistakes. Example: finding the perfect matching and union with spanning tree is included in function 'minimum_weight_matching'. But did not check the redundance adding: 1 edge could be added more than 1 time.
def minimum_weight_matching(MST, G, odd_vert):
import random
random.shuffle(odd_vert)

while odd_vert:
    v = odd_vert.pop()
    length = float("inf")
    u = 1
    closest = 0
    for u in odd_vert:
        if v != u and G[v][u] < length:
            length = G[v][u]
            closest = u
    if (v, closest, length) not in MST:  ----------> to avoid repeated adding
        MST.append((v, closest, length))
        odd_vert.remove(closest)

Some more points but I do not list here.

@Retsediv
Copy link
Owner

@PhanLeSon03 You are welcome in submitting the PR to fix that

@CCSharper
Copy link

Unfortunately this code only finds a perfect matching with a lot of good edges, but it doesn't find the one and only minimal perfect matching (e.g. it is possible that the minimal perfect matching doesn't contain the edge with lowest weight)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants