<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; 算法</title>
	<atom:link href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>zhiqiang&#039;s personal blog</description>
	<lastBuildDate>Thu, 02 Sep 2010 00:59:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>一个简单图的三染色算法问题</title>
		<link>http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html#comments</comments>
		<pubDate>Thu, 25 Sep 2008 06:57:19 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[染色]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=851</guid>
		<description><![CDATA[注: 这个问题来自China Theory Week 2008的Open Problems Session。
我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程中并没有说明如何找到它，这种证明方法被称为“非构造性证明”。有些流派的数学家对这种证明方法非常不满，具体这里就不详谈了。
这里要说的是，一个玩意儿，即时你知道它总是存在的，它也是可以算出来的，但是不是...]]></description>
			<content:encoded><![CDATA[<p><font color="#808080">注: 这个问题来自<a href="http://www.itcs.tsinghua.edu.cn/CTW2008/" target="_blank">China Theory Week 2008</a>的Open Problems Session。</font></p>
<p>我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程中并没有说明如何找到它，这种证明方法被称为“非构造性证明”。有些流派的数学家对这种证明方法非常不满，具体这里就不详谈了。</p>
<p>这里要说的是，一个玩意儿，即时你知道它总是存在的，它也是可以算出来的，但是不是就一定能够很快的比如在多项式时间之内算出来呢？</p>
<p><font color="#808080"><a href="http://zhiqiang.org/blog/posts/game-theory-computing-nash-equilibrium.html">Game Theory</a></font><font color="#808080">已经证明了在两人非合作博弈中，</font><font color="#808080"><a href="http://zhiqiang.org/blog/posts/from-the-nash-equilibrium-of-the-bystander-effect.html">纳什均衡</a></font><font color="#808080">总是存在的，但是</font><font color="#808080"><a href="http://zhiqiang.org/blog/posts/game-theory-computing-nash-equilibrium.html">如何计算它确被证明是PPAD-hard</a></font><font color="#808080">的，一个被猜测不属于P的复杂类。</font></p>
<p><font color="#808080">纳什均衡的计算问题不太好理解，下面介绍一个问题，描述比较简单非常容易理解。它有类似的效果，存在性可在数学上被证明，但如何计算它却不知道，复杂性也未知。</font></p>
<p>输入：一个<img src="http://zhiqiang.org/blog/wp-content/cache/tex_fa7674a88b1ff1139a00caf969933a8c.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="3n" />点的图，顶点构成一个正<img src="http://zhiqiang.org/blog/wp-content/cache/tex_fa7674a88b1ff1139a00caf969933a8c.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="3n" />边形，以及它们之间的<img src="http://zhiqiang.org/blog/wp-content/cache/tex_2e0a8f7435e9e48c3f86c8d72266a034.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="6n" />条边。其中<img src="http://zhiqiang.org/blog/wp-content/cache/tex_fa7674a88b1ff1139a00caf969933a8c.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="3n" />条为多边形的边，另外<img src="http://zhiqiang.org/blog/wp-content/cache/tex_fa7674a88b1ff1139a00caf969933a8c.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="3n" />条边构成<img src="http://zhiqiang.org/blog/wp-content/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="n" />个顶点互不重合的三角形。下图为一个<img src="http://zhiqiang.org/blog/wp-content/cache/tex_f4b339682e05755eb7408448ef87e1ca.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="n=3" />的例子。</p>
<p><img style="display: block; float: none; margin-left: auto; margin-right: auto" src="http://lh3.ggpht.com/mathzqy/SNmzOj9_LWI/AAAAAAAAFCw/BIh6tAzGgaw/3color.JPG?imgmax=640"/></p>
<p>输出：这个图的一种三染色方案（给定点染色，使任何有边相连的顶点都不同色）。</p>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a> <small>
下面是wikipedia上算法的定义：

算法是指完成一个任务所需要的具体步骤和方法。也就是说给定初始状态或输入数据，经过计算机程序的有限次运算...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">有序矩阵的中位数算法</a> <small>给定$$n\times n$$的实数矩阵，每行和每列都是递增的，求这$$n^2$$个数的中位数。 使用类似Tarjan的线性中位数的方法，每次找每列中位数，然后找中位数...</small></li><li><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a> <small>今天香港中文大学的Prof. Cai给我们上graph algorithm。第一节课上教我们玩魔方，先给每人发了一个。我喜欢这样的教学...</small></li><li><a href="http://zhiqiang.org/blog/science/color-points-on-plane.html">点染色问题</a> <small>继上次的硬币游戏（解答在此）之后，Sariel又出了一个有意思的题目。 给定平面上若干个点。证明总存在一个黑白染色方法，使得对于平面上任何一个...</small></li><li><a href="http://zhiqiang.org/blog/science/countable-coloring-of-real.html">实数上的可数颜色染色问题</a> <small>   命题：实数集$$\mathcal{R}$$上的任何一个可数种颜色染色方案，都存在四个不等的同色点$$x, y, z, w$$使得$$x+y=z+w$$。   这个问题是一个月前在Computational C...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">求平方根倒数的算法</a> <small>下面这个求$$1/\sqrt{x}$$的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。     float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&amp;x...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a> <small>更新：证明的关键一步发现错误，作者更新了论文，结论甚至论文标题都改了（废话），新版本On the graph isomorphism problem。  提交论文到arxiv不需要审阅...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/programs-depend-on-date-struct-not-algorithm.html">编程的核心是数据结构，而不是算法</a> <small>Rob Pike, 最伟大的 C 语言大师之一 , 在Notes on C Programming(英文原文)中从另一个稍微不同的角度表述了 Unix 的哲学:   你无法断定程序会在什么地方耗费运...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a> <small>Xie Xie给我看了一个链接性能调优--永远超乎想象，里面提到了素数筛法的复杂度，作者用实验发现此筛法是线形的。 所谓素数筛法就是那个求小于n的...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a> <small>珍爱生命，远离政治。我们继续讨论算法。 2008/04/01补充：此算法有重大缺陷。详情请见留言部分。 一年前，我们讨论过一个算法问题，perfect shuffle，...</small></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html#comments">5 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>求平方根倒数的算法</title>
		<link>http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html#comments</comments>
		<pubDate>Fri, 19 Sep 2008 04:10:28 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[算法分析]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/inverse-square-root-algorithm-analysis.html</guid>
		<description><![CDATA[下面这个求的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。
float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&#38;x;
    i = 0x5f3759df - (i&#62;&#62;1);
    y = *(float*)&#38;i;
    y = y*(1.5f - xhalf*y*y);
    return x;
}

我们这里分析一下它的原理（指程序的正确性，而不是解释为何快）。
分析程序之前，我们必须解释一下float数据在计算机里的表示方式。一般而言，一个flo...]]></description>
			<content:encoded><![CDATA[<p>下面这个求<img src="http://zhiqiang.org/blog/wp-content/cache/tex_c316ab9d453dd89c01a6fdb29cfb28de.png" style="vertical-align:middle; " class="tex" alt="1/\sqrt{x}" />的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。</p>
<blockquote><pre><span style="color: #0000ff">float</span> InvSqrt (<span style="color: #0000ff">float</span> x){
    <span style="color: #0000ff">float</span> xhalf = 0.5f*x;
    <span style="color: #0000ff">int</span> i = *(<span style="color: #0000ff">int</span>*)&amp;x;
    i = 0x5f3759df - (i&gt;&gt;1);
    y = *(<span style="color: #0000ff">float</span>*)&amp;i;
    y = y*(1.5f - xhalf*y*y);
    <span style="color: #0000ff">return</span> x;
}</pre>
</blockquote>
<p>我们这里分析一下它的原理（指程序的正确性，而不是解释为何快）。</p>
<p>分析程序之前，我们必须解释一下float数据在计算机里的表示方式。一般而言，一个float数据<img src="http://zhiqiang.org/blog/wp-content/cache/tex_9dd4e461268c8034f5c8564e155c67a6.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="x" />共32个bit，和int数据一样。其中前23位为有效数字<img src="http://zhiqiang.org/blog/wp-content/cache/tex_832cab6245118c67b73c5ef0be7cf7e8.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="M_x" />，后面接着一个8位数据<img src="http://zhiqiang.org/blog/wp-content/cache/tex_893be2279f4c4bc665184cf9f87da90c.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="E_x" />表示指数，最后一位表示符号，由于这里被开方的数总是大于0，所以我们暂不考虑最后一个符号位。此时</p>
<p>
<p style='text-align:center;'><img src="http://zhiqiang.org/blog/wp-content/cache/tex_1e4110c15e61979363c8a50c9be6667c.png" style="vertical-align:middle;" class="tex" alt="x=1.M_x 2^{E_x-127} " /></p>
</p>
<p>如果我们把计算机内的浮点数<img src="http://zhiqiang.org/blog/wp-content/cache/tex_9dd4e461268c8034f5c8564e155c67a6.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="x" />看做一个整数<img src="http://zhiqiang.org/blog/wp-content/cache/tex_e0c12d615090d3574f32ebeab63f5601.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="I_x" />，那么
<p style='text-align:center;'><img src="http://zhiqiang.org/blog/wp-content/cache/tex_104440c83c5fffbe38f981757bd33978.png" style="vertical-align:middle;" class="tex" alt="I_x = 2^{23}E_x+M_x " /></p>
</p>
<p>现在开始逐步分析函数。这个函数的主体有四个语句，分别的功能是：</p>
<blockquote>
<p><span style="color: #0000ff">int</span> i = *(<span style="color: #0000ff">int</span>*)&amp;x; 这条语句把<img src="http://zhiqiang.org/blog/wp-content/cache/tex_9dd4e461268c8034f5c8564e155c67a6.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="x" />转成<img src="http://zhiqiang.org/blog/wp-content/cache/tex_1efc275e97e02ac108c7836caad83cc0.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="i=I_x" />。</p>
<p>i = 0x5f3759df - (i&gt;&gt;1); 这条语句从<img src="http://zhiqiang.org/blog/wp-content/cache/tex_e0c12d615090d3574f32ebeab63f5601.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="I_x" />计算<img src="http://zhiqiang.org/blog/wp-content/cache/tex_ed49444b7e57c2fc4dfc8f056fae6bc4.png" style="vertical-align:middle; " class="tex" alt="I_{1/\sqrt{x}}" />。</p>
<p>y = *(<span style="color: #0000ff">float</span>*)&amp;i; 这条语句将<img src="http://zhiqiang.org/blog/wp-content/cache/tex_ed49444b7e57c2fc4dfc8f056fae6bc4.png" style="vertical-align:middle; " class="tex" alt="I_{1/\sqrt{x}}" />转换为<img src="http://zhiqiang.org/blog/wp-content/cache/tex_c316ab9d453dd89c01a6fdb29cfb28de.png" style="vertical-align:middle; " class="tex" alt="1/\sqrt{x}" />。</p>
<p>y = y*(1.5f - xhalf*y*y); 这时候的y是近似解；此步就是经典的牛顿迭代法。迭代次数越多越准确。</p>
</blockquote>
<p>关键是第二步 i = 0x5f3759df - (i&gt;&gt;1); 这条语句从<img src="http://zhiqiang.org/blog/wp-content/cache/tex_e0c12d615090d3574f32ebeab63f5601.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="I_x" />计算<img src="http://zhiqiang.org/blog/wp-content/cache/tex_ed49444b7e57c2fc4dfc8f056fae6bc4.png" style="vertical-align:middle; " class="tex" alt="I_{1/\sqrt{x}}" />，原理:</p>
<p>令<img src="http://zhiqiang.org/blog/wp-content/cache/tex_aa1b97bb7383597663a51e7ad5b0da35.png" style="vertical-align:middle; " class="tex" alt="y=1/\sqrt{x}" />，用<img src="http://zhiqiang.org/blog/wp-content/cache/tex_1759e9f3f5a3125e49343a92a2b7cf7c.png" style="vertical-align:middle; " class="tex" alt="x=(1+m_x)2^{e_x}" />和<img src="http://zhiqiang.org/blog/wp-content/cache/tex_8a525bf37b169a440892a25eb7403799.png" style="vertical-align:middle; " class="tex" alt="y=(1+m_y)2^{e_y}" />带入之后两边取对数，再利用近似表示<img src="http://zhiqiang.org/blog/wp-content/cache/tex_ea3d4f8b071da0aba0556f4b7de23443.png" style="vertical-align:middle; " class="tex" alt="\log_2(1+z)\sim z+\delta" />，算一算就得到</p>
<p>
<p style='text-align:center;'><img src="http://zhiqiang.org/blog/wp-content/cache/tex_97c1e31a970b3aae9c98ba334dd4c151.png" style="vertical-align:middle;" class="tex" alt="I_y = \frac{2}{3}(127-\delta)2^{23}-I_x/2 " /></p>
</p>
<p>若取<img src="http://zhiqiang.org/blog/wp-content/cache/tex_322863072b22b9062d0ad72cb98692f7.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="\delta=0.0450465679168701171875" />，<img src="http://zhiqiang.org/blog/wp-content/cache/tex_5c30a3072abd9f51847218d4ddede824.png" style="vertical-align:middle; " class="tex" alt="\frac{2}{3}(127-\delta)2^{23}" />就是程序里所用的常量0x5f3759df。至于为何选择这个<img src="http://zhiqiang.org/blog/wp-content/cache/tex_77a3b715842b45e440a5bee15357ad29.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="\delta" />，则应该是曲线拟合实验的结果。</p>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a> <small>Xie Xie推荐了一本今年出版的一本新书，Encyclopedia of Algorithms，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。 翻了一下，...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a> <small>一个面试题，号称是微软的  输入$$a_1, a_2, ..., a_n, b_1, b_2, ..., b_n$$，如何在O(n)的时间，用O(1)的空间，将这个序列顺序改为$$a_1, b_1, ..., a_n, b_n$$。 刚一...</small></li><li><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a> <small>今天香港中文大学的Prof. Cai给我们上graph algorithm。第一节课上教我们玩魔方，先给每人发了一个。我喜欢这样的教学...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-recursive-algorithm.html">递归算法的复杂度</a> <small>递归算法的复杂度通常很难衡量，一般都认为是每次递归分支数的递归深度次方。但通常情况下没有这个大，如果我们可以保存每次子递归的结果的话...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a> <small>更新：证明的关键一步发现错误，作者更新了论文，结论甚至论文标题都改了（废话），新版本On the graph isomorphism problem。  提交论文到arxiv不需要审阅...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/programs-depend-on-date-struct-not-algorithm.html">编程的核心是数据结构，而不是算法</a> <small>Rob Pike, 最伟大的 C 语言大师之一 , 在Notes on C Programming(英文原文)中从另一个稍微不同的角度表述了 Unix 的哲学:   你无法断定程序会在什么地方耗费运...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a> <small>Xie Xie给我看了一个链接性能调优--永远超乎想象，里面提到了素数筛法的复杂度，作者用实验发现此筛法是线形的。 所谓素数筛法就是那个求小于n的...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a> <small>珍爱生命，远离政治。我们继续讨论算法。 2008/04/01补充：此算法有重大缺陷。详情请见留言部分。 一年前，我们讨论过一个算法问题，perfect shuffle，...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">有序矩阵的中位数算法</a> <small>给定$$n\times n$$的实数矩阵，每行和每列都是递增的，求这$$n^2$$个数的中位数。 使用类似Tarjan的线性中位数的方法，每次找每列中位数，然后找中位数...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a> <small>
下面是wikipedia上算法的定义：

算法是指完成一个任务所需要的具体步骤和方法。也就是说给定初始状态或输入数据，经过计算机程序的有限次运算...</small></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html#comments">20 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>算法百科全书 - Encyclopedia of Algorithms</title>
		<link>http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html#comments</comments>
		<pubDate>Wed, 16 Jul 2008 04:50:20 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[Encyclopedia of Algorithms]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/download-encyclopedia-of-algorithm.html</guid>
		<description><![CDATA[Xie Xie推荐了一本今年出版的一本新书，Encyclopedia of Algorithms，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。
翻了一下，很多算法都不是直接给出的，只是给出一些原理描述和参考文献。看来这本也真只能当当索引用了。
没事做的话可以先欣赏一下第698页的O(n)的平面图判定算法。
按照推荐人xiexie的说法，看在原书高达309欧元的价格份上，也得下...]]></description>
			<content:encoded><![CDATA[<p>Xie Xie推荐了一本今年出版的一本新书，<em>Encyclopedia of Algorithms</em>，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。</p>
<p>翻了一下，很多算法都不是直接给出的，只是给出一些原理描述和参考文献。看来这本也真只能当当索引用了。</p>
<p>没事做的话可以先欣赏一下第698页的O(n)的平面图判定算法。</p>
<p>按照推荐人xiexie的说法，看在原书高达309欧元的价格份上，也得下载一本回来爽一爽。</p>
<p>听说个人blog上不要放版权材料，下载地址大家自己找吧。<a href="http://ifile.it/vucae2q/__3930210__via_gigapedia.info__.html" target="_blank"></a></p>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a> <small>一个面试题，号称是微软的  输入$$a_1, a_2, ..., a_n, b_1, b_2, ..., b_n$$，如何在O(n)的时间，用O(1)的空间，将这个序列顺序改为$$a_1, b_1, ..., a_n, b_n$$。 刚一...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a> <small>注: 这个问题来自China Theory Week 2008的Open Problems Session。 我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程...</small></li><li><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a> <small>今天香港中文大学的Prof. Cai给我们上graph algorithm。第一节课上教我们玩魔方，先给每人发了一个。我喜欢这样的教学...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a> <small>更新：证明的关键一步发现错误，作者更新了论文，结论甚至论文标题都改了（废话），新版本On the graph isomorphism problem。  提交论文到arxiv不需要审阅...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/programs-depend-on-date-struct-not-algorithm.html">编程的核心是数据结构，而不是算法</a> <small>Rob Pike, 最伟大的 C 语言大师之一 , 在Notes on C Programming(英文原文)中从另一个稍微不同的角度表述了 Unix 的哲学:   你无法断定程序会在什么地方耗费运...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a> <small>Xie Xie给我看了一个链接性能调优--永远超乎想象，里面提到了素数筛法的复杂度，作者用实验发现此筛法是线形的。 所谓素数筛法就是那个求小于n的...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a> <small>珍爱生命，远离政治。我们继续讨论算法。 2008/04/01补充：此算法有重大缺陷。详情请见留言部分。 一年前，我们讨论过一个算法问题，perfect shuffle，...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">有序矩阵的中位数算法</a> <small>给定$$n\times n$$的实数矩阵，每行和每列都是递增的，求这$$n^2$$个数的中位数。 使用类似Tarjan的线性中位数的方法，每次找每列中位数，然后找中位数...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a> <small>
下面是wikipedia上算法的定义：

算法是指完成一个任务所需要的具体步骤和方法。也就是说给定初始状态或输入数据，经过计算机程序的有限次运算...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">求平方根倒数的算法</a> <small>下面这个求$$1/\sqrt{x}$$的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。     float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&amp;x...</small></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html#comments">11 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>有序矩阵的中位数算法</title>
		<link>http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html#comments</comments>
		<pubDate>Mon, 09 Jun 2008 03:23:03 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[中位数]]></category>
		<category><![CDATA[矩阵]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/median-algorithm-of-ordered-matrix.html</guid>
		<description><![CDATA[给定的实数矩阵，每行和每列都是递增的，求这个数的中位数。
使用类似Tarjan的线性中位数的方法，每次找每列中位数，然后找中位数的中位数，之后可以删除前一半列的上半部分或者后一半列的下半部分，这样可以实现复杂性。
但是这个问题是有的算法的，在Top Language Google Group的Obtuse Sword的指点下，找到了这个问题的原始论文：Generalized Selection and Ranking: Sorted Matrices。事...]]></description>
			<content:encoded><![CDATA[<p>给定<img src="http://zhiqiang.org/blog/wp-content/cache/tex_607acaa73c762411b20745149a11e90b.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="n\times n" />的实数矩阵，每行和每列都是递增的，求这<img src="http://zhiqiang.org/blog/wp-content/cache/tex_6595d679e306a127a3fe53268bcaddb2.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="n^2" />个数的中位数。</p>
<p>使用类似Tarjan的线性中位数的方法，每次找每列中位数，然后找中位数的中位数，之后可以删除前一半列的上半部分或者后一半列的下半部分，这样可以实现复杂性<img src="http://zhiqiang.org/blog/wp-content/cache/tex_f7abf0190aa2a37a63aff83e6af0da07.png" style="vertical-align:middle; " class="tex" alt="O(n\log^2n)" />。</p>
<p>但是这个问题是有<img src="http://zhiqiang.org/blog/wp-content/cache/tex_7ba55e7c64a9405a0b39a1107e90ca94.png" style="vertical-align:middle; " class="tex" alt="O(n)" />的算法的，在<a href="http://groups.google.com/group/pongba" target="_blank">Top Language Google Group</a>的<a href="http://groups.google.com/group/pongba/msg/c2e0e047c127c2b7" target="_blank">Obtuse Sword的指点</a>下，找到了这个问题的原始论文：<a href="http://scitation.aip.org/getabs/servlet/GetabsServlet?prog=normal&amp;id=SMJCAT000013000001000014000001&amp;idtype=cvips&amp;gifs=yes" target="_blank">Generalized Selection and Ranking: Sorted Matrices</a>。事实上这篇论文证明了更强的结论：</p>
<blockquote><p>对于一个<img src="http://zhiqiang.org/blog/wp-content/cache/tex_252d3754c0db62a55b9e25c870a524a5.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="n\times m" />(<img src="http://zhiqiang.org/blog/wp-content/cache/tex_a9c6d1101125bbf3954a5821e556cced.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="n\leq m" />)的矩阵，若每行和每列都是递增的，则可以在<img src="http://zhiqiang.org/blog/wp-content/cache/tex_2a5a84e2d2b3faed163740163b33fb7b.png" style="vertical-align:middle; " class="tex" alt="O(n\log2m/n)" />找到第<img src="http://zhiqiang.org/blog/wp-content/cache/tex_8ce4b16b22b58894aa86c421e8759df3.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="k" />大的数。</p>
</blockquote>
<p>算法的基本思路是将矩阵依次对半划分成更小的子矩阵，然后删除不可能包含所求中位数的子矩阵。通过对每次划分后子矩阵个数的估计，发现此算法时间复杂度为<img src="http://zhiqiang.org/blog/wp-content/cache/tex_2a5a84e2d2b3faed163740163b33fb7b.png" style="vertical-align:middle; " class="tex" alt="O(n\log2m/n)" />。</p>
<p>使用同样的技巧，可以证明更更强的结论(这个算法具体过程就没细看了):</p>
<blockquote><p>一堆<img src="http://zhiqiang.org/blog/wp-content/cache/tex_677228ad87e3811d7e2772f3b3897674.png" style="vertical-align:middle; padding-bottom:2px;" class="tex" alt="n_i\times m_i" />(<img src="http://zhiqiang.org/blog/wp-content/cache/tex_29e47411160e0bc1091c57173a55b0d3.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="n_i\leq m_i" />)的矩阵，若每个矩阵的每行和每列都是递增的，则selection problem（即找第<img src="http://zhiqiang.org/blog/wp-content/cache/tex_8ce4b16b22b58894aa86c421e8759df3.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="k" />大的数）的时间复杂度为<img src="http://zhiqiang.org/blog/wp-content/cache/tex_12294fa07ca565682ae047f236a83874.png" style="vertical-align:middle; " class="tex" alt="O(\sum n_i\log2m_i/n_i)" />。</p>
</blockquote>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a> <small>一个面试题，号称是微软的  输入$$a_1, a_2, ..., a_n, b_1, b_2, ..., b_n$$，如何在O(n)的时间，用O(1)的空间，将这个序列顺序改为$$a_1, b_1, ..., a_n, b_n$$。 刚一...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a> <small>注: 这个问题来自China Theory Week 2008的Open Problems Session。 我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程...</small></li><li><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a> <small>今天香港中文大学的Prof. Cai给我们上graph algorithm。第一节课上教我们玩魔方，先给每人发了一个。我喜欢这样的教学...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a> <small>更新：证明的关键一步发现错误，作者更新了论文，结论甚至论文标题都改了（废话），新版本On the graph isomorphism problem。  提交论文到arxiv不需要审阅...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/programs-depend-on-date-struct-not-algorithm.html">编程的核心是数据结构，而不是算法</a> <small>Rob Pike, 最伟大的 C 语言大师之一 , 在Notes on C Programming(英文原文)中从另一个稍微不同的角度表述了 Unix 的哲学:   你无法断定程序会在什么地方耗费运...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a> <small>Xie Xie给我看了一个链接性能调优--永远超乎想象，里面提到了素数筛法的复杂度，作者用实验发现此筛法是线形的。 所谓素数筛法就是那个求小于n的...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a> <small>珍爱生命，远离政治。我们继续讨论算法。 2008/04/01补充：此算法有重大缺陷。详情请见留言部分。 一年前，我们讨论过一个算法问题，perfect shuffle，...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a> <small>Xie Xie推荐了一本今年出版的一本新书，Encyclopedia of Algorithms，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。 翻了一下，...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a> <small>
下面是wikipedia上算法的定义：

算法是指完成一个任务所需要的具体步骤和方法。也就是说给定初始状态或输入数据，经过计算机程序的有限次运算...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">求平方根倒数的算法</a> <small>下面这个求$$1/\sqrt{x}$$的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。     float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&amp;x...</small></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html#comments">4 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Perfect Shuffle的算法</title>
		<link>http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html#comments</comments>
		<pubDate>Tue, 01 Apr 2008 00:35:27 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[memo]]></category>
		<category><![CDATA[perfect shuffle]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[面试题]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/perfect-shuffle%e7%9a%84%e7%ae%97%e6%b3%95.html</guid>
		<description><![CDATA[珍爱生命，远离政治。我们继续讨论算法。
2008/04/01补充：此算法有重大缺陷。详情请见留言部分。
一年前，我们讨论过一个算法问题，perfect shuffle，据称是个微软面试题：
输入，如何用的时间，的空间，将这个序列顺序改为。

那一次讨论我们翻出了问题的来源，一篇长达12页的论文Computing the Cycles in the Perfect Shuffle Permutation，算法那是非常的复杂，我估计贴出来都没几个人...]]></description>
			<content:encoded><![CDATA[<p>珍爱生命，远离政治。我们继续讨论算法。</p>
<p><strong>2008/04/01补充：此算法有重大缺陷。详情请见留言部分。</strong></p>
<p>一年前，我们讨论过一个算法问题，perfect shuffle，据称是个微软面试题：</p>
<blockquote><p>输入<img src="http://zhiqiang.org/blog/wp-content/cache/tex_b50799dad81f21d20410095ecd84c892.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="a_1,a_2,\cdots,a_n,b_1,\cdots,b_n" />，如何用<img src="http://zhiqiang.org/blog/wp-content/cache/tex_7ba55e7c64a9405a0b39a1107e90ca94.png" style="vertical-align:middle; " class="tex" alt="O(n)" />的时间，<img src="http://zhiqiang.org/blog/wp-content/cache/tex_5e079a28737d5dd019a3b8f6133ee55e.png" style="vertical-align:middle; " class="tex" alt="O(1)" />的空间，将这个序列顺序改为<img src="http://zhiqiang.org/blog/wp-content/cache/tex_d8523d1d0596a2a4106b0177db60807a.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="a_1,b_1,\cdots,a_n,b_n" />。</p>
</blockquote>
<p><a href="http://zhiqiang.org/blog/posts/an-algorithm-face-interviews-question-test.html">那一次讨论</a>我们翻出了问题的来源，一篇长达12页的论文<a href="http://www.cs.uvic.ca/%7Ejellis/Publications/shuffle.ps">Computing the Cycles in the Perfect Shuffle Permutation</a>，算法那是非常的复杂，我估计贴出来都没几个人仔细看。</p>
<p>这一次，Xie Xie（没错，又是他 <img src='http://zhiqiang.org/blog/wp-includes/images/smilies/smile.gif' alt=':)' class='wp-smiley' /> ）翻出了Art of Computer Programming, Volume 3上的一个难度为40分的Merge Sort习题：</p>
<blockquote><p>已知数列<img src="http://zhiqiang.org/blog/wp-content/cache/tex_7ad9ebc8e2af92fd300bda645d389cde.png" style="vertical-align:middle; padding-bottom:1px;" class="tex" alt="x_1\leq x_2\leq \cdots x_M, x_{M+1}\leq x_{M+2}\leq\cdots\leq x_n" />，设计算法使得在线性时间，常数空间实现归并，也就是将原数组排序。</p>
</blockquote>
<p>而Perfect Shuffle问题是可以规约到这个Merge Sort问题的：</p>
<blockquote><pre>mergeSort(x[1...2n]); <span style="color: #008000">// if this function solve Merge Sort problem</span>
perfectShuffle(x[1...2n]): <span style="color: #008000">// then this solve Perfect Shuffle problem</span>
     m &lt;- <span style="color: #0000ff">max</span>(x[1...2n])+1;
     x[i] &lt;- x[i]+(2i-1)m,
     x[i+n] &lt;- x[i+n] + 2im
     mergeSort(x[1...2n])
     x[i] &lt;- x[i]%m</pre>
</blockquote>
<p>原来我还不相信这个问题会是一个面试题。现在我信了。因为那个归并排序的算法还是有些牛人会知道的。</p>
<p>很好很强大。</p>
<p>&nbsp;</p>
<p>另注：从2006年4月1号愚人节<a href="http://zhiqiang.org/blog/1.html">开始</a>，阅微堂成立两周年了。</p>
<p>过去一年，阅微堂共发表了201篇文章，其中包括Liu Tao先生的<a href="http://zhiqiang.org/blog/author/liutao">19篇文章</a>，sog white的<a href="http://zhiqiang.org/blog/author/sog">17篇文章</a>，以及一些授权转载的网友原创作品。共3389条留言。</p>
<p>感谢大家的关注和对阅微堂的支持。</p>
<div><h2>相关文章</h2><ul><li><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a> <small>一个面试题，号称是微软的  输入$$a_1, a_2, ..., a_n, b_1, b_2, ..., b_n$$，如何在O(n)的时间，用O(1)的空间，将这个序列顺序改为$$a_1, b_1, ..., a_n, b_n$$。 刚一...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a> <small>更新：证明的关键一步发现错误，作者更新了论文，结论甚至论文标题都改了（废话），新版本On the graph isomorphism problem。  提交论文到arxiv不需要审阅...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/163-written-test-problems.html">网易笔试题</a> <small>笔试多了，便会发现题目大同小异，很多笔试考的时候就是考经验和见世面。下面收集一些。 Fibonacci数列中，一个Fibonacci数如果与它之前的Fibonacci数均...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a> <small>Xie Xie推荐了一本今年出版的一本新书，Encyclopedia of Algorithms，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。 翻了一下，...</small></li><li><a href="http://zhiqiang.org/blog/science/game-two-hats.html">帽子游戏二</a> <small>这个题目听说是MSRA的面试题。

在这个游戏的开头，我们设想自己要参加一个电视游戏大奖赛。规则呢，是这样。我们有 n 个人，作为一个小组来参...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">求平方根倒数的算法</a> <small>下面这个求$$1/\sqrt{x}$$的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。     float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&amp;x...</small></li><li><a href="http://zhiqiang.org/blog/science/googles-crazy-face-questions.html">Google的疯狂面试题</a> <small>有一些是火星题，比如最后一个海盗分金，不太可能还用来作为面试题，估计是被拉过来凑数的。  原文（英文）直接到这里看吧，27floors给出了中文翻...</small></li><li><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a> <small>注: 这个问题来自China Theory Week 2008的Open Problems Session。 我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程...</small></li><li><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a> <small>今天香港中文大学的Prof. Cai给我们上graph algorithm。第一节课上教我们玩魔方，先给每人发了一个。我喜欢这样的教学...</small></li><li><a href="http://zhiqiang.org/blog/it/emc-2008-10-writing-test.html">2008年10月EMC笔试</a> <small>今天下午去EMC笔试了。笔试题目分四部分：求职意向3题，专业基础知识和智力题25道，编程题3道，英语作文。全英文题目和作答。虽然前面关于操作系...</small></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html#comments">19 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
