diff options
| author | Dan Schult <dschult@colgate.edu> | 2016-01-06 13:21:18 -0500 |
|---|---|---|
| committer | Dan Schult <dschult@colgate.edu> | 2016-01-06 13:21:18 -0500 |
| commit | c6ac93b333210d9b044a536f864a61a720bb0723 (patch) | |
| tree | 106cf1149fdd357852c2513baa463bcad730b444 | |
| parent | 82f46bb98cccb6bfb92a4e5c2f474b7777bb0034 (diff) | |
| parent | 712e2e452d1458cb0f0fec03bde46215d4a2bb22 (diff) | |
| download | networkx-c6ac93b333210d9b044a536f864a61a720bb0723.tar.gz | |
Merge pull request #1908 from Arafatk/patch-4
Exclude self-loops from maximal matching
| -rw-r--r-- | networkx/algorithms/matching.py | 2 | ||||
| -rw-r--r-- | networkx/algorithms/tests/test_matching.py | 14 |
2 files changed, 15 insertions, 1 deletions
diff --git a/networkx/algorithms/matching.py b/networkx/algorithms/matching.py index afc505e5..6bb064de 100644 --- a/networkx/algorithms/matching.py +++ b/networkx/algorithms/matching.py @@ -46,7 +46,7 @@ def maximal_matching(G): for u,v in G.edges(): # If the edge isn't covered, add it to the matching # then remove neighborhood of u and v from consideration. - if u not in nodes and v not in nodes: + if u not in nodes and v not in nodes and u!=v: matching.add((u,v)) nodes.add(u) nodes.add(v) diff --git a/networkx/algorithms/tests/test_matching.py b/networkx/algorithms/tests/test_matching.py index 05fa8c1b..ac86da9b 100644 --- a/networkx/algorithms/tests/test_matching.py +++ b/networkx/algorithms/tests/test_matching.py @@ -251,6 +251,20 @@ def test_maximal_matching(): u, v = edge ok_(len(set([v]) & vset) > 0 or len(set([u]) & vset) > 0, \ "not a proper matching!") + graph = nx.Graph() + graph.add_edge(1, 1) + graph.add_edge(1, 2) + graph.add_edge(2, 2) + graph.add_edge(2, 3) + matching = nx.maximal_matching(graph) + assert(len(matching)==1) + vset = set(u for u, v in matching) + vset = vset | set(v for u, v in matching) + + for edge in graph.edges(): + u, v = edge + ok_(len(set([v]) & vset) > 0 or len(set([u]) & vset) > 0, \ + "not a proper matching!") def test_maximal_matching_ordering(): # check edge ordering |
