﻿<?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-Skynet-随笔分类-算法/函数</title><link>http://www.blogjava.net/Skynet/category/42816.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 08 Dec 2009 08:15:29 GMT</lastBuildDate><pubDate>Tue, 08 Dec 2009 08:15:29 GMT</pubDate><ttl>60</ttl><item><title>跟我一起学 - 算法导论 - 线性时间排序</title><link>http://www.blogjava.net/Skynet/archive/2009/12/08/305156.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Tue, 08 Dec 2009 06:21:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/12/08/305156.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/305156.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/12/08/305156.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/305156.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/305156.html</trackback:ping><description><![CDATA[<strong>下面介绍的排序都为：非比较排序法</strong><br />
<br />
这里个人认为在某些特定的地方非比较排序的速度非常明显；<br />
比如 ： 对待排数据中有顺序分类，使用鸽巢总体分类，然后对不同类别的待排小数据集合采用 插入，快排等排序方式<br />
<br />
<br />
Counting sort ：计数排序<br />
描述：迭代待排序数组出元素x，确定小于此元素[z]个数，然后把x放到它在的最终输出数组[z]上。<br />
特性：与待排值有关；稳定的排序算法；待排序数据要求过于严格，无实际用处；<br />
算法的步骤如下：<br />
&nbsp;&nbsp; 1. 找出待排序的数组中最大和最小的元素<br />
&nbsp;&nbsp; 2. 统计数组中每个值为i的元素出现的次数，存入数组C的第i项<br />
&nbsp;&nbsp; 3. 对所有的计数累加（从C中的第一个元素开始，每一项和前一项相加）<br />
&nbsp;&nbsp; 4. 反向填充目标数组：将每个元素i放在新数组的第C(i)项，每放一个元素就将C(i)减去1<br />
<br />
<br />
Radix sort：基数排序<br />
描述：将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零. 然后, 从最低位开始, 依次进行一次排序.这样从最低位排序一直到最高位排序完成以后, 数列就变成一个有序序列.<br />
排序方式：LSD 由右向左排；MSD 由左向右排<br />
特性：非比较排序；待排数据需要统一格式；<br />
假设需排序数列的取值范围从1...k,我们于是建一个K+1元的数组 a[]，并赋初值为0，然后便开始排序工作：<br />
算法的步骤如下：<br />
&nbsp;&nbsp; 1. 按输入顺序读入数列，如果所读的元素为i(1&lt;=i&lt;=k),我们就将a[i]的值加一，这样直到读完所有的元素。<br />
&nbsp;&nbsp; 2. 读出元素并排序：我们遍历整个数组，如果a[i]=j(j&gt;=0),我们就输出j次i(表示元素i在原先数列中出现了j次),这样输出的序列就是已排序的。<br />
&nbsp;&nbsp; 3. 由于一般排序算法涉及到元素之间的比较，如果化成比较树可以知道，这样的排序算法复杂度的下限是O(N*lnN),而计数排序没有比较元素，所以所需排序时间就少了，我们可以看到计数排序的复杂度为O(n*k)，其中k（k的定义同上）为合并排列所需的时间，是个常数。<br />
&nbsp;&nbsp; 4. 此算法适合所需排列的元素取值范围不大的情况下，否则会造成空间的消耗，比如，一共100个元素，其取值范围从1-100000，显然这个时候用基数排序是不合适的。<br />
<br />
<br />
<br />
Bucket sort：桶排序<br />
描述：工作的原理是将阵列分到有限数量的桶子里。每个桶子再个别排序（有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序）。<br />
桶排序以下列程序进行：<br />
&nbsp;&nbsp; 1. 设置一个定量的阵列当作空桶子。<br />
&nbsp;&nbsp; 2. 寻访序列，并且把项目一个一个放到对应的桶子去。<br />
&nbsp;&nbsp; 3. 对每个不是空的桶子进行排序。<br />
&nbsp;&nbsp; 4. 从不是空的桶子里把项目再放回原来的序列中。<br />
<br />
<br />
<br />
Pigeonhole sort：鸽巢排序<br />
描述：是一种时间复杂度为O(n)且在不可避免遍历每一个元素并且排序的情况下效率最好的一种排序算法. 但它只有在差值(或者可被映射在差值)很小的范围内的数值排序的情况下实用.<br />
算法的步骤如下：与桶排同<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/305156.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-12-08 14:21 <a href="http://www.blogjava.net/Skynet/archive/2009/12/08/305156.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 算法导论 - 快速排序</title><link>http://www.blogjava.net/Skynet/archive/2009/12/03/304668.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Thu, 03 Dec 2009 09:11:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/12/03/304668.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/304668.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/12/03/304668.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/304668.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/304668.html</trackback:ping><description><![CDATA[<br />
# 快排 和 分治 很像 都是<strong>分而治之</strong> ，但他们却是 相反的 方式排序 ：<br />
分治 是想拆分完成后，合并以有序的小段进行 <strong>排序</strong> ，而快排是直接由原始的&#8220;拆分&#8221;来<strong>排序</strong> 。<br />
<br />
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008000;">#</span><span style="color: #008000;">encoding=utf-8</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">从&nbsp;大&nbsp;到&nbsp;小</span><span style="color: #008000;"><br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;partition(A,p,r):<br />
&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000;">=</span><span style="color: #000000;">A[p]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;True&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">r&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;A[p]&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;tmp&nbsp;:&nbsp;p</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;r</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">p&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;A[r]&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;tmp&nbsp;:&nbsp;r</span><span style="color: #000000;">-=</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;A[p]</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">A[r]:&nbsp;A[p],A[r]</span><span style="color: #000000;">=</span><span style="color: #000000;">A[r],A[p]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;r</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">p&nbsp;:&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;p<br />
<br />
<br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;quickSort(A,p,r):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">r:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q</span><span style="color: #000000;">=</span><span style="color: #000000;">partition(A,p,r)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quickSort(A,p,q)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;quickSort(A,q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,r)<br />
<br />
A</span><span style="color: #000000;">=</span><span style="color: #000000;">[</span><span style="color: #000000;">9</span><span style="color: #000000;">,</span><span style="color: #000000;">61</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">,</span><span style="color: #000000;">14</span><span style="color: #000000;">,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">,</span><span style="color: #000000;">667</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">8</span><span style="color: #000000;">]<br />
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;A<br />
quickSort(A,0,len(A)</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;A<br />
<strong># 结果 </strong></span><strong>[667, 61, 14, 9, 8, 7, 7, 6, 3, -1]</strong></div>
<br />
<br />
<br />
图解：<br />
一次迭代过程描述 （从小到大）：<br />
1. 以 A[0] 为切分点 用临时变量 记录 这里是 <strong>切分点</strong> = [5] <br />
2. 分别起 2枚指针 [切分起左,右]<br />
3. 分别向中间 靠拢 ， 当左指针指向值大于 切分点 停止 左 ， 右指针指向值 小于 切分点 停止 右 。<br />
4. 判断 是否是&nbsp; 停止点 上 左值 小于 右值 是：交换两指针值 ！ <br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/42926/qs.jpg" alt="" border="0" /><br />
<strong>第一次迭代后 :&nbsp; <br />
&nbsp; 以初始分隔 [5]&nbsp; 就已经切分好了 小于 5 的左 ，大于等于5 的右</strong><br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/304668.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-12-03 17:11 <a href="http://www.blogjava.net/Skynet/archive/2009/12/03/304668.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 算法导论 - 堆</title><link>http://www.blogjava.net/Skynet/archive/2009/12/01/304352.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Tue, 01 Dec 2009 04:05:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/12/01/304352.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/304352.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/12/01/304352.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/304352.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/304352.html</trackback:ping><description><![CDATA[<strong><br />
转自【</strong>http://www.cnblogs.com/coderzh/archive/2008/09/22/1296195.html<strong>】<br />
</strong>作者：<a target="_blank" href="http://coderzh.cnblogs.com/">CoderZh</a>（<a target="_blank" href="http://coderzh.cnblogs.com/">CoderZh的技术博客 - 博客园</a>）<br />
出处：<a target="_blank" href="http://coderzh.cnblogs.com/">http://coderzh.cnblogs.com/</a><br />
# 我这 加了些 个人理解的 说明<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008000;">#</span><span style="color: #008000;">encoding:UTF-8</span><span style="color: #008000;"><br />
#</span><span style="color: #008000;">&nbsp;[递归]&nbsp;-&nbsp;单条路&nbsp;自下往上排序&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;heap_adjust(data,&nbsp;s,&nbsp;m):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;m:</span><span style="color: #0000ff;">return</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;声明&nbsp;预设父节点位置</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;temp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;s&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;[左]子节点值&nbsp;大于&nbsp;父节点值&nbsp;&nbsp;:&nbsp;&nbsp;预设父节点位置&nbsp;为&nbsp;左子节点位置</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;data[</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">s&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;data[temp]:&nbsp;temp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">s</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;[右]子节点值&nbsp;大于&nbsp;预设父节点&nbsp;:&nbsp;预设父节点位置&nbsp;为&nbsp;右子节点位置</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;m&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">and</span><span style="color: #000000;">&nbsp;data[</span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">s]&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;data[temp]:&nbsp;temp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;s<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;交换值&nbsp;满足&nbsp;堆特性&nbsp;此为&nbsp;[&nbsp;父节点&nbsp;小于&nbsp;子节点&nbsp;&nbsp;]</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;temp&nbsp;</span><span style="color: #000000;">&lt;&gt;</span><span style="color: #000000;">&nbsp;s&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[s&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;data[temp]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data[temp],&nbsp;data[s&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;heap_adjust(data,&nbsp;temp&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;m)<br />
<br />
<br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;heap_sort(data):<br />
&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;len(data)&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;构建&nbsp;堆树</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;测试数据&nbsp;[3,2,1]&nbsp;数组值为&nbsp;所以非底层叶节点</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;range(m&nbsp;,&nbsp;0,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;heap_adjust(data,&nbsp;i,&nbsp;len(data))<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;从堆树中&nbsp;[出栈]&nbsp;排序输出</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;测试数据&nbsp;[5,&nbsp;4,&nbsp;3,&nbsp;2]</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;data[0],&nbsp;data[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;data[0]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;range(len(data)&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;heap_adjust(data,&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;n)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data[0],&nbsp;data[n&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;data[n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">],&nbsp;data[0]<br />
<br />
<br />
<br />
data</span><span style="color: #000000;">=</span><span style="color: #000000;">[</span><span style="color: #000000;">2</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">6</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">868</span><span style="color: #000000;">,</span><span style="color: #000000;">9</span><span style="color: #000000;">,</span><span style="color: #000000;">8</span><span style="color: #000000;">,</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]<br />
heap_sort(data)<br />
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;data<br />
</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;[-1,&nbsp;2,&nbsp;3,&nbsp;3,&nbsp;6,&nbsp;8,&nbsp;9,&nbsp;868]</span></div>
<strong><br />
<br />
转自 【 </strong>http://www.cppblog.com/guogangj/<strong> 】<br />
堆存储</strong>
<br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/42926/o_ds_binary_heap_array.png" alt="" border="0" /><br />
<br />
<br />
<strong>堆 入栈 复杂度为&#927;(logn)</strong><br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/42926/r_ds_binary_heap_insert.png" alt="" border="0" /><br />
<br />
<br />
<br />
<br />
<strong>堆 出栈&nbsp; &#927;(logn)</strong><br />
<br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/42926/o_ds_binary_heap_dequeue.png" alt="" border="0" /><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/304352.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-12-01 12:05 <a href="http://www.blogjava.net/Skynet/archive/2009/12/01/304352.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 算法导论 - 递归式</title><link>http://www.blogjava.net/Skynet/archive/2009/11/25/303704.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Wed, 25 Nov 2009 15:09:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/25/303704.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/303704.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/25/303704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/303704.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/303704.html</trackback:ping><description><![CDATA[<br />
看第二节&nbsp; - 递归树方法 ：<br />
突发奇想 能否 使用 txt 构造出 递归过程&nbsp; <br />
还是有 上此提到的 递归方法 分治排序<br />
<br />
<br />
<br />
&nbsp;<br />
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;encoding:&nbsp;utf-8&nbsp;&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">arr</span><span style="color: #000000;">=</span><span style="color: #000000;">[]<br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;printTree():<br />
&nbsp;&nbsp;&nbsp;&nbsp;ac&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;ii&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;r&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;arr&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c,ss,cc&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;r&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[</span><span style="color: #800000;">'</span><span style="color: #800000;">&nbsp;</span><span style="color: #800000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;xrange(cc</span><span style="color: #000000;">*</span><span style="color: #000000;">(c</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">))]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;xrange(len(sc))&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;cc&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;0&nbsp;:&nbsp;sc[i]</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">│</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">print&nbsp;"ci&nbsp;%s&nbsp;ii&nbsp;%s&nbsp;=&nbsp;%s&nbsp;"%(ci,ii,ii&nbsp;&lt;&nbsp;ci)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;ii</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">c&nbsp;&nbsp;:&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">""</span><span style="color: #000000;">.join(sc)</span><span style="color: #000000;">+</span><span style="color: #800000;">"</span><span style="color: #800000;">├─</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">ss</span><span style="color: #000000;">+</span><span style="color: #800000;">'</span><span style="color: #800000;">&nbsp;&nbsp;</span><span style="color: #800000;">'</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">""</span><span style="color: #000000;">.join(sc)</span><span style="color: #000000;">+</span><span style="color: #800000;">"</span><span style="color: #800000;">└─</span><span style="color: #800000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">ss<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;c<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ac.append(&nbsp;sc&nbsp;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;r&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;ac[::</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;r<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;MERGE(A,p,q,r):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">print&nbsp;"%s:%s&nbsp;-&nbsp;%s:%s"&nbsp;%&nbsp;(p,q+1,q+1,r+1)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">==</span><span style="color: #000000;">q&nbsp;:&nbsp;L&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[A[p],</span><span style="color: #000000;">10</span><span style="color: #000000;">**</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;:&nbsp;L&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;A[p:q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">+</span><span style="color: #000000;">[</span><span style="color: #000000;">10</span><span style="color: #000000;">**</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">==</span><span style="color: #000000;">r&nbsp;:&nbsp;R&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[A[r],</span><span style="color: #000000;">10</span><span style="color: #000000;">**</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;:&nbsp;R&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;A[q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">:r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">+</span><span style="color: #000000;">[</span><span style="color: #000000;">10</span><span style="color: #000000;">*</span><span style="color: #000000;">10</span><span style="color: #000000;">]<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;0<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;k&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;xrange(p,r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;L[i]</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">R[j]&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[k]</span><span style="color: #000000;">=</span><span style="color: #000000;">L[i]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[k]</span><span style="color: #000000;">=</span><span style="color: #000000;">R[j]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;print&nbsp;"%s:%s&nbsp;=&nbsp;%s&nbsp;\n%s:%s&nbsp;=&nbsp;%s\n\n%s"&nbsp;%&nbsp;(&nbsp;p,q,&nbsp;L&nbsp;,&nbsp;q+1,r,R,&nbsp;A)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;"><br />
<br />
</span><span style="color: #0000ff;">def</span><span style="color: #000000;">&nbsp;MERGE_SORT(A,p,r,c</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">r:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(p</span><span style="color: #000000;">+</span><span style="color: #000000;">r)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MERGE_SORT(A,p,q,c</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MERGE_SORT(A,q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,r,c</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr.append(&nbsp;(c,</span><span style="color: #800000;">"</span><span style="color: #800000;">%s&nbsp;-&nbsp;%s</span><span style="color: #800000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;(&nbsp;A[p:q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">],A[q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">:r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">])&nbsp;,&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">&nbsp;)&nbsp;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">#</span><span style="color: #008000;">&nbsp;Debugging(A,p,q,r,&nbsp;sc&nbsp;)</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MERGE(A,p,q,r)<br />
<br />
A</span><span style="color: #000000;">=</span><span style="color: #000000;">[</span><span style="color: #000000;">5</span><span style="color: #000000;">,</span><span style="color: #000000;">2</span><span style="color: #000000;">,</span><span style="color: #000000;">7</span><span style="color: #000000;">,</span><span style="color: #000000;">4</span><span style="color: #000000;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">,</span><span style="color: #000000;">3</span><span style="color: #000000;">,</span><span style="color: #000000;">2</span><span style="color: #000000;">,</span><span style="color: #000000;">6</span><span style="color: #000000;">]<br />
MERGE_SORT(A,0,len(A)</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />
</span><span style="color: #0000ff;">print</span><span style="color: #000000;">&nbsp;A<br />
printTree()&nbsp;<br />
<br />
<br />
</span></div>
<br />
<br />
输出 （重下往上看&nbsp; 输出 排序过程 ，我就不多说了 应该很好理解了！！）：<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #000000;">[1,&nbsp;2,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7]<br />
├─[2,&nbsp;4,&nbsp;5,&nbsp;7]&nbsp;-&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;6]<br />
│&nbsp;&nbsp;├─[1,&nbsp;3]&nbsp;-&nbsp;[2,&nbsp;6]<br />
│&nbsp;&nbsp;│&nbsp;&nbsp;├─[2]&nbsp;-&nbsp;[6]<br />
│&nbsp;&nbsp;│&nbsp;&nbsp;└─[1]&nbsp;-&nbsp;[3]<br />
│&nbsp;&nbsp;├─[2,&nbsp;5]&nbsp;-&nbsp;[4,&nbsp;7]<br />
│&nbsp;&nbsp;│&nbsp;&nbsp;├─[7]&nbsp;-&nbsp;[4]<br />
│&nbsp;&nbsp;│&nbsp;&nbsp;└─[5]&nbsp;-&nbsp;[2]</span></div>
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/303704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-25 23:09 <a href="http://www.blogjava.net/Skynet/archive/2009/11/25/303704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 算法导论 - 函数的增长</title><link>http://www.blogjava.net/Skynet/archive/2009/11/23/303404.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Mon, 23 Nov 2009 15:33:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/23/303404.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/303404.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/23/303404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/303404.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/303404.html</trackback:ping><description><![CDATA[<br />
<img alt="" src="http://www.blogjava.net/images/blogjava_net/skynet/ywl.jpg" border="0" width="161" height="186" /><br />
<strong><br />
<br />
<br />
3.1 渐近号</strong><br />
<br />
渐近范围 &nbsp;&nbsp;&nbsp;&nbsp; f(n) =&nbsp;&#952;(g(n))&nbsp; ~a=b&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
渐近上界&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f(n) = &#927;(g(n))&nbsp; ~a&lt;=b &nbsp;&nbsp; 0&#8804;f(n)&#8804;cg(n)
<br />
渐近下界&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f(n) = &#937;(g(n))&nbsp; ~a&gt;=b&nbsp;&nbsp;&nbsp; 0&#8804;cg(n)&#8804;f(n)<br />
非渐近上界&nbsp;&nbsp; f(n) = o(g(n))&nbsp;&nbsp;&nbsp; ~a&lt;b&nbsp;&nbsp;&nbsp;&nbsp; 0&#8804;f(n)&lt;cg(n)&nbsp;&nbsp;
=&gt;lim[n&lt;=&#8734;](f(n)/g(n))=0<br />
非渐近下界&nbsp;&nbsp; f(n) = &#969;(g(n))&nbsp;&nbsp; ~a&gt;b&nbsp;&nbsp;&nbsp;&nbsp; 0&#8804;cg(n)&lt;f(n)&nbsp;&nbsp;
=&gt;lim[n&lt;=&#8734;](f(n)/g(n))=0<br />
<br />
<br />
渐近号使用（目前我能理解到的！）: <br />
当渐近符号出现在某个公式中时，我们将其解释为一个不在乎其名称的署名函数。<br />
例：2n^2+3n+1 = 2n^2+&#952;(n) ，这种用法有助于屏蔽无关紧要的细节，如低阶项。。<br />
<br />
∑[1&#8804;k&#8804;n]O(i)<br />
<br />
<br />
<strong>3.2 标准记号和常量函数</strong><br />
单调性 ： 单调递增 ， 单调递减<br />
# 传说中的广播体操原来是 上下取整啊 ！ 呵呵<br />
下取整，上取整 ： x-1 &lt; └X┘ &lt;=&nbsp; x&nbsp;&nbsp; &lt;=&nbsp; ┌X┐&nbsp; &lt;&nbsp; x+1<br />
<br />
取模运算&nbsp; a mod n&nbsp; = a-└a/n┘n <br />
<br />
多项式&nbsp; p(n) = ∑[0&#8804;i&#8804;d] a.i n^i<br />
<br />
指数 (a^m)^n = a^(m*n)&nbsp;&nbsp; ;&nbsp; a^m*a^n = a^(m+n)<br />
<br />
# 指数中的 特殊符号 e <br />
# e不论对x微分几次，结果都还是e！难怪数学系学生会用e比喻坚定不移的爱情！ <br />
# 数学中的爱情符号 e 哈哈！！<br />
e <span name="whlm" id="whlm">= lim[</span>n&#8804;&#8734;<span name="whlm" id="whlm">](1+1/n)^n&nbsp; </span><br />
<br />
<br />
对数 <br />
lgn = log_2(n)<br />
lnn=log_e(n)<br />
lg^k(n)=(lgn)^k<br />
lg lg n = lg(lgn)<br />
<br />
<br />
阶乘&nbsp; n!<br />
<br />
<br />
函数迭代<br />
<br />
<br />
斐波那切 <br />
F0 = 0<br />
F1 = 1<br />
..<br />
Fi = Fi-1+Fi-2<br />
&nbsp;<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/303404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-23 23:33 <a href="http://www.blogjava.net/Skynet/archive/2009/11/23/303404.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>推荐算法 - 皮尔逊函数</title><link>http://www.blogjava.net/Skynet/archive/2009/11/22/303271.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Sun, 22 Nov 2009 15:56:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/22/303271.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/303271.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/22/303271.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/303271.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/303271.html</trackback:ping><description><![CDATA[<br />
<br />
公式：<br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/113f06f5bbf.jpg" alt="" border="0" height="171" width="426" /><br />
<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">数据&nbsp;elt&nbsp;清洗后(txt)</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;一般&nbsp;user&nbsp;和&nbsp;item&nbsp;分值化&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;比如&nbsp;用户下载，收藏，试听&nbsp;某item&nbsp;等等</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">user&nbsp;&nbsp;&nbsp;&nbsp;items&nbsp;&nbsp;&nbsp;&nbsp;score<br />
<img src="http://www.blogjava.net/Images/dot.gif" alt="" />.<br />
<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;结果输出&nbsp;(bdb)</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;user&nbsp;&nbsp;&nbsp;&nbsp;item1:score1,item2:score2,item3:score3<img src="http://www.blogjava.net/Images/dot.gif" alt="" /><img src="http://www.blogjava.net/Images/dot.gif" alt="" />.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
python</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">EOF<br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;bsddb<br />
db&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;bsddb.hashopen(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-items.db</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;row&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;open(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-item-sc.txt</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;row</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">row.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">\n</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)[0]<br />
&nbsp;&nbsp;&nbsp;&nbsp;dr&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;row.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;db.has_key(dr[0])&nbsp;:&nbsp;db[dr[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;db[dr[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">db[dr[0]]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dr[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br />
<br />
db.close()<br />
EOF<br />
<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;结果输出&nbsp;(txt)</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;user&nbsp;&nbsp;&nbsp;&nbsp;user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><img src="http://www.blogjava.net/Images/dot.gif" alt="" /><br />
<br />
python</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">EOF<br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;bsddb<br />
</span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);">&nbsp;math&nbsp;</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"><br />
db&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;bsddb.hashopen(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-items.db</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
<br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;ps(u1,u2):<br />
&nbsp;&nbsp;&nbsp;&nbsp;um1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;db[u1].split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;um1[v[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">float(v[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br />
&nbsp;&nbsp;&nbsp;&nbsp;um2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />
&nbsp;&nbsp;&nbsp;&nbsp;si</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;db[u2].split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v.split(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;um2[v[0]]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">float(v[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;um1.has_key(&nbsp;v[0]&nbsp;)&nbsp;:&nbsp;si.append(v[0])<br />
&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;len(si)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;n&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);">&nbsp;:</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;None<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;sum1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum(&nbsp;[um1[it]&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;it&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;si]&nbsp;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;sum2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum(&nbsp;[um2[it]&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;it&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;si]&nbsp;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;sum1Sq</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum([&nbsp;pow(um1[it],</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;it&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;si])<br />
&nbsp;&nbsp;&nbsp;&nbsp;sum2Sq</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum([&nbsp;pow(um2[it],</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;it&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;si])<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;pSum&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;sum(&nbsp;[&nbsp;um1[it]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">um2[it]&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;it&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;si&nbsp;]&nbsp;)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;pSum&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;(sum1</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">sum2</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">n)<br />
&nbsp;&nbsp;&nbsp;&nbsp;den&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;sqrt(&nbsp;(sum1Sq</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">pow(sum1,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">n&nbsp;)</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(&nbsp;sum2Sq</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">pow(sum2,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">n&nbsp;)&nbsp;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;den</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;None<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;num</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">den<br />
<br />
fc&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;open(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-user-sc.txt</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">w</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">43381</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;j&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">43381</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;ps(str(i),str(j))&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;sc&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;None:&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">fc,&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%s\t%s\t%s</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">(i,j,sc)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
fc.close()<br />
<br />
EOF<br />
<br />
<br />
<br />
<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;测试使用</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">python</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">EOF<br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;bsddb<br />
db&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;bsddb.hashopen(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">user-items.db</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;db[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />
EOF<br />
<br />
</span><span style="color: rgb(0, 0, 0);">25</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">30604</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br />
<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;um1[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">468</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">],um1[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">471</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />
</span><span style="color: rgb(0, 0, 0);">2.0</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br />
(Pdb)&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;um2[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">468</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">],um2[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">471</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />
</span><span style="color: rgb(0, 0, 0);">2.0</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br />
<br />
<br />
<br />
</span></div>
<br />
<br />
<br />
如果对大家对 推荐有一些了解，数据能到 用户与用户关系(分值化) ,是能干很多事情了。<br />
比如：<br />
&nbsp; 1. 首先得到某用户相近度最高的几位活跃用户，看这几位用户在看什么，听什么 然后推荐出去 <br />
<br />
扩展：<br />
&nbsp; 把初始值 反过来&nbsp; item&nbsp; user&nbsp; score ，然后统计出 item 和 item 之间的关系 。<br />
&nbsp; 当 消费某一产品 ，马上推荐出 其他的相近的产品 （時时推荐）<br />
<br />
&nbsp; <br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/303271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-22 23:56 <a href="http://www.blogjava.net/Skynet/archive/2009/11/22/303271.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 算法导论 - 分治算法</title><link>http://www.blogjava.net/Skynet/archive/2009/11/22/303269.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Sun, 22 Nov 2009 15:26:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/22/303269.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/303269.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/22/303269.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/303269.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/303269.html</trackback:ping><description><![CDATA[算法导论,一章二小节 ,分治算法<br />
<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;MERGE(A,p,q,r):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%s:%s&nbsp;-&nbsp;%s:%s</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">&nbsp;(p,q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,r</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;p</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">q&nbsp;:&nbsp;L&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[A[p],</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">**10]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;L&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;A[p:q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">10**10</span><span style="color: rgb(0, 0, 0);">]<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">r&nbsp;:&nbsp;R&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[A[r],</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">**10]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;R&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;A[q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:r</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">*10]<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;j&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;0<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(p,r</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;L[i]</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">R[j]&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">L[i]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">R[j]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;print&nbsp;"%s:%s&nbsp;=&nbsp;%s&nbsp;\n%s:%s&nbsp;=&nbsp;%s\n\n%s"&nbsp;%&nbsp;(&nbsp;p,q,&nbsp;L&nbsp;,&nbsp;q+1,r,R,&nbsp;A)</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
<br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;Debugging(A,p,q,r,c):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%s\t%s:%s&nbsp;-&nbsp;%s:%s</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">&nbsp;(c,p,q,q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,r)<br />
<br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;MERGE_SORT(A,p,r,c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;p</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">r:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(p</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">r)</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MERGE_SORT(A,p,q,c</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MERGE_SORT(A,q</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,r,c</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">Debugging(A,p,q,r,c)</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MERGE(A,p,q,r)<br />
<br />
A</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">]<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;A<br />
MERGE_SORT(A,0,len(A)</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;A<br />
<br />
</span></div>
结果输出》》<br />
python 2f.py<br />
[5, 2, 7, 4, 1, 3, 2, 6]<br />
[1, 2, 2, 3, 4, 5, 6, 7]<br />
<br />
<br />
分享些细节：算法并不难，但确实写了很久，调试让我很郁闷。<br />
直到写了 def <span style="color: rgb(0, 0, 0);">Debugging&nbsp; </span>目测：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);">python&nbsp;2f.py<br />
</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0:0&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0:</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);"><br />
</span></div>
看 每层 对数组的 数组下标取值 ： <br />
在 python 中当 <br />
arr = [1,2,3,4] 我希望能取出 [2,3] 是 arr[1:3] 是最后一位不计算在内的<br />
最典型的&nbsp; arr[0,1]&nbsp; == [1]<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/303269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-22 23:26 <a href="http://www.blogjava.net/Skynet/archive/2009/11/22/303269.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>理解高斯函数，以及在推荐算法中的应用</title><link>http://www.blogjava.net/Skynet/archive/2009/11/19/302901.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Thu, 19 Nov 2009 03:14:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/19/302901.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/302901.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/19/302901.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/302901.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/302901.html</trackback:ping><description><![CDATA[<br />
这是一个很有用的 公式比如：用户消费分值权重 ， 产品关联分值权重 等等 <br />
<br />
<br />
公式&nbsp; <img src="http://www.blogjava.net/images/blogjava_net/skynet/gsfun.png" alt="" border="0" /><br />
<br />
在 http://www.wolframalpha.com 中表示 :<br />
e = (1+1/n) ^n<br />
a*e^(-(x-b)^2/c^2)&nbsp; <br />
a 峰值最大值 <br />
b 峰值x轴偏移量<br />
c 弧度跨度 <br />
<br />
<br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/gs-z1.gif" alt="" border="0" />&nbsp; =&nbsp; 1*e^(-(x-1)^2/1^2) <br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/gs0.gif" alt="" border="0" /><br />
<br />
<br />
<br />
修改 峰值 a = 2<br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/gs-z2.gif" alt="" border="0" /><br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/gs-z2-c.gif" alt="" border="0" /><br />
<br />
这里 就 不一一展现 b 峰值x轴偏移量 ， c 弧度跨度 了 大家可以 去 wolframalpha 自己去尝试<br />
<br />
<br />
<br />
实例1 与时间有关的递减 ：<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;math<br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;gaussian(x,peak</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);">,axis</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);">,span</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;peak</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">math.e</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">(x</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">axis)</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">(span)</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">&nbsp;)<br />
<br />
<br />
跨度&nbsp;c&nbsp;参考:<br />
c&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;在2.</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">&nbsp;附件急剧衰减<br />
c&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);"><br />
c&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">18</span><span style="color: rgb(0, 0, 0);">&nbsp;:</span><span style="color: rgb(0, 0, 0);">30</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;这个数&nbsp;衰减统计&nbsp;一个月&nbsp;不错</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">c&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">55</span><span style="color: rgb(0, 0, 0);">&nbsp;:</span><span style="color: rgb(0, 0, 0);">90</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;衰减统计&nbsp;一个季度&nbsp;不错</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">简单应用&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">消费1次得峰值4分&nbsp;浏览1次峰值2分&nbsp;<br />
统计某用户季度得分<br />
数据：在前10天浏览10次，消费1次&nbsp;，前11天浏览5次&nbsp;<br />
d10&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;gaussian(</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">,span</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">55.0</span><span style="color: rgb(0, 0, 0);">)<br />
d11&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;gaussian(</span><span style="color: rgb(0, 0, 0);">11</span><span style="color: rgb(0, 0, 0);">,span</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">55.0</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;d10</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">d10</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">d11</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">结果&nbsp;33.0407089687</span></div>
<br />
<br />
倒的高斯　－　实例2&nbsp; ：<br />
公式 = <img src="http://www.blogjava.net/images/blogjava_net/skynet/gs-jjfun.gif" alt="" border="0" /><br />
<img src="http://www.blogjava.net/images/blogjava_net/skynet/gs_jj.gif" alt="" border="0" /><br />
&nbsp;<br />
目的　与次数有关的产品分值化
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">用户&nbsp;对&nbsp;某产品&nbsp;分值化</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;比如&nbsp;某用户&nbsp;用过某产品&nbsp;n次，我希望&nbsp;n&nbsp;无限大是一个&nbsp;渐进某个值&nbsp;而不是和&nbsp;n&nbsp;无限递增的</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">下面的&nbsp;fun&nbsp;结果是&nbsp;&nbsp;1.6&nbsp;～&nbsp;10&nbsp;分值直接的区域,&nbsp;也就是&nbsp;传说中的&nbsp;产品感兴趣&nbsp;&#8220;10分制&#8221;&nbsp;简易版&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(x,peak</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">9.0</span><span style="color: rgb(0, 0, 0);">,axis</span><span style="color: rgb(0, 0, 0);">=-</span><span style="color: rgb(0, 0, 0);">2.0</span><span style="color: rgb(0, 0, 0);">,span</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">11.0</span><span style="color: rgb(0, 0, 0);">):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%.4f</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">peak</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">math.e</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">(x</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">axis)</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">(span)</span><span style="color: rgb(0, 0, 0);">**</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">&nbsp;)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">peak</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />
<br />
<br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">1.6451</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">2.1148</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">2.6800</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">3.3161</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">3.9970</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">4.6969</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;gs(</span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);">)<br />
</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">10.0000</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
</span></div>
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/302901.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-19 11:14 <a href="http://www.blogjava.net/Skynet/archive/2009/11/19/302901.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 算法导论 - 插入排序</title><link>http://www.blogjava.net/Skynet/archive/2009/11/18/302864.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Wed, 18 Nov 2009 15:29:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/18/302864.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/302864.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/18/302864.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/302864.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/302864.html</trackback:ping><description><![CDATA[算法导论 - 算法入门 ,一小节（插入排序,复杂度）
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;插入排序&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 复杂度</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;insertion_sort(arr):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;j&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,len(arr)&nbsp;):&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n-1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[j]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n-1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n-1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">0&nbsp;</span><span style="color: rgb(0, 0, 255);">and</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i]</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;key&nbsp;: </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n(n-1)/2</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n(n-1)/2</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n(n-1)/2</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n-1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;arr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;n-1</span><span style="color: rgb(0, 128, 0);"><br />
</span></div>
<br />
验证结果 :<br />
&gt;&gt;&gt; arr=[5,2,4,6,1,3]<br />
&gt;&gt;&gt; insertion_sort(arr)<br />
[2, 5, 4, 6, 1, 3]<br />
[2, 4, 5, 6, 1, 3]<br />
[2, 4, 5, 6, 1, 3]<br />
[1, 2, 4, 5, 6, 3]<br />
[1, 2, 3, 4, 5, 6]<br />
<br />
<br />
<br />
<br />
<br />
验证复杂度：<br />
z = 5(n-1)+1+3n(n-1)/2 <br />
我们测试数据 为&nbsp; n=6&nbsp; <br />
当数据极端情况就是需要全部重新排列 <br />
就是 [6,5,4,3,2,1] 要排出 [1,2,3,4,5,6] 这样<br />
&gt;&gt; z = 71 <br />
<br />
一种比较笨的 验证方法 供大家拍砖 :<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;insertion_sort(arr):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0<br />
&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;j&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,len(arr)&nbsp;):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[j]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">0&nbsp;</span><span style="color: rgb(0, 0, 255);">and</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i]</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;key&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arr[i]&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;arr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">----</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,str(ii)<br />
</span></div>
<br />
&gt;&gt;&gt; arr=[6,5,4,3,2,1]<br />
&gt;&gt;&gt; insertion_sort(arr)<br />
[5, 6, 4, 3, 2, 1]<br />
[4, 5, 6, 3, 2, 1]<br />
[3, 4, 5, 6, 2, 1]<br />
[2, 3, 4, 5, 6, 1]<br />
[1, 2, 3, 4, 5, 6]<br />
---- 71&nbsp; #复杂度验证为 71 <br />
<br />
<br />
罗嗦下 n(n-1)/2<br />
极端情况下&nbsp; <br />
i=1 ; j 需要挪动 1次<br />
i=2 ; j 挪动 1+2次<br />
i=3 ; j 挪动 1+2+3次<br />
....<br />
i=n ; j 挪动 1+2....+n<br />
<br />
我们又找到 (1+n)+(2+n-1)+(3+n-2).... = (1+n)n/2<br />
我们这 的 i 是从 2 次开始的 也就是说&nbsp; (n-1)n/2 了 <br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;tn(ii):<br />
&nbsp;&nbsp;&nbsp;&nbsp;ti</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(ii)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;j&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;xrange(i)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ti</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;ti<br />
<br />
<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;tn(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">1&nbsp;=&nbsp;1</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;tn(</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">)&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">3&nbsp;=&nbsp;1+2</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;tn(</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">)&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">6&nbsp;=&nbsp;1+2+3</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">..<br />
<br />
<br />
</span></div>
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/302864.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-18 23:29 <a href="http://www.blogjava.net/Skynet/archive/2009/11/18/302864.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>算法 复杂度分析图</title><link>http://www.blogjava.net/Skynet/archive/2009/11/17/302742.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Tue, 17 Nov 2009 15:12:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/17/302742.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/302742.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/17/302742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/302742.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/302742.html</trackback:ping><description><![CDATA[发现 自己写个小程序 - 文本存储二叉结构的hashMap。 那个费劲！ 痛下决心 仔细学习 算法 。<br />
（如果大家有兴趣就跟我一起 - <strong>《算法导论》</strong>，也望大家监督我能每天拿出一小时和大家分享算法,算法代码我会尽量使用 py 和 解决一些分析日志的应用上靠 （其实，上面费劲的 <strong>二叉hash</strong> 是为了分析日志中 - 希望能实现 多个大文件不需要合并就能根据某列排序输出! 目前的解决办法 find .. -exec cat {} \; | perl |sort 的笨方法 ） <br />
<br />
1. 算法在计算中的作用 （笔记） :<br />
&nbsp;&nbsp; 算法(algorithm):就是定义良好的计算过程，它取一个或一组作为输出，并产生一个或一组自作为输出。<br />
<br />
一些函数运行级别&nbsp; # http://www.wolframalpha.com/ 函数都可在网站里运行<br />
这里 n=一亿条数据 :<br />
log_2(n)&nbsp;&nbsp;&nbsp;&nbsp; 30<br />
n^0.5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 31622<br />
n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10^9<br />
n*log_2(n)&nbsp;&nbsp; 2.9^10<br />
n^2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10^18<br />
n^3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10^27<br />
2^n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 无穷<br />
n!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10^362880<br />
<br />
<br />
需要知道的复杂度 - 在某一个临界点后 合并会别插入要快的<br />
&nbsp; 插入排序&nbsp; 复杂度 n^2 &nbsp;&nbsp; http://www.wolframalpha.com/input/?i=n^2<br />
&nbsp; 合并排序&nbsp; 复杂度 n*log_2(n)&nbsp; http://www.wolframalpha.com/input/?i=n*log_2%28n%29+<br />
<br />
网上的查找到的一些名称：参考 http://www.51testing.com/?uid-130868-action-viewspace-itemid-66729<br />
1.1<strong>稳定排序&nbsp; 非稳定排序 -</strong><br />
&nbsp; 稳定排序是所有相等的数经过某种排序方法后，仍能保持它们在排序之前的相对次序，。反之，就是非稳定的排序。<br />
1.2<strong>内排序&nbsp; 外排序</strong><br />
&nbsp; 在排序过程中，所有需要排序的数都在内存，并在内存中调整它们的存储顺序，称为内排序； 在排序过程中，只有部分数被调入内存，并借助内存调整数在外存中的存放顺序排序方法称为外排序。<br />
1.3<strong>算法的时间复杂度&nbsp; 空间复杂度</strong><br />
&nbsp; 所谓算法的时间复杂度，是指执行算法所需要的计算工作量。 一个算法的空间复杂度，一般是指执行这个算法所需要的内存空间。<br />
<br />
<br />
几种常见的算法复杂度：<br />
2.1冒泡排序 （Bubble Sort）&nbsp;&nbsp;&nbsp; O(n^2)<br />
2.2选择排序 （Selection Sort） O(n^2 )<br />
2.3插入排序 （Insertion Sort） O(n^2)<br />
2.4堆排序&nbsp;&nbsp;&nbsp; O( nlog(n) )<br />
2.5归并排序&nbsp; O( nlog_2(n)&nbsp; )<br />
2.6快速排序&nbsp; 最好 O( nlog_2(n) ) 最坏 O(n^2)<br />
<br />
<br />
<br />
<br />
wiki 参考 ：http://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95<span class="mw-headline" id=".E7.A9.A9.E5.AE.9A.E7.9A.84"><br />
<strong>穩定的</strong></span>
<ul>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F" title="冒泡排序">冒泡排序</a>（bubble sort） — O(<em>n</em><sup>2</sup>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E9%B8%A1%E5%B0%BE%E9%85%92%E6%8E%92%E5%BA%8F" title="雞尾酒排序">雞尾酒排序</a> (Cocktail sort, 雙向的冒泡排序) — O(<em>n</em><sup>2</sup>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F" title="插入排序">插入排序</a> （insertion sort）— O(<em>n</em><sup>2</sup>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E6%A1%B6%E6%8E%92%E5%BA%8F" title="桶排序">桶排序</a> （bucket sort）— O(<em>n</em>); 需要 O(<em>k</em>) 額外空間</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E8%AE%A1%E6%95%B0%E6%8E%92%E5%BA%8F" title="計數排序">計數排序</a> (counting sort) — O(<em>n</em>+<em>k</em>); 需要 O(<em>n</em>+<em>k</em>) 額外空間</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E5%90%88%E4%BD%B5%E6%8E%92%E5%BA%8F" title="合併排序" class="mw-redirect">合併排序</a> （merge sort）— O(<em>n</em> log <em>n</em>); 需要 O(<em>n</em>) 額外空間</li>
    <li>原地<a href="http://zh.wikipedia.org/zh-tw/%E5%90%88%E4%BD%B5%E6%8E%92%E5%BA%8F" title="合併排序" class="mw-redirect">合併排序</a> — O(<em>n</em><sup>2</sup>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E4%BA%8C%E5%8F%89%E6%8E%92%E5%BA%8F%E6%A0%91" title="二叉排序樹" class="mw-redirect">二叉排序樹</a>排序 （Binary tree sort） — O(<em>n</em> log <em>n</em>)期望時間; O(<em>n</em><sup>2</sup>)最壞時間; 需要 O(<em>n</em>) 額外空間</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E9%B8%BD%E5%B7%A2%E6%8E%92%E5%BA%8F" title="鴿巢排序">鴿巢排序</a> (Pigeonhole sort) — O(<em>n</em>+<em>k</em>); 需要 O(<em>k</em>) 額外空間</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F" title="基數排序">基數排序</a> （radix sort）— O(<em>n</em>&#183;<em>k</em>); 需要 O(<em>n</em>) 額外空間</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Gnome_sort&amp;action=edit&amp;redlink=1" class="new" title="Gnome sort （頁面未存在）">Gnome sort</a> — O(<em>n</em><sup>2</sup>)</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Library_sort&amp;action=edit&amp;redlink=1" class="new" title="Library sort （頁面未存在）">Library sort</a> — O(<em>n</em> log <em>n</em>) with high probability, 需要 (1+&#949;)<em>n</em> 額外空間</li>
</ul>
<h3><span class="editsection">[<a href="http://zh.wikipedia.org/w/index.php?title=%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;action=edit&amp;section=4" title="編輯段落: 不穩定">編輯</a>]</span> <span class="mw-headline" id=".E4.B8.8D.E7.A9.A9.E5.AE.9A">不穩定</span></h3>
<ul>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E9%81%B8%E6%93%87%E6%8E%92%E5%BA%8F" title="選擇排序" class="mw-redirect">選擇排序</a> （selection sort）— O(<em>n</em><sup>2</sup>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F" title="希爾排序">希爾排序</a> （shell sort）— O(<em>n</em> log <em>n</em>) 如果使用最佳的現在版本</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Comb_sort&amp;action=edit&amp;redlink=1" class="new" title="Comb sort （頁面未存在）">Comb sort</a> — O(<em>n</em> log <em>n</em>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E5%A0%86%E6%8E%92%E5%BA%8F" title="堆排序" class="mw-redirect">堆排序</a> （heapsort）— O(<em>n</em> log <em>n</em>)</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Smoothsort&amp;action=edit&amp;redlink=1" class="new" title="Smoothsort （頁面未存在）">Smoothsort</a> — O(<em>n</em> log <em>n</em>)</li>
    <li><a href="http://zh.wikipedia.org/zh-tw/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F" title="快速排序">快速排序</a> （quicksort）— O(<em>n</em> log <em>n</em>) 期望時間, O(<em>n</em><sup>2</sup>) 最壞情況; 對於大的、亂數串列一般相信是最快的已知排序</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Introsort&amp;action=edit&amp;redlink=1" class="new" title="Introsort （頁面未存在）">Introsort</a> — O(<em>n</em> log <em>n</em>)</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Patience_sorting&amp;action=edit&amp;redlink=1" class="new" title="Patience sorting （頁面未存在）">Patience sorting</a> — O(<em>n</em> log <em>n</em> + <em>k</em>) 最壞情況時間，需要 額外的 O(<em>n</em> + <em>k</em>) 空間，也需要找到<a href="http://zh.wikipedia.org/w/index.php?title=%E6%9C%80%E9%95%B7%E7%9A%84%E9%81%9E%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97&amp;action=edit&amp;redlink=1" class="new" title="最長的遞增子序列 （頁面未存在）">最長的遞增子序列</a>（longest increasing subsequence）</li>
</ul>
<h3><span class="editsection">[<a href="http://zh.wikipedia.org/w/index.php?title=%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&amp;action=edit&amp;section=5" title="編輯段落: 不實用的排序演算法">編輯</a>]</span> <span class="mw-headline" id=".E4.B8.8D.E5.AF.A6.E7.94.A8.E7.9A.84.E6.8E.92.E5.BA.8F.E7.AE.97.E6.B3.95">不實用的排序演算法</span></h3>
<ul>
    <li><a href="http://zh.wikipedia.org/zh-tw/Bogo%E6%8E%92%E5%BA%8F" title="Bogo排序">Bogo排序</a> — O(<em>n</em> &#215; <em>n</em>!) 期望時間，無窮的最壞情況。</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Stupid_sort&amp;action=edit&amp;redlink=1" class="new" title="Stupid sort （頁面未存在）">Stupid sort</a> — O(<em>n</em><sup>3</sup>); 遞迴版本需要 O(<em>n</em><sup>2</sup>) 額外記憶體</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Bead_sort&amp;action=edit&amp;redlink=1" class="new" title="Bead sort （頁面未存在）">Bead sort</a> — O(<em>n</em>) or O(&#8730;<em>n</em>), 但需要特別的硬體</li>
    <li><a href="http://zh.wikipedia.org/w/index.php?title=Pancake_sorting&amp;action=edit&amp;redlink=1" class="new" title="Pancake sorting （頁面未存在）">Pancake sorting</a> — O(<em>n</em>), 但需要特別的硬體</li>
</ul>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/302742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-17 23:12 <a href="http://www.blogjava.net/Skynet/archive/2009/11/17/302742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>算法公式 - 图形化 </title><link>http://www.blogjava.net/Skynet/archive/2009/11/05/301255.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Thu, 05 Nov 2009 05:09:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/11/05/301255.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/301255.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/11/05/301255.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/301255.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/301255.html</trackback:ping><description><![CDATA[<br />
<link href="BLOCKQUOTE%7Bmargin-Top:%200px;%20margin-Bottom:%200px;%20margin-Left:%202em%7D" rel="stylesheet" />
<div>&nbsp;在定制 数学公式的时候 可能 希望有个直观的 展现 </div>
<div><a href="http://www.wolframalpha.com/">http://www.wolframalpha.com/</a>
</div>
<div>我们前一段时间用到的&nbsp; { 推荐分值 90 天 递减公式，如果这东西早发现 公式就不会错了！} </div>
<div>目前我们递减的公式</div>
<div>y=-x/90+1 </div>
<div id="subpod_0100_1">
<div id="scannerresult_0100_1"><img id="i_0100_1" title="y = -x/90+1..." alt="y = -x/90+1" src="http://www2.wolframalpha.com/Calculate/MSP/MSP2581983g78d62f15hba000032a28886edgf4bdc?MSPStoreType=image/gif&amp;s=22" /></div>
</div>
<div id="subpod_0300_1">
<div id="scannerresult_0300_1"><img id="i_0300_1" title="" alt="" src="http://www2.wolframalpha.com/Calculate/MSP/MSP2431983g78dd9b75ae800001ae754fh4986bf32?MSPStoreType=image/gif&amp;s=22" /></div>
</div>
<div>&nbsp;</div>
<div>&nbsp;在比如我们优化下&nbsp; 90天改成 </div>
<div>(y*10)^2=-x+90 </div>
<div><img id="i_0200_1" title="100 y^2 = 90-x..." alt="100 y^2 = 90-x" src="http://www2.wolframalpha.com/Calculate/MSP/MSP11231983g33198dbh008000062b914gi4e3dei04?MSPStoreType=image/gif&amp;s=1" /></div>
<div><img id="i_0400_1" title="" alt="" src="http://www2.wolframalpha.com/Calculate/MSP/MSP5971983g5i6d234b3ag00004208ei5925i0i9b9?MSPStoreType=image/gif&amp;s=4" usemap="#map-0400_1" /><font color="#c0c0c0" size="2">2009-11-05 </font></div>
<font size="2">
<hr style="width: 122px; height: 2px;" align="left" size="2" />
</font><br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/301255.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-11-05 13:09 <a href="http://www.blogjava.net/Skynet/archive/2009/11/05/301255.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>apriori-实现</title><link>http://www.blogjava.net/Skynet/archive/2009/09/01/293479.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Tue, 01 Sep 2009 09:43:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/09/01/293479.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/293479.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/09/01/293479.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/293479.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/293479.html</trackback:ping><description><![CDATA[谢谢 同事 <strong>孙超 </strong>讲解
<br />
这就 把他的 思想 画个图
<img src="file:///C:/Documents%20and%20Settings/lky/%E6%A1%8C%E9%9D%A2/apriori-%E5%AE%9E%E7%8E%B0.PNG" alt="" /><br />
<br />
<img alt="" src="http://www.blogjava.net/images/blogjava_net/skynet/apriori.jpg" height="608" width="886" /><br />
<img src ="http://www.blogjava.net/Skynet/aggbug/293479.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-09-01 17:43 <a href="http://www.blogjava.net/Skynet/archive/2009/09/01/293479.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>apriori 算法（py）</title><link>http://www.blogjava.net/Skynet/archive/2009/08/31/293309.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Mon, 31 Aug 2009 06:25:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/08/31/293309.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/293309.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/08/31/293309.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/293309.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/293309.html</trackback:ping><description><![CDATA[<br />
<br />
代码:<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;-*-&nbsp;coding:&nbsp;UTF8&nbsp;-*-</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;sys<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;最小&nbsp;支持度</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">sup_min&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;int(sys.argv[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])<br />
<br />
ss&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;交易&nbsp;数据&nbsp;库</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">D</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[<br />
&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A,B,C,D</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />
&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">B,C,E</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />
&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A,B,C,E</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />
&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">B,D,E</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,<br />
&nbsp;</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A,B,C,D</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />
]<br />
<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">交易数据库展现</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;arr&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;D&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;arr<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"><br />
<br />
<br />
</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">rows</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">int(sys.argv[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">])</span><br />
<span style="color: rgb(128, 0, 0);">D=[]<br />
for&nbsp;tid&nbsp;in&nbsp;open('BuyMusic.20090722.mob.prodIds').readlines()[:rows]&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;D.append(tid.split("\n")[0].split("\t")[1])<br />
<br />
<br />
print&nbsp;"读取&nbsp;文件结束&nbsp;BuyMusic.20090722.mob.prodIds&nbsp;!"<br />
</span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">全局&nbsp;频繁项&nbsp;收集</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">sup_data_map&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{}<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">全局&nbsp;&nbsp;最大频繁项&nbsp;收集</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">is_zsup</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;遍历过程&nbsp;临时&nbsp;局部&nbsp;&nbsp;频繁项&nbsp;收集</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">mapL&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{}<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;第一次&nbsp;频繁项&nbsp;收集</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;find_frequent_1_itemset(I):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;I</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">null</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">or</span><span style="color: rgb(0, 0, 0);">&nbsp;I</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL.has_key(I):&nbsp;mapL[I]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:&nbsp;mapL[I]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
<br />
map(find_frequent_1_itemset,[&nbsp;I&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;TID&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;D&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;I&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;TID.split(ss)&nbsp;])<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;刷选掉&nbsp;小于&nbsp;最小支持度&nbsp;的&nbsp;频繁项</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;remove_not_sup_min(map,supmin</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sup_min):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;[k&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k,v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;map.items()&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;v</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">supmin]&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;map[k]<br />
remove_not_sup_min(mapL)<br />
<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">第一次&nbsp;筛选&nbsp;频繁项&nbsp;结束!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;装载&nbsp;全局&nbsp;频繁项&nbsp;最大频繁项</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k,v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL.items()&nbsp;:&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;sup_data_map[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />
&nbsp;&nbsp;&nbsp;&nbsp;is_zsup[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;判定&nbsp;是否&nbsp;'BD'&nbsp;属于&nbsp;&nbsp;'BCD'&nbsp;中&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">isInTid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">lambda</span><span style="color: rgb(0, 0, 0);">&nbsp;I,TID&nbsp;:&nbsp;len(I.split(ss))&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;len([i&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;I&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;TID.split(ss)])<br />
<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;组合&nbsp;&nbsp;[A,B]&nbsp;+&nbsp;[A,C]&nbsp;=&nbsp;[A,B.C]</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;comb(arr1,arr2):<br />
&nbsp;&nbsp;&nbsp;&nbsp;tmap</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;arr1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">arr2&nbsp;:&nbsp;tmap[v]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;tmap.keys()<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;apriori&nbsp;迭代核心</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;runL(mapL,dep):<br />
&nbsp;&nbsp;&nbsp;&nbsp;mapL2&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;{}<br />
&nbsp;&nbsp;&nbsp;&nbsp;C</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />
&nbsp;&nbsp;&nbsp;&nbsp;keys&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL.keys()<br />
&nbsp;&nbsp;&nbsp;&nbsp;iik</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;jjk</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;根据&nbsp;上次&nbsp;&nbsp;频繁项&nbsp;，生成本次&nbsp;'可能频繁项'&nbsp;集合&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;ii&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(len(keys))&nbsp;:&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;jj&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(ii</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,len(keys))&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keystr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">comb([ch&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;ch&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;keys[ii].split(ss)],[ch&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;ch&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;keys[jj].split(ss)])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;len(keystr)&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;dep&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keystr.sort()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tk</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ss.join(keystr)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);">&nbsp;tk&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;C&nbsp;:&nbsp;C[tk]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(keys[ii],keys[jj])<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;'可能频繁项'&nbsp;对比&nbsp;交易数据库&nbsp;&nbsp;计数</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;tk,z&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;C.items():<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;TID&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;D:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;isInTid(tk,TID)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL2.has_key(tk):&nbsp;mapL2[tk]</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:&nbsp;mapL2[tk]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;刷选掉&nbsp;小于&nbsp;最小支持度&nbsp;的&nbsp;频繁项</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;remove_not_sup_min(mapL2)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k,v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;is_zsup.items()&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k1,v1&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL2.items()&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;isInTid(k,k1)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;is_zsup[k]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;全局&nbsp;频繁项&nbsp;,最大频繁项&nbsp;&nbsp;收集&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k,v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL2.items()&nbsp;:&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sup_data_map[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is_zsup[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">第</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">str(dep)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">次&nbsp;筛选&nbsp;频繁项&nbsp;结束!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL2<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;真正&nbsp;运行&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">ii</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;ii</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ii</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;mapL&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;runL(mapL,ii)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;mapL<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;全局&nbsp;&nbsp;频繁项&nbsp;中&nbsp;去除&nbsp;最大频繁项</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k,v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;is_zsup.items()&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;sup_data_map.has_key(k)&nbsp;:&nbsp;</span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);">&nbsp;sup_data_map[k]<br />
<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">频繁项</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;sup_data_map<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">最大频繁项</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;is_zsup<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;<br />
<br />
</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">可信度&nbsp;展现</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k,v&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;sup_data_map.items()&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;k1,v1&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;is_zsup.items()&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;isInTid(k,k1)&nbsp;:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;k,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">-&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,k1,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\t%.1f</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">((float(is_zsup[k1])</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">float(sup_data_map[k]))</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">%</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
</span></div>
<br />
<br />
<br />
<br />
结果:<br />
-bash-3.00$ python ap.py 2<br />
交易数据库展现<br />
A,B,C,D<br />
B,C,E<br />
A,B,C,E<br />
B,D,E<br />
A,B,C,D<br />
<br />
第一次 筛选 频繁项 结束!<br />
{'A': 3, 'C': 4, 'B': 5, 'E': 3, 'D': 3}<br />
第2次 筛选 频繁项 结束!<br />
{'C,D': 2, 'C,E': 2, 'A,D': 2, 'A,B': 3, 'A,C': 3, 'B,E': 3, 'B,D': 3, 'B,C': 4}<br />
第3次 筛选 频繁项 结束!<br />
{'A,B,D': 2, 'A,B,C': 3, 'B,C,D': 2, 'B,C,E': 2, 'A,C,D': 2}<br />
第4次 筛选 频繁项 结束!<br />
{'A,B,C,D': 2}<br />
第5次 筛选 频繁项 结束!<br />
{}<br />
频繁项<br />
{'A': 3, 'C': 4, 'B': 5, 'E': 3, 'D': 3, 'C,D': 2, 'C,E': 2, 'A,D': 2, 'A,B': 3, 'A,C': 3, 'A,B,D': 2, 'B,C,D': 2, 'A,C,D': 2, 'B,E': 3, 'B,D': 3, 'B,C': 4, 'A,B,C': 3}<br />
<br />
最大频繁项<br />
{'B,C,E': 2, 'A,B,C,D': 2}<br />
<br />
可信度 展现<br />
A -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;66.7%<br />
C -&gt; B,C,E &nbsp;&nbsp; &nbsp;50.0%<br />
C -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;50.0%<br />
B -&gt; B,C,E &nbsp;&nbsp; &nbsp;40.0%<br />
B -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;40.0%<br />
E -&gt; B,C,E &nbsp;&nbsp; &nbsp;66.7%<br />
D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;66.7%<br />
C,D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;100.0%<br />
C,E -&gt; B,C,E &nbsp;&nbsp; &nbsp;100.0%<br />
A,D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;100.0%<br />
A,B -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;66.7%<br />
A,C -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;66.7%<br />
A,B,D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;100.0%<br />
B,C,D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;100.0%<br />
A,C,D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;100.0%<br />
B,E -&gt; B,C,E &nbsp;&nbsp; &nbsp;66.7%<br />
B,D -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;66.7%<br />
B,C -&gt; B,C,E &nbsp;&nbsp; &nbsp;50.0%<br />
B,C -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;50.0%<br />
A,B,C -&gt; A,B,C,D &nbsp;&nbsp; &nbsp;66.7%<br />
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/293309.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-08-31 14:25 <a href="http://www.blogjava.net/Skynet/archive/2009/08/31/293309.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>k-means （python）算法 </title><link>http://www.blogjava.net/Skynet/archive/2009/08/07/290242.html</link><dc:creator>刘凯毅</dc:creator><author>刘凯毅</author><pubDate>Fri, 07 Aug 2009 08:20:00 GMT</pubDate><guid>http://www.blogjava.net/Skynet/archive/2009/08/07/290242.html</guid><wfw:comment>http://www.blogjava.net/Skynet/comments/290242.html</wfw:comment><comments>http://www.blogjava.net/Skynet/archive/2009/08/07/290242.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Skynet/comments/commentRss/290242.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Skynet/services/trackbacks/290242.html</trackback:ping><description><![CDATA[<br />
转:http://www.daniweb.com/forums/thread31449.html<br />
什么都不说了，直接看代码吧。<br />
注解 应该写的比较详细<br />
<br />
<br />
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 0);"># liukaiyi&nbsp;</span><span style="color: rgb(0, 128, 0);"> <br />
# 注&nbsp;k-means&nbsp;，维度类型&nbsp;-&nbsp;数值形式&nbsp;(&nbsp;199&nbsp;或&nbsp;23.13&nbsp;<img src="http://www.blogjava.net/Images/dot.gif" alt="" />)&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;sys,&nbsp;math,&nbsp;random<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;类化&nbsp;'数据'&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;在&nbsp;n-维度空间</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;Point:<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self,&nbsp;coords,&nbsp;reference</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.coords&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;coords<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.n&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;len(coords)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.reference&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;reference<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">__repr__</span><span style="color: rgb(0, 0, 0);">(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;str(self.coords)<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;类化&nbsp;'聚集点&nbsp;/&nbsp;聚类平均距离&nbsp;点&nbsp;'&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;在&nbsp;n-维度空间</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;k-means&nbsp;核心类</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;每次&nbsp;聚集各点&nbsp;围绕她&nbsp;进行聚集&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;并提供方法&nbsp;求-聚集后的计算中心点，同时记入&nbsp;此次&nbsp;中心点(聚集各点平均距离)，为下一次聚集提供中心点.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;Cluster:<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self,&nbsp;points):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;len(points)&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;0:&nbsp;</span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ILLEGAL:&nbsp;EMPTY&nbsp;CLUSTER</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.points&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;points<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.n&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;points[0].n<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;p&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;points:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;p.n&nbsp;</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.n:&nbsp;</span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ILLEGAL:&nbsp;MULTISPACE&nbsp;CLUSTER</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;求&nbsp;聚集各点后&nbsp;平均点</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;self.centroid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.calculateCentroid()<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">__repr__</span><span style="color: rgb(0, 0, 0);">(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;str(self.points)<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;更新&nbsp;中心点，并返回&nbsp;原中心点&nbsp;与&nbsp;现中心点(聚集各点平均距离)距离&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;update(self,&nbsp;points):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old_centroid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.centroid<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.points&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;points<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.centroid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;self.calculateCentroid()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;getDistance(old_centroid,&nbsp;self.centroid)<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;计算平均点&nbsp;（聚集/收集各点（离本类的中心点）最近数据,后生成新的&nbsp;中心点&nbsp;）</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;calculateCentroid(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centroid_coords&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;维度&nbsp;迭代</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(self.n):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centroid_coords.append(</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;收集各点&nbsp;迭代&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;p&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;self.points:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centroid_coords[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;centroid_coords[i]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">p.coords[i]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;centroid_coords[i]&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;centroid_coords[i]</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">len(self.points)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;Point(centroid_coords)<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;返回根据&nbsp;k-means&nbsp;聚集形成的&nbsp;数据集&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;kmeans(points,&nbsp;k,&nbsp;cutoff):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;Randomly&nbsp;sample&nbsp;k&nbsp;Points&nbsp;from&nbsp;the&nbsp;points&nbsp;list,&nbsp;build&nbsp;Clusters&nbsp;around&nbsp;them</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;initial&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.sample(points,&nbsp;k)<br />
&nbsp;&nbsp;&nbsp;&nbsp;clusters&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;p&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;initial:&nbsp;clusters.append(Cluster([p]))<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;迭代&nbsp;k-means&nbsp;直到&nbsp;每次迭代&nbsp;各收集点&nbsp;别的&nbsp;最多&nbsp;不超过&nbsp;0.5&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;True:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;&nbsp;k&nbsp;个收集&nbsp;数组</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lists&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;c&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;clusters:&nbsp;lists.append([])<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;迭代&nbsp;每个&nbsp;数据点&nbsp;，并计算与每个中心点距离</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;并把数据点添加入相应最短的中心点收集数组中</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;在迭代中&nbsp;smallest_distance&nbsp;为每个点与各中心点最短距离&nbsp;参数，请注意看</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;p&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;points:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smallest_distance&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getDistance(p,&nbsp;clusters[0].centroid)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;0<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(len(clusters[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:])):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distance&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getDistance(p,&nbsp;clusters[i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">].centroid)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;distance&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;smallest_distance:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smallest_distance&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;distance<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;添加到&nbsp;离最短中心距离的&nbsp;数组中</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lists[index].append(p)<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;聚集完，计算新&nbsp;中心点</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;并&nbsp;cluster.centroid&nbsp;属性记入下&nbsp;新中心点（下一次&nbsp;聚集的中心点&nbsp;）</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;并&nbsp;计算与上一次&nbsp;中心点&nbsp;距离&nbsp;，如果&nbsp;差值在&nbsp;cutoff&nbsp;0.5&nbsp;以下&nbsp;,跳出迭代&nbsp;（结束，返回最后一次&nbsp;聚集集合）</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;biggest_shift&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(len(clusters)):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shift&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;clusters[i].update(lists[i])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;biggest_shift&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;max(biggest_shift,&nbsp;shift)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;biggest_shift&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;cutoff:&nbsp;</span><span style="color: rgb(0, 0, 255);">break</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;clusters<br />
<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;得到欧几里德距离两点之间&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;getDistance(a,&nbsp;b):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;Forbid&nbsp;measurements&nbsp;between&nbsp;Points&nbsp;in&nbsp;different&nbsp;spaces</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;a.n&nbsp;</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">&nbsp;b.n:&nbsp;</span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ILLEGAL:&nbsp;NON-COMPARABLE&nbsp;POINTS</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;Euclidean&nbsp;distance&nbsp;between&nbsp;a&nbsp;and&nbsp;b&nbsp;is&nbsp;sqrt(sum((a[i]-b[i])^2)&nbsp;for&nbsp;all&nbsp;i)</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0.0</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(a.n):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;ret</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">pow((a.coords[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">b.coords[i]),&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;math.sqrt(ret)<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;在&nbsp;n-维度&nbsp;空间中创建&nbsp;随机点</span><span style="color: rgb(0, 128, 0);"><br />
#</span><span style="color: rgb(0, 128, 0);">&nbsp;--&nbsp;随机生成&nbsp;测试数据</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;makeRandomPoint(n,&nbsp;lower,&nbsp;upper):<br />
&nbsp;&nbsp;&nbsp;&nbsp;coords&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(n):&nbsp;coords.append(random.uniform(lower,&nbsp;upper))<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;Point(coords)<br />
<br />
</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;main&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);">&nbsp;main(args):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;参数说明</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;num_points,&nbsp;&nbsp;&nbsp;&nbsp;n,&nbsp;&nbsp;&nbsp;&nbsp;k,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cutoff,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lower,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;upper&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;随机数据数量&nbsp;,&nbsp;维度,&nbsp;聚集数,&nbsp;跳出迭代最小距离&nbsp;,&nbsp;&nbsp;&nbsp;维度数最大值,维度数最小值</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;num_points,&nbsp;n,&nbsp;k,&nbsp;cutoff,&nbsp;lower,&nbsp;upper&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0.5</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">200</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">200</span><span style="color: rgb(0, 0, 0);"><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;在&nbsp;n-维度空间里&nbsp;,&nbsp;创建&nbsp;num_points&nbsp;随机点</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;测试数据生成&nbsp;</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;points&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;[]<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;range(num_points):&nbsp;points.append(makeRandomPoint(n,&nbsp;lower,&nbsp;upper))<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">&nbsp;使用&nbsp;k-means&nbsp;算法，来&nbsp;聚集数据点&nbsp;(算法入口点)</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;clusters&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;kmeans(points,&nbsp;k,&nbsp;cutoff)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\nPOINTS:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;p&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;points:&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">P:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;p<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\nCLUSTERS:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;c&nbsp;</span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&nbsp;clusters:&nbsp;</span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">C:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;c<br />
</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 128);">__name__</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">__main__</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:&nbsp;main(sys.argv)<br />
</span></div>
<br />
<img src ="http://www.blogjava.net/Skynet/aggbug/290242.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Skynet/" target="_blank">刘凯毅</a> 2009-08-07 16:20 <a href="http://www.blogjava.net/Skynet/archive/2009/08/07/290242.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>