Network Flows and Perfect Marriage Problem

(This post is dedicated to Taha and Rıza, my fellow classmates.)


I am trying to survive finals and tomorrow I have a CS final, so I wanted to write about a fun problem in computer science (and maths).

As you have probably noticed, CS people tend to give geeky names to algorithms and methods (we have The Chinese Restaurant Process and Monte Carlo Algorithm, for example). Perfect marriage problem is actually nothing but a funny name for an optimal matching problem.

Let me tell you the problem first: suppose you are the top coder for a celebrity matchmaking website. The company is not very famous yet, so you just have fourteen, heterosexual clients for now, and they tell you their preferences among the opposite sex clients:

Scarlett Johansson: “I would marry Justin Timberlake or Jared Leto. They are both very handsome.”

Jennifer Aniston: “I love Brad Pitt. However, if I marry Orlando Bloom or Justin Theroux I wouldn’t get so sad.”

Angelina Jolie: “Brad Pitt is the love of my life. Please do not match him with anyone else!”

Lindsay Lohan: “It doesn’t really matter to me. Just do not match me with Brad Pitt or Justin Theroux. They are booo-ring.”

Paris Hilton: “Umm, I guess Jared Leto or Leonardo di Caprio would do.”

Demi Moore: “I would marry Brad Pitt or Leonardo di Caprio. I like blondies.”

Miranda Kerr: “Leonardo di Caprio or Orlando Bloom, please.”

(Suppose if Jennifer Aniston,  Demi Moore and Angelina Jolie want to marry Brad Pitt, he also wants to marry one of these women.)
(yes, these statements are made up just for procrastination purposes)
(…and yes, I actually googled “celebrities love triangles”.)

It is very convenient to represent this as a bipartite graph with two sets of vertices (women and men):

(seems like a complicated one!)

Before jumping right in to the problem, however, I guess we have to talk about network flow problems. In graph theory,  flow network is a directed graph where each edge has a capacity and each edge receives a flow. Amount of flow into a vertex equals amount of flow out of it, unless it is a source (a vertex with only outgoing flow) or a sink (a vertex with only incoming flow). In real life applications, flow network is used to model traffic, water pipes, electric circuits, cargo delivery (actually anything you want that involves something travelling through a network of nodes).

For example, let the flow network below represent the traffic in a road system in Ezgiland (a capacity of 4 cars, because why not).


Here are some of the possible flows:

The black numbers in the graph are the capacities (how much car can be on a road at the same time) and the red ones are the flows (how much car we allow on that road at that time).

Well, you could try every possibility and find the optimum (maximum here) flow but for bigger flow networks (for example, the road system in Istanbul) the trial-and-error method would take ages.

Ford-Fulkerson algorithm to the rescue! In this algorithm, we try to find the augmenting flow chains, the sequences of the edges that forms a path in the network when the directions are ignored and that we can add more flow. After we find a path, we can try to optimize the flow with this algorithm.

Let’s try to improve our worst flow in the road system of Ezgiland:

The blue writings in the form (where the flow came from, additional flow) represent my attempts on finding an augmenting flow chain in this flow network. From the source vertex, I can push 3 more “cars” to V2 so I write (+, +5) near V2. From V2, I can push 3 more cars to V4 but after that I would get stuck since the roads with capacities 7 and 4 are already at their maximum capacity. The same thing happens when I try to push 3 more cars to V1. So what will I do next?

Since I am looking for an augmenting flow chain, I am allowed to backflow, so I continue with the road having capacity 9. Whenever I take away flow, I put a minus sign before the additional flow. While travelling from V2 to V3, I take away 4 cars from the road so I write (V2, -4) near vertex V3. Those 4 cars that I took away from the road, I can add them to the road connected to the sink vertex. This augmented chain has bottleneck capacity of 4 (we started with pushing 5 from the source but ended up with 4 cars at the end). That would increase the total flow to 15 + 4 + 4 = 23, which gives us the optimal solution. You could try to find other augmenting paths but I believe this is the one with the biggest bottleneck capacity (if you find a better solution please let me know).

Since we learned the algorithm, FINALLY, we can go back to our perfect marriage problem.

I am expecting this question from you now: “What does the network flow problem have to do with the perfect marriage problem? The graph we previously had in the matchmaking example doesn’t even have any source or sink vertex, and it is undirected!”

