def _testMatching(): g = SimpleGraph() verts = list(range(7)) g.addVertices(verts) assert g.isMatching() g.addEdge(0, 1) assert g.isMatching() g.addEdge(1, 2) assert not g.isMatching() g.addEdge(2, 3) g.addEdge(4, 5) assert not g.isMatching() g.removeEdge(1, 2) assert g.isMatching() assert not g.isPerfectMatching() g.addEdge(6, g.pushVertex()) assert g.isPerfectMatching() g = _build4by4() assert not g.isMatching() assert g.maximumMatching().isPerfectMatching() v_a = g.pushVertex() g.addEdge(0, v_a) m = g.maximumMatching() assert m.isMatching() assert not m.isPerfectMatching() v_b = g.pushVertex() g.addEdge(v_a, v_b) assert g.maximumMatching().isPerfectMatching() g.addEdge(v_b, 4) assert g.maximumMatching().isPerfectMatching() g = SimpleGraph() A = _makeChain(g, 9) assert not g.isCycle(A) assert g.maximumMatching().edgeCount() == 4 g.addEdge(A[0], A[-1]) assert g.isCycle(A) assert g.maximumMatching().edgeCount() == 4 hub = g.pushVertex() for Av in A: g.addEdge(hub, Av) assert g.maximumMatching().isPerfectMatching() for Av in A: g.addEdge(Av, _makeLoop(g, 5)[0]) assert not g.maximumMatching().isPerfectMatching() g.removeVertex(hub) assert g.maximumMatching().isPerfectMatching() g = _build4by4() g.removeVertices([4, 8, 3, 15]) assert g.maximumMatching().isPerfectMatching()
def _build4by4(): # makes a grid structure like: # 0 - 1 - 2 - 3 # ! ! ! ! # 4 - 5 - 6 - 7 # ! ! ! ! # 8 - 9 - 10- 11 # ! ! ! ! # 12- 13- 14- 15 g = SimpleGraph() g.addVertices(range(16)) for k in range(0, 16, 4): for i in range(k, k + 3): g.addEdge(i, i + 1) for k in range(4): for i in range(k, 12, 4): g.addEdge(i, i + 4) for i in [0, 3, 12, 15]: assert g.degree(i) == 2 for i in [1, 2, 7, 11, 14, 13, 8, 4]: assert g.degree(i) == 3 for i in [5, 6, 9, 10]: assert g.degree(i) == 4 return g