Ask Your Question

Compute perfect matchings in a graph

asked 2016-02-12 17:42:52 -0500

MLainz gravatar image

updated 2016-02-13 00:04:43 -0500

I am trying to calculate all the perfect matchings of a graph G (i. e., the subgraphs of G such that every two points are connected)

def matchings(G):
    """Returns a list of the perfect matchings of G.
    It uses a recursive algorithm. Get a vertex x in G.
    Since all the perfect matchings contain exactly one edge for every vertex,
    we have that matchings(G) = {<x,y> U matchings(G\{x,y}) for y adjacent to x}.
    Ms=[]   #List of mathchings
    x = G.random_vertex()
    for e in G.edges_incident(x):
        for M in matchings(H):
    return Ms

But this gives me

line 14 for M in matchings(H):
SyntaxError: invalid syntax

I don't understand the error (the loop seems fine to me). What's wrong?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-02-13 05:53:16 -0500

tmonteil gravatar image

I did not check the whole correctedness of your algorithm, but i can see a syntax error at the line


At the end of the line, a closing parenthesis is missing.

edit flag offensive delete link more


Thank you. Now I use the Jupyter notebook. It's much easier noticing that mistakes with syntax colouring.

MLainz gravatar imageMLainz ( 2016-02-14 16:50:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-02-12 17:42:52 -0500

Seen: 6,478 times

Last updated: Feb 13 '16