Well… you are right. In fact, in order to use Ford-Fulkerson algorithm in perfect matching problem, we add source and sink vertices and we make the graph directed!


Then, we arbitrarily choose husbands for each female celeb. Scarlett Johansson (SJ) matches with Justin Timberlake (JT), Jennifer Aniston (JA) with Brad Pitt (BP) (…yes I did this on purpose lol), Lindsay Lohan (LL) with Leonardo di Caprio (LC), Paris Hilton (PH) with Jared Leto (JL) and Miranda Kerr (MK) with Orlando Bloom (OB). Then we are left with Angelina Jolie (AJ) and Demi Moore (DM). Although there are two male celebs left, since their dream husbands are matched with others now, they remain single.

Is this the perfect matching? We have to inspect the graph. We cannot directly say “Oh, there are two women and two men left, so there should be a better solution”.

Sometimes we will not find the perfect marriage. There won’t be enough men for every women, or every women will want the same man. Maybe a woman will refuse to marry any of these man even if it’s the end of the world. Angelina Jolie, for example, does not accept that there’s plenty of handsome fish in the sea. She just wants to marry Brad Pitt. If Brad Pitt was not in this set, she would definitely remain single. marriageford1

Anyway, let’s go back to graphs and network flows. In this graph, every edge has a maximum capacity of 1. If we push a flow of 1 to an edge, this means the endpoints of the edge are getting married. An empty edge means 0 flow.

We apply Ford-Fulkerson algorithm to find the optimal solution. From the source, we first push 1 to AJ to maximize the flow. Then, there is only one edge coming out from AJ, which is going in to BP. Then we do something interesting: since male celebs always have incoming edges, we have to separate BP from his current wife and hope that his wife will not get upset and can marry someone else. BP’s current wife turns out to be JA ( 😦 ) so we send -1 flow from BP to JA (You could think of +1 as a wedding proposal and -1 as filing for divorce). Then, we see that Justin Theroux (JTo) is not taken yet, so we match them.




We managed to solve this one. We still have the problem of matching DM to someone though.

Do not give up. We can do it!

From the source, we again push 1, but this time to DM. We check the men set and we see DM either wants to marry BP or LC. They are both taken, so we definitely have to seperate a couple. However, BP has just got divorced, so we do not want to make him upset again (he will refuse to pay us if we do). We choose to seperate LC from LL. LL said he would nearly marry anyone (thank goodness), so we would match her with Adam Levine (AL), who is currently single. marriageford4


Aaand we are done! Your clients will live happily ever after (?), the matchmaking company will soon become famous and you will get a raise as the top coder. Congrats!

The final matching looks like this, if you are curious:

(Scarlett Johansson – Justin Timberlake, Jennifer Aniston – Justin Theroux, Angelina Jolie – Brad Pitt, Lindsay Lohan – Adam Levine, Paris Hilton – Jared Leto, Demi Moore – Leonardo di Caprio, Miranda Kerr – Orlando Bloom)


Thanks for reading. Wish me luck on my finals!


  1. Tutorial (a very good one)
  2. Perfect Marriage and Maximum Flow
  3. Maximum Flow Problem



“Draw Without Lifting Pencil” Puzzles (Euler Paths & Circuits)

Shape 1

Remember this shape? I bet you could tell me the answer without me asking the question. Most of us solved this puzzle million times when we were at primary school.

Let me ask the question again: how can you draw this shape without tracing the same line twice and without taking the pencil off the paper?

I will talk about the mathematical approach to this problem in this post, which includes graph theory (so it would be very nice if you go and read the basics of graph theory before continuing, although it is not that necessary since I assume you will understand the general concept).

For this shape, you probably memorized the answer by doing it so many times. You know where to start and where to finish. However, suppose you see a shape like this:

Shape 2

Now we have to modify our question a little: can you draw this?

We can convert the shape into a graph by assigning vertices to intersections and assigning edges to the lines between the vertices. Let’s try this with our popular shape:


(There are ten edges in this graph: 1-2, 1-3, 2-3, 2-4, 2-5, 3-4, 3-6, 4-5, 4-6 and 5-6.)

Now we are looking for a path (or a cycle) in the graph that visits every edge exactly once. This problem was solved by the famous mathematician Euler in 1736 and is considered to be the beginning of the graph theory. The problem is often referred as an Euler path or Euler circuit problem. An Euler path starts and ends at different vertices, whereas an Euler circuit starts and ends at the same vertex.

