This is a pretty common problem. It also shows up in hardware design. There has been a lot of work on algorithms to solve it.
If you are going to write something yourself, start by checking out "Hu's Algorithm".
If you just want a solution, these functions are built into architectural synthesis programs. Look at the Wikipedia pages on high level synthesis and logic synthesis. There are several professional tools that can handle this, if you can get access to them through school or work.
There are university programs you can often get for free that can also handle this problem. I'm not up-to-date on what is currently available. An very old one is MIS II from Berkeley. It's scripting language was Tcl, not Python.