<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Algorithm on BoilTask's Blog</title><link>https://boiltask.com/en/tags/algorithm/</link><description>Recent content in Algorithm on BoilTask's Blog</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Tue, 09 Aug 2016 18:14:29 +0800</lastBuildDate><atom:link href="https://boiltask.com/en/tags/algorithm/index.xml" rel="self" type="application/rss+xml"/><item><title>Knapsack — The Greedy Trap</title><link>https://boiltask.com/en/knowledge/knapsack-problem/</link><pubDate>Tue, 09 Aug 2016 18:14:29 +0800</pubDate><guid>https://boiltask.com/en/knowledge/knapsack-problem/</guid><description>&lt;blockquote&gt;
&lt;p&gt;There are n items; the i-th item (i = 1, 2, 3…n) has value vi and weight wi. We have a knapsack that can carry at most m. We pick some items to put in it, at most one of each, and obviously the total weight must not exceed m. We want the total value to be as large as possible — so which items do we pick? Assume every number involved is a positive integer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you have not thought the knapsack problem through carefully, the first instinct tends to be greedy.&lt;/p&gt;
&lt;p&gt;Sort by how valuable an item is, for example, and take the valuable ones first.&lt;br&gt;
But consider this data:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;n = 3, m = 3&lt;br&gt;
v = (2, 2, 3)&lt;br&gt;
w = (1, 2, 3)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The &amp;ldquo;most valuable first&amp;rdquo; strategy takes the item worth 3 and the knapsack is full — yet if we take the first two items the total value reaches 4.&lt;/p&gt;
&lt;p&gt;Sort by value for weight instead:
For this data&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;n = 3, m = 7&lt;br&gt;
v = (2, 3, 4)&lt;br&gt;
w = (3, 4, 5)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Since 2/3 &amp;lt; 3/4 &amp;lt; 4/5, we take the third item first — and once it is in, nothing else fits! Total value 4, while the first two items would have given 5.&lt;/p&gt;
&lt;p&gt;The reason is that greedy assumes the global optimum can be built out of each step&amp;rsquo;s local optimum, whereas in the knapsack problem whether taking a step is optimal depends on the whole — that is, it is &amp;ldquo;dynamic&amp;rdquo;. Which is why the knapsack problem is a canonical dynamic-programming problem.&lt;/p&gt;
&lt;p&gt;Now look at a fractional knapsack problem:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A traveller has a knapsack that can carry at most m kilograms, and there are n items whose weight and value are w[i] and c[i]. What is the maximum value the traveller can get?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This one &lt;em&gt;is&lt;/em&gt; a greedy problem: keep taking the best value-for-weight until the bag is full. So whether greedy works comes down to whether an item may be taken partially.&lt;/p&gt;
&lt;p&gt;The knapsack problem then spawns many variants, and the DP formulations differ, so it takes practice to get comfortable with this whole family of problems.&lt;/p&gt;
&lt;p&gt;Reference:&lt;br&gt;
51NOD&lt;br&gt;
Baidu Zhidao&lt;br&gt;
Baidu Wenku&lt;/p&gt;</description></item><item><title>Binary Search — Thinking by Comparing with the Answer</title><link>https://boiltask.com/en/knowledge/binary-search/</link><pubDate>Tue, 26 Jul 2016 17:24:57 +0800</pubDate><guid>https://boiltask.com/en/knowledge/binary-search/</guid><description>&lt;p&gt;Binary search is not so much an algorithm
as a way of thinking.&lt;/p&gt;
&lt;h2 id="core-code"&gt;&lt;a href="#core-code" class="header-anchor"&gt;&lt;/a&gt;Core code
&lt;/h2&gt;
&lt;div class="highlight-wrapper"&gt;
&lt;div class="highlight-header"&gt;
&lt;span class="highlight-language"&gt;cpp&lt;/span&gt;
&lt;/div&gt;
&lt;div class="highlight-body"&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;judge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="advantages"&gt;&lt;a href="#advantages" class="header-anchor"&gt;&lt;/a&gt;Advantages
&lt;/h2&gt;&lt;p&gt;It brings an ordinary search down from O(n) to O(log n).&lt;/p&gt;
&lt;h2 id="requirements"&gt;&lt;a href="#requirements" class="header-anchor"&gt;&lt;/a&gt;Requirements
&lt;/h2&gt;&lt;p&gt;The data you search must be sorted.&lt;/p&gt;
&lt;h2 id="explanation"&gt;&lt;a href="#explanation" class="header-anchor"&gt;&lt;/a&gt;Explanation
&lt;/h2&gt;&lt;p&gt;Binary search shrinks the range in which the answer can lie, again and again, until the optimum is found. In coursework and in contests you will often meet problems that combine binary thinking with some other algorithm.&lt;/p&gt;
&lt;h2 id="uses"&gt;&lt;a href="#uses" class="header-anchor"&gt;&lt;/a&gt;Uses
&lt;/h2&gt;&lt;h3 id="finding-a-value-in-a-sorted-array"&gt;&lt;a href="#finding-a-value-in-a-sorted-array" class="header-anchor"&gt;&lt;/a&gt;Finding a value in a sorted array
&lt;/h3&gt;&lt;p&gt;Binary search can answer, for a given array,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;whether a value exists&lt;/li&gt;
&lt;li&gt;where that value sits&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="solving-an-equation-within-a-tolerance"&gt;&lt;a href="#solving-an-equation-within-a-tolerance" class="header-anchor"&gt;&lt;/a&gt;Solving an equation within a tolerance
&lt;/h3&gt;&lt;p&gt;Halve the interval over and over until the solution is precise enough.&lt;/p&gt;
&lt;h3 id="maximising-the-minimum"&gt;&lt;a href="#maximising-the-minimum" class="header-anchor"&gt;&lt;/a&gt;Maximising the minimum
&lt;/h3&gt;&lt;h3 id="minimising-the-average"&gt;&lt;a href="#minimising-the-average" class="header-anchor"&gt;&lt;/a&gt;Minimising the average
&lt;/h3&gt;&lt;p&gt;Problems of this shape are easy to compare
because you can tell whether a candidate is above or below the real answer.
So even without knowing what the answer is,
you can shrink its range by comparison.&lt;/p&gt;
&lt;p&gt;Reference:&lt;br&gt;
&lt;em&gt;Programming Contest Challenge Book&lt;/em&gt;&lt;br&gt;
Baidu Baike&lt;/p&gt;</description></item><item><title>Euclid — A Discussion Sparked by GCD</title><link>https://boiltask.com/en/knowledge/extended-gcd/</link><pubDate>Thu, 21 Jul 2016 15:08:12 +0800</pubDate><guid>https://boiltask.com/en/knowledge/extended-gcd/</guid><description>&lt;h2 id="euclidean-algorithm"&gt;&lt;a href="#euclidean-algorithm" class="header-anchor"&gt;&lt;/a&gt;Euclidean algorithm
&lt;/h2&gt;&lt;h3 id="task"&gt;&lt;a href="#task" class="header-anchor"&gt;&lt;/a&gt;Task
&lt;/h3&gt;&lt;p&gt;Find the greatest common divisor gcd(a, b) of two integers a and b.&lt;/p&gt;
&lt;h3 id="explanation"&gt;&lt;a href="#explanation" class="header-anchor"&gt;&lt;/a&gt;Explanation
&lt;/h3&gt;&lt;p&gt;By Bézout&amp;rsquo;s identity [if a and b are integers, there exist integers x and y such that ax + by = gcd(a, b)], we have gcd(a, b) = gcd(b, a - b) with a ≥ b. Iterating like this until b = 0 yields the gcd of the original pair. Pure subtraction would be too slow, so observe that while a - b is still larger than b the same operation runs again — we can subtract b from a until a is smaller than b. Hence gcd(a, b) = gcd(b, a mod b), which finds the gcd in logarithmic time.&lt;/p&gt;
&lt;h3 id="prototype"&gt;&lt;a href="#prototype" class="header-anchor"&gt;&lt;/a&gt;Prototype
&lt;/h3&gt;&lt;p&gt;int gcd(int a, int b);&lt;/p&gt;
&lt;h3 id="complexity"&gt;&lt;a href="#complexity" class="header-anchor"&gt;&lt;/a&gt;Complexity
&lt;/h3&gt;&lt;p&gt;O(log N), where N is on the same order as a and b.&lt;/p&gt;
&lt;h3 id="input"&gt;&lt;a href="#input" class="header-anchor"&gt;&lt;/a&gt;Input
&lt;/h3&gt;&lt;p&gt;Two integers a and b.&lt;/p&gt;
&lt;h3 id="output"&gt;&lt;a href="#output" class="header-anchor"&gt;&lt;/a&gt;Output
&lt;/h3&gt;&lt;p&gt;The gcd of a and b.&lt;/p&gt;
&lt;h3 id="code"&gt;&lt;a href="#code" class="header-anchor"&gt;&lt;/a&gt;Code
&lt;/h3&gt;
&lt;div class="highlight-wrapper"&gt;
&lt;div class="highlight-header"&gt;
&lt;span class="highlight-language"&gt;cpp&lt;/span&gt;
&lt;/div&gt;
&lt;div class="highlight-body"&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;gcd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nl"&gt;a&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;gcd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="extended-euclidean-algorithm"&gt;&lt;a href="#extended-euclidean-algorithm" class="header-anchor"&gt;&lt;/a&gt;Extended Euclidean algorithm
&lt;/h2&gt;&lt;h3 id="task-1"&gt;&lt;a href="#task-1" class="header-anchor"&gt;&lt;/a&gt;Task
&lt;/h3&gt;&lt;p&gt;Find the gcd of A and B, and find X and Y such that AX + BY = GCD(A, B).&lt;/p&gt;
&lt;h3 id="explanation-1"&gt;&lt;a href="#explanation-1" class="header-anchor"&gt;&lt;/a&gt;Explanation
&lt;/h3&gt;&lt;p&gt;We need X and Y satisfying AX + BY = GCD(A, B).
When B = 0, X = 1 and Y = 0 do the job.
When B &amp;gt; 0, building on the Euclidean algorithm, we know:
GCD(A, B) = GCD(B, A mod B)
First recurse to obtain X&amp;rsquo; and Y&amp;rsquo; satisfying:
B X&amp;rsquo; + (A mod B) Y&amp;rsquo; = GCD(B, A mod B) = GCD(A, B)
Then expand and work backwards:
B X&amp;rsquo; + (A - A/B&lt;em&gt;B) Y&amp;rsquo; = GCD(A, B)
A Y&amp;rsquo; + B X&amp;rsquo; - (A/B)&lt;em&gt;B Y&amp;rsquo; = GCD(A, B)
Here the division is integer division. Collecting the terms that contain B:
A Y&amp;rsquo; + B (X&amp;rsquo; - A/B&lt;/em&gt;Y&amp;rsquo;) = GCD(A, B)
Therefore X = Y&amp;rsquo;, Y = X&amp;rsquo; - A/B&lt;/em&gt;Y'&lt;/p&gt;
&lt;h3 id="prototype-1"&gt;&lt;a href="#prototype-1" class="header-anchor"&gt;&lt;/a&gt;Prototype
&lt;/h3&gt;&lt;p&gt;int extend_gcd(int a, int b, int &amp;amp;x, int &amp;amp;y);&lt;/p&gt;
&lt;h3 id="complexity-1"&gt;&lt;a href="#complexity-1" class="header-anchor"&gt;&lt;/a&gt;Complexity
&lt;/h3&gt;&lt;p&gt;O(log N), where N is on the same order as a and b.&lt;/p&gt;
&lt;h3 id="input-1"&gt;&lt;a href="#input-1" class="header-anchor"&gt;&lt;/a&gt;Input
&lt;/h3&gt;&lt;p&gt;Two integers a and b, plus the references &amp;amp;x and &amp;amp;y for one solution of ax + by = GCD(a, b).&lt;/p&gt;
&lt;h3 id="output-1"&gt;&lt;a href="#output-1" class="header-anchor"&gt;&lt;/a&gt;Output
&lt;/h3&gt;&lt;p&gt;The gcd of a and b; after the call, x and y satisfy ax + by = GCD(a, b).&lt;/p&gt;
&lt;h3 id="code-1"&gt;&lt;a href="#code-1" class="header-anchor"&gt;&lt;/a&gt;Code
&lt;/h3&gt;
&lt;div class="highlight-wrapper"&gt;
&lt;div class="highlight-body"&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;int extend_gcd(int a,int b,int &amp;amp;x,int &amp;amp;y) {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if(b==0) {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; x=1;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; y=0;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return a;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; } else {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; int r=extend_gcd(b,a%b,y,x);
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; y-=x*(a/b);
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return r;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="references"&gt;&lt;a href="#references" class="header-anchor"&gt;&lt;/a&gt;References
&lt;/h2&gt;&lt;p&gt;ACM International Collegiate Programming Contest: Algorithms and Implementations
Baidu Baike&lt;/p&gt;</description></item><item><title>Bipartite Graph — Matching Items from One Set into Another</title><link>https://boiltask.com/en/knowledge/bipartite-graph/</link><pubDate>Fri, 01 Apr 2016 07:07:18 +0800</pubDate><guid>https://boiltask.com/en/knowledge/bipartite-graph/</guid><description>&lt;p&gt;Example problems:&lt;br&gt;
There are N computers and K tasks, and each computer can handle a different set of task types — how many tasks can be handled at most?&lt;br&gt;
On a roller coaster every girl is willing to sit next to only a few particular boys — how many pairs can be arranged at most?&lt;/p&gt;
&lt;p&gt;Hungarian algorithm&lt;/p&gt;
&lt;p&gt;References:&lt;br&gt;
&lt;em&gt;Programming Contest Challenge Book&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Competitive Programming&lt;/em&gt; (the classic introduction to contest algorithms)&lt;br&gt;
&lt;a class="link" href="https://baike.baidu.com/item/%E5%8C%88%E7%89%99%E5%88%A9%E7%AE%97%E6%B3%95/9089246" target="_blank" rel="noopener"&gt;Hungarian algorithm — Baidu Baike&lt;/a&gt;&lt;br&gt;
&lt;a class="link" href="http://blog.csdn.net/dark_scope/article/details/8880547" target="_blank" rel="noopener"&gt;A playful take on the Hungarian algorithm&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Shortest Path — Minimum Total Weight Between Two Vertices</title><link>https://boiltask.com/en/knowledge/shortest-path/</link><pubDate>Wed, 17 Feb 2016 14:49:25 +0800</pubDate><guid>https://boiltask.com/en/knowledge/shortest-path/</guid><description>&lt;h2 id="single-source-shortest-path"&gt;&lt;a href="#single-source-shortest-path" class="header-anchor"&gt;&lt;/a&gt;Single-source shortest path
&lt;/h2&gt;&lt;p&gt;Finding the shortest path from one vertex to every other vertex.&lt;/p&gt;
&lt;h3 id="shortest-path-with-bfs"&gt;&lt;a href="#shortest-path-with-bfs" class="header-anchor"&gt;&lt;/a&gt;Shortest path with BFS
&lt;/h3&gt;&lt;p&gt;&lt;em&gt;Competitive Programming&lt;/em&gt;&lt;br&gt;
&lt;a class="link" href="http://blog.csdn.net/zzyafyj/article/details/39323369#t2" target="_blank" rel="noopener"&gt;Chapter 6, the graph part — zzyafyj&amp;rsquo;s column&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="dijkstra"&gt;&lt;a href="#dijkstra" class="header-anchor"&gt;&lt;/a&gt;Dijkstra
&lt;/h3&gt;&lt;p&gt;&lt;a class="link" href="http://ahalei.blog.51cto.com/4767671/1387799" target="_blank" rel="noopener"&gt;Algorithms on the toilet, part 7: Dijkstra&amp;rsquo;s shortest path&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We find dis[3] = 12, while dis[2] + e[2][3] = 1 + 9 = 10. Since dis[3] &amp;gt; dis[2] + e[2][3], dis[3] must be updated to 10. This process has a name: &amp;ldquo;relaxation&amp;rdquo;. The distance from vertex 1 to vertex 3 — dis[3] — is successfully relaxed through the edge 2-&amp;gt;3. That is the main idea of Dijkstra: relax the distances from vertex 1 to all the other vertices through edges.&lt;br&gt;
Each time, take the vertex closest to the source (vertex 1 in the example), expand from it as the new centre, and you eventually get the shortest paths from the source to all remaining vertices.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="all-pairs-shortest-paths"&gt;&lt;a href="#all-pairs-shortest-paths" class="header-anchor"&gt;&lt;/a&gt;All-pairs shortest paths
&lt;/h2&gt;&lt;p&gt;Finding the shortest path between every pair of vertices.&lt;/p&gt;
&lt;h3 id="floydwarshall"&gt;&lt;a href="#floydwarshall" class="header-anchor"&gt;&lt;/a&gt;Floyd–Warshall
&lt;/h3&gt;&lt;p&gt;&lt;a class="link" href="http://ahalei.blog.51cto.com/4767671/1383613" target="_blank" rel="noopener"&gt;Algorithms on the toilet: Floyd&amp;rsquo;s shortest path in five lines&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;At first only vertex 1 may be used as an intermediate point, then only vertices 1 and 2, … and finally all vertices 1..n. Put in one sentence: the shortest path from vertex i to vertex j using only the first k vertices as intermediates.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Reference:&lt;br&gt;
&lt;em&gt;Programming Contest Challenge Book&lt;/em&gt;&lt;br&gt;
&lt;a class="link" href="http://developer.51cto.com/art/201403/433874.htm" target="_blank" rel="noopener"&gt;Algorithms on the toilet: Floyd&amp;rsquo;s shortest path in five lines&lt;/a&gt;&lt;br&gt;
&lt;a class="link" href="http://ahalei.blog.51cto.com/4767671/1387799" target="_blank" rel="noopener"&gt;Algorithms on the toilet, part 7: Dijkstra&amp;rsquo;s shortest path&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>