For an Euler path P, for every vertex v other than the endpoints, the path enters v the same number of times it leaves v (what goes in must come out). Then, there should be twice this number of edges having v as an endpoint (try to visualize this: -*-, where asterisk is a vertex which has one entrance = one exit, and to which one times two edges are connected).  Therefore, every v should have an even degree (even number of edges should be connected to v).

Now suppose P starts at vertex p and ends at vertex q. Then P should leave p one more time than it enters, and enter q one more time than it leaves. This makes degrees of p and q even degree minus one, therefore, the endpoints of P should have odd degrees (odd number edges should be connected to v).

Then the conclusion  for P is this:

“If a graph has an Euler path, then it must have exactly two odd vertices.”

For an Euler circuit C, the starting point must be the same with the endpoint, so C enters the endpoint the same number of times it leaves it, which makes it a vertex of even degree.

Then the conclusion for C is this:

“If a graph has an Euler circuit, then all of its vertices must be even vertices.”

Let’s apply these on our examples. In shape 1, vertices 1, 2, 3 and 4 all have even degrees of two, four, four and four, respectively. Vertices 5 and 6 both have odd degrees of three. Then, this graph has at least one Euler path but it does not have any Euler circuit. In shape 2, there are four vertices of odd degree and one vertex of even degree, so it does not have any Euler path or Euler circuit.

Question: Is it possible for a graph to have both an Euler path and an Euler circuit? 
Answer: No. (I think you can figure this out by yourself. I trust you!)

Now we have to focus on our main question: if a path/circuit exists, how do we find it?

For small graphs, you could try every possibility, of course, but in real life applications there will surely be graphs with thousands or millions of vertices and trial-and-error method will take so much time even with a computer program.

A systematic approach would be Fleury’s Algorithm. In this algorithm, our motto is this old proverb: Don’t burn your bridges behind you (it also exists in Turkish: Gectigin kopruleri yakma. I like the English version better though). In graph theory, bridge is the only edge which connects two separate sections of the graph. Removing this edge from the graph would make it disconnected.


To find an Euler path/circuit in a graph:

  • Make sure it has one.
  •  If you are looking for an Euler path, start from any odd vertex. Else, start from any vertex.
  • A non-bridge ALWAYS has priority over a bridge. ALWAYS CHOOSE THE NON-BRIDGE.
  • Delete the edge that you have traversed.
  • If you do not have any edges left, stop.

Let’s see an example (I am always taking the images from the other sites in this post but I will start with a different vertex, I promise).


In this graph, vertices A, B, C, D, E and F are all even, so we will find an Euler circuit. We could start with any vertex, say B. (1) Then we would proceed again with any one of them, say A. From A, there is no bridge so we can safely (2) travel to D. Then from D, we would either (3) travel to F or C (say C) BUT DEFINITELY NOT B since we would get stuck at B, we would burn the bridge (…ne gemiler yaktim). Then we could proceed with (4) A or E (say A) (not F!), then with (5) E, then (6) C, (7) F, (8) D, (9) B, and since there is no edge left we would finally stop.

(Notice that we started and ended with vertex B, as we were supposed to do.)

If we go back to shape 1, it does not matter if you start from 5 or 6 since the solutions are mirror images of each other. There are 44 possible solutions if you start from vertex 5 and there are only 10 ways to lose.

Ways to win this game (Do not forget to say das ist das Haus vom Ni-ko-laus while trying these at home!)

Since there are so many solutions to this, let’s see an example of a failure. If we follow the path 6-5-4-3-6-4-2 and delete all the edges that we travel, the final graph would look like this:


If we continue with vertex 5, which is a bridge, we would get stuck there, so we would have to lift our pencil to draw this shape.

I hope you are not bored yet. If you are, though, here is a small exercise for you. The question is this: can you draw the shape below without tracing the same line twice and without taking the pencil off the paper?


I hear you say “No, since it neither contains zero nor two odd vertices.”

Well, the answer is…

YES!!!! You could draw it!

“WHAT?! But how? I trusted you, Ezgi!”

Here’s how:

(Well, he did lift his hand… So Euler and I are not liars after all…)


  3. Haus vom Nikolaus
  4. Data Structures and Algorithm Analysis in C++, Mark A. Weiss, Fourth Edition