<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Neeldhara</title>
<link>https://www.neeldhara.com/cp.html</link>
<atom:link href="https://www.neeldhara.com/cp.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.6.40</generator>
<lastBuildDate>Thu, 30 Sep 2021 18:30:00 GMT</lastBuildDate>
<item>
  <title>Sam I Am</title>
  <link>https://www.neeldhara.com/blog/cp/sam-i-am/</link>
  <description><![CDATA[ 





<p>This is mostly about solving Sam I Am (<a href="https://onlinejudge.org/external/114/11419.pdf">UVa 11419</a>); en route, we will end up discovering Kőnig’s theorem, which is a delightful fact about the special relationship shared by vertex covers and maximum matchings in bipartite graphs.</p>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The Problem</h2>
<p>Here’s an abridged version of the problem statement.</p>
<blockquote class="blockquote">
<p>Sam is facing a temple which can be described by a <img src="https://latex.codecogs.com/png.latex?m%20%5Ctimes%20n"> grid and <strong>he has the locations of all enemies in the temple</strong> (each location can be thought of as the intersection of a row and a column in this grid).</p>
</blockquote>
<p>All of a sudden, he realizes that he can kill the enemies without entering the temple using the great cannon ball which spits out a gigantic ball bigger than him killing anything it runs into and keeps on rolling until it finally explodes.</p>
<p><strong>But the cannonball can only shoot horizontally or vertically and all the enemies along the path of that cannon ball will be killed.</strong></p>
<p>Sam wants to know the <strong>minimum number of cannon balls</strong> and the positions from which he can shoot the cannonballs <strong>to eliminate all enemies</strong> from outside that temple. &gt;</p>
</section>
<section id="some-initial-observations-with-an-example" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="some-initial-observations-with-an-example">Some initial observations with an example</h2>
<p>So to begin with, we have a grid with some cells identified as locations where Sam’s enemies are positioned, and here’s an example:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.003.jpeg" class="img-fluid figure-img"></p>
<figcaption>A first example of a configuration of Sam’s enemies location</figcaption>
</figure>
</div>
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="margin-aside">Conveniently for us, the enemies don’t move around.</span></div></div>
<p>We want to <em>hit</em> all of these locations, and what we have at our disposal is giant cannon balls which can destroy all enemies that are positioned on a single row, or a single column. For example, if we were obsessed about only firing along rows, we would need four cannon balls to tackle ’em all, like so:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.006.jpeg" class="img-fluid figure-img"></p>
<figcaption>A solution were we fire along rows alone</figcaption>
</figure>
</div>
<p>If Sam was superstitious about shooting along columns only, then he would again need four of these cannon balls to take care of everything:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.007.jpeg" class="img-fluid figure-img"></p>
<figcaption>A solution were we fire along columns alone</figcaption>
</figure>
</div>
<p>However, our friend Sam is smart, not superstitious! And if there is anything that he is obsessed about, it is ruthlessly optimal destruction! In other words, he wants to fix everything up, but while using the smallest number of cannon balls possible… and if that means mixing up ranks and files, so be it — notice that you can manage with just three once you combine the use of both axes:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.008.jpeg" class="img-fluid figure-img"></p>
<figcaption>A solution leveraging a combination of rows and columns</figcaption>
</figure>
</div>
<p>And for this example in particular, notice that three cannon balls are <em>necessary</em>, because we have at least three enemies positioned at locations that share <em>neither a row nor a column</em>, implying that no row-fire or column-fire can handle more than one of these locations at once:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.009.jpeg" class="img-fluid figure-img"></p>
<figcaption>Demonstrating that three canonballs are in fact necessary</figcaption>
</figure>
</div>
<p>So for this example, we know that:</p>
<ul>
<li>three cannon balls are necessary &amp;</li>
<li>three cannon balls are sufficient.</li>
</ul>
<p>In general, let’s say that two enemy positions are <em>mutually independent</em> if they are on different columns <em>and</em> on different rows. Let <img src="https://latex.codecogs.com/png.latex?k"> be the size of a largest collection of mutually independent positions. Then it is clear that:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?k"> cannon balls are necessary to handle all enemy locations;</li>
</ul>
<p>because any <img src="https://latex.codecogs.com/png.latex?%5Cell"> fires that handle <em>all</em> enemy locations must in particular handle these <img src="https://latex.codecogs.com/png.latex?k"> mutually independent ones, and each individual fire can get to (at best) one of them — by definition of what it means for two positions to be mutually independent. So if we have a valid solution involving <img src="https://latex.codecogs.com/png.latex?%5Cell"> cannon balls, then <img src="https://latex.codecogs.com/png.latex?%5Cell%20%5Cgeq%20k">.</p>
<p>What is a less obvious, but considerably fascinating, is the fact that:</p>
<ul>
<li>there is always a strategy to hit <em>all</em> locations using just <img src="https://latex.codecogs.com/png.latex?k"> cannon balls. 🤯</li>
</ul>
<p>A striking situation, no pun intended — the obvious estimate of what is needed turns out to be enough as well! The easy lower bound has a matching upper bound ❤️</p>
</section>
<section id="an-auxiliary-graph" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="an-auxiliary-graph">An auxiliary graph</h2>
<p>Alright, I think that’s enough with the advertising.</p>
<p>How does this work?</p>
<p>Let’s construct the following graph <img src="https://latex.codecogs.com/png.latex?G%20=%20(V,E)"> associated with the grid and the information about enemy positions:</p>
<ul>
<li>Introduce a vertex for every row in the grid; say <img src="https://latex.codecogs.com/png.latex?r_i"> for <img src="https://latex.codecogs.com/png.latex?1%20%5Cleq%20i%20%5Cleq%20m">. These are the <em>row vertices.</em></li>
<li>Introduce a vertex for every column in the grid; say <img src="https://latex.codecogs.com/png.latex?c_j"> for <img src="https://latex.codecogs.com/png.latex?1%20%5Cleq%20j%20%5Cleq%20n">. These are the <em>column vertices.</em></li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.011.jpeg" class="img-fluid figure-img"></p>
<figcaption>The vertices of the auxiliary graph</figcaption>
</figure>
</div>
<ul>
<li>Introduce the edge <img src="https://latex.codecogs.com/png.latex?(r_i,c_j)">, <img src="https://latex.codecogs.com/png.latex?1%20%5Cleq%20i%20%5Cleq%20n;%201%20%5Cleq%20j%20%5Cleq%20m"> if and only if the location at the intersection of the <img src="https://latex.codecogs.com/png.latex?i%5E%7Bth%7D"> row and the <img src="https://latex.codecogs.com/png.latex?j%5E%7Bth%7D"> column corresponds to an enemy position.</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.027.jpeg" class="img-fluid figure-img"></p>
<figcaption>The edges of the auxiliary graph</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.028.jpeg" class="img-fluid figure-img"></p>
<figcaption>A bipartite graph!</figcaption>
</figure>
</div>
<p>Observe that:</p>
<ul>
<li>Any matching in <img src="https://latex.codecogs.com/png.latex?G"> (a collection of mutually disjoint edges) corresponds to a collection of mutually independent enemy positions back in the grid.</li>
<li>What we are looking for is a smallest-sized subset <img src="https://latex.codecogs.com/png.latex?S"> of <img src="https://latex.codecogs.com/png.latex?V(G)"> such that every edge <img src="https://latex.codecogs.com/png.latex?e"> in <img src="https://latex.codecogs.com/png.latex?G"> has at least one of its endpoints in <img src="https://latex.codecogs.com/png.latex?S">. Such a subset is called a vertex cover.</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.030.jpeg" class="img-fluid figure-img"></p>
<figcaption>A vertex cover</figcaption>
</figure>
</div>
<p>So our claim in the language of grids now translates to:</p>
<blockquote class="blockquote">
<p>📝 The size of a maximum matching in <img src="https://latex.codecogs.com/png.latex?G"> is equal to the size of a minimum vertex cover in <img src="https://latex.codecogs.com/png.latex?G">.</p>
</blockquote>
<p>in the language of graphs. Keep in mind that as graphs go <img src="https://latex.codecogs.com/png.latex?G">, happens to be a bipartite graph; which is to say that its vertex set can be parittioned into two parts† such that every edge has exactly one endpoint in each part.</p>
<div class="page-columns page-full"><p></p><div class="no-row-height column-margin column-container"><span class="margin-aside">† In this example, these parts correspond to subsets of row vertices and column vertices.</span></div></div>
</section>
<section id="bring-in-the-flows" class="level2 page-columns page-full">
<h2 class="anchored" data-anchor-id="bring-in-the-flows">Bring in the flows</h2>
<p>Is this much ado for nothing? We seem to have done some translation work, but there’s no proof of this bold claim in sight just yet… 😬</p>
<p>Fair. So here’s a roadmap for what we plan to do next:</p>
<ul>
<li>Use the graph <img src="https://latex.codecogs.com/png.latex?G"> as the basis of a flow network.</li>
<li>Recall the maxflow-mincut duality.</li>
<li><del>Profit.</del> Show the duality that we are interested in by hooking it up the known one.</li>
</ul>
<p>So first things first: we are going to setup a flow network around the graph <img src="https://latex.codecogs.com/png.latex?G">, and here’s a partial picture of what it looks like:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.032.jpeg" class="img-fluid figure-img"></p>
<figcaption>The flow graph</figcaption>
</figure>
</div>
<p>Here’s the official description of how we build this up:</p>
<ul>
<li>Start with the graph <img src="https://latex.codecogs.com/png.latex?G">, and orient every edge between a row vertex and a column vertex so that every such edge originates <em>from</em> the row vertex and latches on <em>to</em> the column vertex.</li>
<li>We assign infinite capacities to all the edges in <img src="https://latex.codecogs.com/png.latex?G">. Go unlimited on the originals! We will even dub these edges <em>original edges</em> going forward.</li>
<li>Add a source vertex <img src="https://latex.codecogs.com/png.latex?s"> and add unit-capacity edges <img src="https://latex.codecogs.com/png.latex?(s,r)"> for every row vertex <img src="https://latex.codecogs.com/png.latex?r">. We will call these edges the <em>row selectors.</em></li>
<li>Add a target vertex <img src="https://latex.codecogs.com/png.latex?t"> and add unit-capacity edges <img src="https://latex.codecogs.com/png.latex?(c,t)"> for every column vertex <img src="https://latex.codecogs.com/png.latex?c">. We will refer to these edges as <em>column selectors.</em></li>
</ul>
<p>That’s it, that’s the flow network <img src="https://latex.codecogs.com/png.latex?(%5Ctilde%7BG%7D,%5Ckappa)"> based on <img src="https://latex.codecogs.com/png.latex?G">, where I’m using <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> to denote the capacity function. Now let’s stare at any valid integral flow in this network — what does it pull out from the middle? 🤔</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.035.jpeg" class="img-fluid figure-img"></p>
<figcaption>A matching hiding in plain sight</figcaption>
</figure>
</div>
<p>Let’s make the following quick observations in the context of a valid integral flow <img src="https://latex.codecogs.com/png.latex?f"> in <img src="https://latex.codecogs.com/png.latex?(%5Ctilde%7BG%7D,%5Ckappa)">:</p>
<ul>
<li>The flow on any edge <img src="https://latex.codecogs.com/png.latex?e"> from <img src="https://latex.codecogs.com/png.latex?G"> (i.e, an original edge) is either zero or one. Indeed, if <img src="https://latex.codecogs.com/png.latex?f(e)%20%3E%201">, then we violate conservation constraints at both endpoints of <img src="https://latex.codecogs.com/png.latex?e">.</li>
<li>For any row or column vertex, at most one original edge incident to it is used by the flow <img src="https://latex.codecogs.com/png.latex?f">. In other words, <img src="https://latex.codecogs.com/png.latex?f(e)%20=%200"> for all but at most one original edge incident to any row or column vertex. Again, if not, combined with the fact that <img src="https://latex.codecogs.com/png.latex?f"> is integral and that the row and column selectors have unit capacity, we will violate conservation constraints on the vertex under consideration.</li>
</ul>
<p>Based on these observations, we have that the set of original edges for which <img src="https://latex.codecogs.com/png.latex?f(e)%20=%201"> forms a matching in back in <img src="https://latex.codecogs.com/png.latex?G">, and in particular, if <img src="https://latex.codecogs.com/png.latex?f"> was maximum flow, then this set would correspond to a maximum-sized matching. Now, let’s look at the corresponding mincut by building the residual graph:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.040.jpeg" class="img-fluid figure-img"></p>
<figcaption>Edges in the residual graph that have a residual capacity of zero are not shown. Also, the original edges that were used by <img src="https://latex.codecogs.com/png.latex?f"> have infinite residual capacity but their corresponding back-edges have unit capacity, but this distinction is not emphasised in the picture because it’s not particularly relevant to our discussion.</figcaption>
</figure>
</div>
<p>Edges in the residual graph that have a residual capacity of zero are not shown. Also, the original edges that were used by <img src="https://latex.codecogs.com/png.latex?f"> have infinite residual capacity but their corresponding back-edges have unit capacity, but this distinction is not emphasised in the picture because it’s not particularly relevant to our discussion.</p>
<p>and considering what vertices are reachable from <img src="https://latex.codecogs.com/png.latex?s">:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.042.jpeg" class="img-fluid figure-img"></p>
<figcaption>The vertices reachable from <img src="https://latex.codecogs.com/png.latex?s"> are marked green, while all remaining vertices are marked red.</figcaption>
</figure>
</div>
<p>The vertices reachable from <img src="https://latex.codecogs.com/png.latex?s"> are marked green, while all remaining vertices are marked red.</p>
<p>In the residual graph, I would like to draw your attention to:</p>
<ul>
<li>row vertices that are unreachable from <img src="https://latex.codecogs.com/png.latex?s">,</li>
<li>column vertices from where it is impossible to reach <img src="https://latex.codecogs.com/png.latex?t">.</li>
</ul>
<p>We will refer to these vertices as the <em>misfits —</em> they are highlighted for you in the picture below:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.044.jpeg" class="img-fluid figure-img"></p>
<figcaption>The misfits</figcaption>
</figure>
</div>
<p>Now roll up your sleeves for some magic. Let’s pull up the cut — which we know is in fact a mincut — obtained by considering the set of vertices reachable from <img src="https://latex.codecogs.com/png.latex?s"> the residual graph corresponding to the maxflow <img src="https://latex.codecogs.com/png.latex?f">. In pictures, note how we have attracted some column vertices to the <img src="https://latex.codecogs.com/png.latex?s">-side, and pushed away some row vertices to the <img src="https://latex.codecogs.com/png.latex?t">-side:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.neeldhara.com/blog/cp/sam-i-am/CP-Week09-Mod03.046.jpeg" class="img-fluid figure-img"></p>
<figcaption>Some reorganization</figcaption>
</figure>
</div>
<p>Note that this is a minimum cut, that is to say, the total capacity of the edges crossing the cut is as small as possible — which means that, in particular, the total capacity is at least (or should that be at most?) finite, and that implies, even more particularly, that <em>none</em><sup>1</sup> of the original edges cross this cut.</p>
<div class="no-row-height column-margin column-container"><div id="fn1"><p><sup>1</sup>&nbsp;Remember how their capacities were infinite? So they just cannot afford to cross a minimum-capacity cut.</p></div></div><p>So every original edge is confined to the <img src="https://latex.codecogs.com/png.latex?s">-camp or the <img src="https://latex.codecogs.com/png.latex?t">-camp; but note that every original edge is an edge between a row vertex and a column vertex; so if you put two and two together, you see that, in fact, <em>every edge must be incident to a misfit vertex.</em> This means that the misfits are what we were looking for all along — they form a vertex cover of <img src="https://latex.codecogs.com/png.latex?G">!</p>
<p>So at least we have <em>some</em> solution. Is this the best we can hope for?</p>
<p>Why yes!</p>
<p>Note that every misfit vertex contributes <em>exactly one unit-capacity edge</em> to the minimum cut: the misfits on the <img src="https://latex.codecogs.com/png.latex?s">-side are incident to column selectors, and these edges connect with <img src="https://latex.codecogs.com/png.latex?t"> on the other side; while misfits on the <img src="https://latex.codecogs.com/png.latex?t">-side are incident to row selectors, and these edges connect with <img src="https://latex.codecogs.com/png.latex?s">, which is again on the opposite end. So every misfit vertex contributes exactly one edge to the minimum cut — and there are no other edges that cross the cut, so we have the following sequence of equalities:</p>
<ul>
<li>size of the proposed solution = #misfits</li>
<li>#misfits = capacity of the minimum cut</li>
<li>capacity of the minimum cut = value of the maximum flow</li>
<li>value of the maximum flow = size of a maximum matching back in <img src="https://latex.codecogs.com/png.latex?G"></li>
<li>size of a maximum matching back in <img src="https://latex.codecogs.com/png.latex?G"> = lower bound on our solution</li>
</ul>
<p>Therefore, we have proposed a solution whose cost matches a lower bound on it, making it optimal! With a slight adjustment of language (dropping misfits in favor of vertex cover), the sequence of inequalities above also shows that the size of a minimum vertex cover in a bipartite graph equals the size of a maximum matching in the graph.</p>
<p>This was the relationship I’d promised to cover when we started, and it goes by Kőnig’s theorem in case you’d like to find out more — the argument we came up with here isn’t perhaps the traditional proof, and this is a fact that can be established in several different ways, all fun in their own way ❤️</p>


</section>


 ]]></description>
  <category>cp</category>
  <category>lecturenotes</category>
  <guid>https://www.neeldhara.com/blog/cp/sam-i-am/</guid>
  <pubDate>Thu, 30 Sep 2021 18:30:00 GMT</pubDate>
</item>
</channel>
</rss>
