Skip to content

Commit

Permalink
Updated divide conquer doc for 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jdgarciauc3m committed Feb 28, 2018
1 parent 18e0474 commit 736527b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/0.3.1/divide-conquer_8md.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@
<li><b>Generic problem divide/conquer</b>: Applies the <em>divide/conquer</em> pattern to a generic problem and returns a solution.</li>
</ul>
<h2>Key elements in divide/conquer</h2>
<p>The key elements of the <b>divide/conquer</b> pattern are: a <b>Divider</b> operation that divides a problem into subproblems, a <b>Solver</b> operation that is used to solve a subproblem, and a <b>Combiner</b> operation that is used to merge results of subproblems.</p>
<p>The key elements of the <b>divide/conquer</b> pattern are: a <b>Divider</b> operation that divides a problem into subproblems, a <b>Predicate</b> that signals if a problem is already an elemental problem, a <b>Solver</b> operation that is used to solve a subproblem, and a <b>Combiner</b> operation that is used to merge results of subproblems.</p>
<p>A <b>Divider</b> is any C++ callable entity that takes a problem and returns a collection of subproblems. The returned collection of subproblems must be iterable. This allows returning any standard C++ sequence container, or even a plain array. When a problem cannot be divided into subproblems, the divider returns a collection with a single subproblem.</p>
<p>A <b>Predicate</b> is any C++ callable entity that takes a problem and returns a boolean value. Returning <em>true</em> means that the problem is already elemental and should be solved using the <b>Solver</b>. Returning <em>false</em> means that the problem needs to be divided into subproblems by the <b>Divider</b>.</p>
<p>The <b>Solver</b> is any C++ callable entity that takes a problem and turns it into a solution. The signature of the solver takes as argument a problem and returns the corresponding solution.</p>
<p>The <b>Combiner</b> is any C++ callable entity capable to combine two solutions. The signature of the combiner takes two solutions and returns a new combined solution.</p>
<h2>Details on divide/conquer variants</h2>
<h3>Generic divide/conquer</h3>
<p>The <b>divide/conquer</b> pattern takes an input problem and generates an output problem. </p><hr/>
<p> <b>Example</b>: Merge sort of an array. </p><div class="fragment"><div class="line">vector&lt;int&gt; v{1,3,5,7,2,4,6,8};</div><div class="line"></div><div class="line"><span class="keyword">struct </span>range {</div><div class="line"> std::vector&lt;int&gt;::iterator first, last;</div><div class="line"> <span class="keyword">auto</span> size()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> distance(first,last); }</div><div class="line">};</div><div class="line"></div><div class="line">std::vector&lt;range&gt; divide(range r) {</div><div class="line"> <span class="keyword">auto</span> mid = r.first + distance(r.first,r.last)/2;</div><div class="line"> <span class="keywordflow">return</span> { {r.first,mid} , {mid, r.last} };</div><div class="line">}</div><div class="line"></div><div class="line">range problem{begin(v), end(v)};</div><div class="line"></div><div class="line"><span class="keyword">auto</span> res = <a class="code" href="group__divide__conquer__pattern.html#gac2ebc10ffc0be3d2f8170e621dbdbca2">grppi::divide_conquer</a>(exec,</div><div class="line"> problem,</div><div class="line"> [](<span class="keyword">auto</span> r) -&gt; vector&lt;range&gt; {</div><div class="line"> <span class="keywordflow">if</span> (1&gt;=r.size()) { <span class="keywordflow">return</span> {r}; }</div><div class="line"> <span class="keywordflow">else</span> { <span class="keywordflow">return</span> divide(r); }</div><div class="line"> },</div><div class="line"> [](<span class="keyword">auto</span> x) { <span class="keywordflow">return</span> x; },</div><div class="line"> [](<span class="keyword">auto</span> r1, <span class="keyword">auto</span> r2) {</div><div class="line"> std::inplace_merge(r1.first, r1.last, r2.last);</div><div class="line"> <span class="keywordflow">return</span> range{r1.first, r2.last};</div><div class="line"> }</div><div class="line">);</div></div><!-- fragment --><hr/>
<p> <b>Example</b>: Merge sort of an array. </p><div class="fragment"><div class="line">vector&lt;int&gt; v{1,3,5,7,2,4,6,8};</div><div class="line"></div><div class="line"><span class="keyword">struct </span>range {</div><div class="line"> std::vector&lt;int&gt;::iterator first, last;</div><div class="line"> <span class="keyword">auto</span> size()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> distance(first,last); }</div><div class="line">};</div><div class="line"></div><div class="line">std::vector&lt;range&gt; divide(range r) {</div><div class="line"> <span class="keyword">auto</span> mid = r.first + distance(r.first,r.last)/2;</div><div class="line"> <span class="keywordflow">return</span> { {r.first,mid} , {mid, r.last} };</div><div class="line">}</div><div class="line"></div><div class="line">range problem{begin(v), end(v)};</div><div class="line"></div><div class="line"><span class="keyword">auto</span> res = <a class="code" href="group__divide__conquer__pattern.html#gac2ebc10ffc0be3d2f8170e621dbdbca2">grppi::divide_conquer</a>(exec,</div><div class="line"> problem,</div><div class="line"> [](<span class="keyword">auto</span> r) -&gt; vector&lt;range&gt; { <span class="keywordflow">return</span> divide(r); },</div><div class="line"> [](<span class="keyword">auto</span> r) { <span class="keywordflow">return</span> r.size()&lt;=1; },</div><div class="line"> [](<span class="keyword">auto</span> x) { <span class="keywordflow">return</span> x; },</div><div class="line"> [](<span class="keyword">auto</span> r1, <span class="keyword">auto</span> r2) {</div><div class="line"> std::inplace_merge(r1.first, r1.last, r2.last);</div><div class="line"> <span class="keywordflow">return</span> range{r1.first, r2.last};</div><div class="line"> }</div><div class="line">);</div></div><!-- fragment --><hr/>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Expand Down

0 comments on commit 736527b

Please sign in to comment.