Weird, I had a project doing this exact same thing, just not as far along and not very well documented. I never actually had a use for it, just thought it was a cool problem. I had a nice functional algorithm for doing it. (Again, not documented except through some graphviz output.)
Seeing this, I may stop working on it.