﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-Change Dir</title><link>http://www.blogjava.net/changedi/</link><description>先知cd——热爱生活是一切艺术的开始</description><language>zh-cn</language><lastBuildDate>Sat, 02 May 2026 10:21:54 GMT</lastBuildDate><pubDate>Sat, 02 May 2026 10:21:54 GMT</pubDate><ttl>60</ttl><item><title>未来的技术blog更新地址</title><link>http://www.blogjava.net/changedi/archive/2015/11/25/428360.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 25 Nov 2015 08:46:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/11/25/428360.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/428360.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/11/25/428360.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/428360.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/428360.html</trackback:ping><description><![CDATA[<a href="https://www.zybuluo.com/changedi/note/226966" target="_blank" style="color: #0088cc; text-decoration: none; font-family: 'Microsoft Yahei', 'Hiragino Sans GB', 'WenQuanYi Micro Hei', sans-serif; line-height: 20px; background-color: #f9f9f5;">https://www.zybuluo.com/changedi/note/226966<br /><br />未来就在上面的地址更新blog了，欢迎大家阅读指正共同进步。<br /></a><img src ="http://www.blogjava.net/changedi/aggbug/428360.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-11-25 16:46 <a href="http://www.blogjava.net/changedi/archive/2015/11/25/428360.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>docker初体验之docker-tomcat</title><link>http://www.blogjava.net/changedi/archive/2015/09/14/427310.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 14 Sep 2015 12:05:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/09/14/427310.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/427310.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/09/14/427310.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/427310.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/427310.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">docker</span><span style="font-family:宋体">已经是现在最热的容器技术，最近也去体验了一下，在</span><span style="font-family:Calibri">daocloud</span><span style="font-family: 宋体">注册了一个账号，并开始本机实战</span><span style="font-family:Calibri">docker</span><span style="font-family:宋体">。daocloud免费有两个容器可用，体验送T恤，邀请送书，这里我分享一个daocloud的邀请码</span><a href="https://account.daocloud.io/signup?invite_code=mxeq2jkmcur37vz6ven8">https://account.daocloud.io/signup?invite_code=mxeq2jkmcur37vz6ven8</a><span style="font-family: 宋体; font-size: 10.5pt;">，daocloud是非常棒的容器云平台，使用体验好，问题响应也及时，绑定微信还送一个额外容器。</span></p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">好吧，停止广告，daocloud再好，也不给我发钱，真是的。<br /><br /></span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这里就介绍一下我最简单的一个</span><span style="font-family:Calibri">docker-tomcat</span><span style="font-family:宋体">容器。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">首先在我的</span><span style="font-family:Calibri">linux</span><span style="font-family: 宋体">机器上安装</span><span style="font-family:Calibri">docker</span><span style="font-family:宋体">：</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">安装教程特别多，随便可以找到，我的本地环境是</span><span style="font-family:Calibri">64</span><span style="font-family:宋体">位的</span><span style="font-family:Calibri">centos7.0</span><span style="font-family:宋体">，所以安装就被简化为：</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">Sudo yum install docker</p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">这么一句话了。</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">docker</span><span style="font-family:宋体">的核心思想是</span><span style="font-family:Calibri">everything is container</span><span style="font-family:宋体">，</span><span style="font-family:Calibri">docker</span><span style="font-family:宋体">运行的就是一个个的容器，而容器是用户完全自定义的镜像，我个人认为最灵活的就是写一个</span><span style="font-family:Calibri">dockerfile</span><span style="font-family: 宋体">（我理解就算一个镜像了），然后把它</span><span style="font-family:Calibri">build</span><span style="font-family:宋体">为镜像并</span><span style="font-family:Calibri">run</span><span style="font-family:宋体">，一个可用的容器环境就完全起来了。</span></p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">步骤：</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:宋体">，创建一个项目，最好在</span><span style="font-family:Calibri">git</span><span style="font-family:宋体">上</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:宋体">，写</span><span style="font-family:Calibri">Dockerfile</span><span style="font-family: 宋体">文件，描述你的镜像</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:宋体">，</span><span style="font-family:Calibri">docker build</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">4</span><span style="font-family:宋体">，</span><span style="font-family:Calibri">docker run</span></p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">这样就好了。</p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">于是，以我个人的项目为例：</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:宋体">，创建项目：</span><span style="font-family:Calibri">https://github.com/changedi/mydocker</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:宋体">，编辑</span><span style="font-family:Calibri">Dockerfile</span><span style="font-family: 宋体">：</span><span style="font-family:Calibri">https://github.com/changedi/mydocker/blob/master/Dockerfile</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:宋体">，在本地项目中运行</span><span style="font-family:Calibri">docker build &lt;image_name&gt; .</span><span style="font-family:宋体">，</span><span style="font-family:Calibri">build</span><span style="font-family:宋体">一个自定义名字的镜像</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">4</span><span style="font-family:宋体">，启动这个镜像容器：</span><span style="font-family:Calibri">docker run -d -p 80:80 &lt;image_name&gt;</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这样，这个镜像就好了，服务也起来了，后续可以通过</span><span style="font-family:Calibri">docker ps</span><span style="font-family: 宋体">来查看启动的镜像，通过</span><span style="font-family:Calibri">docker stop</span><span style="font-family:宋体">来停止容器。</span></p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">我这里解释一下我的</span><span style="font-family:Calibri">Dockerfile</span><span style="font-family: 宋体">：</span></p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;<span style="font-size: 13px; font-family: verdana, 'courier new'; color: #008080;">&nbsp;1</span><span style="font-size: 13px; font-family: verdana, 'courier new'; background-color: #eeeeee;">&nbsp;</span><span style="font-size: 13px; font-family: verdana, 'courier new'; background-color: #eeeeee;">#&nbsp;Using&nbsp;a&nbsp;compact&nbsp;OS</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><span style="color: #008080; ">&nbsp;2</span>&nbsp;FROM&nbsp;ubuntu:13.10&nbsp;<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;MAINTAINER&nbsp;changedi&nbsp;&lt;jysoftware@gmail.com&gt;&nbsp;<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;<br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;#&nbsp;update&nbsp;source&nbsp;&nbsp;<br /><span style="color: #008080; ">&nbsp;7</span>&nbsp;RUN&nbsp;echo&nbsp;"deb&nbsp;http://archive.ubuntu.com/ubuntu&nbsp;precise&nbsp;main&nbsp;universe"&gt;&nbsp;/etc/apt/sources.list&nbsp;&nbsp;<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;RUN&nbsp;apt-<span style="color: #0000FF; ">get</span>&nbsp;update&nbsp;&nbsp;<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">10</span>&nbsp;#&nbsp;Install&nbsp;curl&nbsp;&nbsp;<br /><span style="color: #008080; ">11</span>&nbsp;RUN&nbsp;apt-<span style="color: #0000FF; ">get</span>&nbsp;-y&nbsp;install&nbsp;curl&nbsp;&nbsp;<br /><span style="color: #008080; ">12</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">13</span>&nbsp;#&nbsp;Install&nbsp;JDK&nbsp;7&nbsp;&nbsp;<br /><span style="color: #008080; ">14</span>&nbsp;RUN&nbsp;cd&nbsp;/tmp&nbsp;&amp;&amp;&nbsp;&nbsp;curl&nbsp;-L&nbsp;<span style="color: #008000; ">'</span><span style="color: #008000; ">http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz'&nbsp;-H&nbsp;'Cookie:&nbsp;oraclelicense=accept-securebackup-cookie;&nbsp;gpw_e24=Dockerfile'&nbsp;|&nbsp;tar&nbsp;-xz&nbsp;&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #008000; "></span>RUN&nbsp;mkdir&nbsp;-p&nbsp;/usr/lib/jvm&nbsp;&nbsp;<br /><span style="color: #008080; ">16</span>&nbsp;RUN&nbsp;mv&nbsp;/tmp/jdk1.7.0_65/&nbsp;/usr/lib/jvm/java-7-oracle/&nbsp;&nbsp;<br /><span style="color: #008080; ">17</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">18</span>&nbsp;#&nbsp;<span style="color: #0000FF; ">Set</span>&nbsp;Oracle&nbsp;JDK&nbsp;7&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;default&nbsp;Java&nbsp;&nbsp;<br /><span style="color: #008080; ">19</span>&nbsp;RUN&nbsp;update-alternatives&nbsp;--install&nbsp;/usr/bin/java&nbsp;java&nbsp;/usr/lib/jvm/java-7-oracle/bin/java&nbsp;300&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">20</span>&nbsp;RUN&nbsp;update-alternatives&nbsp;--install&nbsp;/usr/bin/javac&nbsp;javac&nbsp;/usr/lib/jvm/java-7-oracle/bin/javac&nbsp;300&nbsp;&nbsp;<br /><span style="color: #008080; ">21</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">22</span>&nbsp;ENV&nbsp;JAVA_HOME&nbsp;/usr/lib/jvm/java-7-oracle/&nbsp;&nbsp;<br /><span style="color: #008080; ">23</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">24</span>&nbsp;#&nbsp;Install&nbsp;tomcat7&nbsp;&nbsp;<br /><span style="color: #008080; ">25</span>&nbsp;RUN&nbsp;cd&nbsp;/tmp&nbsp;&amp;&amp;&nbsp;curl&nbsp;-L&nbsp;<span style="color: #008000; ">'</span><span style="color: #008000; ">http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz'&nbsp;|&nbsp;tar&nbsp;-xz&nbsp;&nbsp;</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">26</span>&nbsp;<span style="color: #008000; "></span>RUN&nbsp;mv&nbsp;/tmp/apache-tomcat-7.0.8/&nbsp;/opt/tomcat7/&nbsp;&nbsp;<br /><span style="color: #008080; ">27</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">28</span>&nbsp;ENV&nbsp;CATALINA_HOME&nbsp;/opt/tomcat7&nbsp;&nbsp;<br /><span style="color: #008080; ">29</span>&nbsp;ENV&nbsp;PATH&nbsp;$PATH:$CATALINA_HOME/bin:$JAVA_HOME/bin&nbsp;&nbsp;<br /><span style="color: #008080; ">30</span>&nbsp;<br /><span style="color: #008080; ">31</span>&nbsp;#&nbsp;ADD&nbsp;ace-java-demo-1.0.0.war&nbsp;/tmp/myapp/<br /><span style="color: #008080; ">32</span>&nbsp;#&nbsp;RUN&nbsp;cd&nbsp;/tmp/myapp&nbsp;&amp;&amp;&nbsp;jar&nbsp;-xvf&nbsp;ace-java-demo-1.0.0.war<br /><span style="color: #008080; ">33</span>&nbsp;#&nbsp;RUN&nbsp;rm&nbsp;-rf&nbsp;ace-java-demo-1.0.0.war<br /><span style="color: #008080; ">34</span>&nbsp;#&nbsp;RUN&nbsp;cp&nbsp;-R&nbsp;/tmp/myapp&nbsp;/opt/tomcat7/webapps/myapp<br /><span style="color: #008080; ">35</span>&nbsp;#&nbsp;RUN&nbsp;mkdir&nbsp;/opt/tomcat7/webapps/myapp<br /><span style="color: #008080; ">36</span>&nbsp;RUN&nbsp;rm&nbsp;-rf&nbsp;/opt/tomcat7/webapps/ROOT/*<br /><span style="color: #008080; ">37</span>&nbsp;ADD&nbsp;ace-java-demo-1.0.0.war&nbsp;/opt/tomcat7/webapps/ROOT/ace-java-demo-1.0.0.war<br /><span style="color: #008080; ">38</span>&nbsp;RUN&nbsp;cd&nbsp;/opt/tomcat7/webapps/ROOT/&nbsp;&amp;&amp;&nbsp;jar&nbsp;-xvf&nbsp;ace-java-demo-1.0.0.war<br /><span style="color: #008080; ">39</span>&nbsp;&nbsp;<br /><span style="color: #008080; ">40</span>&nbsp;ADD&nbsp;tomcat-users.xml&nbsp;/opt/tomcat7/conf/tomcat-users.xml<br /><span style="color: #008080; ">41</span>&nbsp;#&nbsp;ADD&nbsp;server.xml&nbsp;/opt/tomcat7/conf/server.xml<br /><span style="color: #008080; ">42</span>&nbsp;RUN&nbsp;cat&nbsp;/opt/tomcat7/conf/serv*.xml<br /><span style="color: #008080; ">43</span>&nbsp;RUN&nbsp;ls&nbsp;/opt/tomcat7/conf<br /><span style="color: #008080; ">44</span>&nbsp;RUN&nbsp;ls&nbsp;/opt/tomcat7/webapps<br /><span style="color: #008080; ">45</span>&nbsp;<br /><span style="color: #008080; ">46</span>&nbsp;ADD&nbsp;tomcat7.sh&nbsp;/etc/init.d/tomcat7&nbsp;&nbsp;<br /><span style="color: #008080; ">47</span>&nbsp;RUN&nbsp;chmod&nbsp;755&nbsp;/etc/init.d/tomcat7&nbsp;&nbsp;<br /><span style="color: #008080; ">48</span>&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080; ">49</span>&nbsp;#&nbsp;install&nbsp;nginx<br /><span style="color: #008080; ">50</span>&nbsp;RUN&nbsp;apt-<span style="color: #0000FF; ">get</span>&nbsp;install&nbsp;nginx&nbsp;-y<br /><span style="color: #008080; ">51</span>&nbsp;ADD&nbsp;./nginx/nginx-conf&nbsp;/etc/nginx/conf.d<br /><span style="color: #008080; ">52</span>&nbsp;<br /><span style="color: #008080; ">53</span>&nbsp;#&nbsp;Expose&nbsp;ports<br /><span style="color: #008080; ">54</span>&nbsp;EXPOSE&nbsp;80<br /><span style="color: #008080; ">55</span>&nbsp;<br /><span style="color: #008080; ">56</span>&nbsp;<br /><span style="color: #008080; ">57</span>&nbsp;#&nbsp;Define&nbsp;default&nbsp;command.&nbsp;&nbsp;<br /><span style="color: #008080; ">58</span>&nbsp;ENTRYPOINT&nbsp;service&nbsp;tomcat7&nbsp;start&nbsp;&amp;&amp;&nbsp;tail&nbsp;-f&nbsp;/opt/tomcat7/logs/catalina.out</div>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">主要做几件事：</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:宋体">，拉个操作系统：这里是</span><span style="font-family:Calibri">Ubuntu</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:宋体">，装工具：</span><span style="font-family:Calibri">curl</span><span style="font-family: 宋体">、</span><span style="font-family:Calibri">jdk</span><span style="font-family:宋体">、</span><span style="font-family:Calibri">tomcat</span><span style="font-family:宋体">、</span><span style="font-family:Calibri">nginx</span><span style="font-family: 宋体">，配置环境变量，配置</span><span style="font-family:Calibri">tomcat</span><span style="font-family:宋体">，配置</span><span style="font-family:Calibri">nginx</span><span style="font-family:宋体">（</span><span style="font-family:Calibri">tomcat</span><span style="font-family: 宋体">和</span><span style="font-family:Calibri">nginx</span><span style="font-family:宋体">的配置都是本地搞好，直接</span><span style="font-family: Calibri">add</span><span style="font-family:宋体">）</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:宋体">，部署应用（自己的</span><span style="font-family:Calibri">war</span><span style="font-family:宋体">包），这里我直接利用</span><span style="font-family:Calibri">docker</span><span style="font-family:宋体">的</span><span style="font-family:Calibri">ADD</span><span style="font-family:宋体">操作，把</span><span style="font-family:Calibri">git</span><span style="font-family:宋体">项目中的文件直接</span><span style="font-family:Calibri">add</span><span style="font-family:宋体">到容器中</span></p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:宋体;font-size:10.5pt">参考资料：</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:宋体">，</span><span style="font-family:Calibri">docker</span><span style="font-family: 宋体">安装</span> <a href="http://www.linuxidc.com/Linux/2014-12/110034.htm"><span style="font-family: Calibri">http://www.linuxidc.com/Linux/2014-12/110034.htm</span></a></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:宋体">，</span><span style="font-family:Calibri">dockerfile</span><span style="font-family: 宋体">编写</span> <a href="http://blog.csdn.net/we_shell/article/details/38445979"><span style="font-family:Calibri">http://blog.csdn.net/we_shell/article/details/38445979</span></a></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/427310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-09-14 20:05 <a href="http://www.blogjava.net/changedi/archive/2015/09/14/427310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用MAT对java内存分析</title><link>http://www.blogjava.net/changedi/archive/2015/08/17/426849.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 17 Aug 2015 11:08:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/08/17/426849.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/426849.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/08/17/426849.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/426849.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/426849.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">这是一篇阅读MAT helper的笔记。<br /><br />Heap dump</span><span style="font-family:SimSun">是</span><span style="font-family:Calibri">java</span><span style="font-family: SimSun">进程在特定时间的一个内存快照。通常在触发</span><span style="font-family:Calibri">heap dump</span><span style="font-family:SimSun">之前会进行一次</span><span style="font-family:Calibri">full gc</span><span style="font-family: SimSun">，这样</span><span style="font-family:Calibri">dump</span><span style="font-family:SimSun">出来的内容就包含的是被</span><span style="font-family: Calibri">gc</span><span style="font-family:SimSun">后的对象。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">dump</span><span style="font-family:SimSun">文件包含的内容：</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，全部的对象：类，域，原生值和引用；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，全部的类：</span><span style="font-family:Calibri">classloader</span><span style="font-family:SimSun">，类名，超类，静态域；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">GC root</span><span style="font-family: SimSun">：被</span><span style="font-family:Calibri">JVM</span><span style="font-family:SimSun">定义的可触达的对象；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">4</span><span style="font-family:SimSun">，线程栈和本地变量：线程的</span><span style="font-family:Calibri">call stack</span><span style="font-family: SimSun">，本地对象每帧的信息。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">dump</span><span style="font-family:SimSun">文件不包含内存的分配信息，因此无法查询谁创建了哪个对象这样的信息。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">Shallow heap</span><span style="font-family:SimSun">是一个对象占用的内存空间，一个对象需要</span><span style="font-family:Calibri">32</span><span style="font-family:SimSun">或者</span><span style="font-family:Calibri">64bits</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">Retained set of X</span><span style="font-family:SimSun">是</span><span style="font-family:Calibri">X</span><span style="font-family:SimSun">在被</span><span style="font-family:Calibri">jvm gc</span><span style="font-family:SimSun">回收后被</span><span style="font-family:Calibri">remove</span><span style="font-family:SimSun">的一组</span><span style="font-family:Calibri">object</span><span style="font-family: SimSun">。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">Retained heap of X</span><span style="font-family:SimSun">是在</span><span style="font-family:Calibri">retained set of X</span><span style="font-family:SimSun">中的所有对象的</span><span style="font-family:Calibri">shallow heap size</span><span style="font-family:SimSun">的和。换句话说就是保持</span><span style="font-family: Calibri">X</span><span style="font-family:SimSun">活着需要的内存空间。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">通俗的讲，</span><span style="font-family:Calibri">shallow heap</span><span style="font-family:SimSun">是一个对象在内存中的实际空间，而</span><span style="font-family:Calibri">retained heap</span><span style="font-family:SimSun">是一个对象被</span><span style="font-family: Calibri">gc</span><span style="font-family:SimSun">回收后内存释放出来的空间。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in"><img src="http://www.blogjava.net/images/blogjava_net/changedi/未命名图片.png" width="487" height="271" alt="" /><br /></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这张图可以看懂什么是</span><span style="font-family:Calibri">leading set</span><span style="font-family:SimSun">什么是</span><span style="font-family:Calibri">retained set</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">Dominator tree</span><span style="font-family:SimSun">：定义一个对象</span><span style="font-family:Calibri">x dominate </span><span style="font-family:SimSun">对象</span><span style="font-family:Calibri">y</span><span style="font-family:SimSun">，当每一条从</span><span style="font-family:Calibri">root</span><span style="font-family: SimSun">开始到</span><span style="font-family:Calibri">y</span><span style="font-family:SimSun">的路径都经过</span><span style="font-family: Calibri">x</span><span style="font-family:SimSun">。说白了就是只要有</span><span style="font-family:Calibri">y</span><span style="font-family:SimSun">对象的存活，那么一定会有一个</span><span style="font-family:Calibri">x</span><span style="font-family:SimSun">对象。</span><span style="font-family:Calibri">Dominator tree</span><span style="font-family:SimSun">就是将对象引用图转换成的树形结构。帮助发现在对象间保持</span><span style="font-family:Calibri">alive</span><span style="font-family: SimSun">的依赖，同时也能识别出</span><span style="font-family:Calibri">retained</span><span style="font-family:SimSun">内存的最大的</span><span style="font-family:Calibri">chunk</span><span style="font-family: SimSun">。</span><span style="font-family:Calibri">Immediate dominator x of y</span><span style="font-family:SimSun">是离</span><span style="font-family:Calibri">y</span><span style="font-family:SimSun">最近的</span><span style="font-family:Calibri">dominator</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">Dominator tree</span><span style="font-family:SimSun">有几个属性：</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，对象</span><span style="font-family:Calibri">x</span><span style="font-family:SimSun">的子树包含的对象（</span><span style="font-family:Calibri">x dominate</span><span style="font-family:SimSun">的对象集），代表了</span><span style="font-family:Calibri">x</span><span style="font-family:SimSun">的</span><span style="font-family:Calibri">retained set</span><span style="font-family:SimSun">；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，如果</span><span style="font-family:Calibri">x</span><span style="font-family:SimSun">是</span><span style="font-family:Calibri">y</span><span style="font-family:SimSun">的</span><span style="font-family:Calibri">immediate dominator</span><span style="font-family:SimSun">，那么</span><span style="font-family:Calibri">x</span><span style="font-family:SimSun">的</span><span style="font-family:Calibri">immediate dominator</span><span style="font-family:SimSun">同样</span><span style="font-family:Calibri">dominate y</span><span style="font-family:SimSun">，以此类推；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">dominate tree</span><span style="font-family:SimSun">中的边不代表对象引用图里对应的边，并非严格的直接的对象引用。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in"><img src="http://www.blogjava.net/images/blogjava_net/changedi/未命名图片2.png" width="639" height="307" alt="" /><br /></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这张图反应了一个对象引用图转换成</span><span style="font-family:Calibri">dominator tree</span><span style="font-family:SimSun">的示例。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">Gc root</span><span style="font-family:SimSun">：一个</span><span style="font-family:Calibri">gc</span><span style="font-family:SimSun">根就是一个对象，这个对象从堆外可以访问读取。以下一些方法可以使一个对象成为</span><span style="font-family: Calibri">gc</span><span style="font-family:SimSun">根。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">System class</span><span style="font-family:SimSun">：被</span><span style="font-family:Calibri">bootstrap</span><span style="font-family:SimSun">或者</span><span style="font-family:Calibri">system</span><span style="font-family: SimSun">类加载器加载的类，比如</span><span style="font-family:Calibri">rt.jar</span><span style="font-family:SimSun">里的</span><span style="font-family:Calibri">java.util.*</span><span style="font-family:SimSun">；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">JNI local</span><span style="font-family: SimSun">：</span><span style="font-family:Calibri">native</span><span style="font-family:SimSun">代码里的</span><span style="font-family:Calibri">local</span><span style="font-family:SimSun">变量，比如用户定义的</span><span style="font-family:Calibri">JNI</span><span style="font-family:SimSun">代码和</span><span style="font-family:Calibri">JVM</span><span style="font-family:SimSun">的内部代码；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">JNI global</span><span style="font-family: SimSun">：</span><span style="font-family:Calibri">native</span><span style="font-family:SimSun">代码里的</span><span style="font-family:Calibri">global</span><span style="font-family:SimSun">变量；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">4</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">Thread block</span><span style="font-family:SimSun">：当前活跃的线程</span><span style="font-family: Calibri">block</span><span style="font-family:SimSun">中引用的对象；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">5</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">Thread</span><span style="font-family: SimSun">：已经启动并且没有</span><span style="font-family:Calibri">stop</span><span style="font-family:SimSun">的线程；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">6</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">busy monitor</span><span style="font-family:SimSun">：被调用了</span><span style="font-family: Calibri">wait()</span><span style="font-family:SimSun">或者</span><span style="font-family:Calibri">notify()</span><span style="font-family: SimSun">或者被</span><span style="font-family:Calibri">synchronized</span><span style="font-family:SimSun">同步的对象，如果是</span><span style="font-family: Calibri">synchronized</span><span style="font-family:SimSun">方法，那么静态方法指的类，非静态方法指的是对象；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">7</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">java local</span><span style="font-family: SimSun">：</span><span style="font-family:Calibri">local</span><span style="font-family:SimSun">变量，比如方法的入参和方法内创建的变量；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">8</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">native stack</span><span style="font-family:SimSun">：</span><span style="font-family:Calibri">native</span><span style="font-family:SimSun">代码里的出入参数，比如</span><span style="font-family:Calibri">file/net/IO</span><span style="font-family:SimSun">方法以及反射的参数；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">9</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">finalizable</span><span style="font-family:SimSun">：在一个队列里等待它的</span><span style="font-family: Calibri">finalizer </span><span style="font-family:SimSun">运行的对象；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">10</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">unfinalized</span><span style="font-family:SimSun">：一个有</span><span style="font-family:Calibri">finalize</span><span style="font-family:SimSun">方法的对象，还没有被</span><span style="font-family:Calibri">finalize</span><span style="font-family: SimSun">，同时也没有进入</span><span style="font-family:Calibri">finalizer</span><span style="font-family:SimSun">队列等待</span><span style="font-family:Calibri">finalize</span><span style="font-family:SimSun">；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">11</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">unreachable</span><span style="font-family:SimSun">：不会被触碰到的对象，在</span><span style="font-family:Calibri">MAT</span><span style="font-family:SimSun">里被标记为</span><span style="font-family:Calibri">root</span><span style="font-family:SimSun">用来</span><span style="font-family:Calibri">retain object</span><span style="font-family:SimSun">，否则是不会在分析中出现的；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">12</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">java stack frame</span><span style="font-family:SimSun">：</span><span style="font-family:Calibri">java</span><span style="font-family:SimSun">栈帧包含了本地变量，当</span><span style="font-family:Calibri">dump</span><span style="font-family: SimSun">被解析时且在</span><span style="font-family:Calibri">preferences</span><span style="font-family:SimSun">里设置过把栈帧当做对象，这时才会产生；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">13</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">unknown</span><span style="font-family: SimSun">：位置的</span><span style="font-family:Calibri">root</span><span style="font-family:SimSun">类型。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">接下来是一些获取</span><span style="font-family:Calibri">dump</span><span style="font-family:SimSun">的方法：</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，在</span><span style="font-family:Calibri">oom</span><span style="font-family:SimSun">时</span><span style="font-family:Calibri">dump</span><span style="font-family:SimSun">：</span><span style="font-family:Calibri">JVM</span><span style="font-family:SimSun">参数：</span><span style="font-family:Calibri">-XX:+HeapDumpOnOutOfMemoryError</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，交互式环境下</span><span style="font-family:Calibri">dump</span><span style="font-family: SimSun">：</span></p>  <p style="margin:0in;margin-left:.375in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">）</span><span style="font-family:Calibri">JVM</span><span style="font-family:SimSun">参数：</span><span style="font-family:Calibri">-XX:+HeapDumpOnCtrlBreak</span></p>  <p style="margin:0in;margin-left:.375in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">）用外部</span><span style="font-family:Calibri">tools</span><span style="font-family:SimSun">：</span><span style="font-family:Calibri">jmap -dump:format=b,file=&lt;filename.hprof&gt; &lt;pid&gt;</span></p>  <p style="margin:0in;margin-left:.375in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:SimSun">）用外部</span><span style="font-family:Calibri">tools</span><span style="font-family:SimSun">：</span><span style="font-family:Calibri">jconsole</span></p>  <p style="margin:0in;margin-left:.375in;font-size:10.5pt"><span style="font-family:Calibri">4</span><span style="font-family:SimSun">）用外部工具：</span><span style="font-family:Calibri">MAT</span></p>  <p style="margin:0in;margin-left:.375in;font-size:10.5pt"><span style="font-family:Calibri">5</span><span style="font-family:SimSun">）</span><span style="font-family:Calibri">kill -3 &lt;pid&gt;</span></p>  <p style="margin:0in;margin-left:.375in;font-size:10.5pt"><span style="font-family:Calibri">6</span><span style="font-family:SimSun">）</span><span style="font-family:Calibri">jstack -l &lt;pid&gt; &gt; &lt;dumpfile&gt;</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">一些排查方法：</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，通过</span><span style="font-family:Calibri">top consumers</span><span style="font-family:SimSun">查找大对象，可以按照</span><span style="font-family: Calibri">class</span><span style="font-family:SimSun">、</span><span style="font-family:Calibri">classloader</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">package</span><span style="font-family:SimSun">进行</span><span style="font-family:Calibri">group by</span><span style="font-family: SimSun">；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，通过</span><span style="font-family:Calibri">immediate dominator</span><span style="font-family:SimSun">找到责任对象，对于快速定位一组对象的持有者非常有用，这个操作直接解决了&#8220;谁让这些对象</span><span style="font-family:Calibri">alive</span><span style="font-family: SimSun">&#8221;的问题，而不是&#8220;谁有这些对象的引用&#8221;的问题，更直接高效；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:SimSun">，运行</span><span style="font-family:Calibri">classloader</span><span style="font-family:SimSun">分析，这个重要性体现在亮点：第一，应用使用不同的</span><span style="font-family:Calibri">classloader</span><span style="font-family:SimSun">加载类，第二，不同</span><span style="font-family: Calibri">classloader</span><span style="font-family:SimSun">加载的类存储在不同的永久代，这理论上也是可以被回收的。当有一个类被不同的</span><span style="font-family: Calibri">classloader</span><span style="font-family:SimSun">加载时，这时要根据各自</span><span style="font-family:Calibri">loader</span><span style="font-family:SimSun">下的</span><span style="font-family:Calibri">instance</span><span style="font-family:SimSun">数量判断哪个</span><span style="font-family:Calibri">loader</span><span style="font-family: SimSun">更重要，从而要把另一个回收掉；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">4</span><span style="font-family:SimSun">，分析线程，本身</span><span style="font-family:Calibri">heap dump</span><span style="font-family: SimSun">里包含了</span><span style="font-family:Calibri">thread</span><span style="font-family:SimSun">信息，可以通过</span><span style="font-family: Calibri">MAT</span><span style="font-family:SimSun">来查看</span><span style="font-family:Calibri">threads </span><span style="font-family: SimSun">的</span><span style="font-family:Calibri">overview</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">detail</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">detail</span><span style="font-family: SimSun">中有线程的堆内存信息，也有线程栈，同时还包含了操作系统本地栈。假设不做</span><span style="font-family:Calibri">heap dump</span><span style="font-family: SimSun">，我们检查到系统有问题，如何通过线程的角度来排查呢？首先</span><span style="font-family: Calibri">top -H -p &lt;pid&gt;</span><span style="font-family:SimSun">以线程的模式查看</span><span style="font-family:Calibri">java</span><span style="font-family:SimSun">应用的运行情况，找到占用</span><span style="font-family:Calibri">cpu</span><span style="font-family:SimSun">或者内存大的线程，记录线程</span><span style="font-family:Calibri">id</span><span style="font-family:SimSun">，然后</span><span style="font-family:Calibri">printf %x &lt;tid&gt;</span><span style="font-family:SimSun">转为</span><span style="font-family:Calibri">16</span><span style="font-family:SimSun">进制，再</span><span style="font-family:Calibri">jstack -l &lt;pid&gt; &gt; thread.log</span><span style="font-family: SimSun">把</span><span style="font-family:Calibri">java</span><span style="font-family:SimSun">进程的</span><span style="font-family:Calibri">thread dump</span><span style="font-family:SimSun">出来，从里面找到</span><span style="font-family:Calibri">tid</span><span style="font-family:SimSun">，分析是哪个线程占用了系统资源。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">5</span><span style="font-family:SimSun">，分析</span><span style="font-family:Calibri">java</span><span style="font-family: SimSun">容器类，因为</span><span style="font-family:Calibri">java</span><span style="font-family:SimSun">的容器类是最常用来存储对象的，所以理论上发生内存泄露的风险也最高。可以从几个角度来看：</span><span style="font-family:Calibri">1</span><span style="font-family:SimSun">）</span><span style="font-family:Calibri">array</span><span style="font-family:SimSun">填充率查询（填充率</span><span style="font-family: Calibri">fill ratio</span><span style="font-family:SimSun">是数组中非空元素的比例），打印非原生类型数组的填充率频率分布，从而排查系统中</span><span style="font-family: Calibri">array</span><span style="font-family:SimSun">的利用率；</span><span style="font-family:Calibri">2</span><span style="font-family:SimSun">）数组按照</span><span style="font-family:Calibri">size</span><span style="font-family:SimSun">分组查询，打印一个按</span><span style="font-family: Calibri">size</span><span style="font-family:SimSun">分组的直方图；</span><span style="font-family:Calibri">3</span><span style="font-family:SimSun">）</span><span style="font-family:Calibri">collection</span><span style="font-family:SimSun">的填充率查询，</span><span style="font-family: Calibri">ArrayList/HashMap/Hashtable/Properties/Vector/WeakHashMap/ConcurrentHashMap$Segment</span><span style="font-family:SimSun">；</span><span style="font-family:Calibri">4</span><span style="font-family:SimSun">）</span><span style="font-family:Calibri">collection</span><span style="font-family: SimSun">按照</span><span style="font-family:Calibri">size</span><span style="font-family:SimSun">分组直方图；</span><span style="font-family: Calibri">5</span><span style="font-family:SimSun">）查看一个</span><span style="font-family:Calibri">list</span><span style="font-family: SimSun">里的所有对象；</span><span style="font-family:Calibri">6</span><span style="font-family:SimSun">）查看</span><span style="font-family:Calibri">hashmap</span><span style="font-family:SimSun">里的所有对象；</span><span style="font-family:Calibri">7</span><span style="font-family:SimSun">）查看</span><span style="font-family:Calibri">hashset</span><span style="font-family:SimSun">里的对象；</span><span style="font-family: Calibri">8</span><span style="font-family:SimSun">）检查</span><span style="font-family:Calibri">map</span><span style="font-family:SimSun">的碰撞率；</span><span style="font-family:Calibri">9</span><span style="font-family:SimSun">）检查所有只有一个常量的</span><span style="font-family:Calibri">array</span><span style="font-family: SimSun">。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">6</span><span style="font-family:SimSun">，分析</span><span style="font-family:Calibri">Finalizer</span><span style="font-family: SimSun">，</span><span style="font-family:Calibri">1</span><span style="font-family:SimSun">）查询</span><span style="font-family:Calibri">finalizer</span><span style="font-family:SimSun">正在处理的对象；</span><span style="font-family:Calibri">2</span><span style="font-family:SimSun">）查询</span><span style="font-family:Calibri">finalizer</span><span style="font-family:SimSun">准备处理的对象；</span><span style="font-family: Calibri">3</span><span style="font-family:SimSun">）直接查看</span><span style="font-family:Calibri">finalizer</span><span style="font-family: SimSun">线程；</span><span style="font-family:Calibri">4</span><span style="font-family:SimSun">）查看</span><span style="font-family:Calibri">finalizer</span><span style="font-family:SimSun">线程的</span><span style="font-family:Calibri">thread local</span><span style="font-family:SimSun">对象。</span></p><img src ="http://www.blogjava.net/changedi/aggbug/426849.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-08-17 19:08 <a href="http://www.blogjava.net/changedi/archive/2015/08/17/426849.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>blogjava的广告</title><link>http://www.blogjava.net/changedi/archive/2015/07/13/426209.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 13 Jul 2015 14:58:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/07/13/426209.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/426209.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/07/13/426209.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/426209.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/426209.html</trackback:ping><description><![CDATA[blogjava最近没什么节操啊，为什么全是chinese lady。这是技术blog，不至于吧~~~<br /><br />为智能上网提供一个url，很好用，先体验再买，智能上网全年无忧~~<br /><div><br />http://honx.in/_VZPFA4kWGkB3nugl</div><img src ="http://www.blogjava.net/changedi/aggbug/426209.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-07-13 22:58 <a href="http://www.blogjava.net/changedi/archive/2015/07/13/426209.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>该如何良好的实践Java中的Exception机制</title><link>http://www.blogjava.net/changedi/archive/2015/02/26/423050.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Thu, 26 Feb 2015 07:19:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/02/26/423050.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/423050.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/02/26/423050.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/423050.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/423050.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-family:SimSun;font-size:10.5pt">首先，我先声明一点，我讨论的仅限于互联网数据产品，当然可能会涉及到一些其他的抽象，但是所有的结论不代表能复用到所有场景。</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:SimSun">几乎每个</span><span style="font-family:Calibri">Java</span><span style="font-family:SimSun">程序员都清楚知道</span><span style="font-family:Calibri">Java</span><span style="font-family: SimSun">的异常和错误机制，无论是在面试过程中，还是在学习中，你看到</span><span style="font-family: Calibri">Exception</span><span style="font-family:SimSun">，无非就是了解一下继承关系、子类、和</span><span style="font-family:Calibri">Error</span><span style="font-family:SimSun">的关系等等。当然这些知识点是基础，那么在实践中，用到了吗？你确定你使用</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">时没有偷懒？我的经验告诉我，良好的使用</span><span style="font-family:Calibri">Exception</span><span style="font-family:SimSun">能让你的程序</span><span style="font-family:Calibri">bug</span><span style="font-family:SimSun">更少，或者至少能保证你的程序更容易被理解和跟踪。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:SimSun">先回到老的知识点吧&#8212;&#8212;</span><span style="font-family: Calibri">Java</span><span style="font-family:SimSun">的异常机制，我们知道</span><span style="font-family:Calibri">Java</span><span style="font-family: SimSun">里的异常是完全继承</span><span style="font-family:Calibri">Throwable</span><span style="font-family:SimSun">的，正如</span><span style="font-family:Calibri">java doc</span><span style="font-family: SimSun">里注释的，无论你</span><span style="font-family:Calibri">throw</span><span style="font-family:SimSun">的还是</span><span style="font-family:Calibri">JVM throw</span><span style="font-family:SimSun">的，抑或是你想</span><span style="font-family:Calibri">catch</span><span style="font-family: SimSun">的，都必须继承</span><span style="font-family:Calibri">Throwable</span><span style="font-family:SimSun">。我这里帮助大家纠正的第一个点就是：</span><span style="font-family:Calibri">java.lang.Throwable</span><span style="font-family:SimSun">是一个</span><span style="font-family:Calibri">class</span><span style="font-family:SimSun">，不是一个</span><span style="font-family:Calibri">interface</span><span style="font-family: SimSun">，千万别被名字欺骗。优秀的程序员有好的命名习惯，当</span><span style="font-family: Calibri">Java</span><span style="font-family:SimSun">程序员默认认为带有</span><span style="font-family:Calibri">-able</span><span style="font-family: SimSun">后缀的都该是接口时，</span><span style="font-family:Calibri"> Josh Bloch</span><span style="font-family:SimSun">给大家上了一课&#8212;&#8212;</span><span style="font-family:Calibri">Throwable</span><span style="font-family: SimSun">就是类。回到正题，</span><span style="font-family:Calibri">Throwable</span><span style="font-family:SimSun">有两大子类，一个是</span><span style="font-family:Calibri">java.lang.Error</span><span style="font-family:SimSun">，一个是</span><span style="font-family:Calibri">java.lang.Excpetion</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">Error</span><span style="font-family:SimSun">是错误，一般多用于系统异常和底层错误，</span><span style="font-family:Calibri">Error</span><span style="font-family: SimSun">抛出就会导致程序终止；而</span><span style="font-family:Calibri">Exception</span><span style="font-family:SimSun">是异常，有程序引起，又分为受检的</span><span style="font-family:Calibri">checked</span><span style="font-family: SimSun">和非受检的</span><span style="font-family:Calibri">unchecked</span><span style="font-family:SimSun">（不知道哪本书这么翻译的来着），受检的异常是普通异常，就是你需要</span><span style="font-family:Calibri">catch</span><span style="font-family: SimSun">的来打日志或者补救的（这种做法叫吞掉异常），非受检的多数是</span><span style="font-family: Calibri">RuntimeException</span><span style="font-family:SimSun">，就是运行时异常，这类异常不建议</span><span style="font-family:Calibri">catch</span><span style="font-family:SimSun">，因为这个是程序</span><span style="font-family: Calibri">bug</span><span style="font-family:SimSun">，需要被人发现，所以建议抛出引起程序终止。</span><span style="font-family:Calibri">Blah~blah~ </span><span style="font-family:SimSun">这些都是老生常谈的话题，需要深入的点是有几个：</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:SimSun">第一，</span><span style="font-family:Calibri">Error</span><span style="font-family:SimSun">是建议到系统级别的错误的，包括虚拟机的，我们常见的就是</span><span style="font-family:Calibri">java.lang.VirtualMachineError</span><span style="font-family:SimSun">，这是一个</span><span style="font-family: Calibri">JVM</span><span style="font-family:SimSun">级别的抽象</span><span style="font-family:Calibri">Error</span><span style="font-family: SimSun">，如果你觉得没见过？那么不用奇怪，它的两个儿子你肯定见过：</span><span style="font-family: Calibri">OutOfMemoryError</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">StackOverflowError</span><span style="font-family:SimSun">。</span><span style="font-family:Calibri">Error</span><span style="font-family:SimSun">其实真没好说的，一般情况不建议捕获，程序员也用的较少，但是你看很多基础框架或者系统软件，都是有自定义</span><span style="font-family:Calibri">Error</span><span style="font-family: SimSun">的，所以当你的工作层次或者范围你能确定比较底层时，其实是可以自定义一些</span><span style="font-family:Calibri">Error</span><span style="font-family: SimSun">来控制程序的错误的。我这样说可能也不是很好理解，换个简单的话：你的架构设计中需要考虑到异常的处理，那么首先要对异常定级别，如果有可能有偏底层的异常时，或者是本不该出现且不建议用户（多数是依赖你的库进行开发的其他程序员）捕获时，定义为</span><span style="font-family:Calibri">Error</span><span style="font-family: SimSun">是个不错的选择。当然也不是说做上层开发的程序员就不能使用</span><span style="font-family: Calibri">Error</span><span style="font-family:SimSun">，只要你设计合理，你可以在必要时抛出</span><span style="font-family:Calibri">Error</span><span style="font-family: SimSun">来终止程序&#8212;&#8212;比如提醒你的老板再不加薪就</span><span style="font-family:Calibri">Error</span><span style="font-family:SimSun">到死：）</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:SimSun">第二，</span><span style="font-family:Calibri">Exception</span><span style="font-family:SimSun">分两类这事几乎人人皆知，受检的异常往往是</span><span style="font-family:Calibri">web</span><span style="font-family:SimSun">后端开发或者服务开发自定义的业务异常比如</span><span style="font-family:Calibri">BizServiceException</span><span style="font-family:SimSun">或者常见的</span><span style="font-family:Calibri">DAOException</span><span style="font-family:SimSun">，这些异常在开发定义时总是直接</span><span style="font-family:Calibri">extends Exception</span><span style="font-family:SimSun">，而忽视了究竟这些异常我们对它们的期望是什么，这里我想强调一点，我们在业务系统架构中考虑到异常机制，自定义的异常不是为了有异常而定义异常，一定对它本身是有期望的。我们对异常的一个基本期望是异常究竟该被谁捕获，如果被你的服务下游捕获，那么这必须是一个受检的异常，如果是系统自身需要，那么这个我个人认为是分阶段设计的，在初期，也就是未发布状态，这些</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">应该总是被抛出的，因为这样可以快速的让测试和质量控制人员发现系统崩溃的点。在发布阶段，异常可能需要被内部消化，这时受检异常就要提供给业务系统，让业务开发自行捕获异常。当然，好的系统架构可能会把</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">作为一个内部可见外部不可见的内容，而基于此完全封装一套</span><span style="font-family: Calibri">error code</span><span style="font-family:SimSun">对外，这应该算是比较友好的做法了，也是很多</span><span style="font-family:Calibri">API</span><span style="font-family:SimSun">设计时的标准规范。毕竟对外部透明，不要让用户看到你的</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">，这是非常友好的做法。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:SimSun">第三，关于</span><span style="font-family: Calibri">catch</span><span style="font-family:SimSun">，就针对上面的第二点讲，吞异常这事不是没人干过，我们往往担心系统错误而一个</span><span style="font-family:Calibri">try catch </span><span style="font-family: SimSun">捕获所有</span><span style="font-family:Calibri">Exception</span><span style="font-family:SimSun">，有的甚至不够，还升一级，捕获</span><span style="font-family:Calibri">Throwable</span><span style="font-family: SimSun">，这应该是最糟糕的代码设计（但不幸的是在我现在开发的系统和曾经开发过的业务系统中，这类代码非常普遍）。开发人员不应该因为时间紧、赶进度等接口而忽视</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">，就拿最上层的业务开发举例，开发可能会调用各类服务、访问数据库、访问缓存和文件系统等等，而这些服务必然包含了各种异常，而</span><span style="font-family:Calibri">catch</span><span style="font-family: SimSun">一个</span><span style="font-family:Calibri">Exception</span><span style="font-family:SimSun">，试图通过吞噬异常保护系统或者页面正常访问，而打日志到后台，通过分析日志来偷偷的解决</span><span style="font-family:Calibri">bug</span><span style="font-family:SimSun">&#8230;&#8230;说起来真是汗毛倒竖。我的观点：如果有错误，那么让它尽早暴露出来，我们应该通过尽量多的测试和优化来避免错误，而不是偷偷的隐藏。事实也证明，日志里大量的</span><span style="font-family:Calibri">NPE</span><span style="font-family:SimSun">或者其他</span><span style="font-family:Calibri">RuntimeException</span><span style="font-family:SimSun">存在，但是无人问津，&#8220;系统不是好好的吗？&#8221;，&#8220;页面不是没问题吗&#8221;这样的说辞可以让开发看起来毫无责任，但是这为系统长期的维护和后续的扩展都带来了无尽的烦恼和坑。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:SimSun">综上，我个人的经验告诉我几点对待</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">的方法：</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1</span><span style="font-family:SimSun">，花时间了解涉及到的每个服务和方法所可能抛出的异常。事实往往是理解异常的关系和机制其实不花时间，了解后再开发，你会比别人知道更多的异常点，这能保证你程序的健壮性；</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2</span><span style="font-family:SimSun">，无论你在服务开发还是服务使用层级，都要尝试或者想到封装异常，提供友好错误设计的方案，最简单的是自定义一个业务</span><span style="font-family:Calibri">Exception</span><span style="font-family: SimSun">来封装。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3</span><span style="font-family:SimSun">，不要在你的方法开始</span><span style="font-family: Calibri">try</span><span style="font-family:SimSun">，结束时</span><span style="font-family:Calibri">catch</span><span style="font-family: SimSun">，这防御性太强了，很美品位。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4</span><span style="font-family:SimSun">，前三点可能都是错的，因为我自己也没有完全实践：）</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/423050.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-02-26 15:19 <a href="http://www.blogjava.net/changedi/archive/2015/02/26/423050.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>2014总结</title><link>http://www.blogjava.net/changedi/archive/2015/02/25/423025.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 25 Feb 2015 03:01:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/02/25/423025.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/423025.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/02/25/423025.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/423025.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/423025.html</trackback:ping><description><![CDATA[<p style="margin: 0in; font-size: 10.5pt;">&nbsp; &nbsp; &nbsp;<span style="font-family:宋体">今天是春节休假后第一天上班，趁着大家还在休假，抽空总结一下过去的</span><span style="font-family:Calibri">2014</span><span style="font-family: 宋体">，为自己苦逼的</span><span style="font-family:Calibri">2014</span><span style="font-family:宋体">画个句号，迎接新的</span><span style="font-family: Calibri">2015.</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体">苦逼用来形容自己，是稍显偏颇了，</span><span style="font-family:Calibri">2014</span><span style="font-family: 宋体">虽然经历了两拨</span><span style="font-family:Calibri">996</span><span style="font-family:宋体">的洗礼（年初和年末），但是技术成长确实没有想象的多，工作四年来，两年时间用来做</span><span style="font-family:Calibri">web</span><span style="font-family:宋体">的后端开发，从</span><span style="font-family:Calibri">spring</span><span style="font-family:宋体">、</span><span style="font-family:Calibri">webx</span><span style="font-family:宋体">的源码，到慢慢深入到一些数据库中间件的使用</span><span style="font-family:Calibri">ibatis</span><span style="font-family: 宋体">和</span><span style="font-family:Calibri">tddl</span><span style="font-family:宋体">（淘宝内部）、消息中间件</span><span style="font-family: Calibri">metaq</span><span style="font-family:宋体">（淘宝内部）、服务框架</span><span style="font-family:Calibri">HSF</span><span style="font-family:宋体">和其他的整个后端相关的内容，大概理解了一遍所有</span><span style="font-family:Calibri">backend</span><span style="font-family:宋体">后，团队缺前端的状况逼的自己开始做</span><span style="font-family:Calibri">frontend</span><span style="font-family: 宋体">，使用</span><span style="font-family:Calibri">jquery</span><span style="font-family:宋体">和</span><span style="font-family:Calibri">kissy</span><span style="font-family:宋体">（</span><span style="font-family:Calibri">brix</span><span style="font-family: 宋体">），使用</span><span style="font-family:Calibri">bootstrap</span><span style="font-family:宋体">，开发自己的基于</span><span style="font-family:Calibri">raphaeljs</span><span style="font-family:宋体">的</span><span style="font-family:Calibri">vnix</span><span style="font-family: 宋体">数据绘图框架等等，对前端的正常业务开发也基本熟练。回顾</span><span style="font-family:Calibri">2013</span><span style="font-family:宋体">年的总结，发现自己去年就开始有些负面情绪了，虽然接触了</span><span style="font-family:Calibri">storm</span><span style="font-family: 宋体">开发，正常时间写</span><span style="font-family:Calibri">hive</span><span style="font-family:宋体">和</span><span style="font-family:Calibri">odps</span><span style="font-family:宋体">的</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">，开发数据报表，但是这些工作对于技术的直接提升并不明显。在无线反作弊领域做两年了，整个反作弊的设计架构也从最开始的统计分析发展到机器学习后来又逐渐变为混合模式，也算经历了三代的演变。乐观的审视自己这</span><span style="font-family:Calibri">4</span><span style="font-family:宋体">年的蜕变，技术上的提升最大还是在</span><span style="font-family:Calibri">2-3</span><span style="font-family:宋体">年的时候，现在的很多认知提升，都慢慢的转到了架构设计和团队管理，慢慢的自己开始不断挑刺了，我一直不理解我这种变化的原因究竟是什么，开始不满团队的管理模式，经常设想如果自己来干，哪些地方该优化该改进；开始不满产品的技术架构；开始不满团队成员的&#8220;不够优秀&#8221;。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体">回家过了个年，胡吃海喝后，自己开始熟悉原来身边的环境，也开始能看到自己内心去了。你要做成功一个事情，这是之前在阿里巴巴的一个承诺。其实这是很难的，不仅是阿里，你在哪家大公司都很难。那么发泄不满是最低效最没有品位的做法，你只能让身边的人知道你不满，但这重要吗？</span><span style="font-family:Calibri">The thing is to acitvate them</span><span style="font-family:宋体">。一个卓越的人，首先要能包容自己对身边的不满，我们不乱发泄，我们沟通，我们建设。要做成一件成功的事，不是要和一个完美的团队配合，而是要能配合出一个完美的团队。能容别人所不能容，能帮助别人成就别人。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2014</span><span style="font-family:宋体">整个一年我自己总结就是懒惰的一年，这一年，我没怎么读过书，没怎么关注过新技术，没怎么写过有趣的代码，反而还经常玩手游，跟人诉苦水，传播负能量。人生苦短，在即将</span><span style="font-family:Calibri">30</span><span style="font-family:宋体">岁的时候，我却开始纠结一些芝麻绿豆的事情，工资高低啦，晋升啦？</span><span style="font-family:Calibri">These all shit</span><span style="font-family:宋体">。新的一年，自己要有远大的目标，我一定要做成一个事情，而为了这个目标，要持续学习、读书。记得小时候，每到寒暑假，都会把整个假期及未来学期的计划写清楚，虽然那时很幼稚，计划总是什么几点睡觉，几点起床，读哪几本书之类，但是这种习惯，在我们长大后却逐渐褪去了，我们成了没有目标的混日子的混子。想起</span><span style="font-family:Calibri">Fenng</span><span style="font-family: 宋体">之前的一篇文章，讲大公司的混子，还真别说，大公司最多的就是混子。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体">不要拘泥于要成为什么样的人，只要知道你要做成什么事就够了，因为只要你努力为这个目标奋斗，你就会成为一个能做成这个目标的人。</span><span style="font-family:Calibri">That's enough</span><span style="font-family:宋体">。</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/423025.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-02-25 11:01 <a href="http://www.blogjava.net/changedi/archive/2015/02/25/423025.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-181~183-3个easy题目连发</title><link>http://www.blogjava.net/changedi/archive/2015/02/06/422790.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Fri, 06 Feb 2015 05:39:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/02/06/422790.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422790.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/02/06/422790.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422790.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422790.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri" lang="en-US">181</span><span style="font-family:SimSun" lang="zh-CN">题目地址：</span><span style="font-family:Calibri" lang="zh-CN">https://oj.leetcode.com/problems/employees-earning-more-than-their-managers/</span></p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">181</span><span style="font-family:SimSun">题又是一个简单题目，给定一个</span><span style="font-family:Calibri">Employee</span><span style="font-family: SimSun">表，里面存储了雇员的工资信息，包括名字、工资、经理</span><span style="font-family: Calibri">ID</span><span style="font-family:SimSun">，题目要求写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">查找出所有那些自身工资比经理还高的雇员的名字。</span></p><p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+-------+--------+-----------+<br /> | Id | Name&nbsp; | Salary | ManagerId |<br /> +----+-------+--------+-----------+<br /> | 1&nbsp; | Joe&nbsp;&nbsp; | 70000&nbsp; | 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | 2&nbsp; | Henry | 80000&nbsp; | 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | 3&nbsp; | Sam&nbsp;&nbsp; | 60000&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | 4&nbsp; | Max&nbsp;&nbsp; | 90000&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /> +----+-------+--------+-----------+</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个题目很简单，现有表不能做就是因为现有的一行记录里没有包含经理的工资信息，但是有经理的</span><span style="font-family:Calibri">ID</span><span style="font-family:SimSun">，那么我们做一下关联，把工资信息拿到，再过滤就好了，于是思路</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">如下：</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />Name&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;Employee<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span><br />o1.Name<br />,o1.Salary&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;s<br />,o2.Salary&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;m<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee<br />)o1<br /><span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.ManagerId<span style="color: #808080; ">=</span>o2.Id)<br />)t<br /><span style="color: #0000FF; ">where</span>&nbsp;s<span style="color: #808080; ">&gt;</span>m</div><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">其中</span><span style="font-family:Calibri">s</span><span style="font-family:SimSun">是自己的工资，</span><span style="font-family: Calibri">m</span><span style="font-family:SimSun">是经理的工资</span><span style="font-family:Calibri">~~</span><span style="font-family:SimSun">一目了然</span></p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">182</span><span style="font-family:SimSun">题目地址：</span><span style="font-family:Calibri">https://oj.leetcode.com/problems/duplicate-emails/</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">182</span><span style="font-family:宋体">也是</span><span style="font-family:Calibri">Easy</span><span style="font-family: 宋体">级别题目，题目描述就是写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">，把</span><span style="font-family:Calibri">Person</span><span style="font-family: 宋体">表中有重复</span><span style="font-family:Calibri">Email</span><span style="font-family:宋体">的记录拉出来。</span></p><p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+---------+<br /> | Id | Email&nbsp;&nbsp; |<br /> +----+---------+<br /> | 1&nbsp; | a@b.com |<br /> | 2&nbsp; | c@d.com |<br /> | 3&nbsp; | a@b.com |<br /> +----+---------+</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">很容易想到的思路：按照</span><span style="font-family:Calibri">email</span><span style="font-family: 宋体">做聚合，把</span><span style="font-family:Calibri">count&gt;1</span><span style="font-family:宋体">的取出来，对应</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">如下：</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;Email<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;Email<br />&nbsp;&nbsp;&nbsp;&nbsp;,<span style="color: #FF00FF; ">count</span>(Id)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;cnt<br /><span style="color: #0000FF; ">from</span>&nbsp;Person<br /><span style="color: #0000FF; ">group</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;Email<br />)t<br /><span style="color: #0000FF; ">where</span>&nbsp;cnt<span style="color: #808080; ">&gt;</span><span style="color: #800000; font-weight: bold; ">1</span></div><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">183</span><span style="font-family:宋体">题目地址：</span><span style="font-family:Calibri">https://oj.leetcode.com/problems/customers-who-never-order/</span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">183</span><span style="font-family:宋体">题，一个网站包含两张表，一张</span><span style="font-family:Calibri">Customers</span><span style="font-family: 宋体">表存放客户数据，一张</span><span style="font-family:Calibri">Orders</span><span style="font-family:宋体">表存放产生订单的客户</span><span style="font-family: Calibri">ID</span><span style="font-family:宋体">，题目要求写</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">查出没有在网站产生过订单的客户。说白了就是查询在</span><span style="font-family:Calibri">Customers</span><span style="font-family:宋体">里而不在</span><span style="font-family:Calibri">Orders</span><span style="font-family: 宋体">里的数据，</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">如下：</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />o1.Name&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;Customers<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Customers<br />)o1<br /><span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Orders<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.Id<span style="color: #808080; ">=</span>o2.CustomerId&nbsp;)<br /><span style="color: #0000FF; ">where</span>&nbsp;o2.CustomerId&nbsp;<span style="color: #0000FF; ">is</span>&nbsp;<span style="color: #0000FF; ">null</span><br />&nbsp;</div><p style="margin:0in;font-family:Calibri;font-size:10.5pt"><br /></p><p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">3</span><span style="font-family:宋体">个简单题目，夯实</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">基础</span><span style="font-family:Calibri">~~</span></p><p style="margin:0in;font-size:10.5pt">                                                                                                                          </p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:16.0pt"><span style="font-family:Calibri"></span><span style="font-family:SimSun"></span></p><div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422790.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-02-06 13:39 <a href="http://www.blogjava.net/changedi/archive/2015/02/06/422790.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-180-Consecutive Numbers-Medium</title><link>http://www.blogjava.net/changedi/archive/2015/01/29/422554.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Thu, 29 Jan 2015 11:01:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/29/422554.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422554.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/29/422554.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422554.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422554.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">题目地址：</span><span style="font-family:Calibri"><a href="https://oj.leetcode.com/problems/consecutive-numbers/">https://oj.leetcode.com/problems/consecutive-numbers/</a></span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这个题目是要求写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">，查询出表中连续出现三次的记录。表结构非常简单如下：</span></p><p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+-----+<br /> | Id | Num |<br /> +----+-----+<br /> | 1&nbsp; |&nbsp; 1&nbsp; |<br /> | 2&nbsp; |&nbsp; 1&nbsp; |<br /> | 3&nbsp; |&nbsp; 1&nbsp; |<br /> | 4&nbsp; |&nbsp; 2&nbsp; |<br /> | 5&nbsp; |&nbsp; 1&nbsp; |<br /> | 6&nbsp; |&nbsp; 2&nbsp; |<br /> | 7&nbsp; |&nbsp; 2&nbsp; |<br /> +----+-----+</span></p><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这个</span><span style="font-family:Calibri">Logs</span><span style="font-family: 宋体">表里，只有</span><span style="font-family:Calibri">Id</span><span style="font-family:宋体">和</span><span style="font-family:Calibri">Num</span><span style="font-family:宋体">字段，而题目就是要找出连续出现</span><span style="font-family:Calibri">3</span><span style="font-family:宋体">次的</span><span style="font-family:Calibri">Num</span><span style="font-family:宋体">，对于这个表，答案就是</span><span style="font-family: Calibri">1</span><span style="font-family:宋体">了。</span></p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">思路很直观暴力的一个想法就是</span><span style="font-family:Calibri">Logs</span><span style="font-family: 宋体">表自己关联</span><span style="font-family:Calibri">3</span><span style="font-family:宋体">次，关联条件依次是</span><span style="font-family: Calibri">Id+1</span><span style="font-family:宋体">，这样就可以把连续记录关联出来了</span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">我的代码如下：</p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br /><span style="color: #0000FF; ">distinct</span>&nbsp;o1.Num<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs<br />)o1<br /><span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.Num<span style="color: #808080; ">=</span>o2.Num&nbsp;<span style="color: #808080; ">and</span>&nbsp;o1.Id<span style="color: #808080; ">=</span>o2.Id<span style="color: #808080; ">+</span><span style="color: #800000; font-weight: bold; ">1</span>)<br /><span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs<br />)o3<br /><span style="color: #0000FF; ">on</span>(o2.Num<span style="color: #808080; ">=</span>o3.Num&nbsp;<span style="color: #808080; ">and</span>&nbsp;o2.Id<span style="color: #808080; ">=</span>o3.Id<span style="color: #808080; ">+</span><span style="color: #800000; font-weight: bold; ">1</span>)</div><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">这个题目虽然可以这样解掉，但是很自然的会联想，如果</span><span style="font-family:Calibri">3</span><span style="font-family:宋体">变成</span><span style="font-family:Calibri">n</span><span style="font-family:宋体">呢，题目变为求连续出现</span><span style="font-family: Calibri">n</span><span style="font-family:宋体">次的记录，那该如何解？显然暴力解法是不可行的。鉴于能力有限，我从</span><span style="font-family:Calibri">discuss</span><span style="font-family: 宋体">区找到了一个很赞的解法，通过定义变量，很巧妙的解了这个扩展的问题，原作者</span><a href="https://oj.leetcode.com/discuss/user/kent-huang"><span style="font-family: Calibri">kent-huang</span></a></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">代码如下：</p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">DISTINCT</span>&nbsp;num&nbsp;<br /><span style="color: #0000FF; ">FROM</span>&nbsp;(<br />&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;num,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">case</span>&nbsp;<span style="color: #0000FF; ">when</span>&nbsp;<span style="color: #008000; ">@record</span>&nbsp;<span style="color: #808080; ">=</span>&nbsp;num&nbsp;<span style="color: #0000FF; ">then</span>&nbsp;<span style="color: #008000; ">@count</span>:<span style="color: #808080; ">=</span><span style="color: #008000; ">@count</span><span style="color: #808080; ">+</span><span style="color: #800000; font-weight: bold; ">1</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">when</span>&nbsp;<span style="color: #008000; ">@record</span>&nbsp;<span style="color: #808080; ">&lt;&gt;</span>&nbsp;<span style="color: #008000; ">@record</span>:<span style="color: #808080; ">=</span>num&nbsp;<span style="color: #0000FF; ">then</span>&nbsp;<span style="color: #008000; ">@count</span>:<span style="color: #808080; ">=</span><span style="color: #800000; font-weight: bold; ">1</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">end</span>&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;n&nbsp;<br />&nbsp;&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs&nbsp;,(<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">@count</span>:<span style="color: #808080; ">=</span><span style="color: #800000; font-weight: bold; ">0</span>,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">@record</span>:<span style="color: #808080; ">=</span>(<span style="color: #0000FF; ">SELECT</span>&nbsp;num&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Logs&nbsp;limit&nbsp;<span style="color: #800000; font-weight: bold; ">0</span>,<span style="color: #800000; font-weight: bold; ">1</span>)<br />&nbsp;&nbsp;)&nbsp;r&nbsp;<br />)&nbsp;a&nbsp;<br /><span style="color: #0000FF; ">where</span>&nbsp;a.n<span style="color: #808080; ">&gt;=</span><span style="color: #800000; font-weight: bold;">3</span></div><p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">简单分析一下，作者通过定义两个变量</span><span style="font-family:Calibri">record</span><span style="font-family: 宋体">和</span><span style="font-family:Calibri">count</span><span style="font-family:宋体">来控制记录和对应的</span><span style="font-family: Calibri">rank</span><span style="font-family:宋体">值，首先通过一个</span><span style="font-family:Calibri">select @count:=0,@record:=(SELECT num from Logs limit 0,1)</span><span style="font-family:宋体">语句来初始化这两个变量</span><span style="font-family:Calibri">count=0</span><span style="font-family: 宋体">，</span><span style="font-family:Calibri">record=</span><span style="font-family:宋体">表里第一条记录的</span><span style="font-family:Calibri">num</span><span style="font-family:宋体">。接下来通过普通查询，将</span><span style="font-family:Calibri">Logs</span><span style="font-family: 宋体">表里每一条记录查出来，和</span><span style="font-family:Calibri">record</span><span style="font-family:宋体">对比，如果相同，则</span><span style="font-family: Calibri">count</span><span style="font-family:宋体">自增</span><span style="font-family:Calibri">1</span><span style="font-family:宋体">，如果不同，那么新的</span><span style="font-family:Calibri">record</span><span style="font-family:宋体">被赋值，同时</span><span style="font-family:Calibri">count</span><span style="font-family:宋体">置</span><span style="font-family:Calibri">1</span><span style="font-family:宋体">，很漂亮的自定义变量用</span><span style="font-family:Calibri">sql</span><span style="font-family:宋体">实现了我们直觉上需要用逻辑代码来完成的功能。而且这个代码的一大优势是不需要用到Id字段~~非常棒</span></p><p style="margin:0in;font-family:宋体;font-size:10.5pt">&nbsp;</p><p style="margin:0in;font-size:10.5pt">                                                        </p><p style="margin:0in;font-size:10.5pt"><span style="font-family:宋体">还有好的思路，请一定分享给我</span><span style="font-family:Calibri">~~:)</span></p><p style="margin:0in;font-family:Calibri;font-size:16.0pt"></p><div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422554.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-29 19:01 <a href="http://www.blogjava.net/changedi/archive/2015/01/29/422554.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-178-Rank Scores-Medium</title><link>http://www.blogjava.net/changedi/archive/2015/01/28/422512.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 28 Jan 2015 08:50:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/28/422512.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422512.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/28/422512.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422512.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422512.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">题目地址：</span><a href="https://oj.leetcode.com/problems/rank-scores/"><span style="font-family: Calibri">https://oj.leetcode.com/problems/rank-scores/</span></a></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个问题很有趣，写一个类似</span><span style="font-family:Calibri">oracle</span><span style="font-family:SimSun">里的窗口函数</span><span style="font-family: Calibri">rank()</span><span style="font-family:SimSun">，具体描述一下，有一张数据表</span><span style="font-family:Calibri">Scores</span><span style="font-family: SimSun">，里面有两个字段</span><span style="font-family:Calibri">Id</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">Score</span><span style="font-family:SimSun">，具体结构如下：</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+-------+<br /> | Id | Score |<br /> +----+-------+<br /> | 1&nbsp; | 3.50&nbsp; |<br /> | 2&nbsp; | 3.65&nbsp; |<br /> | 3&nbsp; | 4.00&nbsp; |<br /> | 4&nbsp; | 3.85&nbsp; |<br /> | 5&nbsp; | 4.00&nbsp; |<br /> | 6&nbsp; | 3.65&nbsp; |<br /> +----+-------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">任务是要写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">来给</span><span style="font-family:Calibri">Score</span><span style="font-family:SimSun">字段打一个</span><span style="font-family:Calibri">rank</span><span style="font-family: SimSun">标识，条件是按照</span><span style="font-family:Calibri">Score</span><span style="font-family:SimSun">从大到小排序，相等情况时</span><span style="font-family:Calibri">rank</span><span style="font-family: SimSun">相同，且</span><span style="font-family:Calibri">rank</span><span style="font-family:SimSun">之间没有&#8220;洞&#8221;，即</span><span style="font-family: Calibri">rank</span><span style="font-family:SimSun">字段是连续值。显然这个任务比</span><span style="font-family:Calibri">rank</span><span style="font-family: SimSun">窗口函数容易一些，但是也是一个棘手的问题。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">题目具体给出了输出示例：</p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+-------+------+<br /> | Score | Rank |<br /> +-------+------+<br /> | 4.00&nbsp; | 1&nbsp;&nbsp;&nbsp; |<br /> | 4.00&nbsp; | 1&nbsp;&nbsp;&nbsp; |<br /> | 3.85&nbsp; | 2&nbsp;&nbsp;&nbsp; |<br /> |</span><span style="background:whitesmoke">&nbsp;3.65&nbsp; | 3&nbsp;&nbsp;&nbsp; |<br /> </span><span style="background:whitesmoke">| 3.65&nbsp; | 3&nbsp;&nbsp;&nbsp; |<br /> | 3.50&nbsp; | 4&nbsp;&nbsp;&nbsp; |<br /> +-------+------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">平常说实话</span><span style="font-family:Calibri">rank</span><span style="font-family:SimSun">函数或者</span><span style="font-family:Calibri">row_number</span><span style="font-family:SimSun">函数用多了，很少考虑实现，面对这个问题，硬着头皮用笛卡尔积的</span><span style="font-family:Calibri">join</span><span style="font-family: SimSun">解决了，若是在</span><span style="font-family:Calibri">hive</span><span style="font-family:SimSun">中，</span><span style="font-family:Calibri">strict</span><span style="font-family:SimSun">模式可能拒绝笛卡尔积的</span><span style="font-family:Calibri">join</span><span style="font-family: SimSun">，这时还是求助于窗口函数</span><span style="font-family:Calibri">rank</span><span style="font-family:SimSun">吧</span><span style="font-family:Calibri">~~</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">实现代码如下：</p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />o1.Score<br />,<span style="color: #FF00FF; ">count</span>(o2.Score)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;Rank<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Scores<br />)o1<br /><span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span>(<br /><span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">distinct</span>&nbsp;Score&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Scores<br />)o2<br /><span style="color: #0000FF; ">on</span>(o1.Score<span style="color: #808080; ">&lt;=</span>o2.Score)<br /><span style="color: #0000FF; ">group</span>&nbsp;<span style="color: #0000FF; ">by</span><br />o1.Id<br /><span style="color: #0000FF; ">order</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;o1.Score&nbsp;<span style="color: #0000FF; ">desc</span></div>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422512.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-28 16:50 <a href="http://www.blogjava.net/changedi/archive/2015/01/28/422512.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-177-Nth Highest Salary-Medium</title><link>http://www.blogjava.net/changedi/archive/2015/01/27/422482.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 Jan 2015 08:59:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/27/422482.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422482.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/27/422482.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422482.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422482.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">题目地址：</span><a href="https://oj.leetcode.com/problems/nth-highest-salary/"><span style="font-family:Calibri">https://oj.leetcode.com/problems/nth-highest-salary/</span></a></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个题目其实是</span><span style="font-family:Calibri">176</span><span style="font-family:SimSun">的扩展，刚才不是要找第二大的</span><span style="font-family:Calibri">salary</span><span style="font-family: SimSun">吗，那好，现在直接扩展到任意，第</span><span style="font-family:Calibri">N</span><span style="font-family:SimSun">大，而且这次是要写一个</span><span style="font-family:Calibri">Function</span><span style="font-family: SimSun">，</span><span style="font-family:Calibri">N</span><span style="font-family:SimSun">作为参数。表还是之前的</span><span style="font-family: Calibri">Employee</span><span style="font-family:SimSun">表。</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+--------+<br /> | Id | Salary |<br /> +----+--------+<br /> | 1&nbsp; | 100&nbsp;&nbsp;&nbsp; |<br /> | 2&nbsp; | 200&nbsp;&nbsp;&nbsp; |<br /> | 3&nbsp; | 300&nbsp;&nbsp;&nbsp; |<br /> +----+--------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">我不知道为什么这个题目的通过率那么低，但是事实是使用</span><span style="font-family:Calibri">176</span><span style="font-family:SimSun">题那篇文章的&#8220;错误&#8221;做法即可解掉这个题目。注意</span><span style="font-family:Calibri">limit</span><span style="font-family: SimSun">是从</span><span style="font-family:Calibri">0</span><span style="font-family:SimSun">开始，所以变量要默认自减</span><span style="font-family:Calibri">1</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">CREATE</span>&nbsp;<span style="color: #0000FF; ">FUNCTION</span>&nbsp;getNthHighestSalary(N&nbsp;<span style="font-weight: bold;">INT</span>)&nbsp;<span style="color: #0000FF; ">RETURNS</span>&nbsp;<span style="font-weight: bold;">INT</span><br /><span style="color: #0000FF; ">BEGIN</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">declare</span>&nbsp;n1&nbsp;<span style="font-weight: bold;">int</span>;<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">set</span>&nbsp;n1&nbsp;<span style="color: #808080; ">=</span>&nbsp;N<span style="color: #808080; ">-</span><span style="color: #800000; font-weight: bold; ">1</span>;<br />&nbsp;&nbsp;<span style="color: #0000FF; ">RETURN</span>&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;Write&nbsp;your&nbsp;MySQL&nbsp;query&nbsp;statement&nbsp;below.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Salary<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">from</span>(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #0000FF; ">distinct</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)t<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">order</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">desc</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;limit&nbsp;n1,<span style="color: #800000; font-weight: bold; ">1</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;);<br /><span style="color: #0000FF; ">END</span></div>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">大写的部分是题目已经给的框架，我们只需要在</span><span style="font-family:Calibri">Return</span><span style="font-family:SimSun">语句里填写内容即可，我</span><span style="font-family:Calibri">declare</span><span style="font-family: SimSun">了一个变量，不知道是否是一个</span><span style="font-family:Calibri">hack</span><span style="font-family:SimSun">手段，但是</span><span style="font-family:Calibri">it works</span><span style="font-family: SimSun">.</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">当然本着在大数据平台下sql的经验，如果换做是在hive下写这个，思路是什么呢？</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">1</span><span style="font-family:SimSun">，我建议直接写一个</span><span style="font-family:Calibri">UDAF</span><span style="font-family: SimSun">解决。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">2</span><span style="font-family:SimSun">，不具备</span><span style="font-family:Calibri">UDAF</span><span style="font-family: SimSun">能力的话，可以在一个</span><span style="font-family:Calibri">key</span><span style="font-family:SimSun">下做</span><span style="font-family:Calibri">sort by</span><span style="font-family: SimSun">后，把他们</span><span style="font-family:Calibri">group_concat</span><span style="font-family:SimSun">或者</span><span style="font-family:Calibri">wm_concat</span><span style="font-family:SimSun">起来，然后</span><span style="font-family:Calibri">get</span><span style="font-family:SimSun">第</span><span style="font-family:Calibri">n</span><span style="font-family:SimSun">个</span><span style="font-family:Calibri">item</span><span style="font-family:SimSun">即可，当然这会利用到</span><span style="font-family:Calibri">hive</span><span style="font-family: SimSun">的默认的几个</span><span style="font-family:Calibri">UDAF</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">UDF</span><span style="font-family:SimSun">。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">思路肯定有很多，欢迎大家一起来</span><span style="font-family:Calibri">share~</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422482.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-27 16:59 <a href="http://www.blogjava.net/changedi/archive/2015/01/27/422482.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-176-Second Highest Salary-Easy</title><link>http://www.blogjava.net/changedi/archive/2015/01/27/422478.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 Jan 2015 08:38:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/27/422478.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422478.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/27/422478.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422478.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422478.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:Calibri">leetcode</span><span style="font-family:SimSun">地址：</span><span style="font-family:Calibri"><a href="https://oj.leetcode.com/problems/second-highest-salary/">https://oj.leetcode.com/problems/second-highest-salary/</a></span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这个问题很有趣，是要求我们写个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">来查询</span><span style="font-family:Calibri">Employee</span><span style="font-family:SimSun">表里第二高的工资，如果没有第二高的，那么返回</span><span style="font-family:Calibri">null</span><span style="font-family: SimSun">。</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+----+--------+<br /> | Id | Salary |<br /> +----+--------+<br /> | 1&nbsp; | 100&nbsp;&nbsp;&nbsp; |<br /> | 2&nbsp; | 200&nbsp;&nbsp;&nbsp; |<br /> | 3&nbsp; | 300&nbsp;&nbsp;&nbsp; |<br /> +----+--------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">看到这个问题，可能很多人会想，这很简单啊，写个</span><span style="font-family:Calibri">order by desc</span><span style="font-family:SimSun">，然后找到第二个即可。</span></p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee&nbsp;<span style="color: #0000FF; ">order</span>&nbsp;<span style="color: #0000FF; ">by</span>&nbsp;Salary&nbsp;<span style="color: #0000FF; ">desc</span>&nbsp;limit&nbsp;<span style="color: #800000; font-weight: bold; ">1</span>,<span style="color: #800000; font-weight: bold; ">1</span></div>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">试试提交呗？</span><span style="font-family:Calibri">Wrong answer</span><span style="font-family:SimSun">，为什么？看条件约束啊，没有第二要返回</span><span style="font-family:Calibri">null</span><span style="font-family: SimSun">，我看到</span><span style="font-family:Calibri">null</span><span style="font-family:SimSun">的第一直觉是通过</span><span style="font-family: Calibri">join</span><span style="font-family:SimSun">搞到</span><span style="font-family:Calibri">null</span><span style="font-family: SimSun">值，于是有了下面的</span><span style="font-family:Calibri">ac sql</span><span style="font-family:SimSun">：</span></p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">select</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #FF00FF; ">max</span>(Salary)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;SecondHighestSalary<br /><span style="color: #0000FF; ">from</span>(<br /><span style="color: #0000FF; ">select</span><br />o1.<span style="color: #808080; ">*</span><br />,<span style="color: #FF00FF; ">case</span>&nbsp;<span style="color: #0000FF; ">when</span>&nbsp;o2.s&nbsp;<span style="color: #0000FF; ">is</span>&nbsp;<span style="color: #0000FF; ">null</span>&nbsp;<span style="color: #0000FF; ">then</span>&nbsp;<span style="color: #800000; font-weight: bold; ">1</span>&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;<span style="color: #800000; font-weight: bold; ">0</span>&nbsp;<span style="color: #0000FF; ">end</span>&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;nt<br /><span style="color: #0000FF; ">from</span><br />(<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee)o1<br /><span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span><br />(<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #FF00FF; ">max</span>(Salary)&nbsp;<span style="color: #0000FF; ">as</span>&nbsp;s&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Employee)o2<br /><span style="color: #0000FF; ">on</span>(o1.Salary<span style="color: #808080; ">=</span>o2.s)<br />)t<br /><span style="color: #0000FF; ">where</span>&nbsp;nt<span style="color: #808080; ">=</span><span style="color: #800000; font-weight: bold; ">1</span></div>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">思路简单说就是通过全表左外联最大</span><span style="font-family:Calibri">salary</span><span style="font-family:SimSun">，从关联不到的</span><span style="font-family: Calibri">salary</span><span style="font-family:SimSun">里再找最大不就是第二大吗？</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">最后的结果是</span><span style="font-family:Calibri">894ms</span><span style="font-family:SimSun">，当然我坚信有很多更快更高效的结果。</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422478.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-27 16:38 <a href="http://www.blogjava.net/changedi/archive/2015/01/27/422478.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Leetcode-Database-175-Combine Two Tables-Easy</title><link>http://www.blogjava.net/changedi/archive/2015/01/27/422477.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 Jan 2015 08:23:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2015/01/27/422477.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/422477.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2015/01/27/422477.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/422477.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/422477.html</trackback:ping><description><![CDATA[<p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">大概上周看到</span><span style="font-family:Calibri">leetcode</span><span style="font-family:SimSun">开始做数据相关的挑战题目，目前是基于</span><span style="font-family:Calibri">MySQL</span><span style="font-family: SimSun">的</span><span style="font-family:Calibri">Sql</span><span style="font-family:SimSun">测试题目。作为一个现在</span><span style="font-family: Calibri">hive sql</span><span style="font-family:SimSun">占掉大部分工作时间的码农，还是可以选择来练练手，今天立帖把这些题目一一解决。</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">第一题的描述：题目地址：<span style="font-family: verdana, 'courier new';"><a href="https://oj.leetcode.com/problems/combine-two-tables/">https://oj.leetcode.com/problems/combine-two-tables/</a></span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">一张表叫做</span><span style="font-family:Calibri">Person</span><span style="font-family:SimSun">，主键是</span><span style="font-family:Calibri">PersonId</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+-------------+---------+<br /> | Column Name | Type&nbsp;&nbsp;&nbsp; |<br /> +-------------+---------+<br /> | PersonId&nbsp;&nbsp;&nbsp; | int&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | FirstName&nbsp;&nbsp; | varchar |<br /> | LastName&nbsp;&nbsp;&nbsp; | varchar |<br /> +-------------+---------+</span></p>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">另一张表叫做</span><span style="font-family:Calibri">Address</span><span style="font-family:SimSun">，主键是</span><span style="font-family:Calibri">AddressId</span></p>  <p style="margin:0in;font-family:Menlo;font-size:9.75pt;color:#333333"><span style="background:whitesmoke">+-------------+---------+<br /> | Column Name | Type&nbsp;&nbsp;&nbsp; |<br /> +-------------+---------+<br /> | AddressId&nbsp;&nbsp; | int&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | PersonId&nbsp;&nbsp;&nbsp; | int&nbsp;&nbsp;&nbsp;&nbsp; |<br /> | City&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | varchar |<br /> | State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | varchar |<br /> +-------------+---------+</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">题目要求写一个</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">完成查询任务：把</span><span style="font-family: Calibri">Person</span><span style="font-family:SimSun">表中每个人的</span><span style="font-family:Calibri">FirstName</span><span style="font-family: SimSun">，</span><span style="font-family:Calibri">LastName</span><span style="font-family:SimSun">，</span><span style="font-family:Calibri">City</span><span style="font-family:SimSun">和</span><span style="font-family:Calibri">State</span><span style="font-family: SimSun">都查询出来。</span></p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">这明显是个非常简单的</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">，只要拿</span><span style="font-family:Calibri">Person</span><span style="font-family:SimSun">表做左表进行</span><span style="font-family:Calibri">left outer join</span><span style="font-family:SimSun">即可（当然做右表进行</span><span style="font-family: Calibri">right join</span><span style="font-family:SimSun">也行）</span></p>  <div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #0000FF; ">select</span><br /><span style="color: #008080; ">&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o1.FirstName<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,o1.LastName<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,o2.City<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;,o2.State<br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #0000FF; ">from</span>(<br /><span style="color: #008080; ">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Person<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;)o1<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #FF00FF; ">left</span>&nbsp;<span style="color: #0000FF; ">outer</span>&nbsp;<span style="color: #0000FF; ">join</span>(<br /><span style="color: #008080; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">select</span>&nbsp;<span style="color: #808080; ">*</span>&nbsp;<span style="color: #0000FF; ">from</span>&nbsp;Address<br /><span style="color: #008080; ">11</span>&nbsp;)o2<br /><span style="color: #008080; ">12</span>&nbsp;<span style="color: #0000FF; ">on</span>(o1.PersonId&nbsp;<span style="color: #808080; ">=</span>&nbsp;o2.PersonId)</div>  <p style="margin:0in;font-family:Calibri;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-size:10.5pt"><span style="font-family:SimSun">最后，啰嗦几句，在我们现在的软件开发过程中，数据处理应该是一个工程师必备的技能，身在大公司，可能</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">的编写有很多的限制，有的甚至不需要工程师来编写，直接交给</span><span style="font-family:Calibri">DBA</span><span style="font-family:SimSun">就行了。在这样的环境下，开发工程师往往丢掉了数据库的基本功。而在大数据的浪潮下，在</span><span style="font-family:Calibri">hive\pig\ODPS</span><span style="font-family:SimSun">下编写</span><span style="font-family:Calibri"> sql </span><span style="font-family:SimSun">也成了数据开发工程师的基本技能，数据开发不能仅仅停留在写</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">实现功能，最重要的是理解</span><span style="font-family:Calibri">Hadoop</span><span style="font-family:SimSun">生态下，各种</span><span style="font-family: Calibri">sql</span><span style="font-family:SimSun">语句的原理。就像开发一样，要写出最高效的</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">来处理数据。我相信</span><span style="font-family:Calibri">leetcode</span><span style="font-family:SimSun">对于</span><span style="font-family:Calibri">sql</span><span style="font-family:SimSun">的挑战会是一个不错的平台，大家加油</span><span style="font-family:Calibri">~~</span></p> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div> <div id="haloword-lookup" class="ui-widget-content"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/422477.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2015-01-27 16:23 <a href="http://www.blogjava.net/changedi/archive/2015/01/27/422477.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>初探IMEI【译】</title><link>http://www.blogjava.net/changedi/archive/2014/11/27/420789.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Thu, 27 Nov 2014 09:30:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/11/27/420789.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/420789.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/11/27/420789.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/420789.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/420789.html</trackback:ping><description><![CDATA[<p>一直想清楚的理解IMEI是什么,但是怎么也找不到合适的下笔思路,最终还是把imei.org上的这篇介绍翻译过来，做个记录。 </p><p>原文地址：<a href="http://imei.org/2013/05/imei-number-decode/">http://imei.org/2013/05/imei-number-decode/</a> 原文标题：<strong>What Is IMEI Number and How To Decode It</strong> </p><p>译文： </p><p>&#8220; </p><p>你可能听说过IMEI，也知道它是移动设备的标识ID，但是你知道它的具体功用吗？IMEI自手机出产后就伴随其一生，究竟什么是IMEI，如何理解它的数字含义？ </p><p>智能手机是全世界范围内偷窃事件的&#8220;主角&#8221;，不要低估IMEI的重要性，把IMEI备份一下是一个明智的决定。本文会解释为什么我们的手机需要IMEI，同时会列出如何找到并记录IMEI的技术方法。 </p><h2>IMEI是什么？</h2> <p>IMEI的全称是International Mobile Equipment Identity，每台设备都必备的一个唯一标识，用来区分设备与设备。当你购买一台新的手机设备时，你会在收据上看到IMEI；当你需要修理手机时，手机服务商可能会要求你提供IMEI。标准的IMEI是一个14位数字，同时也有IMEI/SV这样的16位数字形式（仅新设备有），SV是由软件设计的。当然苹果的iPhone GSM有 15位数字，而CDMA是14位数字。不管如何，IMEI的设计动机不仅仅是一个ID标识，它也可以用来阻止网络访问，还可以用IMEI来进行运营商解锁。 </p><p>如果你的手机失窃，你可以将情况上报到你的手机服务商，他们会阻止这台手机进行一切的网络访问，同时警察也可以通过IMEI来识别丢失的设备。 </p><h2>IMEI怎么查看？</h2> <p>多数手机在你输入拨号*#06#后就能看到IMEI了，当然也有其他的一些方法： </p><ul> <li>iOS：Settings-&gt;General-&gt;About（设置-&gt;通用-&gt;关于手机），这样也可以看到有IMEI（需要自己下滑寻找）。iPhone手机同样可以在sim卡托盘上找到IMEI，当然如果你不是使用原生的托盘就看不到了。</li></ul> <ul> <li>Android：Settings-&gt;About（设置-&gt;关于），这里可以看到IMEI，序列号和其他的信息。 </li><li>老的Sony或者索爱：输入 * Right * Left Left * Left * </li><li>新的索爱或者Blackberry：Options-&gt;Status（选项-&gt;状态）</li></ul> <p>你可以使用<a href="http://imei.org/check-iphone-carrier/">http://imei.org/check-iphone-carrier/</a> 服务通过输入IMEI来获得手机的运营商信息 </p><h2>如何解释IMEI：</h2> <p>2004年以来，统一的IMEI格式为：AA-BBBBBB-CCCCCC-D，这是一个15位数字号，其中： </p><ul> <li>AA：两位数字号，表示Reporting Body Identifier，用来表示由TAC（Type Allocation Code）分配的GSMA。 </li><li>BBBBBB：TAC(FAC)的剩余部分。 </li><li>CCCCCC：机器序列号（SNR） </li><li>D：Luhn 检测位</li></ul> <p>举例来说，现在iPhone 5的TAC为01-332700，而三星的Galaxy S2为35-853704，而C部分的SNR是由手机制造商自定义的生产序列号，最后一位校验位是通过算法来生成的。 </p><p>IMEI是手机相关的，与sim卡无关，当你的手机被偷后，无论手机重置还是更换sim卡，IMEI都是不变的，这时你是可以通过联系你的手机服务商来锁住手机的服务的。如果这行不通，可以联系你所在地区的运营商来锁定IMEI对于运营商网络的访问。 </p><p>有时候IMEI是变化的，尽管这不合法。有些窃贼有能力将合法的新的IMEI安装到你的手机里从而重新启用这个手机。另外鉴于犯罪动机，还有人会利用IMEI来监听设备。 </p><p>综上，IMEI是手机的重要的唯一性ID，你需要去备份并记住它。 </p><p>&#8221; </p><p>翻译结束。 </p><p>最后附加一些说明。 </p><p>关于TAC：http://en.wikipedia.org/wiki/Type_Allocation_Code </p><p>关于最后一位校验位的算法和python code： </p><p>以14位IMEI为例，校验位假设是C </p><ol> <li>从后向前，记录下每隔两位的数字，记为列表A </li><li>从后向前，记录下除A以外的数字，记为列表B </li><li>将A中的数字都乘以2，如果一个数字乘以2后大于10，那么把这个结果拆为两个数字（个位数一个，十位数一个），记为列表A' </li><li>把A'和B中的所有数字求和，记为S </li><li>计算S*9%10，记为F，如果F等于C，校验通过，否则IMEI有问题。</li></ol> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">if</span> len(arg0)==15:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>             check_bit = <span style="color: #0000ff">int</span>(arg0[-1])</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>             i = len(arg0)-2</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>             l,r = [],[]</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>             <span style="color: #0000ff">while</span> i&gt;=0:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>                 m = <span style="color: #0000ff">int</span>(arg0[i])*2</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>                 <span style="color: #0000ff">if</span> m&lt;10:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>                     l.append(m)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>                 <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                     l.append(m%10)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>                     l.append(m/10)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>                 r.append(<span style="color: #0000ff">int</span>(arg0[i-1]))</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>                 i-=2</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>             l.reverse()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>             r.reverse()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>             <span style="color: #0000ff">if</span> sum((sum(l),sum(r)))*9%10==check_bit:</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>                 <span style="color: #0000ff">return</span> True</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>         <span style="color: #0000ff">return</span> False</pre><!--CRLF--></div></div> <div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><img src ="http://www.blogjava.net/changedi/aggbug/420789.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-11-27 17:30 <a href="http://www.blogjava.net/changedi/archive/2014/11/27/420789.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最近在看NFL</title><link>http://www.blogjava.net/changedi/archive/2014/10/27/419100.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 27 Oct 2014 09:27:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/10/27/419100.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/419100.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/10/27/419100.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/419100.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/419100.html</trackback:ping><description><![CDATA[<p>最近有2个月放空，没有沉淀一些技术上的内容，反而是调整一个工作状态，把更多的时间还原到了生活和娱乐中。当然，就目前来看，这种适当的放空收到了奇效，当时考虑业务的前景时非常的不明朗，甚至对数据质量本身的要求极度偏激，导致对团队的成员有偏见。不过在适当的休息后，这种感觉没有了，取而代之的是清晰的业务方向和思路。</p> <p>为什么会这样，我想这个和我最近痴迷橄榄球NFL有关（怎么这个句式特别像广告呢）。年初的超级碗全场录像把橄榄球运动拉入了我的视野，随着2014-2015NFL新赛季的开始，我也开始关注NFL，美式橄榄球是项非常棒的运动，为什么这么说？运动本身有几个特点非常吸引人：1）高强度身体对抗——无疑这是一项男人的运动，比赛中最常见的就是身体接触。2）规则和体系非常合理，橄榄球的进攻防守规则是非常合理的，进攻组司职攻城略地，而防守组负责守卫家园，这个对于原始的战争游戏模拟的非常到位。3）策略和战术要求极高，橄榄球进攻和防守战术非常多样化，进攻端可以采用地面冲球和传球，传球的话又可以分远距离、中距离和近距离传球，传球的对象，也可以是外接或者跑卫，外接的跑动路线……说不完啊。4）数据拟合准确，当然这是让我最痴迷NFL的原因，美式橄榄球把数据运用到了极致，对于球员的指标衡量非常的准确形象。</p> <p>比起拳击那样的攻击与防御，它显得安全了很多；比起网球、乒乓球这样的球类运动，就相当的刺激荷尔蒙；比起足球和篮球这样的流行运动，橄榄球的规则和完整的体系以及无可出其右的策略性，是最吸引人的。当然足球的魅力在于团队配合可以打得非常精彩，同时个人英雄的出现也可以拯救世界，但是足球里衡量运动员综合素质的指标体系实在是不够好。我个人认为像橄榄球和棒球（NFL和MLB）是最好的数据决策运动体系，好笑的是这都是美国人为之疯狂的运动，而在世界上其他国家却知之甚少。</p> <p>结合橄榄球运动和数据体系构建，确实给了数据从业人员很大灵感，玩NFL Fantasy的玩家都知道每周的Pick’Em可以来预测比赛，要知道一周15场比赛完全猜对的人非常非常多，为什么？因为数据刻画形象且透明，也就是说，我们做预测，不在于算法有多牛逼，而在于指标是否足够好，模型是否足够清晰的能刻画业务本身。这是我一直在做数据挖掘强调的，但是仍然有太多太多的人纠结在算法中不能自拔……NFL有一套自己的rating算法来评估球员的素质，当然这在FM游戏里也有，但是我个人认为NFL的计算更合理，因为橄榄球运动目标性非常强，没有像足球那样过多的回传和组织，橄榄球单箭头的向前就是最好的约束条件，这为数据可操作性提供了基本保障，我个人是这么认为的。另外NFL联盟的运作也类似一个数据体系，联盟约定工资帽，不会出现过分的强弱队之分，一个队伍都是明星大牌，工资帽不允许啊，所以NFL的32支球队，大体水平相当（当然有弱队和强队，这里说的大体相当是指在一场NFL比赛预测中，没有那么明显的胜负趋势，优势微弱=属性连续、分布完美）。另外不同的战术相生相克，在场上随时可以通过暂停来及时调整，这是小步快跑实时运算的等价比喻，足球比赛一开场就至少踢45分钟，这是典型的批处理，教练有个意图都是大声喊，也不见得球员遵循，但是橄榄球就完全不同，每次攻守基本都是战术的完整体现。说起时间，因为足球篮球是连续比赛，而棒球和橄榄球则是分档进攻，这其中的区别体现在数据的可分析性以及一次操作的影响性分析上，其实是很明显的。足球的连续比赛，导致我们在分析一次进攻的成功因素时，需要考虑的变量非常的多，因为时间窗口太大——45分钟，而橄榄球是阶段性的，一次成功的推进，一定是经历一个开球-传球或跑球这样的流程，这明显是规格化的产物，谁告诉你足球进球一定要是本方守门员大脚开球到前锋射门呢？所以对于橄榄球这种阶段性运动，一次分析可以根据本身的进攻和防守阶段被拆成很多的细片，在细片上完成分析，这也符合分析的规律——样本干净且完整。这有点像分布式计算，橄榄球天生分布式，而足球只能一把搞到内存里做完全分析……</p> <p>胡乱扯了一通NFL的东西，美式橄榄球运动是项极度体现智慧的运动，结合数据去玩Fantasy，更觉得其乐无穷。</p> <p>回头有空，再详细举例NFL的数据故事。没准也还聊聊手游和数据的事。</p><img src ="http://www.blogjava.net/changedi/aggbug/419100.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-10-27 17:27 <a href="http://www.blogjava.net/changedi/archive/2014/10/27/419100.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>决策树和Random Forests&amp;mdash;&amp;mdash;优秀的群体智慧</title><link>http://www.blogjava.net/changedi/archive/2014/08/27/417386.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 27 Aug 2014 02:12:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/08/27/417386.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/417386.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/08/27/417386.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/417386.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/417386.html</trackback:ping><description><![CDATA[<p>What is Decision Tree:  <p>说起决策树，就好像是一个ML算法的标配一样，在每本ML的教程里，每个ML的代码库里，每个ML的软件包里，都有决策树的存在。个人认为决策树是一种人类本能的规则判别的算法表达，当我们遇到一个分类问题时，本能的会以一些IF-THEN规则去划分，这是本质的关联和分类的思维在起作用。比如我们在做决策时总会找一些阈值x，大于x该怎么做，小于x又该怎么做。或者我们换句话说，决策树是一种归纳推理的典型，而我们数学推理的模式无非是归纳和演绎。  <p>决策树分类是希望通过数据集中各个属性的特点，归纳得到一组规则作为分类条件，而这些规则是通过一棵树来展现的，每个节点都是一个规则，得到规则后在遇到新的数据实例时，只要从树根按照顺序遍历树，评估每个节点的规则条件，最后到达叶子时就得到了决策。我们就拿Quinlan论文中的例子来讲什么是决策树。下面的表是一个训练数据集，其中表示了每天的一个天气状况（有4个特征维度：forecast, temperature, humidity, windy），而需要给出的决策结论(class)是当天是否要play tennis。而下面的图就是决策树。  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image001%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[10]" border="0" alt="clip_image001[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image001%5B10%5D_thumb.png" width="244" height="147"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image002%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002[10]" border="0" alt="clip_image002[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image002%5B10%5D_thumb.png" width="244" height="135"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image003%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image003[10]" border="0" alt="clip_image003[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image003%5B10%5D_thumb.png" width="244" height="151"></a>  <p>其实稍加推断，不难发现，根据训练集去构建决策树，树的数目是不唯一的，甚至是无限多的（当然前提是数据集足够充分），比如上面就有两棵决策树，它们都是正确的，但是从复杂程度上来看有了区分。但是我们构建分类器的目的是要尽可能的具备优秀的泛化能力，即除了对训练数据集有好的效果，对于未知数据的分类要准确这才是我们想要的结果。基于此，我们构建决策树不是随便的，我们倾向于更简单的树，更简单的规则来分类数据，因为这样明显具有更好的泛化能力（显而易见）。具体如何构建呢？决策树算法有成千上万种，paper也遍地都是，但是ID3算法是构建决策树的最经典的算法之一，也是Quinlan的经典作。  <p>ID3是一个迭代的算法，自顶向下构建决策树，步骤可以分作3步：  <ol> <li>在所有没有选定的属性中，计算每个属性的信息增益</li></ol> <ol> <li>选择信息增益最大的特征作为节点</li></ol> <ol> <li>对于选定的节点特征，选择其所有的可能取值作为子节点，重复步骤a，直到叶子节点的熵为0或者属性节点全部被计算过</li></ol> <p>直白的解释是，构建树嘛，就要不断的选好节点和子节点咯，选的规则嘛，很简单——信息增益，为啥是信息增益？信息增益怎么算？这要引出一段故事了：  <p>熵是信息论中广泛使用的度量标准，表述的是数据集的纯度，在信息论中的解释是对信息编码的最少二进制位数。  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image004%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004[10]" border="0" alt="clip_image004[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image004%5B10%5D_thumb.png" width="375" height="46"></a>  <p>以上面的数据集为例，14个数据实例的样本集S，有9个P，5个N，所以它p(class:P)=9/14，p(class:N)=5/14，在class上的熵为-(9/14)log2(9/14)-(5/14)log2(5/14)=0.94。熵值在0~1之间分布，熵越小说明数据集越纯。因为分类树的目的是找到规则属性来尽量的划分不同类别，那么也就是说如果能找到一个属性来划分数据集从而使期望的熵最小，则这个节点的属性就是好的，表示这个属性的这种程度被定义为信息增益（information gain）  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image005%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image005[10]" border="0" alt="clip_image005[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image005%5B10%5D_thumb.png" width="426" height="39"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image006%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006[10]" border="0" alt="clip_image006[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image006%5B10%5D_thumb.png" width="978" height="77"></a>  <p>Values(Windy)=True, False  <p>S = [9 P, 5 N]  <p>=[3 P, 3 N] <a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image007%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image007[10]" border="0" alt="clip_image007[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image007%5B10%5D_thumb.png" width="220" height="23"></a>  <p>=[6 P, 2 N] <a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image008%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008[10]" border="0" alt="clip_image008[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image008%5B10%5D_thumb.png" width="210" height="23"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image009%5B11%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image009[11]" border="0" alt="clip_image009[11]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image009%5B11%5D_thumb.png" width="608" height="34"></a>  <p>同样的道理我们计算所有的属性的信息增益得到  <p>Gain(S,Outlook)=0.246, Gain(S,Humidity)=0.151, Gain(S,Temprature)=0.029  <p>属性Outlook具有最大信息增益，所以第一个节点就选Outlook，而剩下的过程照此迭代，于是得到了最后的决策树就如上面图片中较简单的树形。当然作为衡量属性优劣的度量标准除了信息增益外还有增益比率（gain ratio）和基尼指数（gini index）等。  <p>决策树的分支条件除了上述的方法，还有通过做转移函数来进行斜分的和基于最近邻的图的方式，具体几种划分的几何表示见下图。我们一般的划分数据方法也是采用第一种。  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image010%5B11%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010[11]" border="0" alt="clip_image010[11]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image010%5B11%5D_thumb.png" width="656" height="210"></a>  <p>决策树学习算法包含了3个部分：特征选择、树构建和剪枝，特征选择可以帮助我们构建更简单有效的树，这个过程其实在很多的机器学习算法中都必不可少，正如Quinlan在论文中提到的噪音问题和丢失值问题，这些都会影响树的构建。而树构建由上面介绍的ID3算法可知是一个启发式搜索过程，如果采用暴力的做法，那么构建决策树是一个需要搜索出所有可能的树然后选择最优的问题，这是NP的，所以全局最优的结论是无法做到的，只能通过像ID3这样找到合适的启发准则（信息增益）来做局部最优搜索（贪心搜索）。顺序搜索带来的问题就是过拟合和局部最优性，我们对于一个训练集进行ID3算法，很难找到一个泛化能力强的树形model，所以这也顺其自然的引出了剪枝的过程。剪枝就是把一些不重要的分支砍掉从而达到简化model的目的，同时这也满足决策树在归纳偏置上的倾向——简单胜于复杂。剪枝可以看做是构建的一个逆过程，自底向上的剔除不“合格”的分支，而“合格”的定义取决于代价函数，代价函数定义为  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image011%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image011[10]" border="0" alt="clip_image011[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image011%5B10%5D_thumb.png" width="326" height="46"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image012%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image012[10]" border="0" alt="clip_image012[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image012%5B10%5D_thumb.png" width="908" height="23"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image013%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image013[10]" border="0" alt="clip_image013[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image013%5B10%5D_thumb.png" width="325" height="41"></a>  <p>如果把代价函数的第一项表示为  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image014%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image014[10]" border="0" alt="clip_image014[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image014%5B10%5D_thumb.png" width="307" height="43"></a>  <p>那么最后的代价函数形式为  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image015%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image015[10]" border="0" alt="clip_image015[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image015%5B10%5D_thumb.png" width="331" height="22"></a>  <p>这里C(T)表示预测误差，|T|表示模型的复杂度。剪枝的算法描述如下：  <ol> <li>计算节点的经验熵  <li><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image016%5B11%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image016[11]" border="0" alt="clip_image016[11]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image016%5B11%5D_thumb.png" width="863" height="40"></a>  <li>重复步骤b直到得到最小子树。</li></ol> <p>经过剪枝的决策树将原有的可能过拟合和有点复杂的树缩减到一棵简单树，完成了模型的泛化。  <p>在应用ID3决策树时需要考虑的问题还有很多，比如缺失值、噪声数据、数据属性要求是nominal或者是enum等等，而Quinlan后面的C4.5算法作为ID3的升级版优化解决了部分问题，比如对于特征属性已经可以兼容连续数值型属性，使用信息增益比率来代替信息增益作为度量标准等等。因为决策树是随机森林的基础，所以特地结合教科书和Quinlan的paper做一个简单介绍，由于发展历史较为悠久，所以变种的决策树多如牛毛，决策树算法的各个阶段都有非常多的paper改进，包括各种剪枝方法理论、树的大数据挑战和可视化等等，感兴趣的话自行查阅搜索，后面可能会单独介绍CART和C4.5这两种树的算法并对比ID3,C4.5和CART。  <p>What is Random Forests:  <p>随机森林是一个组合分类的方法，和bagging和boosting一样都非常常用，组合分类的思想就是将k个学习模型组合在一起，从而创建一个复合分类模型，最后通过投票来决定分类决策。随机森林的一个通俗解释就是：由一组决策树构建的复合分类器，它的特点是每个决策树在样本域里使用随机的特征属性进行分类划分。最后分类时投票每棵分类树的结论取最高票为最终分类结论。  <p>如上所述，如果我们理解随机森林是一组决策树构成的，那么我们需要首先找到构建决策树的方法，同时我们也需要一个判别函数去将各个决策树的分类结果汇总起来并且能保证他们的准确度。首先，如何构建这些决策树呢，显然不能用同样的数据集和同样的特征向量，否则构建n个决策树和构建一个是等价的。那么随机化如何呢？随机化是ML的一个很重要的方法，也是一个很有用的工具，例如交叉验证、随机抽样等等。一种方法来构建决策树就是随机的选择n个特征子集来训练n个决策树，那如上所说，树构建好后需要判别函数来把结果整合，定义判别函数之前我们先看一个后验概率  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image017%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image017[10]" border="0" alt="clip_image017[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image017%5B10%5D_thumb.png" width="442" height="57"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image018%5B11%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image018[11]" border="0" alt="clip_image018[11]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image018%5B11%5D_thumb.png" width="908" height="42"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image019%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image019[10]" border="0" alt="clip_image019[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image019%5B10%5D_thumb.png" width="446" height="31"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image020%5B10%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image020[10]" border="0" alt="clip_image020[10]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/RandomForests_8FFD/clip_image020%5B10%5D_thumb.png" width="919" height="57"></a>  <p>随机森林是集体智慧的象征，通过随机分配一些特征向量来各自学习最后选举结果，准确率据说可以媲美AdaBoost，且对错误和利群点更鲁棒，通过调整森林中树的个数可以使泛化误差收敛，解决过拟合问题。同时调整个体树的属性选择可以提高准确率，通常的选择方法是log2d+1个属性。  <img src ="http://www.blogjava.net/changedi/aggbug/417386.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-08-27 10:12 <a href="http://www.blogjava.net/changedi/archive/2014/08/27/417386.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Logistic Regression&amp;mdash;&amp;mdash;用线解决问题</title><link>http://www.blogjava.net/changedi/archive/2014/08/05/416565.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 05 Aug 2014 01:38:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/08/05/416565.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/416565.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/08/05/416565.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/416565.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/416565.html</trackback:ping><description><![CDATA[<p>LR一般用于预测二分类问题，即yes no和0\1问题，我们构建了X(设备属性和行为属性)，我们需要预测Y(是否异常)，而反作弊项目中，我们需要预测的就是设备作弊or正常,即0\1问题。选择LR一个很重要的原因是我们相信X和Y是有联系的，不同的类型的Y会有不同的X表现。  <p>而X和Y是否有线性关系？ 普通线性回归(Ordinary Linear Regression)是否就能满足需求？我们需要先论证这个问题，我们看一下普通线性回归，模型是这样的：  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image001%5B8%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[8]" border="0" alt="clip_image001[8]" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image001%5B8%5D_thumb.png" width="244" height="16"></a>  <p>在线性回归中，随机误差项服从正态分布。这也意味着回归的Y值是个连续变量，不能是0\1这样的binary变量。另外一个解释就是线性回归对响应变量Y的要求是有常数方差，这也限定了Y的分布，就比如二项分布的Y就没有常数方差。这也正符合我们常规的阅读理解，回归用于定量分析，更多的在拟合和预测量上，而分类是个定性分析。正因为OLR的做法限制太多，因此有了通用线性回归的方法（General Linear Regression），通用方法的思路就是寻求一个转换函数g来作用于输入变量X，从而摆脱对Y的分布限制。具体表现为  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image002_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image002_thumb.png" width="244" height="18"></a>  <p>下面两张图对比了线性回归和逻辑回归数据的特点，也说明了如果用线性回归来分类binary数据是一个什么样的效果。  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image003_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image003" border="0" alt="clip_image003" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image003_thumb.png" width="244" height="244"></a><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image004_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image004_thumb.png" width="244" height="244"></a><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image005_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image005" border="0" alt="clip_image005" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image005_thumb.png" width="244" height="244"></a>  <p>&nbsp; <p>其实比较朴素的理解就是，我们把要分类的数据划分为0和1两类，那么数据其实是会以中间这幅图的形式分布的，必然存在一个平面（直线）把数据划分。而我们希望找到一个判别函数能完成区分，显而易见的是直线无法完成这个事情。  <p>面对线性回归的劣势，我们理解回归的问题在于无法对公式（1）的结果做0\1划分，线性回归的方法做分类，就是要求  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image006_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image006_thumb.png" width="244" height="16"></a>  <p>而逻辑回归作为GLR的一个特殊情况，定义了一函数f()，这个函数可以将Y做0\1处理，从而使线性回归变得有意义。而逻辑回归的做法就是找到了这样一个函数，这个函数也叫logit函数  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image007_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image007" border="0" alt="clip_image007" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image007_thumb.png" width="244" height="31"></a>  <p>其中我们把p等价于原来的 ，解释一下就是p代表Y=1的概率，标准说法p是Y=1时Y的期望。然后把这个p替换掉就得到了  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image009_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image009" border="0" alt="clip_image009" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image009_thumb.png" width="244" height="21"></a>  <p>目标是求解，于是  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image011_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image011" border="0" alt="clip_image011" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image011_thumb.png" width="244" height="18"></a>  <p>这里就不是一个线性函数了，上面提到的公式（2）对应的曲线上面最右边的图——可以看到p的函数曲线，这样的划分对于0\1数据的分类是非常合适的。  <p>那么一个问题就随之而来，logit函数是怎么来的？要想知道答案，我们需要先分析一下这个形式，我们定义这个形式为“优势”（odds），odds有时候也叫做“几率”，odds的函数形式其实是下面左图的样子，而logit函数的形式是右图的样子  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image014_2.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image014" border="0" alt="clip_image014" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image014_thumb.jpg" width="244" height="198"></a><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image015_2.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image015" border="0" alt="clip_image015" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image015_thumb.jpg" width="244" height="201"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image015_2.jpg"></a>&nbsp; <p>odds的理解其实要结合概率的思想，我们认为一个事件发生的概率是p，那么不发生的概率就是1-p，而odds表达的就是事件发生与不发生（或者一个标注yes 或者no）的几率，比如一个事情发生的概率是0.9，而不发生是0.1，那么odds（发生）=0.9/0.1=9，而odds(不发生)=0.1/0.9=0.1111...。这说明一种事物相比较时的优势，但是这个优势在形式上非对称，这样是不完美的，也是难于理解的，毕竟对于一个0-1之间的概率比较，结果却在0-∞之间。而可以看出logit函数是把odds对数化后得到的一个对称的美妙的曲线，拥有我们想要的所有性质，并且解决了OLR所限制的问题，而且由公式（3）我们也能看到，logit函数使结果限定在0-1之间，且是输入变量X的线性表示。  <p>最后当我们计算出p，我们就能确定分类，比如p&gt;0.5时 y=1，而p&lt;0.5时y=0。于是这样的分类任务就结束了。不过我们再看看模型，似乎忘记了什么，我们可以从数据集里得到X和Y，但是对于α和β以及ε的值（参数值），我们需要获取到。而这才是LR作为机器学习方法最关键的一个部分。  <p>我们假设数据集有n个数据样本，且n大于估计的参数的个数，于是我们令  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image016_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image016" border="0" alt="clip_image016" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image016_thumb.png" width="244" height="16"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image017_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image017" border="0" alt="clip_image017" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image017_thumb.png" width="244" height="16"></a>  <p>我们用β表示整个参数向量，那么原来的表达式可以写为Y=X*β。类似最小二乘法的思路，我们有下面的推断  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image018_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image018" border="0" alt="clip_image018" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image018_thumb.png" width="185" height="22"></a>  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image019_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image019" border="0" alt="clip_image019" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image019_thumb.png" width="244" height="18"></a>  <p>其实如果把目标约定为计算β,那么问题也就被归约为一个参数估计问题，而我们最常用的方法就是极大似然估计法。这里不具体介绍MLE，换种角度我们从头开始设计这个分类器。  <p>之前考虑的点我们简化了分析，我们理所当然的认为X特征向量彼此是同等重要的，而事实往往不是这样，因此模型需要加入一个权重影响因子，也就是LR模型被扩展为Weighted Logistic Regression，因为目标是计算公式（5），加入权重后，我们把评估公式（5）的做法替换为去评估  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image020_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image020" border="0" alt="clip_image020" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image020_thumb.png" width="244" height="16"></a>  <p>这里就涉及一个问题：权重具体是个什么定义？权重W是个对角矩阵，其对角元素即每个特征项的权重值，而这个权重定义为预测变量的标准差。此时我们回到一个基本问题，LR用来预测二类问题，而我们一般假设我们的预测变量Y是服从二项分布的即，那么方差，更特殊的，对于单次试验是伯努利分布，所以有，而这个即我们对于预测变量Y的估计值，形如公式（4），换用现在对β的定义就是，回到公式（6）中，我们新引入的变量U即表示为，其中后面这个加项我们这么理解：分子是一个预测误差项，而分母是我们定义的权重，这样一个比值缩放了正确分类的大方差样本点和小方差样本点对结果的影响。另外需要引入的一个概念是岭回归（Ridge Regression），源于一些预测变量y可能是线性相关的，那么这会导致是奇异矩阵，会导致无穷解，因此在带求解的公式（6）的基础上，为可逆矩阵引入一个常量对角矩阵，公式变为  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image022_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image022" border="0" alt="clip_image022" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image022_thumb.png" width="244" height="15"></a>  <p>其中是小常量，而I是单位矩阵。这样做避免了对奇异矩阵求逆，但是带来了问题就是对预测变量结果的偏置，导致目标问题发生了小小的变化。多种方法可以帮助我们寻找一个好的，这里就不具体展开了，具体参考资料即可。  <p>最后就是算法实现了，为了避免复杂的矩阵求逆，我们把形式变一下，将公式（7）转换为求解这样一个线性系统  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image024_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image024" border="0" alt="clip_image024" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image024_thumb.png" width="244" height="18"></a>  <p>其中，。可以看出，算法的输入是特征矩阵X和U（间接得到），输出是β。求解线性系统的算法有很多，其中共轭梯度算法是非常有效的算法，下面左图描述了共轭梯度算法的实现伪代码。  <p>共轭梯度：  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image026_2.gif"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image026" border="0" alt="clip_image026" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image026_thumb.gif" width="174" height="240"></a>  <p>IRLS：  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image027_2.gif"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image027" border="0" alt="clip_image027" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/LogisticRegression_87F5/clip_image027_thumb.gif" width="201" height="240"></a>  <p>其中值得一提的是共轭梯度算法中的“is large enough”或者维基上说的“sufficiently small”有多种判断方式，具体这里就不列出了，可以看参考资料[2]。右图是迭代重加权最小二乘法，用于最终求解β。  <p>我在github上放了一个《machine learning in action》中的LR算法的python实现，对于β的求解使用的是梯度下降法，可以参考<a href="https://github.com/changedi/mlmlml/blob/master/LogisticRegression/logRegres.py">https://github.com/changedi/mlmlml/blob/master/LogisticRegression/logRegres.py</a>，而关于梯度下降等最优化算法，我想还是单独开一篇去介绍比较合适。至此，LR的基本介绍基本就结束了。  <p>&nbsp; <p>文中除流程图外，其他截图取自ref[2]，特此声明。  <p>References:  <p>[1] <a href="http://logisticregressionanalysis.com/86-what-is-logistic-regression/">http://logisticregressionanalysis.com/86-what-is-logistic-regression/</a>  <p>[2] <a href="http://www.omidrouhani.com/research/logisticregression/html/logisticregression.htm">http://www.omidrouhani.com/research/logisticregression/html/logisticregression.htm</a>  <p>[3] 《统计学习方法》，李航  <p>[4] Logit Models for Binary Data, G. Rodriguez, 2007  <p>[5] <a href="http://zh.wikipedia.org/wiki/%E5%85%B1%E8%BD%AD%E6%A2%AF%E5%BA%A6%E6%B3%95">http://zh.wikipedia.org/wiki/%E5%85%B1%E8%BD%AD%E6%A2%AF%E5%BA%A6%E6%B3%95</a>  <p>[6] <a href="http://cnx.org/content/m45285/latest/">http://cnx.org/content/m45285/latest/</a><img src ="http://www.blogjava.net/changedi/aggbug/416565.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-08-05 09:38 <a href="http://www.blogjava.net/changedi/archive/2014/08/05/416565.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（15）</title><link>http://www.blogjava.net/changedi/archive/2014/06/16/414795.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 16 Jun 2014 05:02:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/06/16/414795.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/414795.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/06/16/414795.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/414795.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/414795.html</trackback:ping><description><![CDATA[<p>今天要说的 是0/1背包问题，问题非常经典，即：给定一个袋子，容量为c，c∈N，同时有n个球A={a0,a1,…a(n-1)}，每个球有一自己的价值v(ai)，同时有一定的重量w(ai)，目标就是尽可能的装球使得总价值最高，同时重量不能超出袋子的容量c。所以形式化表示这个问题即，对于Σwixi&lt;=c，求最大max z=Σvixi。这个问题可以被看做一个多阶段决策问题，对于每个ai，都要做决策xi来判断是否要装ai，阶段标示即n个物体A的下标，那么动态规划方程如下：f(i,w)= 0 if i=-1 and 0≤w≤c，f(i,w)=-∞ if i=-1 and w&lt;0，f(i,w)=max {xivi+f(i-1,w-xiwi)} if i≥0。目标自然是计算f(n-1,c)咯。其实现在理解01背包，典型的思路就是能阶段性考虑，递归求解。不妨设c=22，n=3，v={25,24,15}，w={18,15,10}。那么结论就是只选择第一个球，最终切价值最高~~当然这个例子可能特殊了点，但是从公式上推理，我们知道这个递归方程是正确的。</p> <p>源码如下：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.HashSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.Set;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span> <span style="color: #008000"> * Knapsack01Problem.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span> <span style="color: #008000"> * </span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span> <span style="color: #008000"> * @author zunyuan.jy</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span> <span style="color: #008000"> * </span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span> <span style="color: #008000"> * @since 2014年6月13日</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> KS01 {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> knapsackCapacity = 22;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] value = { 25, 24, 15 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] weight = { 18, 15, 10 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> n = value.length; <span style="color: #008000">// number of objects n=3</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> highestIndex = n - 1; <span style="color: #008000">// items are indexed</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>     <span style="color: #008000">// from 0 through n-1</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Set&lt;Integer&gt; calculateDecisionSet(<span style="color: #0000ff">int</span> objInd, <span style="color: #0000ff">int</span> w) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         Set&lt;Integer&gt; decSet = <span style="color: #0000ff">new</span> HashSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>         decSet.add(<span style="color: #0000ff">new</span> Integer(0)); <span style="color: #008000">// decision to not take object</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>         <span style="color: #008000">// is always feasible</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>         <span style="color: #0000ff">if</span> (w &gt;= weight[objInd]) { <span style="color: #008000">// check if there is enough space</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>             <span style="color: #008000">// to take object</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>             decSet.add(<span style="color: #0000ff">new</span> Integer(1));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">return</span> decSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> currentObejctIndex, <span style="color: #0000ff">int</span> weightToGive) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>         <span style="color: #0000ff">if</span> (currentObejctIndex == -1)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>             <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>         Set&lt;Integer&gt; decisionSet = calculateDecisionSet(currentObejctIndex,</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>                 weightToGive);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>         <span style="color: #0000ff">double</span> max = 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> d : decisionSet) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>             <span style="color: #0000ff">double</span> tmp = d</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>                     * value[currentObejctIndex]</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>                     + f(currentObejctIndex - 1, weightToGive - d</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>                             * weight[currentObejctIndex]);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>             <span style="color: #0000ff">if</span> (tmp &gt; max)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>                 max = tmp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         <span style="color: #0000ff">return</span> max;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span>         System.out.println(KS01.f(2, 22));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum71">  71:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum72">  72:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum73">  73:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/414795.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-06-16 13:02 <a href="http://www.blogjava.net/changedi/archive/2014/06/16/414795.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（14）</title><link>http://www.blogjava.net/changedi/archive/2014/06/09/414530.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 09 Jun 2014 03:22:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/06/09/414530.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/414530.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/06/09/414530.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/414530.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/414530.html</trackback:ping><description><![CDATA[<p>不知不觉说了这么多问题了，但是很多最优化经典问题没有提到，今天就掀起整个动态规划系列的高潮，来个经典：线性规划问题（Linear Programming）。<a href="http://zh.wikipedia.org/wiki/%E7%BA%BF%E6%80%A7%E8%A7%84%E5%88%92">维基百科</a>里清晰的描写了什么是线性规划。一个简单的描述就是对于一个抽象后的问题，假设目标就是c1x1+c2x2，这里x1和x2都是变量，c1和c2是常量系数，给定一系列约束a11x1+a12x2≤b1，a21x1+a22x2≤b1，a31x1+a32x2≤b3；求最优的x1和x2使得c1x1+c2x2最大化。我们惯性的用线性代数来表示这个问题就是我们要求最大化的cTx，条件是Ax≤b，这里加个限制就是x是正整数（因为往往实际问题中，正数是有意义的）。对于这个问题，其实关键第一步定义状态就很难，这个问题不像以前很多问题一看就是一个图论最短路或者组合优化，我们的思路需要扩展。我们定义一个状态(j,(y1,y2,…ym))，j表示index号也就是变量x的index，而后面的m元组y就表示当前决策下各个约束的值。而在决策阶段，在阶段j做的决策就是从定义域里挑选一个D赋给x(j+1)，所以DPFE就变为：f(j,(y1,…,ym))=0 当j=n时，f(j,(y1,…,ym))=max x(j+1)∈D{c(j+1)x(j+1)+f(j+1,(y1-a(1,j+1)x(j+1),…,ym-a(m,j+1)x(j+1))} 当j&lt;n。定义域D表示为D=D(j,(y1,...,ym)) ={0,…,min{y1/a(1,j),…,ym/a(m,j)}}，方程的目标就是要求解f(0,(y1,y2,…,ym))。写到这里，花个2-3分钟仔细理解一下这个方程，就能看出，其实又是老套路，线性规划这个看上去复杂的公式也被动态规划的顺序扩展分析方法表示了，递归的选择xi来寻求最值，复杂的约束作为递归的状态不断传递……</p> <p>具体拿例子说是，假设c=(3,5)，b=(4,12,18)，并且A是约束矩阵A=[(1,0),(0,2),(3,2)]这里矩阵是行表示法即3行两列矩阵。那么经过上面的迭代求解，可以知道最优的决策组(x1,x2)=(2,6)，最大化后的函数值为c1x1+c2x2=3*2+5*6=36。</p> <p>最后我们附上解题源码：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.HashSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.Set;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #008000">//IntegerLinearProgramming;</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ILP {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #008000">// objective function coefficients</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] c = { 3, 5 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #008000">// right hand side of constraints vector</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] b = { 4, 12, 18 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     <span style="color: #008000">// constraint matrix</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[][] a = { { 1, 0 }, { 0, 2 }, { 3, 2 } };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> n = c.length;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> m = b.length;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span> infty = Integer.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Set&lt;Integer&gt; calculateDecisionSet(<span style="color: #0000ff">int</span> stage, <span style="color: #0000ff">int</span> y1, <span style="color: #0000ff">int</span> y2,</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>             <span style="color: #0000ff">int</span> y3) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         Set&lt;Integer&gt; result = <span style="color: #0000ff">new</span> HashSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>         <span style="color: #008000">// maxPossibleChoiceBecauseOfResourceiRestriction, i=1,2,3</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #0000ff">int</span> mpc1 = infty;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>         <span style="color: #0000ff">int</span> mpc2 = infty;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         <span style="color: #0000ff">int</span> mpc3 = infty;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>         <span style="color: #0000ff">if</span> (a[0][stage] != 0) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>             mpc1 = y1 / a[0][stage];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>         <span style="color: #0000ff">if</span> (a[1][stage] != 0) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>             mpc2 = y2 / a[1][stage];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">if</span> (a[2][stage] != 0) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>             mpc3 = y3 / a[2][stage];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt;= Math.min(mpc1, Math.min(mpc2, mpc3)); i++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>             result.add(<span style="color: #0000ff">new</span> Integer(i));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>         <span style="color: #0000ff">return</span> result;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>     <span style="color: #008000">// here: yi denotes how much of resource i is still available,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>     <span style="color: #008000">// (in other words how much slack is still available)</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> stage, <span style="color: #0000ff">int</span> y1, <span style="color: #0000ff">int</span> y2, <span style="color: #0000ff">int</span> y3) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>         <span style="color: #0000ff">if</span> (stage == n) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>             <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>         <span style="color: #0000ff">double</span> max = Double.MIN_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> d : calculateDecisionSet(stage, y1, y2, y3)) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>             <span style="color: #0000ff">double</span> t = c[stage]</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>                     * d</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>                     + f(stage + 1, y1 - a[0][stage] * d, y2 - a[1][stage] * d,</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>                             y3 - a[2][stage] * d);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span>             <span style="color: #0000ff">if</span> (t &gt; max)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span>                 max = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span>         <span style="color: #0000ff">return</span> max;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum71">  71:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum72">  72:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum73">  73:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum74">  74:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum75">  75:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum76">  76:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum77">  77:</span>         System.out.println(ILP.f(0, b[0], b[1], b[2]));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum78">  78:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum79">  79:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum80">  80:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/414530.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-06-09 11:22 <a href="http://www.blogjava.net/changedi/archive/2014/06/09/414530.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（13）</title><link>http://www.blogjava.net/changedi/archive/2014/06/03/414355.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 03 Jun 2014 11:24:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/06/03/414355.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/414355.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/06/03/414355.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/414355.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/414355.html</trackback:ping><description><![CDATA[<p>今天介绍的这个问题还是一个任务调度的问题(Flowshop问题)，每个过程i有两个任务A和B，其中有个约束，那就是A一定要在B之前完成，任务A和B分别由两个处理器完成，编号为1和2，每个过程i有对应自己任务的完成时间，选择如何调度这些过程，会产生不同的任务总体完成时间，问题的目标就是要找到最佳完成时间的策略。举例来讲，有4个过程为{0,1,2,3}，每个过程对应的A任务完成时间为{3,4,8,10}，B任务完成时间为{6,2,9,15}，如果处理器按照这个顺序去处理任务，依据约束条件，完成过程如下表表示：</p> <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/13_9AA8/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/13_9AA8/image_thumb.png" width="244" height="33"></a> 可以看出这个方案的调度时长需要40。</p> <p>如果用DP去求解这个问题，首先定义状态，我们定义过程d的A任务完成时间为pd，B任务完成时间为qd，那么引入一个虚拟阶段号k，k表示上一次被确认调度的A和B任务各自完成中间那段流失的时间（可能不是很好理解，这里解释一下，为什么取这个值作为状态，首先对于pd和qd这都是固定计算成本，那么导致任务不能连续运转的原因就是有A在B前完成这个约束，对于上面的例子，我们看到B3任务并不能在B2任务结束后马上开始，而一定要等到A3任务结束才可以，B2结束后到A3结束前这段时间即15-11这4个时间单位的时间就是这里的k了，定义了这个k后，我们就可以知道目标状态是要使k=0且未被调度的任务集合），定义S为剩余的过程集合，所以状态为(k,S)，这个状态对于一个过程的A和B任务如果没有延迟(k&lt;pd)，那么k=qd，反之有延迟则延迟时间为max(k-pd,0)，所以一个调度过程d开始后的时间成本会记为A任务时间pd+延迟时间max(k-pd,0)+B任务时间qd。所以DPFE即f(k,S)=min d∈S{pd+f(max(k-pd)+qd,S-{d})}。初始状态对于S是空集，那么f(k,S)=k。于是对于上面的具体问题，方程求解最优时间是38，决策序列为d1=0，d2=2，d3=3，d4=1。</p> <p>过程源码如下：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.HashSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.Set;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #008000">//Flowshop Problem</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FLOWSHOP {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] first = { 3, 4, 8, 10 }; <span style="color: #008000">// sum=25</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] second = { 6, 2, 9, 15 }; <span style="color: #008000">// sum=32</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #008000">// upper bound on final completion time is 25+32:</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> sum = 57;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> m = first.length;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Set&lt;Integer&gt; setOfAllItems = <span style="color: #0000ff">new</span> HashSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     <span style="color: #0000ff">static</span>{</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         setOfAllItems.add(0);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>         setOfAllItems.add(1);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         setOfAllItems.add(2);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>         setOfAllItems.add(3);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> fct(<span style="color: #0000ff">int</span> t, <span style="color: #0000ff">int</span> d) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>         <span style="color: #0000ff">return</span> Math.max(t - first[d], 0) + second[d];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(Set&lt;Integer&gt; set, <span style="color: #0000ff">int</span> t){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>         <span style="color: #0000ff">if</span>(set.isEmpty())</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>             <span style="color: #0000ff">return</span> t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>         <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> d : set){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>             Set&lt;Integer&gt; tmpSet = copySet(set);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>             tmpSet.remove(d);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>             <span style="color: #0000ff">double</span> tmp = first[d]+f(tmpSet,fct(t,d));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>             <span style="color: #0000ff">if</span>(tmp&lt;min){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>                 min=tmp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>             }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Set&lt;Integer&gt; copySet(Set&lt;Integer&gt; set) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>         Set&lt;Integer&gt; s = <span style="color: #0000ff">new</span> HashSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>         <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> x : set){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>             s.add(x);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         <span style="color: #0000ff">return</span> s;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span>         System.out.println(f(setOfAllItems,0));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum71">  71:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum72">  72:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum73">  73:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/414355.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-06-03 19:24 <a href="http://www.blogjava.net/changedi/archive/2014/06/03/414355.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（12）</title><link>http://www.blogjava.net/changedi/archive/2014/05/27/414150.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 27 May 2014 02:38:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/05/27/414150.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/414150.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/05/27/414150.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/414150.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/414150.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 今天介绍三个经典问题，为什么直接来三个？大概是因为这三个问题我觉得太经典了，以至于在任何人学习任何语言时都可能要实现这些题目的编程训练，所以这里就放到一起，打包讲下求解这些问题的动态规划思路（虽然有些问题不是那么直观的动态规划解）。 第一个问题是编辑距离（Edit Distance Problem)EDP问题，这个问题在维基上有全面的解释，并附有准确的代码实现（也叫levenshtein距离），我...&nbsp;&nbsp;<a href='http://www.blogjava.net/changedi/archive/2014/05/27/414150.html'>阅读全文</a><img src ="http://www.blogjava.net/changedi/aggbug/414150.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-05-27 10:38 <a href="http://www.blogjava.net/changedi/archive/2014/05/27/414150.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（11）</title><link>http://www.blogjava.net/changedi/archive/2014/05/19/413842.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 19 May 2014 05:57:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/05/19/413842.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/413842.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/05/19/413842.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/413842.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/413842.html</trackback:ping><description><![CDATA[<p>动态规划本质上的问题基本就如之前所述，之前的问题也都比较简单。复杂的动态规划问题有几种表现，其中之一就是成本函数计算复杂，状态转移过程需要考虑的细节比较多。今天讲的这个问题就满足这个条件：DPP问题（Discounted Profits Problem）</p> <p>问题是这样的：假设有一个湖，湖里有一些鱼，在第一年有b1条鱼，之后在第t年开始有bt条鱼。渔民靠捕鱼卖鱼为生，在第t年卖掉xt条鱼的话，可以有r(xt)的收益。当然捕获这些鱼也会造成c(xt,bt)的成本。鱼群每年会自然繁殖，假定繁殖规律是固定的，每年以s的比例自然增长。同样渔民手里的钱存在银行每年也会有固定利息y。请问从第一年开始到第T年，每年应该怎么捕鱼才能使利益最大化，不能竭泽而渔，当然也不能等着饿死。</p> <p>乍一看这个描述，目标很明确——最大化利益，但是条件N多，在DP求解时，要仔细考虑清楚状态转移方程中核心的优化条件。首先我们定义状态，年份t是个天然的stage维度，那么要知道利益，已知条件有收益函数、成本函数、增长比例和固定利息，共同的需求就是鱼的数量b，于是状态就是(t,b)，因为t在1~T之间（这本身也是递归收敛条件），所以DPFE方程为分段方程：f(t,b)=0当且仅当t&gt;T，当t≤T时，f(t,b)=<strong>max</strong> <em>xt∈{0,…,b}</em> {r(xt)-c(xt,b)+1/(1+y) * f(t+1,s(b-xt)} 。具体解释一下，就是我们算t年b条鱼时的利益时，假设捕xt条鱼，那么利益等价于卖鱼收益r(xt)减去捕鱼成本c(xt,b)，再加上明年收益及利息。</p> <p>看起来蛮复杂的问题，其实核心的状态定义清楚后，逻辑理清，条件虽然多，但是复杂度只增加在了目标计算上，这样的问题对于DP里来说个人认为算是不太复杂的。</p> <p>实际例子，我们假设T=2，y=0.05，s=2，初始b1=10（单位是千条），收益函数也线性化表示为3xt，成本函数简化为2xt，这样收益我们实际是在求f(1,10)，答案输出是19.05，其最优化决策是x1=0,x2=20，即第一年不捕鱼，第二年捕完所有鱼~~（这个变态的决策，有点。。。。。。）</p> <p>源码：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #008000">//Discounted Profits Problem</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #008000">//A discounted DP problem from Winston/Venkataramanan</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> <span style="color: #008000">//pp.779--780</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #008000">//Used a reproductionFactor of 2 instead of 1.2 so number of</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span> <span style="color: #008000">//fish takes on integral values, without the need to use a</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span> <span style="color: #008000">//round or a floor function.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DPP {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> T = 2; <span style="color: #008000">// planning horizon T=2 years</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> interestRate = 0.05; <span style="color: #008000">// assume 5% interest rate</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> initialFishAmount = 10; <span style="color: #008000">// initially 10,000 fish in lake</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> reproductionFactor = 2; <span style="color: #008000">// in 1 year 100% more fish</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> revenue(<span style="color: #0000ff">int</span> xt) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>         <span style="color: #008000">// simply assume that the sale price is $3 per fish, no matter what</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>         <span style="color: #0000ff">return</span> 3.0 * xt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> cost(<span style="color: #0000ff">int</span> xt, <span style="color: #0000ff">int</span> b) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>         <span style="color: #008000">// simply assume that it costs $2 to catch (and process) a fish, no</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #008000">// matter what</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>         <span style="color: #0000ff">return</span> 2.0 * xt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>     <span style="color: #008000">// t is stage number, each stage represents one year</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     <span style="color: #008000">// b is current number of fish in lake (scaled to thousands)</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> t, <span style="color: #0000ff">int</span> b) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>         <span style="color: #0000ff">if</span> (t == T + 1)<span style="color: #008000">// T+1 is outside planning horizon</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>             <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #008000">// xt is the number of fish to catch and sell</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>         <span style="color: #008000">// during year t</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>         <span style="color: #0000ff">int</span> xt;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>         <span style="color: #0000ff">double</span> max = Double.MIN_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>         <span style="color: #0000ff">for</span> (xt = 0; xt &lt;= b; xt++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>             <span style="color: #0000ff">double</span> earn = revenue(xt) - cost(xt, b) + 1 / (1 + interestRate)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>                     * f(t + 1, reproductionFactor * (b - xt));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>             <span style="color: #0000ff">if</span> (earn &gt; max)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>                 max = earn;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>         <span style="color: #0000ff">return</span> max;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         System.out.println(f(1, initialFishAmount));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/413842.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-05-19 13:57 <a href="http://www.blogjava.net/changedi/archive/2014/05/19/413842.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（10）</title><link>http://www.blogjava.net/changedi/archive/2014/05/12/413565.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 12 May 2014 08:54:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/05/12/413565.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/413565.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/05/12/413565.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/413565.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/413565.html</trackback:ping><description><![CDATA[<p>不知不觉已经写到第10个问题了，动态规划的问题相信大家也发现了一些规律，看到什么样的问题知道动态规划求解呢？基本上所有的问题都类似最短路，定义了一个状态后根据状态列动态规划方程，方程的形式一般是递归式，包含一个子结构的更新过程……接下来就是罗列问题了：第10题是算导中提过的题目，DEADLINE调度问题（其实这个问题还有一个高阶版本）。</p> <p>deadline问题的描述是：一台处理器可以处理n个作业，表示为a1,a2,…,an。每个作业aj有相等的运行时长（如果不等也作为变量呢？），有效益pj以及截止时间dj。机器是串行的，也就是说一个时刻只能处理一个作业。而且作业是原子的，不可拆分。获得效益pj的前提是aj一定要在dj之前完成。请给出一个算法来达到最优调度，即效益最大化。之前讲到，在看到最优化问题时就直接想DP求解，这个当然是个好习惯，但是这里再介绍一个思路就是优先可以先考虑贪心是否可解，相对列动态规划方程定义状态这些事情，贪心往往更直观更简单（人心不足啊）。</p> <p>这个问题是可以使用贪心法求解的，贪心算法不是我们这个系列问题的关键，但是也可以简单谈一下贪心：贪心的核心是要确定正确的贪心目标，就是在一系列决策做出时，每个步骤选择的理由是什么，比如这个问题，是按照处理时间排序后贪心？还是按照效益排序后贪心？根据目标是效益最大化，那么就可以做出正确选择了。</p> <p>当然动态规划比贪心优势的一点就是贪心是个局部策略，不是所有问题都能得到全局最优解，而DP是全局最优的，使用DP总能得到最优决策。针对本题，我们思考如何定义一个状态来列出状态转移方程。就像考虑其他类似组合优化问题时，看到有决策性步骤问题时，我们定义状态是带有步骤标号的，也就是第一章提到的stage decision，每个stage用k表示，到了第i个stage后，剩余的可决策集S作为另一个状态元素，于是我们定义状态(k,S)，其中k是stage，S是表示当前状态下剩余可选决策。为了保证S集是可选择的决策集，我们需要对deadline排序，即按照截止时间排序。这样，我们列DPFE如下：f(k,S)=max d∈S{c(d|s)+f(k+1,S-{d})}，其中c(d|s)=pd，基本条件是f(k,S)=0，当k=1+N或者S为空时。</p> <p>以具体数据为例：5个作业标号为{0,1,2,3,4}，其中每个作业的效益为{10,15,20,1,5}，完成deadline为{1,2,2,3,3}，求最优策略的效益。可以看到按照之前贪心解法，按照效益排序后，为20,15,10,5,1，而对应的截止时间也变为2,2,1,3,3，这样最优解就是20+15+5=40。DP的解法如下源码：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">import</span> java.util.Set;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #0000ff">import</span> java.util.SortedSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #0000ff">import</span> java.util.TreeSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000">//deadline scheduling of unit-time jobs</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DEADLINE {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] profit = { 10, 15, 20, 1, 5 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] deadline = { 1, 2, 2, 3, 3 }; <span style="color: #008000">// sorted</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> N = profit.length; <span style="color: #008000">// no.of jobs</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> SortedSet&lt;Integer&gt; setOfAllJobs = <span style="color: #0000ff">new</span> TreeSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>     <span style="color: #0000ff">public</span> DEADLINE() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; N; i++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>             setOfAllJobs.add(i);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">boolean</span> feasible(SortedSet&lt;Integer&gt; jobs, <span style="color: #0000ff">int</span> k, <span style="color: #0000ff">int</span> d) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>         <span style="color: #0000ff">int</span> j = 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; N; i++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>             <span style="color: #0000ff">if</span> (!(jobs.contains(<span style="color: #0000ff">new</span> Integer(i))) || i == d) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>                 <span style="color: #008000">// if i already chosen or next (and is j-th),</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>                 <span style="color: #008000">// does it meet its deadline?</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>                 <span style="color: #0000ff">if</span> (deadline[i] &lt; ++j) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>                     <span style="color: #0000ff">return</span> false;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>                 }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>             }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         <span style="color: #0000ff">return</span> true;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> cost(SortedSet&lt;Integer&gt; jobs, <span style="color: #0000ff">int</span> k, <span style="color: #0000ff">int</span> d) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #0000ff">if</span> (feasible(jobs, k, d)) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>             <span style="color: #0000ff">return</span> profit[d];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         } <span style="color: #0000ff">else</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>             <span style="color: #0000ff">return</span> 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>     <span style="color: #008000">// jobs not yet chosen at stage k</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> f(SortedSet&lt;Integer&gt; jobs, <span style="color: #0000ff">int</span> k) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">if</span> (k &gt; N)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>             <span style="color: #0000ff">return</span> 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>         <span style="color: #0000ff">int</span> max = Integer.MIN_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> d : jobs) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>             SortedSet&lt;Integer&gt; nJobs = copySet(jobs);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>             nJobs.remove(d);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>             <span style="color: #0000ff">int</span> t = cost(jobs, k, d) + f(nJobs, k + 1);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>             <span style="color: #0000ff">if</span> (t &gt; max)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>                 max = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>         <span style="color: #0000ff">return</span> max;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>     <span style="color: #0000ff">private</span> SortedSet&lt;Integer&gt; copySet(SortedSet&lt;Integer&gt; jobs) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>         SortedSet&lt;Integer&gt; nJobs = <span style="color: #0000ff">new</span> TreeSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i : jobs) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>             nJobs.add(i);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>         <span style="color: #0000ff">return</span> nJobs;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum71">  71:</span>         DEADLINE d = <span style="color: #0000ff">new</span> DEADLINE();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum72">  72:</span>         System.out.println(d.f(setOfAllJobs, 1));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum73">  73:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum74">  74:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum75">  75:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/413565.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-05-12 16:54 <a href="http://www.blogjava.net/changedi/archive/2014/05/12/413565.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（9）</title><link>http://www.blogjava.net/changedi/archive/2014/05/06/413325.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 06 May 2014 07:55:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/05/06/413325.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/413325.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/05/06/413325.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/413325.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/413325.html</trackback:ping><description><![CDATA[<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 今天介绍的动态规划问题是个有意思的问题，名字叫做Covering，问题描述如下：有k朵不同尺寸的花，现在天气变凉，为了防止花被霜冻，现在需要生产不超过n种尺寸的遮罩来保护这些花，条件是n≤k，并且允许大尺寸的遮罩保护小尺寸的花朵，对应生产k种不同尺寸的遮罩，有不同的生产成本，目标就是给出最低成本及方案。举个具体例子来说明这个问题，假设k=10，即有10朵大小不一的花，将这些花按尺寸从小到大排序并编号，为0~k-1，生产对应尺寸遮罩的成本为c={1,4,5,7,8,12,13,18,19,21}，n=3，即现有条件只允许生产3种尺寸的遮罩，那么最优方案是什么，最低成本是多少？</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因为限制了n=3，也就是需要我们在c这个成本序列里添加两个分界把这个序列分为3个子序列，然后每部分按照最大的那个尺寸去加和，然后最后加和3个子序列和来确定成本。如果我们把分界定为3和6（第3个和第6个，指定下标），那么需要生产的其实是c[3]=7和c[6]=13以及c[9]=21的遮罩，而总和C=7*size(0~3)+13*size(4~6)+21*size(7~9)=7*4+13*3+21*3=130。而要想求得最优方案和最低成本，我们明显需要划分出所有可能的组合而求最小值，这典型的DP求解。</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DP求解的第一步就是定义状态，这个在之前也说过无数次了，状态定义是状态转移方程的基础。那么这个问题因为涉及到遮罩和花朵两种对象，所以我们自然的想到用(j,l)来定义状态，其中j是有多少种遮罩需要确定，l是尚未被确定遮罩的花朵中的最大尺寸的花朵（位置下标）。通过这个定义，可以知道目标函数是求f(n,k-1)，而基础条件是f(1,l)=(l+1)*c[l]，当j=1时。DPFE为f(j,l)=min {(l-k)*c[l]+f(j-1,l-k)}当j&gt;1时，且k∈(j-2,…,l-1)。</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 综上，最优分隔应该是4，6和9，显然9是必须在这个分隔组里的。那么最低成本C=8*size(0~4)+13*size(5~6)+21*size(7~9)=8*5+13*2+21*3=129。</p> <p>source code：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #008000">//optimal COVering problem</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> COV {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>     <span style="color: #008000">// cost of cover sizes 0,1,..,9 in dollar</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] cost = { 1, 4, 5, 7, 8, 12, 13, 18, 19, 21 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> numberOfCoverSizes, <span style="color: #0000ff">int</span> largestSize) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>         <span style="color: #008000">//numberOfCoverSizes denotes the stage number in this problem        </span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>         <span style="color: #0000ff">if</span> (numberOfCoverSizes == 1)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>             <span style="color: #0000ff">return</span> (largestSize + 1) * cost[largestSize];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>         <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> nextCoverSize = numberOfCoverSizes - 2; nextCoverSize &lt;= largestSize - 1; nextCoverSize++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>             <span style="color: #0000ff">double</span> t = (largestSize - nextCoverSize) * cost[largestSize]</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>                     + f(numberOfCoverSizes - 1, nextCoverSize);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>             <span style="color: #0000ff">if</span> (t &lt; min)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>                 min = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>         <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>         System.out.println(f(3, 9));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/413325.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-05-06 15:55 <a href="http://www.blogjava.net/changedi/archive/2014/05/06/413325.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（8）</title><link>http://www.blogjava.net/changedi/archive/2014/04/28/413012.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 28 Apr 2014 08:24:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/04/28/413012.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/413012.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/04/28/413012.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/413012.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/413012.html</trackback:ping><description><![CDATA[<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 最优二叉搜索树问题（Optimal Binary Search Tree Problem (BST)）是算导上又一个经典例题，给定一系列数据项X={x0,x1,…,x(n-1)}，每项都有一个访问概率p(xi)，目标是构建一棵二叉搜索树使得访问每个节点的成本最小，即Σ(p(xi) * level(xi))最小，而level(xi)是指xi项在树中的深度。因为树的递归特性再加二叉搜索树的应用广泛性使得用DP来求解这个最优化问题最合适不过。求解思路还是一样，目标是要列出DPFE，那么第一步要理解目标并把状态定义出来，有了状态和执行步骤，转移方程自然就OK了。因为面临的是建树的问题，所以我们的思路会自然想到，自底向上还是自顶向下？而不失一般性的一个规律是，如果是动态规划解，多数是自顶向下，而贪心解可以做自底向上。所以我们不妨以数据项集作为状态，即X为状态，那么自顶向下的执行步骤将是，首先从X中选择一个x作为节点，然后再计算去除x后的集合X’作为左右子树……依次递归。于是DPFE方程为：f(X)=min a{f(Xl) + f(Xr) + r(a,X)}, X≠∅，f(X)=0, X=∅。其中Xl={x属于X，且x&lt;a}（左子树），Xr={x属于X，且x&gt;a}。成本函数r(a,X)=Σp(x) x∈X。看到这个解，其实对于熟悉树结构的人来说，这是非常直观和自然的一个推断结果。</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 以具体例子来解读，一个数据集为{A,B,C,D,E}，对应的搜索概率为{0.25,0.05,0.2,0.4,0.1}。那么最优解f(X)=f({A,B,C,D,E})=1.9，树形为：</p> <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/7_DE63/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/7_DE63/image_thumb.png" width="373" height="250"></a> </p> <p>程序源码：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.SortedSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.TreeSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> BST {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span>[] data = { 0, 1, 2, 3, 4 };<span style="color: #008000">// assume the n data items are</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>                                             <span style="color: #008000">// the ints {0,1,..,n-1}</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>                                             <span style="color: #008000">// corresponding to strings</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>                                             <span style="color: #008000">// { "A", "B", "C", "D",</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>                                             <span style="color: #008000">// "E"}</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">double</span>[] probability = { 0.25, 0.05, 0.2, 0.4, 0.1 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> SortedSet&lt;Integer&gt; setOfAllItems = <span style="color: #0000ff">new</span> TreeSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     <span style="color: #0000ff">public</span> BST() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; data.length; i++)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>             setOfAllItems.add(data[i]);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">double</span> sumOfProbabilitiesOfItems(SortedSet items) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>         <span style="color: #0000ff">double</span> result = 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = ((Integer) items.first()).intValue(); i &lt;= ((Integer) items</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>                 .last()).intValue(); i++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>             result += probability[i];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>         <span style="color: #0000ff">return</span> result;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>     <span style="color: #0000ff">private</span> SortedSet setOfItemsLessThanPivot(SortedSet items, <span style="color: #0000ff">int</span> alpha) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>         <span style="color: #008000">// conveniently use method headSet() from SortedSet</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>         <span style="color: #008000">// headSet() DOES NOT include alpha</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>         <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> TreeSet(items.headSet(<span style="color: #0000ff">new</span> Integer(alpha)));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>     <span style="color: #0000ff">private</span> SortedSet setOfItemsGreaterThanPivot(SortedSet items, <span style="color: #0000ff">int</span> alpha) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>         <span style="color: #008000">// conveniently use method tailSet() from SortedSet</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>         <span style="color: #008000">// headSet() DOES include alpha</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         SortedSet ss = <span style="color: #0000ff">new</span> TreeSet(items.tailSet(<span style="color: #0000ff">new</span> Integer(alpha)));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>         ss.remove(alpha);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>         <span style="color: #0000ff">return</span> ss;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> f(SortedSet&lt;Integer&gt; items) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>         <span style="color: #0000ff">if</span> (items.size() == 0)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>             <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> a : items) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>             <span style="color: #0000ff">double</span> t = sumOfProbabilitiesOfItems(items)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>                     + f(setOfItemsLessThanPivot(items, a))</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span>                     + f(setOfItemsGreaterThanPivot(items, a));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span>             <span style="color: #0000ff">if</span> (t &lt; min)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span>                 min = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum71">  71:</span>         <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum72">  72:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum73">  73:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum74">  74:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum75">  75:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum76">  76:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum77">  77:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum78">  78:</span>         BST bst = <span style="color: #0000ff">new</span> BST();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum79">  79:</span>         System.out.println(bst.f(setOfAllItems));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum80">  80:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum81">  81:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum82">  82:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/413012.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-04-28 16:24 <a href="http://www.blogjava.net/changedi/archive/2014/04/28/413012.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（7）</title><link>http://www.blogjava.net/changedi/archive/2014/04/21/412734.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 21 Apr 2014 05:25:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/04/21/412734.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/412734.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/04/21/412734.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/412734.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/412734.html</trackback:ping><description><![CDATA[<p>本周更新的动态规划问题是<strong>装配线问题</strong>（Assembly Line Balancing (ASMBAL)）。装配线问题没记错的话应该是算导中的动态规划的第一题，具体描述大概是：一件产品从生产到出品要经历一系列过程，其中每个步骤都会有成本开销，而这些过程在流转（也就是说步骤转换）过程也有成本损失，如何平衡选择，可以最小化产品的出品成本。</p> <p>算导的原题：</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 某汽车工厂有2个装配线，每个装配线有n 个装配站（按顺序编号1～n ）标记为Si,j，表示第i个装配线的第j个装配站，两个装配线的对应的装配站执行相同的功能，但所用的时间可能不同。经过第i条流水线（i=1，2）的第j 个装配站所花的时间为a[i][j]。从第i条流水线的第j 个装配站移到第j+1个装配站的时间可以忽略，而移到另外一个流水线的下一个装配站则需要一定的时间t[i][j]。汽车进入流水线需要花时间，进入装配线1需要时间e[1]，进入装配线2需要时间e[2];&nbsp; 汽车出流水线时需要花时间，出装配线1需要时间x[1]，出装配线2需要时间x[2] 。汽车的装配需要按顺序经过所有装配站。<br>　　现在已知装配时间a[i][j] 、转移时间t[i][j]、进入装配线的时间e[i]、出装配线的时间x[i]，要求输出装配一辆汽车所需要的最短时间，以及经过的装配站。&nbsp; <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 对于这个问题，可以将其与之前所解的有向无环图对比，非常类似，不同的是这个在计算转移成本的时候，不仅需要算路径（也就是转移的成本），还要叠加节点本身的生产成本。以具体为例，装配线有14个装配站，其中0号和13号分别是起点和终点，而剩余12个站平均分配在两条流水线上，具体各计算成本如下图：  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/7_A050/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/7_A050/image_thumb.png" width="472" height="142"></a>  <p>其中节点权重为v=[0,7,8,9,5,3,6,4,4,8,5,4,7,0]，即代表装配时间，而转移时间表示为矩阵形式：  <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/7_A050/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/7_A050/image_thumb_1.png" width="244" height="230"></a>  <p>因为已经将过程打上了stage的标签，我们定义一个状态为(k,i)，表示在第k个阶段的第i条装配线上，那么转移时间就表示为(k,i)-&gt;(k+1,j)的一个成本c(k,i,j)。当然由题图可见在同线转移时是没有成本的即c(i,k,k)=0。具体的，定义总成本R(k,i,j)=v(k,i)+c(k,i,j)，其中v(k,i)即i装配线的编号为k的节点权重。定义f(k,i)为状态值，即在k阶段时的i装配线到出产时的总成本，那么DPFE为：f(k,i)=min j{R(k,i,j)+f(k+1,j)}。f(N+1,0)=0是初始条件，表示在最后一个节点时的成本，那么往前递推，f(0,0)作为在起点处的成本就是目标函数，于是f(0,0)=(0+2)+(7+2)+(5+1)+(3+1)+(4+0)+(5+1)+(4+3)=38.  <p>解题程序：  <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ASMBAL {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[][] cost = <span style="color: #008000">// (stage,line)</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     { </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>             { 2, 4 }, <span style="color: #008000">// to next line</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>             { 2, 2 }, { 1, 3 }, { 2, 1 }, { 2, 3 }, { 1, 4 }, <span style="color: #008000">// to next line</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>             { 3, 2 } <span style="color: #008000">// from line</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[][] vertexcost = <span style="color: #008000">// (stage,line)</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     { </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>         { 0, 0 }, { 7, 8 }, { 9, 5 }, { 3, 6 }, { 4, 4 }, { 8, 5 }, { 4, 7 } </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> N = vertexcost.length - 1;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> arccost(<span style="color: #0000ff">int</span> g, <span style="color: #0000ff">int</span> x, <span style="color: #0000ff">int</span> d) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         <span style="color: #0000ff">if</span> (g == 0)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>             <span style="color: #0000ff">return</span> cost[g][d]; <span style="color: #008000">// to next line d</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (g == N)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>             <span style="color: #0000ff">return</span> cost[g][x]; <span style="color: #008000">// from line x</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (x == d)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>             <span style="color: #0000ff">return</span> 0; <span style="color: #008000">// stay same line</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         <span style="color: #0000ff">else</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>             <span style="color: #0000ff">return</span> cost[g][d]; <span style="color: #008000">// to next line d</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> g, <span style="color: #0000ff">int</span> x) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>         <span style="color: #0000ff">if</span> (g &gt; N)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>             <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> d = 0; d &lt;= 1; d++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>             <span style="color: #0000ff">double</span> c = vertexcost[g][x] + arccost(g, x, d) + f(g + 1, d);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>             <span style="color: #0000ff">if</span> (c &lt; min)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>                 min = c;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>         <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>         System.out.println(f(0,0));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/412734.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-04-21 13:25 <a href="http://www.blogjava.net/changedi/archive/2014/04/21/412734.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（6）</title><link>http://www.blogjava.net/changedi/archive/2014/04/16/412531.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 16 Apr 2014 07:06:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/04/16/412531.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/412531.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/04/16/412531.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/412531.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/412531.html</trackback:ping><description><![CDATA[<p>Optimal Alphabetic Radix-Code Tree Problem（ARC）问题是这样一个问题，给定一系列节点，每个节点有构建成本，然后构建一棵树，树的成本计算方式是这样的，如果树的节点是叶子节点，那么它的成本就是它自己，否则如果是非叶节点，那么这棵树的成本等于所有子树的和，而ARC问题就是要求个最优树，使得这棵树的成本最小化。很显然看出这个树的求和是递归的。</p> <p>这个问题的描述很容易让我们想起huffman tree，霍夫曼树是最优二叉树，满足的条件就是节点权值*路径长度，求和后最小。ARC树与霍夫曼树类似，也是二叉树，条件是对所有节点的权值求和后最小，只不过霍夫曼树因为不递归不重叠，所以直接贪心解即可。而ARC树一个明显的特点就是递归包含。所以我们使用动态规划来求解。而前提同huffman一样，需要先对节点权值进行排序。</p> <p>问题具体化：有4个节点，分别具有权重(2,3,1,4)，现求这四个节点构成的ARC树。解决这个问题的思路就在于，如果是动态规划解，那么构建这个树的方法是如何开始呢？我们知道huffman树是一个经典的自底向上方法，而ARC动态规划要转换为自顶向下。一但思路确定，定义DPFE就很明显了，我们抽象表示定义S=(w0,w1,…wn)，这里的w已经有序，接下来定义状态(i,j)为{wi,…,wj}的子树求和，那么动态规划方程为：f(i,j)=min i≤d&lt;j {c(i,j,d)+f(i,d)+f(d+1,j)} 当i&lt;j。基本条件是f(i,j)=0当i=j时，成本计算函数c(i,j,d)=Σwk k=i,…,j。目标函数是求f(0,n)。我们以中缀括号表示一棵ARC树，那么对于具体化问题的节点集(2,3,1,4)，最后的结果就是(((2,1),3),4)。这棵树的权值为f(S)=(2+1)+((2+1)+3)+(2+1+3+4)=19。</p> <p>代码如下：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.Arrays;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ARC {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span>[] weight = { 2, 3, 1, 4 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> N = weight.length - 1;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> sum(<span style="color: #0000ff">int</span> p, <span style="color: #0000ff">int</span> q) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>         <span style="color: #0000ff">double</span> result = 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k = p; k &lt;= q; k++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>             result += weight[k];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>         <span style="color: #0000ff">return</span> result;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> firstIndex, <span style="color: #0000ff">int</span> lastIndex){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         <span style="color: #0000ff">if</span>(firstIndex == lastIndex)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>             <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>         <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> d=firstIndex;d&lt;lastIndex;d++){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>             <span style="color: #0000ff">double</span> t = f(firstIndex,d) + f(d+1,lastIndex) + sum(firstIndex,lastIndex);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>             <span style="color: #0000ff">if</span>(t&lt;min)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>                 min = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>         <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>         Arrays.sort(weight);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>         System.out.println(f(0,N));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/412531.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-04-16 15:06 <a href="http://www.blogjava.net/changedi/archive/2014/04/16/412531.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（5）</title><link>http://www.blogjava.net/changedi/archive/2014/04/08/412038.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 08 Apr 2014 02:30:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/04/08/412038.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/412038.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/04/08/412038.html#Feedback</comments><slash:comments>11</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/412038.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/412038.html</trackback:ping><description><![CDATA[<p>今天主要面对的问题还是路径问题，回顾最短路的Floyd-Warshall算法，决定还是这里把它实现以下。经过前面的训练，当我们确定一个问题可用动态规划方法求解时，最重要的一步就是要先给出状态定义，其后就可以列出DPFE方程了。那么对于FW算法所求解的全路径最短路问题，问题是要求解一个有向图中的所有节点之间的最短路径。也就是说对于一个N个节点的图，总共有N(N-1)/2个可能组合，求解他们的最短路径。对于这时的状态定义，我们可以参考SPC问题，我们可以定义一个状态(k,p,q)表示从p到q的最短路径，其中k表示不超过k段组成。也就是从p到q不超过k段路径的组合中，最短路径的状态。于是，转移方程也就可以列出来了：</p> <p>F(k,p,q) = min r∈S{b(p, r)+F(k−1,r,q)},k&gt;0。其中当p=q时，F(0,p,q)=0，当p≠q时，F(0,p,q)=∞，b(p,p)=0，b是路径长度函数。  <p>这样，列出方程，我们可以轻松写递归代码求解问题了。  <p>当然这里还是补充一下Floyd-Warshall算法的状态转移方程：F(k,p,q) = min{F(k−1,p,q),F(k−1,p,k)+F(k−1,k,q)}，其中当p=q时，F(0,p,q)=0，当p≠q时，F(0,p,q)=b(p,q)。可以看出两者的差异，FW算法缩减了SPC问题求解方法的中间变量r，也就是说每次求解min值不需要在一个状态集合里寻求最短，而把min缩小到(k-1,p,q)和(k-1,p,k)+(k-1,k,q)中，这样直接把计算复杂度降了一个量级。  <p>递归Java source code：  <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> APSP {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span> INF = Integer.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>     <span style="color: #008000">// nodes (s,x,y,t) = {0,1,2,3}, so (s,x) = 3 means d[0][1] = 3</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[][] distance = { </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>         { INF, 5,      3,     INF }, </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>         { INF, INF, 2,     5 },</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>         { INF, 1,       INF, 8 }, </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>         { INF, INF, INF, INF } </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> maxNodeIndex = distance.length-1;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[] possibleSuccessors = { 0, 1 };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> k, <span style="color: #0000ff">int</span> p, <span style="color: #0000ff">int</span> q) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         <span style="color: #0000ff">if</span> (k == 0 &amp;&amp; p == q)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>             <span style="color: #0000ff">return</span> 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #0000ff">if</span> (k == 0 &amp;&amp; p != q)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>             <span style="color: #0000ff">return</span> distance[p][q];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> d : possibleSuccessors) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>             <span style="color: #0000ff">double</span> t = (1 - d) * f(k - 1, p, q) + d * f(k - 1, p, k) + d</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>                     * f(k - 1, k, q);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>             <span style="color: #0000ff">if</span> (t &lt; min)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>                 min = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>         System.out.println(f(maxNodeIndex,0,3));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/412038.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-04-08 10:30 <a href="http://www.blogjava.net/changedi/archive/2014/04/08/412038.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（4）</title><link>http://www.blogjava.net/changedi/archive/2014/04/01/411775.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 01 Apr 2014 03:09:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/04/01/411775.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/411775.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/04/01/411775.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/411775.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/411775.html</trackback:ping><description><![CDATA[<p>第四个动态规划问题是 最优分配问题（Optimal Allotment Problem:ALLOT），其问题描述是：有M个单位的资源，有N个人，把d个单位的资源分配给某个人有一定的成本，如何分配这些资源给这N个人，能使成本最小。这显然是一个典型的DP问题，组合优化策略，解决这个问题，首先就是要抽象DPFE动态规划方程，假设M个单位的资源，N个人，把d个单位资源分配给编号为k的人的成本计算方式为C(k,d)，0≤d≤M，1≤k≤N。回想我们列DPFE方程的思路，首先要想到的就是状态，如何定义一个状态，状态总是针对总体，总共就M个单位的资源，N个人，那么我们不妨定义状态(k,m)，其中k为已经分配到第k个人，m为此时所剩余的资源单位。于是把这个过程扩展，以k为分析点，此时对下一个人进行d个单位资源的分配，于是对于下一个过程，就应该是第k+1个人，总共剩余m-d个单位的资源，表示为状态(k+1,m-d)。这样，我们的DPFE也就初现端倪了：f(k,m)=min{C(k,d)+f(k+1,m-d)}。目标函数是计算f(1,M)，基本条件是f(N+1,m)=0当m≥0。</p> <p>我们把问题具体化，有4个苹果，要分给3个小朋友，小朋友按照1-3编号，每个小朋友拿到一定个数苹果后会哭闹一段时间（因为他们是小朋友，所以会哭闹），具体时间表如下：</p> <p>C(k,d)=[</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ∞,1.0,0.8,0.4,0.0</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ∞,1.0,0.5,0.0,0.0</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ∞,1.0,0.6,0.3,0.0</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ], 1≤k≤3, 0≤d≤4</p> <p>k是小朋友编号，d是分给小朋友的苹果数，可以看到，当分给小朋友0个苹果时（不分苹果），那么小朋友会无休止的哭闹，而把全部（4个）苹果全部分给一个小朋友时，小朋友不会哭闹（0时间）。那么根据这个时间，提供一个最优的分苹果方案，使得小朋友们拿到苹果后的哭闹时间最短。</p> <p>那么，根据这个具体问题，我们抽象的结果就是f(1,4)=1.0+0.5+1.0=2.5，最优分布是d1=1.0，d2=0.5，d3=1.0，这样的分配方案。具体解法见source code：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.HashMap;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.Map;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ALLOT {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> N = 3;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> M = 4;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">double</span> INF=Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Map&lt;Integer,Integer&gt; choices = <span style="color: #0000ff">new</span> HashMap&lt;Integer,Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">double</span> [][] c = {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>         {INF,1.0,0.8,0.4,0.0},</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>         {INF,1.0,0.5,0.0,0.0},</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>         {INF,1.0,0.6,0.3,0.0}</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> cost(<span style="color: #0000ff">int</span> k, <span style="color: #0000ff">int</span> d) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         <span style="color: #0000ff">return</span> c[k-1][d];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> k, <span style="color: #0000ff">int</span> m){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>         <span style="color: #0000ff">if</span>(k&gt;N&amp;&amp;m&gt;=0) <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>         <span style="color: #0000ff">else</span>{</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>             <span style="color: #0000ff">double</span> mm = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>             <span style="color: #0000ff">int</span> dispatch = -1;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>             <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> d=0;d&lt;=m;d++){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>                 <span style="color: #0000ff">double</span> t = cost(k,d)+f(k+1,m-d);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>                 <span style="color: #0000ff">if</span>(t&lt;mm) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>                     mm = t;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>                     dispatch = d;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>                 }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>             }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>             <span style="color: #0000ff">if</span>(!choices.containsKey(k))</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>                 choices.put(k, Integer.MAX_VALUE);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>             <span style="color: #0000ff">if</span>(mm&lt;choices.get(k))</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>                 choices.put(k,dispatch);                </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>             <span style="color: #0000ff">return</span> mm;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         <span style="color: #0000ff">double</span> minCost = f(1,M);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>         System.out.println(<span style="color: #006080">"min cost is : "</span>+minCost);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>         System.out.print(<span style="color: #006080">"solution is : "</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>         <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=1;i&lt;=N;i++){            </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span>             System.out.print(choices.get(i));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span>             <span style="color: #0000ff">if</span>(i&lt;N)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span>                 System.out.print(<span style="color: #006080">","</span>);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum71">  71:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum72">  72:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum73">  73:</span> }</pre><!--CRLF--></div></div>
<p>PS:因为考虑到这里的选择不是很长的代码，可能不影响理解DP，所以把记录选择方案的代码也加进来了。</p><img src ="http://www.blogjava.net/changedi/aggbug/411775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-04-01 11:09 <a href="http://www.blogjava.net/changedi/archive/2014/04/01/411775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hbase配置项粗解（3）</title><link>http://www.blogjava.net/changedi/archive/2014/03/31/411742.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 31 Mar 2014 09:18:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/03/31/411742.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/411742.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/03/31/411742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/411742.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/411742.html</trackback:ping><description><![CDATA[<p>HBase的配置 完结篇：</p> <p>hbase.rpc.server.engine：hbase 做rpc server的调度管理类，实现自org.apache.hadoop.ipc.RpcServerEngine，默认是org.apache.hadoop.hbase.ipc.ProtobufRpcServerEngine；</p> <p>hbase.rpc.timeout：Hbase client发起远程调用时的超时时限，使用ping来确认连接，但是最终会抛出一个TimeoutException，默认值是60000；</p> <p>hbase.rpc.shortoperation.timeout：另一个版本的hbase.rpc.timeout，控制短操作的超时时限，比如region server 汇报master的操作的超时时限可以设置小，这样有利于master的failover，默认是10000；</p> <p>hbase.ipc.client.tcpnodelay：默认是true，具体就是在tcp socket连接时设置 no delay；</p> <p>hbase.master.keytab.file：kerberos keytab 文件的全路径名，用来为HMaster做log，无默认值；</p> <p>hbase.master.kerberos.principal：运行HMaster进程时需要kerberos的principal name，这个配置就是这个name的值，形如：<a href="mailto:hbase/_HOST@EXAMPLE.COM">hbase/_HOST@EXAMPLE.COM</a>；</p> <p>hbase.regionserver.keytab.file：kerberos keytab 文件的全路径名，用来为HRegionServer做log，无默认值；</p> <p>hbase.regionserver.kerberos.principal：运行HRegionServer进程时需要kerberos的principal name，这个配置就是这个name的值，形如：<a href="mailto:hbase/_HOST@EXAMPLE.COM">hbase/_HOST@EXAMPLE.COM</a>；</p> <p>hadoop.policy.file：RPC服务器做权限认证时需要的安全策略配置文件，在Hbase security开启后使用，默认是habse-policy.xml；</p> <p>hbase.superuser：Hbase security 开启后的超级用户配置，一系列由逗号隔开的user或者group；</p> <p>hbase.auth.key.update.interval：Hbase security开启后服务端更新认证key的间隔时间：默认是86400000毫秒；</p> <p>hbase.auth.token.max.lifetime：Hbase security开启后，认证token下发后的生存周期，默认是604800000毫秒；</p> <p>hbase.ipc.client.fallback-to-simple-auth-allowed：client使用安全连接去链接一台非安全服务器时，服务器提示client切换到SASL SIMPLE认证模式（非安全），如果设置为true，则client同意切换到非安全连接，如果false，则退出连接；</p> <p>hbase.coprocessor.region.classes：逗号分隔的Coprocessores列表，会被加载到默认所有表上。在自己实现了一个Coprocessor后，将其添加到Hbase的classpath并加入全限定名。也可以延迟加载，由HTableDescriptor指定；</p> <p>hbase.rest.port：Hbase REST服务器的端口，默认是8080；</p> <p>hbase.rest.readonly：定义REST服务器启动的模式，有两种方式，false：所有http方法都将被通过-GET/PUT/POST/DELETE，true：只有get方法ok。默认值是false；</p> <p>hbase.rest.threads.max：REST服务器线程池的最大线程数，池满的话新请求会自动排队，限制这个配置可以控制服务器的内存量，预防OOM，默认是100；</p> <p>hbase.rest.threads.min：同上类似，最小线程数，为了确保服务器的服务状态，默认是2；</p> <p>hbase.rest.support.proxyuser：使REST服务器支持proxy-user 模式，默认是false；</p> <p>hbase.defaults.for.version.skip：是否跳过hbase.defaults.for.version的检查，默认是false；</p> <p>hbase.coprocessor.master.classes：由HMaster进程加载的coprocessors，逗号分隔，全部实现org.apache.hadoop.hbase.coprocessor.MasterObserver，同coprocessor类似，加入classpath及全限定名；</p> <p>hbase.coprocessor.abortonerror：如果coprocessor加载失败或者初始化失败或者抛出Throwable对象，则主机退出。设置为false会让系统继续运行，但是coprocessor的状态会不一致，所以一般debug时才会设置为false，默认是true；</p> <p>hbase.online.schema.update.enable：设置true来允许在线schema变更，默认是true；</p> <p>hbase.table.lock.enable：设置为true来允许在schema变更时zk锁表，锁表可以组织并发的schema变更导致的表状态不一致，默认是true；</p> <p>hbase.thrift.minWorkerThreads：线程池的core size，在达到这里配置的量级后，新线程才会再新的连接创立时创建，默认是16；</p> <p>hbase.thrift.maxWorkerThreads：顾名思义，最大线程数，达到这个数字后，服务器开始drop连接，默认是1000；</p> <p>hbase.thrift.maxQueuedRequests：Thrift连接队列的最大数，如果线程池满，会先在这个队列中缓存请求，缓存上限就是该配置，默认是1000；</p> <p>hbase.thrift.htablepool.size.max：Thrift服务器上table pool的最大上限，默认是1000；</p> <p>hbase.offheapcache.percentage：JVM参数-XX:MaxDirectMemorySize的百分比值，默认是0，即不开启堆外分配；</p> <p>hbase.data.umask.enable：开启后，文件在regionserver写入时会 有权限相关设定，默认是false不开启；</p> <p>hbase.data.umask：开启上面一项配置后，文件的权限umask，默认是000；</p> <p>hbase.metrics.showTableName：是否为每个指标显示表名前缀，默认是true；</p> <p>hbase.metrics.exposeOperationTimes：是否进行关于操作在使用时间维度的指标报告，比如GET PUT DELETE INCREMENT等，默认是true；</p> <p>hbase.snapshot.enabled：是否允许snapshot被使用、存储和克隆，默认是true；</p> <p>hbase.snapshot.restore.take.failsafe.snapshot：在restore过程中，如果失败则启用snapshot替换，成功则删除掉snapshot，默认开启true；</p> <p>hbase.snapshot.restore.failsafe.name：刚才所说过程中snapshot的名字，默认是hbase-failsafe-{snapshot.name}-{restore.timestamp}；</p> <p>hbase.server.compactchecker.interval.multiplier：检查是否需要compact的时间间隔，一般情况是在比如memstore flush后或者其他事件触发compact的，但是有时也需要不同的compact策略，所以需要周期性的检查具体间隔=hbase.server.compactchecker.interval.multiplier * hbase.server.thread.wakefrequency，默认1000；</p> <p>hbase.lease.recovery.timeout：在dfs 租约超时时限，超时则放弃，默认是900000；</p> <p>hbase.lease.recovery.dfs.timeout：dfs恢复租约调用的超时时限，默认是64000；</p><img src ="http://www.blogjava.net/changedi/aggbug/411742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-03-31 17:18 <a href="http://www.blogjava.net/changedi/archive/2014/03/31/411742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（3）</title><link>http://www.blogjava.net/changedi/archive/2014/03/24/411402.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 24 Mar 2014 01:44:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/03/24/411402.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/411402.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/03/24/411402.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/411402.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/411402.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 继续上一篇的探讨，对于有向无环图我们可以通过动态规划解决，那么对于有向有环图呢？这里列出一个例子解决 有向有环图的动态规划解。 如前所述，在解决图路径的动态规划问题时，我们递归或者迭代求解，列出DPFE方程。这里仍旧拿上一个例子来说，为了把无环变为有环，加一条边(y,x)=2，并且假设图是没有自循环节点的，也就是节点不能到达自己。这样问题就变为一个经典的Shortest Path in Cycli...&nbsp;&nbsp;<a href='http://www.blogjava.net/changedi/archive/2014/03/24/411402.html'>阅读全文</a><img src ="http://www.blogjava.net/changedi/aggbug/411402.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-03-24 09:44 <a href="http://www.blogjava.net/changedi/archive/2014/03/24/411402.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（2）</title><link>http://www.blogjava.net/changedi/archive/2014/03/17/411139.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 17 Mar 2014 05:40:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/03/17/411139.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/411139.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/03/17/411139.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/411139.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/411139.html</trackback:ping><description><![CDATA[<p>在问题1线性搜索里，我们提到了状态转移图模型，因为与通用解法等价，所以基本所有动态规划问题都可以用图论的方法求解，很多问题等价于一个最短路径问题。而本文主要介绍一下通俗的最短路问题&#8212;&#8212;DP第二题：SPA(Shortest Path in an Acyclic Graph)。</p> <p>对于一个无环图来讲（有环图后面讲到），一个起始节点为s，一个目标节点为t，DPFE为：f(p) = min q{ b(p,q) + f(q)}，其中b(p,q)是从p到q的距离，f(p)是从p到t的最短路径，另外这里的p是q的前驱节点，如果p不是q的直接前驱，那么b(p,q)=&#8734;。而目标函数就是求 f(s)，base condition就是f(t)=0。</p> <p>那么举例如下：一个有向无环图，其有四个节点，集合为{s,x,y,t}，五条边，边集合为{(s,x), (s,y), (x,y), (x,t), (y,t)}，长度为{3,5,1,8,5}。那么求从s到t的最短路径。</p> <p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/2_D2FB/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/2_D2FB/image_thumb_1.png" width="244" height="145" data-pinit="registered" /></a> </p> <p>首先使每个节点都有到t的边，如果没有的，加一条虚拟边，长度为&#8734;，即(s,t)=&#8734;。DPFE为：f(s) = min{b(s,x)+f(x), b(s,y)+f(y), b(s,t)+f(t)}，f(x) = min{b(x,y)+f(y), b(x,t)+f(t)}，f(y) = min{b(y,t)+f(t)}，f(t)=0。依次代入，得到f(y)=min{5+0}=5，f(x)=min{1+5,8+0}=6，f(s)=min{3+6,5+5,&#8734;+0}=9。</p> <p>因此最短路径长度为9，路径为s-&gt;x-&gt;y-&gt;t。</p> <p>代码是经典的最短路代码，使用邻接矩阵来表示一个有向无环图，这里仍然用一个递归函数来简化：</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.HashSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.Set;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ShortestPathAcyclic {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span> INF = Integer.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #008000">// nodes (s,x,y,t) = {0,1,2,3}, so (s,x) = 3 means d[0][1] = 3</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span>[][] distance = { </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>         { INF, 3,      5,     INF }, </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>         { INF, INF, 1,     8 },</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>         { INF, INF, INF, 5 }, </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>         { INF, INF, INF, INF } </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>     };</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Set&lt;Integer&gt; possibleNextNodes(<span style="color: #0000ff">int</span> node) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>         Set&lt;Integer&gt; result = <span style="color: #0000ff">new</span> HashSet&lt;Integer&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; distance[node].length; i++) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>             <span style="color: #0000ff">if</span> (distance[node][i] != INF) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>                 result.add(<span style="color: #0000ff">new</span> Integer(i));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>             }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>         <span style="color: #0000ff">return</span> result;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>     </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">double</span> f(<span style="color: #0000ff">int</span> currentNode){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>         <span style="color: #0000ff">if</span>(currentNode==3) <span style="color: #0000ff">return</span> 0.0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>         <span style="color: #0000ff">else</span>{</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>             Set&lt;Integer&gt; possibleSuccessors = possibleNextNodes(currentNode);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>             <span style="color: #0000ff">double</span> min = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>             <span style="color: #0000ff">for</span>(Integer d : possibleSuccessors){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>                 <span style="color: #0000ff">double</span> dis = distance[currentNode][d]+f(d);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>                 <span style="color: #0000ff">if</span>(dis&lt;min) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>                     min = dis;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>                 }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>             }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>             <span style="color: #0000ff">return</span> min;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>     <span style="color: #008000">/**</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span> <span style="color: #008000">     * @param args</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span> <span style="color: #008000">     */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>         <span style="color: #0000ff">double</span> shortest = f(0);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>         System.out.println(shortest);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span> }</pre><!--CRLF--></div></div>
<p>&nbsp;</p>其实这个版本的代码没有记录路径，只求出了最短距离值，路径的记录有很多种方式，最直观的方法是在f函数内部的for循环结束后，把最小距离计算出的对应的后继节点d保存，然后记录一个(currentNode,d)这样的一个节点对，保存到currentNode为key的一个map里，表示对应从currentNode开始到d选定的最短距离的边，最后把map里所有这些边都拿出来拼成一个路径即可。具体代码就不写了（因为考虑到影响对动态规划的理解，记得算法导论里也是把记录路径单独写一个函数去讲解的）。
 <script charset="utf-8"> (function(w, d, g, J) { var e = J.stringify || J.encode; d[g] = d[g] || {}; d[g]['showValidImages'] = d[g]['showValidImages'] || function() { w.postMessage(e({'msg': {'g': g, 'm':'s'}}), location.href); } })(window, document, '__huaban', JSON); </script><div id="haloword-lookup" class="ui-widget-content ui-draggable"><div id="haloword-title"><span id="haloword-word"></span><a herf="#" id="haloword-pron" class="haloword-button" title="发音"></a><audio id="haloword-audio"></audio><div id="haloword-control-container"><a herf="#" id="haloword-add" class="haloword-button" title="加入单词表"></a><a herf="#" id="haloword-remove" class="haloword-button" title="移出单词表"></a><a href="#" id="haloword-open" class="haloword-button" title="查看单词详细释义" target="_blank"></a><a herf="#" id="haloword-close" class="haloword-button" title="关闭查询窗"></a></div></div><div id="haloword-content"></div></div><pinit id="__huaban_Button" style="display: none;"><a id="__huaban_thunderpinBtn" href="#" title="快速采集" class="thunderpin"><strong><em></em></strong></a><a id="__huaban_pinBtn" href="#" title="采集到花瓣" class="pin">采集到花瓣</a></pinit><img src ="http://www.blogjava.net/changedi/aggbug/411139.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-03-17 13:40 <a href="http://www.blogjava.net/changedi/archive/2014/03/17/411139.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深入理解动态规划的一系列问题（1）</title><link>http://www.blogjava.net/changedi/archive/2014/03/10/410810.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 10 Mar 2014 03:23:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/03/10/410810.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/410810.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/03/10/410810.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/410810.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/410810.html</trackback:ping><description><![CDATA[<p>动态规划（Dynamic Programming）是用来解决最优化问题的最常用手段，但是很多初级程序员都不是很理解动态规划算法。这里开始做一个系列的问题剖析研究，通过例子来更好的深入理解动态规划方法。<strong><em>计划每周更新一个问题</em></strong></p> <p>在进入第一个问题之前，先简单介绍一下动态规划（以下简称DP）。第一次知道动态规划不是在大学的算法课上，就是在读经典的《算法导论》里，动态规划解决的问题主要是最优决策问题，就是说在一个目标问题下，需要一系列的操作决策来达到目标，那么最优化的决策是哪个，就是动态规划解决的问题。而我们一般提到最优化，无非最低成本、最高收益等可以通过定义一个函数来量化的东西。更深入的理解我们可能需要先把动态规划的过程抽象化符号化，但是我个人认为理解到这个层次就进了象牙塔，对于实际解决问题太过深奥晦涩，于是一个总结性的抽象化是：H*=(opt (h [d1,d2,…,dn]∈ Δ))&nbsp; ，opt是最优函数、h是目标函数、d1-dn是一系列决策、Δ是全局决策空间，那么这个表示的含义就是在所有决策中找到一组最优决策满足目标函数h。动态规划问题的一个通用特点是具备最优子结构和重叠子问题，最优子结构可以这么理解，目标问题是可以拆分的，比如最短路问题中，你要从a到b的最短路径，这个最短路径包含了子路径从a到c的最短……。重叠子问题同样拿最短路问题举例，那就是你要求从a到b的最短路径，那么其中有多重选择，你可以选a到c再从c到b，也可以选a直接到b或者更多，而这些路径选择中，重叠情况不断出现……</p> <p>具体在动态规划解决问题时的一些表示和说明，我们在描述问题的过程中边描述边解说。</p> <p>下面举例来剖析求解动态规划问题。</p> <p>问题1：线性搜索（Linear Search）</p> <p>描述：一个数组A包含N个元素，每个元素x有权重px，有一个成本函数来计算一种排列的排列成本，线性搜索的目标是做到最小化成本来找出一个数组A的排列。</p> <p>举例：A={a,b,c}，pa=0.2，pb=0.5，pc=0.3；数组A共有6种排列，abc,acb,bac,bca,cab,cba。成本的计算方式是1*c1 + 2*c2 + 3*c3。即对应位置的元素优先级乘位置数，比如对于排列bca来说，成本就是1*pb+2*pc+3*pa=1.7 。</p> <p>问题规范化和求解：我们首先定义状态S作为备选的元素集合，那么接下来的目标就是要做一系列决策决定哪个元素该摆在哪个位置。DPFE（动态规划方程）可以帮助我们更好的理解这个优化问题，DPFE的基本形式是<strong>f(S)=opt d∈D(S){R(S,d) o f(T(S,d))}</strong>. 其中S是状态空间的状态，d是一个从决策空间D(S)中选出的决策，R(S,d)是价值函数（reward function或者称为决策成本，也表示为C(d|S)），T(S,d)是下一个状态的状态转移函数，o是一个二元操作符。具体解释一下DPFE中的每个元素的含义，S作为状态，因为动态规划解决的问题是需要一系列决策的，S就是表示到当前决策时的问题状态；D(S)是决策空间，代表从一个状态到另一个状态转移时，可以选择的决策d的集合；f是目标函数，是关于状态S的函数，代表到达状态S所做的所有决策产生的最优利益或者最低成本；价值函数R(S,d)是个从状态S做下一个决策d所带来的收益或成本的计算函数，区别于f，它是一个单步的计算函数；T(S,d)是个转移函数，表示从S做下一个决策d所带来的结果；o作为二元操作符，多数是加法或者乘法及最大最小值等，为了将决策合并起来。因为DPFE是一个递归的形式，所以需要一个base condition作为递归的结束条件。另外DPFE可能会有高阶形式，对于某些复杂问题，形式可能是<strong>f(S)=opt d∈D(S){R(S,d) o f(T1(S,d)) o f(T2(S,d))} </strong>这样的二阶形式或者是带有权重的<strong>f(S)=opt d∈D(S){R(S,d) o p1*f(T1(S,d)) o p2*f(T2(S,d))} </strong>形式。</p> <p>回到这个问题，本问题的DPFE 形式为 f(S) = min { C(x|S) + f(S-{x})}，其中C(x|S)成本函数已经定义，C(x|S)= (N+1-|S|) * px。于是我们可以递归来描述这个过程了，这里用到一个反向搜索的方法，先从排列的最后开始向前查找。</p> <p>首先base condition f(φ)=0，就是说空集的成本就是0 。</p> <p>然后对于单项集合，a作为最后一个元素，f({a}) = min {C(a|{a})+f(φ)} = min {3*0.2+0} = 0.6；</p> <p>b作为最后一个元素，f({b}) = min {C(b|{b})+f(φ)} = min {3*0.5+0} = 1.5；</p> <p>c作为最后一个元素，f({c}) = min {C(b|{b})+f(φ)} = min {3*0.3+0} = 0.9；</p> <p>依次类推，f({a,b}) = min {C(a|{a,b})+f({b}), C(b|{a,b})+f({a})} = min {2*0.2+1.5, 2*0.5+0.6} = 1.6；</p> <p>f({a,c}) = min {C(a|{a,c})+f({c}), C(c|{a,c})+f({a})} = min {2*0.2+0.9, 2*0.3+0.6} = 1.2；</p> <p>f({b,c}) = min {C(b|{b,c})+f({c}), C(c|{b,c})+f({b})} = min {2*0.5+0.9, 2*0.3+1.5} = 1.9；</p> <p>f({a,b,c}) = min {C(a|{a,b,c})+f({b,c}), <font color="#ff8000">C(b|{a,b,c})+f({a,c})</font>, C(c|{a,b,c})+f({a,b})} = min {1*0.2+1.9, <font color="#ff8040">1*0.5+1.2</font>, 1*0.3+1.6} = 1.7</p> <p>所以，最优答案是1.7，并且组合方式是bac。好吧，问题得解了。</p> <p>换一个解法思路：状态转移图模型（State Transition Graph Model），同DPFE方法一致，只不过状态转移图更直观，状态转移图对每一个状态S当做一个节点，f(S)就是S到达φ的最短路径，之前的标准解法是从后向前的计算思路，而STGM是从前向后的计算思路，形式等价于标准，只不过方程变化一下：f’(S)=min {f’(S’) + C(x|S’)}。其中S’是一个由x决策导致状态S的前置节点，也就是说S’-&gt;S （条件是x决策）。并且f’(S)是源节点S*到S的最短路径，最终目标函数是f’(φ)，起始状态是 S*={a,b,c}。</p> <p>所以整个搜索过程如下：</p> <p>f’({a,b,c})=0；</p> <p>f’({b,c}) = min {f’({a,b,c})+C(a|{a,b,c}) } = min{0+0.2} = 0.2；</p> <p>f’({a,c}) = min {f’({a,b,c})+C(b|{a,b,c}) } = min{0+0.5} = 0.5；</p> <p>f’({a,b}) = min {f’({a,b,c})+C(c|{a,b,c}) } = min{0+0.3} = 0.3；</p> <p>f’({c}) = min {f’({b,c})+C(b|{b,c}), f’({a,c})+C(a|{a,c}) } = min{0.2+2*0.5,0.5+2*0.2} = 0.9；</p> <p>f’({b}) = min {f’({a,b})+C(a|{a,b}), f’({b,c})+C(c|{b,c}) } = min{0.3+2*0.2,0.2+2*0.3} = 0.7；</p> <p>f’({a}) = min {f’({a,b})+C(b|{a,b}), f’({a,c})+C(c|{a,c}) } = min{0.3+2*0.5,0.5+2*0.3} = 1.1；</p> <p>f’(φ) = min {f’({a})+C(a|{a}), f’({b})+C(b|{b}), f’({c})+C(c|{c}) } = min{1.1+3*0.2, 0.7+3*0.5, 0.9+3*0.3} = 1.7；</p> <p>过程已经比较清晰了，更直白的理解就是，倒着数，对于这个集合，排列好后的路径长度是0，有两个已经排列好，剩下放到第一个位置的选择的最小路径，有一个已经排列好，剩下放到前两个的选择的最小路径……有0个已经排列好，剩下放到前3个的选择的最小路径，迭代计算即可。</p> <p>再换一个思路：阶段决策（Stage Decision），这是个顺序决策过程，分阶段，第一阶段决定放在第一个位置的元素是什么，依次类推。方程转变为：f(k,S)=min{C(x|k,S)+f(k+1,S-{x})}。其中k是阶段标识，base condition为f(<a href="mailto:N+@,&phi;)=0">N+1 , φ)=0</a>，目标函数是f(1,A)，因为k=N+1-|S|，所以成本函数C(x|k,S)=(N+1-|S|)*px=k * px。</p> <p>整个过程如下：</p> <p>f(1,{a,b,c}) = min {C(a|1,{a,b,c})+f(2,{b,c}), C(b|1,{a,b,c})+f(2,{a,c}), C(c|1,{a,b,c})+f(2,{a,b})}</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = min {3*0.2+1.1, 3*0.5+0.7, 3*0.3+0.9} = 1.7；</p> <p>f(2,{b,c}) = min {C(b|2,{b,c})+f(3,{c}), C(c|2,{b,c})+f(3,{b})} = min{2*0.5+0.3,2*0.3+0.5}=1.1；</p> <p>f(2,{a,c}) = min {C(a|2,{a,c})+f(3,{c}), C(c|2,{a,c})+f(3,{a})} = min{2*0.2+0.3,2*0.3+0.2}=0.7；</p> <p>f(2,{a,b}) = min {C(a|2,{a,b})+f(3,{b}), C(b|2,{a,b})+f(3,{a})} = min{2*0.2+0.5,2*0.5+0.2}=0.9；</p> <p>f(3,{a}) = min {C(a|3,{a})+f(4,φ)} = min{0.2+0} = 0.2；</p> <p>f(3,{b}) = min {C(b|3,{b})+f(4,φ)} = min{0.5+0} = 0.5；</p> <p>f(3,{c}) = min {C(c|3,{c})+f(4,φ)} = min{0.3+0} = 0.3；</p> <p>f(4,φ) = 0；</p> <p>反向代入后，得解 f(1,{a,b,c}) = 1.7 。</p> <p>最后再介绍一种图论的思想：如果把S定义为从源头一直到决策d的一系列决策集(d1,d2,…,di-1)，S作为一个图的节点的话，图的每个节点都表示了从初始状态φ到S的一个路径（Path-States）。那么方程变为：f(S) = min x∉S {C(x|S) + f(S ∪ {x})}。这个其实是等同于状态转移图的另一种表示。</p> <p>至此，已经通过第一个简单的问题，得到了4种（其实是2种，一种直观的，一种图论的）描述动态规划问题求解策略的方法了，分别是Stage Decision 和 State Transition Graph Model。未来会有更多的动态规划问题通过这些方法来求解。</p> <p>最后再附上这个问题的一个函数代码：不出意外，所有的过程都以Java代码来实现描述。</p> <p>source code:</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">/*</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #008000"> * Copyright (C) 2013 changedi</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> <span style="color: #008000"> * Licensed under the Apache License, Version 2.0 (the "License");</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> <span style="color: #008000"> * you may not use this file except in compliance with the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> <span style="color: #008000"> * You may obtain a copy of the License at</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #008000"> * http://www.apache.org/licenses/LICENSE-2.0</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> <span style="color: #008000"> *</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> <span style="color: #008000"> * Unless required by applicable law or agreed to in writing, software</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> <span style="color: #008000"> * distributed under the License is distributed on an "AS IS" BASIS,</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #008000"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> <span style="color: #008000"> * See the License for the specific language governing permissions and</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #008000"> * limitations under the License.</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span> <span style="color: #008000"> */</span></pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #0000ff">package</span> com.jybat.dp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> <span style="color: #0000ff">import</span> java.util.HashMap;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> <span style="color: #0000ff">import</span> java.util.HashSet;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> <span style="color: #0000ff">import</span> java.util.Map;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span> <span style="color: #0000ff">import</span> java.util.Set;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LinearSearch {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>     <span style="color: #0000ff">int</span> N = 3;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>     Map&lt;String,Double&gt; map = <span style="color: #0000ff">new</span> HashMap&lt;String,Double&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>     <span style="color: #0000ff">public</span> LinearSearch(){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>         map.put(<span style="color: #006080">"a"</span>, 0.2);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>         map.put(<span style="color: #006080">"b"</span>, 0.5);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>         map.put(<span style="color: #006080">"c"</span>, 0.3);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> dp() {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>         Set&lt;String&gt; A = <span style="color: #0000ff">new</span> HashSet&lt;String&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         <span style="color: #0000ff">for</span> (String k : map.keySet()) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>             A.add(k);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>         System.out.println(f(A));</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">double</span> cost(String x, Set&lt;String&gt; A) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>         <span style="color: #0000ff">return</span> map.get(x) * (N + 1 - A.size());</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">double</span> f(Set&lt;String&gt; A) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>         <span style="color: #0000ff">double</span> fx = Double.MAX_VALUE;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>         <span style="color: #0000ff">if</span>(A.isEmpty()){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>             fx = 0;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>         <span style="color: #0000ff">else</span>{</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>             <span style="color: #0000ff">for</span>(String key : A){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum52">  52:</span>                 Set&lt;String&gt; A1 = <span style="color: #0000ff">new</span> HashSet&lt;String&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum53">  53:</span>                 <span style="color: #0000ff">for</span>(String ik : A){</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum54">  54:</span>                     A1.add(ik);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum55">  55:</span>                 }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum56">  56:</span>                 A1.remove(key);                </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum57">  57:</span>                 <span style="color: #0000ff">double</span> tmp = cost(key,A) + f(A1);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum58">  58:</span>                 <span style="color: #0000ff">if</span>(tmp&lt;fx)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum59">  59:</span>                     fx = tmp;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum60">  60:</span>             }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum61">  61:</span>         }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum62">  62:</span>         <span style="color: #0000ff">return</span> fx;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum63">  63:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum64">  64:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum65">  65:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum66">  66:</span>         LinearSearch ls = <span style="color: #0000ff">new</span> LinearSearch();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum67">  67:</span>         ls.dp();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum68">  68:</span>     }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum69">  69:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum70">  70:</span> }</pre><!--CRLF--></div></div><img src ="http://www.blogjava.net/changedi/aggbug/410810.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-03-10 11:23 <a href="http://www.blogjava.net/changedi/archive/2014/03/10/410810.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为什么加班</title><link>http://www.blogjava.net/changedi/archive/2014/02/15/409886.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Sat, 15 Feb 2014 03:36:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/02/15/409886.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/409886.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/02/15/409886.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/409886.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/409886.html</trackback:ping><description><![CDATA[&nbsp; &nbsp; &nbsp; 当一个项目成功上线时，项目经理会做一番汇总，提到整个项目经历中各种感人情节，感谢abcdefg然后感谢大家加班加点的努力&#8230;&#8230; <br /><br />&nbsp; &nbsp; &nbsp; 故事总是这样重复上演，然后一个非常现实的情况是现有的私营企业的加班现状。<br /><br />&nbsp; &nbsp; &nbsp; 首先需要澄清一点的是这里讨论的加班是不带薪的加班（包含不调休等其他直接利益）。 当雇佣制存在时，人们就以&#8220;上班&#8221;来代表自己的工作，有人出钱，有人出力，出钱的人雇佣出力的人，大家为了各自的目的持续这种关系。尤其是私营企业存在时，这种雇佣关系更为明显&#8212;&#8212;老板为自己上班，因为公司是自己的；员工（雇佣工人）为自己上班&#8212;&#8212;赚取工钱（劳动报酬）来养家糊口过日子。虽然都是为自己上班，但是有个概念是老板的自己=公司集体，员工的自己=员工自己（家庭）。也就是说，在这个前提下，两种身份的人上班的目的是不一样的。但是我们知道，雇佣关系的确立需要一个约定，一般就是我们的劳动合同（或者其他劳动规定）。如同其他社会现象一样，工作也有个社会通用约定：比如定点上下班。而加班就是在这个雇佣关系和通用约定之上衍生出来的现象。<br /><br />&nbsp; &nbsp; &nbsp; 企业管理人员明白这种工作目的带来的影响，于是约定如果有加班，则给予更多奖励。于是加班这一现象也就正常了。 但是在正常中必须有异常的陪伴，那就是现在的多数私企做的事情：加班没有报酬。面对这种状况时，有的管理人员会对你说，加班虽然没有加班费，但是你不是没有收获的，加班属于额外的付出，付出会有回报的，你的能力会提升，同时年底可能会有其他的比如年终奖啊晋升啊等等优势&#8230;&#8230;当说出这种话时，如果你信了，那说明你还年轻，容易被忽悠。事实是需要仔细揣摩的，第一点，加班为了什么？如果是你承诺3天做完的活，到日子没交差，那加班搞太正常了，这个不是要讨论的；如果你完全自愿在公司加班学习，那也不要讨论，学习哪里都一样。但是有种加班是所谓的制度或者是强制执行的，项目经理为了让项目顺利执行完，那这个需要讨论一下了。加班本身成为了一种常态，然后又对你承诺是个人提升的前提，那这个就是自相矛盾的了。所有人都休息，你加班，这叫提升；所有人都加班，你也加班，这就不是提升了。如果是短期内项目冲刺，这个加班不叫加班。<br /><br />&nbsp; &nbsp; &nbsp; 注意我说的是常态，就是说公司管理人员把加班作为一项长期事业来进行了~~~这时候其实是你步入到一个低能效倒退的节奏里去了。 亲身经历告诉我，常态的加班带来的后果；第一、低效，员工的工作效率会有个曲线发展，刚开始加班会体现出干的活也多，这时候一切都很美好，但是时间一长，你会发现，原来8小时工作的内容，放到12小时还是这些活，原地不动了~~再过段时间，由于长期的加班，体力意志力以及心智都不是正常发展，所以效率会再降一个档，8小时的工作，12小时加班只能完成一半，而剩下一半不是故意消极怠工就是员工去做自己的事情。于是结果就是本来可以正常发展的业务慢慢走向死亡。第二条后果、员工的全方位亏损，企业发展良好与否，员工是其组成基本，大家都知道招一个人然后到培训他可以完成特定工作这都是有很大成本的，如果这个员工不断成长，那这些成本是值得的，如果这个员工各方面不断的退步，甚至像一颗老鼠屎，那么不好意思，企业这一锅大汤是肯定要毁了，长期的高强度加班带来的人的身体和精神损失我就不多讲了，体验过的都知道。第三条后果就是，企业的名声不好，面对同样的机会，多数人会选择正常的企业，而一个总加班的公司是不会有人选择的。  <br /><br />&nbsp; &nbsp; &nbsp; 最后总结一下，加班是现代企业竞争的常态，但是长期高强度加班不能成为一个企业的常态，说起来这话有点绕。说白了要不要加班就是在考验管理者，正所谓张弛有度，如何调节？不是放一句狠话：天天加班周末不休~~就能打败竞争对手的，真奇怪为什么有的所谓的高层领导那么naive<img src ="http://www.blogjava.net/changedi/aggbug/409886.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-02-15 11:36 <a href="http://www.blogjava.net/changedi/archive/2014/02/15/409886.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>过去的一年</title><link>http://www.blogjava.net/changedi/archive/2014/02/07/409578.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Fri, 07 Feb 2014 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/02/07/409578.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/409578.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/02/07/409578.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/409578.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/409578.html</trackback:ping><description><![CDATA[<p>&nbsp;&nbsp;&nbsp; 公司上班第一天，在一种&#8220;节后综合症&#8221;的气氛下写下这篇一直拖着的总结。</p> <p>&nbsp;&nbsp;&nbsp; 2013已经成了历史的一个数字，在各种数据总结、趋势图表、红包鞭炮、酒席宴请弥漫的后春节状态下，终于回到公司，坐在清冷的办公室，冲一杯咖啡，键盘和聊天声中，开始静静的回忆过去的一年。</p> <p>&nbsp;&nbsp;&nbsp; 3年一个坎儿，也许不假，2013是我最频繁想起跳槽、转行、辞职的一年，为什么？我把它归罪于3年这个坎儿。工作3年整了，在一家公司，一个部门，做着很多类似的事情，有这些想法&#8230;&#8230;正常。先插一段故事，从年前到家到年后的各类聚会，总结起来比上班还累，天天吃肉喝酒，处处谈论着本不感兴趣却不得不谈的现实，开始总跟人抱怨在外地工作辛苦，天天加班拼命，上班路堵不方便，生活成本高，周六日没休息等等。后来所见所想，看着各有各的辛苦，各有各的烦恼，明白抱怨解决不了问题，抱怨只会让大家知道你在外面混的很苦而已&#8230;&#8230;徒劳的。回到正题，总结2013，先谈积极的，生活上，孩子健康成长，父母健康平安，房子按期装修，一切都正常运转按部就班，只是花钱如流水，年底混个0的开始，不过还是很欣慰，一切蛮顺利的。总结总是谈工作更多，那我也就更多的说说工作。</p> <p>&nbsp;&nbsp;&nbsp; 工作好像不那么顺利了，进入了一个很奇怪的循环，也许别人不觉得，但是我自己把现在的状态称作一种尴尬的境地。这一年，在互联网技术这个圈子里，我尝试了4种岗位，2013之前，我是一个专职的Java开发工程师，我关注的是服务端应用的架构设计、代码质量、并发和可扩展，但是到2013年底，我感觉自己成了一个时下流行的ASE&#8212;&#8212;全栈工程师（All Stack Engineer），这种转变是因为团队缺乏&#8220;资源&#8221;而被逼出来的，年初产品改版，全站大换血，没前端资源，没办法，在借了个前端做了2周demo和组件的前提下，我带着一个开发兄弟&#8220;成为&#8221;了前端，写js，写css，后来css变成了less，js也需要自己做前端组件，1个月后，我们真的成为了前端，这时候的我，开始关注数据可视化组件（了解d3和raphael），前端模块化（seajs），前端开发模式(前端OOP)和架构等，看两遍犀牛书已经满足不了我的需求，更多的js书籍进入了我的reading list。项目在快速的迭代后进入稳定，新的需求也可以快速的被完成（因为我们俩已经算是前端熟手了），而后开发人员补充一倍，机器扩容，一切看上去都开始正常。这时一个新的数据项目起步，再次面临&#8220;资源&#8221;紧张，不过这次缺的不是software engineer，而是data algorithm engineer。于是我也重复了这次紧张，进入到数据开发的角色中。数据开发其实是个有意思的角色，关注点主要是写sql，但是在大数据背景下，hadoop要非常熟悉，要理解整个生态，数据开发不是单纯的ETL，写写hive或者pig的ql，而是在结合业务的前提下设计出出色的数据架构，数据架构看上去很虚，但是其作用不亚于软件开发中的架构设计。从某种角度讲，在大数据背景下，好的数据架构起到的作用更大，直观上讲能节省大量的成本。本来数据这块角色可以分很细的，但是同样由于&#8220;资源&#8221;的问题，我承担了全部的工作，当然学习起步阶段得到了不少同事的帮助，同时也得感谢公司的大数据平台的简单易用让大数据这种比较有技术含量的东西可以轻易的被工程师使用。总的来说，2013后半年进入到数据领域，开始各种数据开发尝试。这一切看上去还挺不错的，但其实麻烦在于业务，业务发展过于变动和快速，导致工程师做东西都是填补型甚至是一次性产品，比如前后端开发成了copy&amp;paste，数据开发沦为了copy&amp;paste型的ETL，结构层次不清晰，毫无架构可言。在短时间内开发高质量产品本来是个悖论，又经历长期的加班和强制性的公司政策，这是在束缚创造力和打压创新精神&#8230;&#8230;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp; 于是，作为一个ASE，我们可能热衷于一切可能改变世界的技术，但是产品落地一定依托于某些业务，而业务往往是关键点，所以，真正的循环悖论是我们在不断的重复失败的业务，而技术的轮子由成功的业务驱动，于是周而复始，眼见别人的成功，而无能为力改变自己，这应该算是某类工程师的悲哀吧，我们可以轻易的实现各种算法，可以写大量的Java来实现控制业务逻辑，迅速的组织页面html+css+js来可视化，可以熟练的写成MR或者hive sql在大数据平台下查找数据，甚至可以轻松的把这一切全部一肩扛起，但是~~~我们改变不了产品失败的命运&#8230;&#8230;</p> <p>&nbsp;&nbsp;&nbsp; In my opinion，我们不应该仅仅是为了公司的股票和工资在做事，不能只是因为一次涨薪、一次红包、一次配股而努力，既然选择了，那么这是一份事业&#8212;&#8212;a career not only a job。3年前我放弃导师的博士学位邀约，进入大公司成为了一名普通工程师，3年后，是否要放弃大公司的庸碌而重拾自我？忙碌过后、重新思考，如果这仅仅是工作，那么趁着还不老，追寻自己的梦想吧。</p> <p>&nbsp;&nbsp;&nbsp; 就写这么多吧，总结这东西写起来总没个关注点，洋洋洒洒的，哦对了，最近流行的文体是北上广深究竟给了你什么？好吧，我不在以上四地，但是就杭州来说，我这3年，没想清楚杭州究竟给了我什么。但是我心中有个答案，你不在这些地方存在过，你永远不知道答案。</p><img src ="http://www.blogjava.net/changedi/aggbug/409578.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-02-07 15:35 <a href="http://www.blogjava.net/changedi/archive/2014/02/07/409578.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hbase配置项粗解（2）</title><link>http://www.blogjava.net/changedi/archive/2014/01/03/408449.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Fri, 03 Jan 2014 10:34:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2014/01/03/408449.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/408449.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2014/01/03/408449.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/408449.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/408449.html</trackback:ping><description><![CDATA[<p>hbase的配置接上篇</p> <p>hbase.client.write.buffer：htable客户端写缓冲区大小，默认是2097152BYTE，这个缓冲区就是为了写数据的临时存放，设置大了，浪费客户端和服务端的存储，设置小了，如果写的数据多，太多的RPC又带来网络开销，官方给的一个服务端存储耗费评估计算是：hbase.client.write.buffer*hbase.regionserver.handler.count，服务端的rs的处理handler个数也很关键；</p> <p>hbase.client.pause：pause时长，在hbase发生get或其他操作fail掉的时候进行pause的时间长度，默认是100；</p> <p>hbase.client.retries.number：发生操作fail时的重试次数，结合上一个指标一起来控制总的重试时间，默认是35；</p> <p>hbase.client.max.total.tasks：一个HTable实例可以提交给集群的最大并发任务数，默认是100；</p> <p>hbase.client.max.perserver.tasks：一个HTable实例给一台regionserver提交的最大并发任务数，默认是5；</p> <p>hbase.client.max.perregion.tasks：客户端连接一台region的最大连接数，换句话说，当你有这么多个连接在region时，新的操作不被发送直到有操作完成，默认是1；</p> <p>hbase.client.scanner.caching：做scanner的next操作时（如果再本地client没找到）缓存的数据行数，这个值的设置也需要权衡，缓存的多则快，但吃内存，缓存的少则需要多的拉数据， 需要注意的事项是如果两次调用的时间差大于scanner的timeout，则不要设置该值，默认是100；</p> <p>hbase.client.keyvalue.maxsize：一个KeyValue实例的最大大小，这是存储文件中一个entry的容量上限，合理的设置这个值可以控制regionserver的split，split不会拆keyvalue，所以把keyvalue的大小设置为regionserver大小的一个比例分数（可除）是个不错的选择，默认是10485760；</p> <p>hbase.client.scanner.timeout.period：结合刚才的caching做的一个，scanner的超时时间，默认是60000毫秒；</p> <p>hbase.client.localityCheck.threadPoolSize：做localityCheck的线程池大小，默认是2；</p> <p>hbase.bulkload.retries.number：做bulk load的最大重试次数，默认是0，即代表不断重试；</p> <p>hbase.balancer.period：Master运行balancer的周期，默认是300000毫秒；</p> <p>hbase.regions.slop：如果有regionserver的region数目超过average+(average*slop)，则rebalance，默认是0.2；</p> <p>hbase.server.thread.wakefrequency：服务线程的sleep时间，默认10000毫秒，比如log roller；</p> <p>hbase.server.versionfile.writeattempts：退出前写 version file的重试次数，默认3，每次尝试的间隔由上一个参数控制；</p> <p>hbase.hregion.memstore.flush.size：Memstore写磁盘的flush阈值，超过这个大小就flush，默认是134217728；</p> <p>hbase.hregion.preclose.flush.size：如果一个region的memstore的大小等于或超过这个参数的量，在关闭region时（放置关闭flag），要提前flush，然后region关闭下线，默认大小是5242880；</p> <p>hbase.hregion.memstore.block.multiplier：如果memstore的大小满足hbase.hregion.block.memstore * hbase.hregion.flush.size个byte，那么阻塞update，这个配置可以避免不必要的长时间split或者compact，甚至是OOME，默认是2；</p> <p>hbase.hregion.memstore.mslab.enabled：开启MemStore-Local Allocation Buffer，这个配置可以避免在高写入的情况下的堆内存碎片，可以降低在大堆情况下的stop-the-world GC频率，默认是true；</p> <p>hbase.hregion.max.filesize：HStoreFile的最大尺寸，换句话说，当一个region里的列族的任意一个HStoreFile超过这个大小，那么region进行split，默认是10737418240；</p> <p>hbase.hregion.majorcompaction：一个region的所有HStoreFile进行major compact的时间周期，默认是604800000 毫秒（7天）；</p> <p>hbase.hregion.majorcompaction.jitter：major compaction的发生抖动范围，这么理解比较容易，就是说上一个参数不是一个严格周期，会有个抖动，这个参数就是这个抖动的比例，默认是0.5；</p> <p>hbase.hstore.compactionThreshold：一个HStore存储HStoreFile的个数阈值，超过这个阈值则所有的HStoreFile会被写到一个新的HStore，需要平衡取舍，默认是3；</p> <p>hbase.hstore.blockingStoreFiles：一个HStore存储HStoreFile阻塞update的阈值，超过这个阈值，HStore就进行compaction，直到做完才允许update，默认是10；</p> <p>hbase.hstore.blockingWaitTime：一个更强力的配置，配合上一个参数，当HStore阻塞update时，超过这个时间限制，阻塞取消，就算compaction没有完成，update也不会再被阻塞，默认是90000毫秒；</p> <p>hbase.hstore.compaction.max：每个minor compaction的HStoreFile个数上限，默认是10；</p> <p>hbase.hstore.compaction.kv.max：在flushing或者compacting时允许的最大keyvalue个数，如果有大的KeyValue或者OOME的话则配置一个小的值，如果行数多且小则配置大值，默认是10；</p> <p>hbase.storescanner.parallel.seek.threads：如果并行查找开启的线程池大小，默认是10；</p> <p>hfile.block.cache.size：一个配置比例，允许最大堆的对应比例的内存作为HFile和HStoreFile的block cache，默认是0.4，即40%，设置为0则disable这个比例，不推荐这么做；</p> <p>hfile.block.index.cacheonwrite：在index写入的时候允许put无根（non-root）的多级索引块到block cache里，默认是false；</p> <p>hfile.index.block.max.size：在多级索引的树形结构里，如果任何一层的block index达到这个配置大小，则block写出，同时替换上新的block，默认是131072；</p> <p>hfile.format.version：新文件的HFile 格式版本，设置为1来测试向后兼容，默认是2；</p> <p>hfile.block.bloom.cacheonwrite：对于组合布隆过滤器的内联block开启cache-on-write，默认是false；</p> <p>io.storefile.bloom.block.size：一个联合布隆过滤器的单一块（chunk）的大小，这个值是一个逼近值，默认是131072；</p> <p>hbase.rs.cacheblocksonwrite：当一个HFile block完成时是否写入block cache，默认是false；</p><img src ="http://www.blogjava.net/changedi/aggbug/408449.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2014-01-03 18:34 <a href="http://www.blogjava.net/changedi/archive/2014/01/03/408449.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hbase配置项粗解（1）</title><link>http://www.blogjava.net/changedi/archive/2013/12/09/407372.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 09 Dec 2013 12:07:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/12/09/407372.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/407372.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/12/09/407372.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/407372.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/407372.html</trackback:ping><description><![CDATA[<p>继之前写的设计和使用tip，这里补充一下hbase所有的配置项。之前涉及的一个项目在hbase设计上存在缺陷，当进入时已经存在着很多rowkey设计和读写问题，现在重读hbase文档，把所有的配置项整理一遍。</p> <p>hbase.tmp.dir：本地文件系统的临时目录，默认是${java.io.tmpdir}/hbase-${user.name}；</p> <p>hbase.rootdir：hbase持久化的目录，被所有regionserver共享，默认${hbase.tmp.dir}/hbase，一般设置为hdfs://namenode.example.org:9000/hbase类似，带全限定名；</p> <p>hbase.cluster.distributed：hbase集群模式运作与否的标志，默认是false，开启需要设置为true，false时启动hbase会在一个jvm中运行hbase和zk；</p> <p>hbase.zookeeper.quorum：重要的也是必须设置的，启动zk的服务器列表，逗号分隔，cluster模式下必须设置，默认是localhost，hbase客户端也需要设置这个值去访问zk；</p> <p>hbase.local.dir：本地文件系统被用在本地存储的目录，默认${hbase.tmp.dir}/local/；</p> <p>hbase.master.port：hbase master绑定的端口，默认是60000；</p> <p>hbase.master.info.port：hbase master web 界面的端口，默认是60010，设置为-1可以禁用ui；</p> <p>hbase.master.info.bindAddress：master web界面的绑定地址，默认是0.0.0.0；</p> <p>hbase.master.logcleaner.plugins：清理日志的插件列表，逗号分隔，被LogService调用的LogCleanerDelegate，可以自定义，顺序执行，清理WAL和HLog；默认<code>org.apache.hadoop.hbase.master.cleaner.TimeToLiveLogCleaner</code></p> <p>hbase.master.logcleaner.ttl：HLog在.oldlogdir目录中生存的最长时间，过期则被Master起线程回收，默认是600000；</p> <p>hbase.master.hfilecleaner.plugins：HFile的清理插件列表，逗号分隔，被HFileService调用，可以自定义，默认org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner</p> <p>hbase.master.catalog.timeout：Catalog Janitor从master到META的超时时间，我们知道这个Janitor是定时的去META扫描表目录，来决定回收无用的regions，默认是600000；</p> <p>fail.fast.expired.active.master：如果master过期，那么不需要从zk恢复，直接终止，默认是false；</p> <p>hbase.master.dns.interface：master的dns接口，向该接口提供ip，默认是default；</p> <p>hbase.master.dns.nameserver：master使用的dns主机名或者ip，默认是default；</p> <p>hbase.regionserver.port：regionserver绑定的端口，默认是60020；</p> <p>hbase.regionserver.info.port：regionserver的web界面端口，-1取消界面，默认是60030；</p> <p>hbase.regionserver.info.bindAddress：regionserver的web绑定，默认是0.0.0.0；</p> <p>hbase.regionserver.info.port.auto：master或者regionserver是否自动搜索绑定的端口，默认是false；</p> <p>hbase.regionserver.handler.count：regionserver上rpc listener的个数，<a href="http://kenwublog.com/hbase-performance-tuning">http://kenwublog.com/hbase-performance-tuning</a>把这个配置称为io线程数，其实雷同，就是说在regionserver上一个处理rpc的handler，默认是30；</p> <p>hbase.regionserver.msginterval：regionserver向master发消息的间隔，默认3000毫秒；</p> <p>hbase.regionserver.optionallogflushinterval：如果没有足够的entry触发同步，那么过了这个间隔后HLog将被同步到HDFS，默认是1000毫秒；</p> <p>hbase.regionserver.regionSplitLimit：regionsplit的最大限额，默认是MAX_INT=2147483647，设置这个限制后，在到达限制时region split就不会再进行；</p> <p>hbase.regionserver.logroll.period：不管有多少版本，直接roll掉commit log的周期，也就是说一个固定的时间周期，到期就roll，默认是3600000毫秒；</p> <p>hbase.regionserver.logroll.errors.tolerated：可接受的WAL关闭错误个数，到达后将触发服务器终止；设置为0那么在WAL writer做log rolling失败时就停止region server，默认是2；</p> <p>hbase.regionserver.hlog.reader.impl：HLog 文件reader的实现类，默认是org.apache.hadoop.hbase.regionserver.wal.ProtobufLogReader；</p> <p>hbase.regionserver.hlog.writer.impl：HLog 文件writer的实现类，默认是org.apache.hadoop.hbase.regionserver.wal.ProtobufLogWriter；</p> <p>hbase.regionserver.global.memstore.upperLimit：memstore在regionserver内存中的上限，届时新的update被阻塞并且flush被强制写，默认是0.4就是堆内存的40%；阻塞状态持续到regionserver的所有memstore的容量到达hbase.regionserver.global.memstore.lowerLimit；</p> <p>hbase.regionserver.global.memstore.lowerLimit：memstore在regionserver内存中的最大上限，到达时flush就被强制写，默认是0.38等价于38%的内存容量；</p> <p>hbase.regionserver.optionalcacheflushinterval：一个edit版本在内存中的cache时长，默认3600000毫秒，设置为0的话则禁止自动flush；</p> <p>hbase.regionserver.catalog.timeout：regionserver的Catalog Janitor访问META的超时时间，默认是600000；</p> <p>hbase.regionserver.dns.interface：同master类似~~不讲</p> <p>hbase.regionserver.dns.nameserver：同master类似</p> <p>zookeeper.session.timeout：这是个值得说道一下的配置，首先ZK客户端要用，Hbase使用zk的客户端联系总体，同时也被用来启动一个zk server，作为zk的maxSessionTimeout，总的来说就是regionserver与zk的关键参数，如果连接超时，master会重新的balance，regionserver也会被从集群名单中清除，默认是90000；一个问题是如果zk 由hbase自己维护，那么该参数作为regionserver连接是一个值，如果zk在另外的集群，那么zk自己的maxSessionTimeout参数将优先于Hbase的该参数，届时可能会发生超时时间不同的问题；</p> <p>zookeeper.znode.parent：znode存放root region的地址，默认是root-region-server；</p> <p>zookeeper.znode.acl.parent：root znode的acl，默认acl；</p> <p>hbase.zookeeper.dns.interface：zk的dns接口，默认default；</p> <p>hbase.zookeeper.dns.nameserver：zk的dns服务地址，默认default；</p> <p>hbase.zookeeper.peerport：zk的peer之间的通讯端口，默认是2888；</p> <p>hbase.zookeeper.leaderport：zk选leader的通讯端口，默认是3888；</p> <p>hbase.zookeeper.useMulti：zk支持多重update，要求zk在3.4版本以上，默认是false；</p> <p>hbase.config.read.zookeeper.config：让hbaseconfig去读zk的config，默认false，也不支持开启，这个功能很搞笑~~个人观点；</p> <p>hbase.zookeeper.property.initLimit：zk的配置，同步的属性个数限制，默认10个~~没用；</p> <p>hbase.zookeeper.property.syncLimit：zk的配置，同步时的每次请求的条数，默认5个；</p> <p>hbase.zookeeper.property.dataDir：zk的配置，snapshot存放的目录，默认是${hbase.tmp.dir}/zookeeper；</p> <p>hbase.zookeeper.property.clientPort：zk的配置，client连zk的端口，默认2181；</p> <p>hbase.zookeeper.property.maxClientCnxns：zk的配置，允许接入zk的最大并发连接数的限制，按ip分配，默认300；</p><img src ="http://www.blogjava.net/changedi/aggbug/407372.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-12-09 20:07 <a href="http://www.blogjava.net/changedi/archive/2013/12/09/407372.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（7）</title><link>http://www.blogjava.net/changedi/archive/2013/11/13/406295.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Wed, 13 Nov 2013 06:41:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/11/13/406295.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/406295.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/11/13/406295.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/406295.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/406295.html</trackback:ping><description><![CDATA[<p>hive配置的最终章</p> <p>fs.har.impl：访问Hadoop Archives的实现类，低于hadoop 0.20版本的都不兼容，默认是org.apache.hadoop.hive.shims.HiveHarFileSystem；</p> <p>hive.archive.enabled：是否允许归档操作，默认是false；</p> <p>hive.archive.har.parentdir.settable：在创建HAR文件时必须要有父目录，需要手动设置，在新的hadoop版本会支持，默认是false；</p> <p>hive.support.concurrency：hive是否支持并发，默认是false，支持读写锁的话，必须要起zookeeper；</p> <p>hive.lock.mapred.only.operation：控制是否在查询时加锁，默认是false；</p> <p>hive.lock.numretries：获取锁时尝试的重试次数，默认是100；</p> <p>hive.lock.sleep.between.retries：在重试间隔的睡眠时间，默认60秒；</p> <p>hive.zookeeper.quorum：zk地址列表，默认是空；</p> <p>hive.zookeeper.client.port：zk服务器的连接端口，默认是2181；</p> <p>hive.zookeeper.session.timeout：zk客户端的session超时时间，默认是600000；</p> <p>hive.zookeeper.namespace：在所有zk节点创建后的父节点，默认是hive_zookeeper_namespace；</p> <p>hive.zookeeper.clean.extra.nodes：在session结束时清除所有额外node；</p> <p>hive.cluster.delegation.token.store.class：代理token的存储实现类，默认是org.apache.hadoop.hive.thrift.MemoryTokenStore，可以设置为org.apache.hadoop.hive.thrift.ZooKeeperTokenStore来做负载均衡集群；</p> <p>hive.cluster.delegation.token.store.zookeeper.connectString：zk的token存储连接串，默认是localhost:2181；</p> <p>hive.cluster.delegation.token.store.zookeeper.znode：token存储的节点跟路径，默认是/hive/cluster/delegation；</p> <p>hive.cluster.delegation.token.store.zookeeper.acl：token存储的ACL，默认是sasl:hive/host1@example.com:cdrwa,sasl:hive/host2@example.com:cdrwa；</p> <p>hive.use.input.primary.region：从一张input表创建表时，创建这个表到input表的主region，默认是true；</p> <p>hive.default.region.name：默认region的名字，默认是default；</p> <p>hive.region.properties：region的默认的文件系统和jobtracker，默认是空；</p> <p>hive.cli.print.header：查询输出时是否打印名字和列，默认是false；</p> <p>hive.cli.print.current.db：hive的提示里是否包含当前的db，默认是false；</p> <p>hive.hbase.wal.enabled：写入hbase时是否强制写wal日志，默认是true；</p> <p>hive.hwi.war.file：hive在web接口是的war文件的路径，默认是lib/hive-hwi-xxxx(version).war；</p> <p>hive.hwi.listen.host：hwi监听的host地址，默认是0.0.0.0；</p> <p>hive.hwi.listen.port：hwi监听的端口，默认是9999；</p> <p>hive.test.mode：hive是否运行在测试模式，默认是false；</p> <p>hive.test.mode.prefix：在测试模式运行时，表的前缀字符串，默认是test_；</p> <p>hive.test.mode.samplefreq：如果hive在测试模式运行，并且表未分桶，抽样频率是多少，默认是32；</p> <p>hive.test.mode.nosamplelist：在测试模式运行时不进行抽样的表列表，默认是空；</p><img src ="http://www.blogjava.net/changedi/aggbug/406295.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-11-13 14:41 <a href="http://www.blogjava.net/changedi/archive/2013/11/13/406295.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基数估计</title><link>http://www.blogjava.net/changedi/archive/2013/11/12/406235.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Tue, 12 Nov 2013 02:10:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/11/12/406235.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/406235.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/11/12/406235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/406235.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/406235.html</trackback:ping><description><![CDATA[<p>问题的背景是在大数据冲击下，很多数据指标（尤其是涉及到去重的）的计算无法在合理的空间和时间内完成，比如uv的计算，数学原型问题等价于持续的向一个集合中写数，重复的不记，要求最终给出集合中不重复的元素的个数（集合的势）。而比较暴力的做法是随着数字增多不断的扩展集合的大小，让它放下所有的数，最终数出这个个数就OK。显然这样的空间复杂度在单机下是做不到的，所以多数做法是利用分布式原理将uv数据隔离到不同的计算节点，每个计算节点自行维护一个类似这样的集合（wdm实时里的布隆过滤器），然后分而治之，最后merge为一份结果数据。 </p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 基数估计的初衷就是为了解决在大数据的前提下，如何以低成本的空间复杂度去计算超大集合的势的问题，换句话说，通过基数估计，单机做到计算亿级别uv，误差在4%以内。解决思路主要是概率估计，具体原理和做法参看 blog和论文原文。 </p><p>&nbsp;&nbsp;&nbsp;&nbsp; 出于实验的目的，我简单实现了暴力做法bruteforce-bf，布隆过滤器-bbf，loglog-llc和hyperloglog-hllc四个算法，比较一下基数估计这个计算去重指标的逻辑是否可行（llc非常离谱，可能是我分桶数没有调整好，就不贴出结果了）。 </p><p>预处理方法：1-N生成随机uid，模拟N次（均匀分布），jvm启动-Xmx1024m。 </p><p>实验结果： </p><p><a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_thumb.png" width="244" height="77" /></a>&nbsp;&nbsp; <a href="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blogjava.net/images/blogjava_net/changedi/WindowsLiveWriter/1f27c8e1c043_8EE3/image_thumb_1.png" width="244" height="77" /></a>  </p><p> 附加说明一下，期望值如何计算：其实这个实验的数学原型就是一个长度为k的均匀分布的（1-N)的随机数列，求不重复的元素个数的期望。我实验里k=n，这是一种极端情况（实验设计纯为方便计算，如果k较大会导致计算超慢，uv5000w时根本无法计算出来，增大k理论上会提高精度，我实验过的一组数据是100w uv 500wpv时 hllc的值是991234，误差&lt;1%），理论上k相当于pv，在递推公式中k趋于无穷时期望等于n。 </p><p>这个递推的计算可以通过组合分析推导，推导方法不详说了（当然我有可能推导错了~~数学功底 实在 不行了），通项公式见matlab代码。 </p><p> syms e n;<br /> e = n-(1/n)*((1-2*n+n*n)*((n-1)/n)^(n-2)+(1-n)*n+n*(n-1)); </p><p> vpa(subs(e,'n',1000000),10) </p><p>另外，我个人认为分布式布隆过滤器的方案是非常好的，因为空间和时间都比较均衡，且精确度高，基数估计的方法本质上空间复杂度O(1)，时间复杂度代码高效一点也可以非常快，但是缺点是精确度稍微欠缺，且不易分布式计算（因为它天生适合单进程，llc分桶均衡也是单进程做比较好，分布式完全是牛刀杀鸡）。 </p><p>ref blog: <a href="http://blog.codinglabs.org/articles/cardinality-estimate-exper.html#ref4">http://blog.codinglabs.org/articles/cardinality-estimate-exper.html#ref4</a> </p><p>算法实现的java代码可见github： <a href="https://github.com/changedi/card-estimate">https://github.com/changedi/card-estimate</a> </p><img src ="http://www.blogjava.net/changedi/aggbug/406235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-11-12 10:10 <a href="http://www.blogjava.net/changedi/archive/2013/11/12/406235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（6）</title><link>http://www.blogjava.net/changedi/archive/2013/10/26/405682.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Sat, 26 Oct 2013 11:35:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/10/26/405682.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/405682.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/10/26/405682.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/405682.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/405682.html</trackback:ping><description><![CDATA[<p>hive的index索引相关、统计相关和认证授权相关的配置。</p> <p>hive.index.compact.file.ignore.hdfs：在索引文件中存储的hdfs地址将在运行时被忽略，如果开启的话；如果数据被迁移，那么索引文件依然可用，默认是false；</p> <p>hive.optimize.index.filter.compact.minsize：压缩索引自动应用的最小输入大小，默认是5368709120；</p> <p>hive.optimize.index.filter.compact.maxsize：同上，相反含义，如果是负值代表正无穷，默认是-1；</p> <p>hive.index.compact.query.max.size：一个使用压缩索引做的查询能取到的最大数据量，默认是10737418240 个byte；负值代表无穷大；</p> <p>hive.index.compact.query.max.entries：使用压缩索引查询时能读到的最大索引项数，默认是10000000；负值代表无穷大；</p> <p>hive.index.compact.binary.search：在索引表中是否开启二分搜索进行索引项查询，默认是true；</p> <p>hive.exec.concatenate.check.index：如果设置为true，那么在做ALTER TABLE tbl_name CONCATENATE on a table/partition（有索引） 操作时，抛出错误；可以帮助用户避免index的删除和重建；</p> <p>hive.stats.dbclass：存储hive临时统计信息的数据库，默认是jdbc:derby；</p> <p>hive.stats.autogather：在insert overwrite命令时自动收集统计信息，默认开启true；</p> <p>hive.stats.jdbcdriver：数据库临时存储hive统计信息的jdbc驱动；</p> <p>hive.stats.dbconnectionstring：临时统计信息数据库连接串，默认jdbc:derby:databaseName=TempStatsStore;create=true；</p> <p>hive.stats.defaults.publisher：如果dbclass不是jdbc或者hbase，那么使用这个作为默认发布，必须实现StatsPublisher接口，默认是空；</p> <p>hive.stats.defaults.aggregator：如果dbclass不是jdbc或者hbase，那么使用该类做聚集，要求实现StatsAggregator接口，默认是空；</p> <p>hive.stats.jdbc.timeout：jdbc连接超时配置，默认30秒；</p> <p>hive.stats.retries.max：当统计发布合聚集在更新数据库时出现异常时最大的重试次数，默认是0，不重试；</p> <p>hive.stats.retries.wait：重试次数之间的等待窗口，默认是3000毫秒；</p> <p>hive.client.stats.publishers：做count的job的统计发布类列表，由逗号隔开，默认是空；必须实现org.apache.hadoop.hive.ql.stats.ClientStatsPublisher接口；</p> <p>hive.client.stats.counters：没什么用~~~</p> <p>hive.security.authorization.enabled：hive客户端是否认证，默认是false；</p> <p>hive.security.authorization.manager：hive客户端认证的管理类，默认是org.apache.hadoop.hive.ql.security.authorization.DefaultHiveAuthorizationProvider；用户定义的要实现org.apache.hadoop.hive.ql.security.authorization.HiveAuthorizationProvider；</p> <p>hive.security.authenticator.manager：hive客户端授权的管理类，默认是org.apache.hadoop.hive.ql.security.HadoopDefaultAuthenticator；用户定义的需要实现org.apache.hadoop.hive.ql.security.HiveAuthenticatorProvider；</p> <p>hive.security.authorization.createtable.user.grants：当表创建时自动授权给用户，默认是空；</p> <p>hive.security.authorization.createtable.group.grants：同上，自动授权给组，默认是空；</p> <p>hive.security.authorization.createtable.role.grants：同上，自动授权给角色，默认是空；</p> <p>hive.security.authorization.createtable.owner.grants：同上，自动授权给owner，默认是空；</p> <p>hive.security.metastore.authorization.manager：metastore的认证管理类，默认是org.apache.hadoop.hive.ql.security.authorization.DefaultHiveMetastoreAuthorizationProvider；用户定义的必须实现org.apache.hadoop.hive.ql.security.authorization.HiveMetastoreAuthorizationProvider接口；接口参数要包含org.apache.hadoop.hive.ql.security.authorization.StorageBasedAuthorizationProvider接口；使用HDFS的权限控制认证而不是hive的基于grant的方式；</p> <p>hive.security.metastore.authenticator.manager：metastore端的授权管理类，默认是org.apache.hadoop.hive.ql.security.HadoopDefaultMetastoreAuthenticator，自定义的必须实现org.apache.hadoop.hive.ql.security.HiveAuthenticatorProvider接口；</p> <p>hive.metastore.pre.event.listeners：在metastore做数据库任何操作前执行的事件监听类列表；</p><img src ="http://www.blogjava.net/changedi/aggbug/405682.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-10-26 19:35 <a href="http://www.blogjava.net/changedi/archive/2013/10/26/405682.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hive配置项的含义详解（5）</title><link>http://www.blogjava.net/changedi/archive/2013/10/14/404981.html</link><dc:creator>changedi</dc:creator><author>changedi</author><pubDate>Mon, 14 Oct 2013 09:39:00 GMT</pubDate><guid>http://www.blogjava.net/changedi/archive/2013/10/14/404981.html</guid><wfw:comment>http://www.blogjava.net/changedi/comments/404981.html</wfw:comment><comments>http://www.blogjava.net/changedi/archive/2013/10/14/404981.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/changedi/comments/commentRss/404981.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/changedi/services/trackbacks/404981.html</trackback:ping><description><![CDATA[<p>关于MetaStore：metastore是个独立的关系数据库，用来持久化schema和系统元数据。</p> <p>hive.metastore.local：控制hive是否连接一个远程metastore服务器还是开启一个本地客户端jvm，默认是true，Hive0.10已经取消了该配置项；</p> <p>javax.jdo.option.ConnectionURL：JDBC连接字符串，默认jdbc:derby:;databaseName=metastore_db;create=true；</p> <p>javax.jdo.option.ConnectionDriverName：JDBC的driver，默认<tt>org.apache.derby.jdbc.EmbeddedDriver</tt>；</p> <p>javax.jdo.PersisteneManagerFactoryClass：实现JDO PersistenceManagerFactory的类名，默认org.datanucleus.jdo.JDOPersistenceManagerFactory；</p> <p>javax.jdo.option.DetachAllOnCommit：事务提交后detach所有提交的对象，默认是true；</p> <p>javax.jdo.option.NonTransactionalRead：是否允许非事务的读，默认是true；</p> <p>javax.jdo.option.ConnectionUserName：username，默认APP；</p> <p>javax.jdo.option.ConnectionPassword：password，默认mine；</p> <p>javax.jdo.option.Multithreaded：是否支持并发访问metastore，默认是true；</p> <p>datanucleus.connectionPoolingType：使用连接池来访问JDBC metastore，默认是DBCP；</p> <p>datanucleus.validateTables：检查是否存在表的schema，默认是false；</p> <p>datanucleus.validateColumns：检查是否存在列的schema，默认false；</p> <p>datanucleus.validateConstraints：检查是否存在constraint的schema，默认false；</p> <p>datanucleus.stroeManagerType：元数据存储类型，默认rdbms；</p> <p>datanucleus.autoCreateSchema：在不存在时是否自动创建必要的schema，默认是true；</p> <p>datanucleus.aotuStartMechanismMode：如果元数据表不正确，抛出异常，默认是checked；</p> <p>datanucleus.transactionIsolation：默认的事务隔离级别，默认是read-committed；</p> <p>datanucleus.cache.level2：使用二级缓存，默认是false；</p> <p>datanucleus.cache.level2.type：二级缓存的类型，有两种，SOFT:软引用，WEAK:弱引用，默认是SOFT；</p> <p>datanucleus.identifierFactory：id工厂生产表和列名的名字，默认是datanucleus；</p> <p>datanucleus.plugin.pluginRegistryBundleCheck：当plugin被发现并且重复时的行为，默认是LOG；</p> <p>hive.metastroe.warehouse.dir：数据仓库的位置，默认是/user/hive/warehouse；</p> <p>hive.metastore.execute.setugi：非安全模式，设置为true会令metastore以客户端的用户和组权限执行DFS操作，默认是false，这个属性需要服务端和客户端同时设置；</p> <p>hive.metastore.event.listeners：metastore的事件监听器列表，逗号隔开，默认是空；</p> <p>hive.metastore.partition.inherit.table.properties：当新建分区时自动继承的key列表，默认是空；</p> <p>hive.metastore.end.function.listeners：metastore函数执行结束时的监听器列表，默认是空；</p> <p>hive.metastore.event.expiry.duration：事件表中事件的过期时间，默认是0；</p> <p>hive.metastore.event.clean.freq：metastore中清理过期事件的定时器的运行周期，默认是0；</p> <p>hive.metastore.connect.retries：创建metastore连接时的重试次数，默认是5；</p> <p>hive.metastore.client.connect.retry.delay：客户端在连续的重试连接等待的时间，默认1；</p> <p>hive.metastore.client.socket.timeout：客户端socket超时时间，默认20秒；</p> <p>hive.metastore.rawstore.impl：原始metastore的存储实现类，默认是org.apache.hadoop.hive.metastore.ObjectStore；</p> <p>hive.metastore.batch.retrieve.max：在一个batch获取中，能从metastore里取出的最大记录数，默认是300；</p> <p>hive.metastore.ds.connection.url.hook：查找JDO连接url时hook的名字，默认是javax.jdo.option.ConnectionURL；</p> <p>hive.metastore.ds.retry.attempts：当出现连接错误时重试连接的次数，默认是1次；</p> <p>hive.metastore.ds.retry.interval：metastore重试连接的间隔时间，默认1000毫秒；</p> <p>hive.metastore.server.min.threads：在thrift服务池中最小的工作线程数，默认是200；</p> <p>hive.metastore.server.max.threads：最大线程数，默认是100000；</p> <p>hive.metastore.server.tcp.keepalive：metastore的server是否开启长连接，长连可以预防半连接的积累，默认是true；</p> <p>hive.metastore.sasl.enabled：metastore thrift接口的安全策略，开启则用SASL加密接口，客户端必须要用Kerberos机制鉴权，默认是不开启false；</p> <p>hive.metastore.kerberos.keytab.file：在开启sasl后kerberos的keytab文件存放路径，默认是空；</p> <p>hive.metastore.kerberos.principal：kerberos的principal，_HOST部分会动态替换，默认是<a href="mailto:hive-metastore/_HOST@EXAMPLE.COM">hive-metastore/_HOST@EXAMPLE.COM</a>；</p> <p>hive.metastore.cache.pinobjtypes：在cache中支持的metastore的对象类型，由逗号分隔，默认是Table,StorageDescriptor,SerDeInfo,Partition,Database,Type,FieldSchema,Order；</p> <p>hive.metastore.authorization.storage.checks：在做类似drop partition操作时，metastore是否要认证权限，默认是false；</p> <p>hive.metastore.schema.verification：强制metastore的schema一致性，开启的话会校验在metastore中存储的信息的版本和hive的jar包中的版本一致性，并且关闭自动schema迁移，用户必须手动的升级hive并且迁移schema，关闭的话只会在版本不一致时给出警告，默认是false不开启；</p><img src ="http://www.blogjava.net/changedi/aggbug/404981.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/changedi/" target="_blank">changedi</a> 2013-10-14 17:39 <a href="http://www.blogjava.net/changedi/archive/2013/10/14/404981.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>