??xml version="1.0" encoding="utf-8" standalone="yes"?>91精品久久久久久久久不口人,日韩欧美国产视频,亚洲国产最新http://www.aygfsteel.com/Skynet/category/42816.htmlzh-cnTue, 08 Dec 2009 08:15:29 GMTTue, 08 Dec 2009 08:15:29 GMT60跟我一起学 - 法D - U性时间排?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/12/08/305156.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/305156.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/12/08/305156.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/305156.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Skynet/services/trackbacks/305156.html</trackback:ping><description><![CDATA[<strong>下面介绍的排序都为:非比较排序法</strong><br /> <br /> q里个h认ؓ在某些特定的地方非比较排序的速度非常明显Q?br /> 比如 Q?对待排数据中有顺序分c,使用鸽l(f)M分类Q然后对不同cd的待排小数据集合采用 插入Q快排等排序方式<br /> <br /> <br /> Counting sort Q计数排?br /> 描述QP代待排序数组出元素xQ确定小于此元素[z]个数Q然后把x攑ֈ它在的最l输出数l[z]上?br /> Ҏ(gu):与待排值有养IE_的排序算法;待排序数据要求过于严|无实际用处;<br /> 法的步骤如下:<br />    1. 扑և待排序的数组中最大和最的元素<br />    2. l计数组中每个gؓi的元素出现的ơ数Q存入数lC的第i?br />    3. Ҏ(gu)有的计数累加Q从C中的W一个元素开始,每一和前一相加)<br />    4. 反向填充目标数组Q将每个元素i攑֜新数l的WC(i),每放一个元素就C(i)减去1<br /> <br /> <br /> Radix sortQ基数排?br /> 描述Q将所有待比较数?正整?l一为同LC长度,C较短的数前面补零. 然后, 从最低位开? 依次q行一ơ排?q样从最低位排序一直到最高位排序完成以后, 数列变成一个有序序?<br /> 排序方式QLSD 由右向左排;MSD 由左向右?br /> Ҏ(gu):非比较排序;待排数据需要统一格式Q?br /> 假设需排序数列的取D围从1...k,我们于是Z个K+1元的数组 a[]Qƈ赋初gؓ0Q然后便开始排序工作:<br /> 法的步骤如下:<br />    1. 按输入顺序读入数列,如果所ȝ元素为i(1<=i<=k),我们将a[i]的值加一Q这L到读完所有的元素?br />    2. d元素q排序:我们遍历整个数组Q如果a[i]=j(j>=0),我们p出jơi(表示元素i在原先数列中出现了j?,q样输出的序列就是已排序的?br />    3. ׃一般排序算法涉及到元素之间的比较,如果化成比较树可以知道,q样的排序算法复杂度的下限是O(N*lnN),而计数排序没有比较元素,所以所需排序旉少了,我们可以看到计数排序的复杂度为O(n*k)Q其中kQk的定义同上)为合q排列所需的时_是个常数?br />    4. 此算法适合所需排列的元素取D围不大的情况下,否则会造成I间的消耗,比如Q一?00个元素,其取D围从1-100000Q显然这个时候用基数排序是不合适的?br /> <br /> <br /> <br /> Bucket sortQ桶排序<br /> 描述Q工作的原理是将阵列分到有限数量的桶子里。每个桶子再个别排序Q有可能再用别的排序算法或是以递回方式l箋使用桶排序进行排序)?br /> 桶排序以下列E序q行Q?br />    1. 讄一个定量的阵列当作I桶子?br />    2. 寻访序列Qƈ且把目一个一个放到对应的桶子厅R?br />    3. Ҏ(gu)个不是空的桶子进行排序?br />    4. 从不是空的桶子里把项目再攑֛原来的序列中?br /> <br /> <br /> <br /> Pigeonhole sortQ鸽巢排?br /> 描述Q是一U时间复杂度为O(n)且在不可避免遍历每一个元素ƈ且排序的情况下效率最好的一U排序算? 但它只有在差?或者可被映在差?很小的范围内的数值排序的情况下实?<br /> 法的步骤如下:与桶排同<br /> <br /> <br /> <br /> <img src ="http://www.aygfsteel.com/Skynet/aggbug/305156.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-12-08 14:21 <a href="http://www.aygfsteel.com/Skynet/archive/2009/12/08/305156.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 法D - 快速排?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/12/03/304668.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/304668.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/12/03/304668.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/304668.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Skynet/services/trackbacks/304668.html</trackback:ping><description><![CDATA[<br /> # 快排 ?分治 很像 都是<strong>分而治?/strong> Q但他们却是 相反?方式排序 Q?br /> 分治 是想拆分完成后,合ƈ以有序的段q行 <strong>排序</strong> Q而快排是直接由原始的“拆分”?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;"> partition(A,p,r):<br />     tmp</span><span style="color: #000000;">=</span><span style="color: #000000;">A[p]<br />     </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> True :<br />         </span><span style="color: #0000ff;">while</span><span style="color: #000000;"> p</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;"><</span><span style="color: #000000;">r </span><span style="color: #0000ff;">and</span><span style="color: #000000;"> A[p] </span><span style="color: #000000;">></span><span style="color: #000000;"> tmp : p</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">while</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;">p </span><span style="color: #0000ff;">and</span><span style="color: #000000;"> A[r] </span><span style="color: #000000;"><=</span><span style="color: #000000;"> tmp : r</span><span style="color: #000000;">-=</span><span style="color: #000000;">1</span><span style="color: #000000;">    <br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> A[p]</span><span style="color: #000000;"><=</span><span style="color: #000000;">A[r]: A[p],A[r]</span><span style="color: #000000;">=</span><span style="color: #000000;">A[r],A[p]<br />     </span><span style="color: #0000ff;">if</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;">p : </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> p<br /> <br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> quickSort(A,p,r):<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> p</span><span style="color: #000000;"><</span><span style="color: #000000;">r:<br />         q</span><span style="color: #000000;">=</span><span style="color: #000000;">partition(A,p,r)<br />         quickSort(A,p,q)<br />         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;"> 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;"> A<br /> <strong># l果 </strong></span><strong>[667, 61, 14, 9, 8, 7, 7, 6, 3, -1]</strong></div> <br /> <br /> <br /> 图解Q?br /> 一ơP代过E描q?Q从到大)Q?br /> 1. ?A[0] 为切分点 用时变?记录 q里?<strong>切分?/strong> = [5] <br /> 2. 分别?2枚指?[切分起左,右]<br /> 3. 分别向中?靠拢 Q?当左指针指向值大?切分?停止 ?Q?x针指向?于 切分?停止 ??br /> 4. 判断 是否?nbsp; 停止??左?于 叛_?是:交换两指针?Q?<br /> <img src="http://www.aygfsteel.com/images/blogjava_net/skynet/42926/qs.jpg" alt="" border="0" /><br /> <strong>W一ơP代后 :  <br />   以初始分?[5]  已l切分好?于 5 的左 Q大于等? 的右</strong><br /> <br /> <img src ="http://www.aygfsteel.com/Skynet/aggbug/304668.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-12-03 17:11 <a href="http://www.aygfsteel.com/Skynet/archive/2009/12/03/304668.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 法D - ?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/12/01/304352.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/304352.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/12/01/304352.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/304352.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/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" >CoderZh</a>Q?a target="_blank" >CoderZh的技术博?- 博客?/a>Q?br /> 出处Q?a target="_blank" >http://coderzh.cnblogs.com/</a><br /> # 我这 加了?个h理解?说明<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;</span><span style="color: #008000;"><br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> heap_adjust(data, s, m):<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> s </span><span style="color: #000000;">></span><span style="color: #000000;"> m:</span><span style="color: #0000ff;">return</span><span style="color: #000000;"><br />    <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 声明 预设父节点位|?/span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;"><br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> [左]子节点?nbsp;大于 父节点?nbsp; :  预设父节点位|?nbsp;?nbsp;左子节点位置</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> data[</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;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">] </span><span style="color: #000000;">></span><span style="color: #000000;"> data[temp]: temp </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;">s</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> [右]子节点?nbsp;大于 预设父节?nbsp;: 预设父节点位|?nbsp;?nbsp;叛_节点位置</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> s </span><span style="color: #000000;"><=</span><span style="color: #000000;"> m </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: #0000ff;">and</span><span style="color: #000000;"> data[</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;"> data[temp]: temp </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;">*</span><span style="color: #000000;"> s<br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 交换?nbsp;满 堆特?nbsp;此ؓ [ 父节?nbsp;于 子节?nbsp; ]</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> temp </span><span style="color: #000000;"><></span><span style="color: #000000;"> s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">:<br />         data[s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">], data[temp] </span><span style="color: #000000;">=</span><span style="color: #000000;"> data[temp], data[s </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">]<br />         heap_adjust(data, temp </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">, m)<br /> <br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> heap_sort(data):<br />     m </span><span style="color: #000000;">=</span><span style="color: #000000;"> len(data) </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;"><br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 构徏 堆树</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #008000;">#</span><span style="color: #008000;"> 试数据 [3,2,1] 数组gؓ 所以非底层叶节?/span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> range(m , 0, </span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />         heap_adjust(data, i, len(data))<br /> <br />     <br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> 从堆树中 [出栈] 排序输出</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #008000;">#</span><span style="color: #008000;"> 试数据 [5, 4, 3, 2]</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    data[0], data[</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;"> data[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">], data[0]<br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> n </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> range(len(data) </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;">1</span><span style="color: #000000;">, </span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />         heap_adjust(data, </span><span style="color: #000000;">1</span><span style="color: #000000;">, n)<br />         data[0], data[n </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;">=</span><span style="color: #000000;"> data[n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">], 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;"> data<br /> </span><span style="color: #008000;">#</span><span style="color: #008000;"> [-1, 2, 3, 3, 6, 8, 9, 868]</span></div> <strong><br /> <br /> 转自 ?</strong>http://www.cppblog.com/guogangj/<strong> ?br /> 堆存?/strong> <br /> <img src="http://www.aygfsteel.com/images/blogjava_net/skynet/42926/o_ds_binary_heap_array.png" alt="" border="0" /><br /> <br /> <br /> <strong>?入栈 复杂度ؓΟ(logn)</strong><br /> <img src="http://www.aygfsteel.com/images/blogjava_net/skynet/42926/r_ds_binary_heap_insert.png" alt="" border="0" /><br /> <br /> <br /> <br /> <br /> <strong>?出栈  Ο(logn)</strong><br /> <br /> <img src="http://www.aygfsteel.com/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.aygfsteel.com/Skynet/aggbug/304352.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-12-01 12:05 <a href="http://www.aygfsteel.com/Skynet/archive/2009/12/01/304352.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 法D - 递归?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/11/25/303704.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/303704.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/11/25/303704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/303704.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Skynet/services/trackbacks/303704.html</trackback:ping><description><![CDATA[<br /> 看第二节  - 递归树方?Q?br /> H发奇想 能否 使用 txt 构造出 递归q程  <br /> q是?上此提到?递归Ҏ(gu) 分治排序<br /> <br /> <br /> <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: #000000;">arr</span><span style="color: #000000;">=</span><span style="color: #000000;">[]<br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> printTree():<br />     ac </span><span style="color: #000000;">=</span><span style="color: #000000;"> []<br />     ii </span><span style="color: #000000;">=</span><span style="color: #000000;"> 0 <br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> r </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> arr :<br />         c,ss,cc </span><span style="color: #000000;">=</span><span style="color: #000000;"> r <br />         sc </span><span style="color: #000000;">=</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: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> 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 />         </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> xrange(len(sc)) :<br />             </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> i </span><span style="color: #000000;">%</span><span style="color: #000000;"> cc </span><span style="color: #000000;">==</span><span style="color: #000000;"> 0 : 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;"> <br />         </span><span style="color: #008000;">#</span><span style="color: #008000;">print "ci %s ii %s = %s "%(ci,ii,ii < ci)</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ii</span><span style="color: #000000;">>=</span><span style="color: #000000;">c  : <br />             sc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </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;">  </span><span style="color: #800000;">'</span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> :<br />             sc </span><span style="color: #000000;">=</span><span style="color: #000000;"> </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 />         ii </span><span style="color: #000000;">=</span><span style="color: #000000;"> c<br />         ac.append( sc )<br />         <br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> r </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> ac[::</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;"> r<br />     <br /> <br /> </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> MERGE(A,p,q,r):<br />     </span><span style="color: #008000;">#</span><span style="color: #008000;">print "%s:%s - %s:%s" % (p,q+1,q+1,r+1)</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> p</span><span style="color: #000000;">==</span><span style="color: #000000;">q : L </span><span style="color: #000000;">=</span><span style="color: #000000;"> [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 />     </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> : L </span><span style="color: #000000;">=</span><span style="color: #000000;"> 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 />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> q</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">==</span><span style="color: #000000;">r : R </span><span style="color: #000000;">=</span><span style="color: #000000;"> [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 />     </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> : R </span><span style="color: #000000;">=</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;">]</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 />     i </span><span style="color: #000000;">=</span><span style="color: #000000;"> j </span><span style="color: #000000;">=</span><span style="color: #000000;"> 0<br />     </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> k </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> xrange(p,r</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />         </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> L[i]</span><span style="color: #000000;"><</span><span style="color: #000000;">R[j] :<br />             A[k]</span><span style="color: #000000;">=</span><span style="color: #000000;">L[i]<br />             i</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />         </span><span style="color: #0000ff;">else</span><span style="color: #000000;">:<br />             A[k]</span><span style="color: #000000;">=</span><span style="color: #000000;">R[j]<br />             j</span><span style="color: #000000;">+=</span><span style="color: #000000;">1</span><span style="color: #000000;"><br />     </span><span style="color: #008000;">#</span><span style="color: #008000;"> print "%s:%s = %s \n%s:%s = %s\n\n%s" % ( p,q, L , q+1,r,R, 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;"> MERGE_SORT(A,p,r,c</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">):<br />     </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> p</span><span style="color: #000000;"><</span><span style="color: #000000;">r:<br />         q </span><span style="color: #000000;">=</span><span style="color: #000000;"> (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 />         MERGE_SORT(A,p,q,c</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br />         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 />         arr.append( (c,</span><span style="color: #800000;">"</span><span style="color: #800000;">%s - %s</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">%</span><span style="color: #000000;"> ( 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;">]) , </span><span style="color: #000000;">3</span><span style="color: #000000;"> ) )<br />         </span><span style="color: #008000;">#</span><span style="color: #008000;"> Debugging(A,p,q,r, sc )</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">        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;"> A<br /> printTree() <br /> <br /> <br /> </span></div> <br /> <br /> 输出 Q重下往上看  输出 排序q程 Q我׃多说?应该很好理解了!Q)Q?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, 2, 2, 3, 4, 5, 6, 7]<br /> ├─[2, 4, 5, 7] - [1, 2, 3, 6]<br /> ?nbsp; ├─[1, 3] - [2, 6]<br /> ?nbsp; ?nbsp; ├─[2] - [6]<br /> ?nbsp; ?nbsp; └─[1] - [3]<br /> ?nbsp; ├─[2, 5] - [4, 7]<br /> ?nbsp; ?nbsp; ├─[7] - [4]<br /> ?nbsp; ?nbsp; └─[5] - [2]</span></div> <br /> <br /> <br /> <br /> <img src ="http://www.aygfsteel.com/Skynet/aggbug/303704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-11-25 23:09 <a href="http://www.aygfsteel.com/Skynet/archive/2009/11/25/303704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 法D - 函数的增?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/11/23/303404.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/303404.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/11/23/303404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/303404.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Skynet/services/trackbacks/303404.html</trackback:ping><description><![CDATA[<br /> <img alt="" src="http://www.aygfsteel.com/images/blogjava_net/skynet/ywl.jpg" border="0" width="161" height="186" /><br /> <strong><br /> <br /> <br /> 3.1 渐近?/strong><br /> <br /> 渐近范围      f(n) = θ(g(n))  ~a=b      <br /> 渐近上界      f(n) = Ο(g(n))  ~a<=b    0≤f(n)≤cg(n) <br /> 渐近下界      f(n) = Ω(g(n))  ~a>=b    0≤cg(n)≤f(n)<br /> 非渐q上?nbsp;  f(n) = o(g(n))    ~a<b     0≤f(n)<cg(n)   =>lim[n<=∞](f(n)/g(n))=0<br /> 非渐q下?nbsp;  f(n) = ω(g(n))   ~a>b     0≤cg(n)<f(n)   =>lim[n<=∞](f(n)/g(n))=0<br /> <br /> <br /> 渐近号用(目前我能理解到的Q): <br /> 当渐q符号出现在某个公式中时Q我们将其解释ؓ一个不在乎其名U的|名函数?br /> 例:2n^2+3n+1 = 2n^2+θ(n) Q这U用法有助于屏蔽无关紧要的细节,如低阉。?br /> <br /> ∑[1≤k≤n]O(i)<br /> <br /> <br /> <strong>3.2 标准记号和常量函?/strong><br /> 单调?Q?单调递增 Q?单调递减<br /> # 传说中的q播体操原来?上下取整?Q?呵呵<br /> 下取_上取?Q?x-1 < └X?<=  x   <=  ┌X?nbsp; <  x+1<br /> <br /> 取模q算  a mod n  = a-└a/n┘n <br /> <br /> 多项?nbsp; p(n) = ∑[0≤i≤d] a.i n^i<br /> <br /> 指数 (a^m)^n = a^(m*n)   ;  a^m*a^n = a^(m+n)<br /> <br /> # 指数中的 Ҏ(gu)W号 e <br /> # e不论对x微分几次Q结果都q是eQ难怪数学系学生会用e比喻坚定不移的爱情! <br /> # 数学中的爱情W号 e 哈哈Q!<br /> e <span name="whlm" id="whlm">= lim[</span>n≤∞<span name="whlm" id="whlm">](1+1/n)^n  </span><br /> <br /> <br /> Ҏ(gu) <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 /> 阶乘  n!<br /> <br /> <br /> 函数q代<br /> <br /> <br /> 斐L那切 <br /> F0 = 0<br /> F1 = 1<br /> ..<br /> Fi = Fi-1+Fi-2<br />  <br /> <br /> <br /> <img src ="http://www.aygfsteel.com/Skynet/aggbug/303404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-11-23 23:33 <a href="http://www.aygfsteel.com/Skynet/archive/2009/11/23/303404.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>推荐法 - 皮尔逊函?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/11/22/303271.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/303271.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/11/22/303271.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/303271.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Skynet/services/trackbacks/303271.html</trackback:ping><description><![CDATA[<br /> <br /> 公式Q?br /> <img src="http://www.aygfsteel.com/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);">数据 elt 清洗?txt)</span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> 一?nbsp;user ?nbsp;item 分值化 </span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> 比如 用户下蝲Q收藏,试听 某item {等</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">user    items    score<br /> <img src="http://www.aygfsteel.com/Images/dot.gif" alt="" />.<br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> l果输出 (bdb)</span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> user    item1:score1,item2:score2,item3:score3<img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /><img src="http://www.aygfsteel.com/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);"><<</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);"> bsddb<br /> db </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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);"> row </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> 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 />     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 />     dr </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);">:</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);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> db.has_key(dr[0]) : 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 />     </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> : 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);"> l果输出 (txt)</span><span style="color: rgb(0, 128, 0);"><br /> #</span><span style="color: rgb(0, 128, 0);"> user    user     score</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /><br /> <br /> python</span><span style="color: rgb(0, 0, 0);"><<</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);"> bsddb<br /> </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> math </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"><br /> db </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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);"> ps(u1,u2):<br />     um1</span><span style="color: rgb(0, 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);"> v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> 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);">) :<br />         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 />         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 />     um2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />     si</span><span style="color: rgb(0, 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);"> v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> 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);">) :<br />         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 />         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 />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> um1.has_key( v[0] ) : si.append(v[0])<br />     n </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> len(si)<br /> <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> n </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);"> :</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> None<br />     <br />     sum1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum( [um1[it] </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si] )<br />     sum2</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum( [um2[it] </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si] )<br />     <br />     sum1Sq</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum([ pow(um1[it],</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si])<br />     sum2Sq</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">sum([ pow(um2[it],</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si])<br />     <br />     pSum </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sum( [ um1[it]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">um2[it] </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> it </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> si ] )<br /> <br />     num </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> pSum </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);"> (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 />     den </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqrt( (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 )</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">( 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 ) )<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> 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);"> : </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> None<br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> num</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">den<br /> <br /> fc </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> 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 />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> j </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> 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 />         sc </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ps(str(i),str(j))  <br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> sc </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> None: </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);">fc, </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);"> </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">(i,j,sc)      <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);"> 试使用</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);"><<</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);"> bsddb<br /> db </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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);"> 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);">    </span><span style="color: rgb(0, 0, 0);">30604</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 /> <br /> </span><span style="color: rgb(0, 0, 255);">print</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);">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);"> </span><span style="color: rgb(0, 0, 0);">1.0</span><span style="color: rgb(0, 0, 0);"><br /> (Pdb) </span><span style="color: rgb(0, 0, 255);">print</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);">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);"> </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 /> 如果对大家对 推荐有一些了解,数据能到 用户与用户关p?分值化) ,是能q很多事情了?br /> 比如Q?br />   1. 首先得到某用Lq度最高的几位z跃用户Q看q几位用户在看什么,听什?然后推荐出去 <br /> <br /> 扩展Q?br />   把初始?反过?nbsp; item  user  score Q然后统计出 item ?item 之间的关p??br />   ?消费某一产品 Q马上推荐出 其他的相q的产品 Q時时推荐)<br /> <br />   <br /> <br /> <img src ="http://www.aygfsteel.com/Skynet/aggbug/303271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-11-22 23:56 <a href="http://www.aygfsteel.com/Skynet/archive/2009/11/22/303271.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我一起学 - 法D - 分治法http://www.aygfsteel.com/Skynet/archive/2009/11/22/303269.html刘凯?/dc:creator>刘凯?/author>Sun, 22 Nov 2009 15:26:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/11/22/303269.htmlhttp://www.aygfsteel.com/Skynet/comments/303269.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/11/22/303269.html#Feedback0http://www.aygfsteel.com/Skynet/comments/commentRss/303269.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/303269.html
def MERGE(A,p,q,r):
    
print "%s:%s - %s:%s" % (p,q+1,q+1,r+1)
    
if p==q : L = [A[p],10**10]
    
else : L = A[p:q+1]+[10**10]

    
if q+1==r : R = [A[r],10**10]
    
else : R = A[q+1:r+1]+[10*10]

    i 
= j = 0
    
for k in xrange(p,r+1):
        
if L[i]<R[j] :
            A[k]
=L[i]
            i
+=1
        
else:
            A[k]
=R[j]
            j
+=1
    
# print "%s:%s = %s \n%s:%s = %s\n\n%s" % ( p,q, L , q+1,r,R, A)


def Debugging(A,p,q,r,c):
    
print "%s\t%s:%s - %s:%s" % (c,p,q,q+1,r)

def MERGE_SORT(A,p,r,c=1):
    
if p<r:
        q 
= (p+r)/2
        MERGE_SORT(A,p,q,c
+1)
        MERGE_SORT(A,q
+1,r,c+1)
        
#Debugging(A,p,q,r,c)
        MERGE(A,p,q,r)

A
=[5,2,7,4,1,3,2,6]
print A
MERGE_SORT(A,0,len(A)
-1)
print A

l果输出》?br /> python 2f.py
[5, 2, 7, 4, 1, 3, 2, 6]
[1, 2, 2, 3, 4, 5, 6, 7]


分n些细节:法q不难,但确实写了很久,调试让我很郁闗?br /> 直到写了 def Debugging  目测Q?br />
python 2f.py
3       0:0 - 1:1
3       2:2 - 3:3
2       0:1 - 2:3
3       4:4 - 5:5
3       6:6 - 7:7
2       4:5 - 6:7
1       0:3 - 4:7
?每层 Ҏ(gu)l的 数组下标取?Q?
?python 中当
arr = [1,2,3,4] 我希望能取出 [2,3] ?arr[1:3] 是最后一位不计算在内?br /> 最典型?nbsp; arr[0,1]  == [1]











]]>
理解高斯函数Q以及在推荐法中的应用http://www.aygfsteel.com/Skynet/archive/2009/11/19/302901.html刘凯?/dc:creator>刘凯?/author>Thu, 19 Nov 2009 03:14:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/11/19/302901.htmlhttp://www.aygfsteel.com/Skynet/comments/302901.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/11/19/302901.html#Feedback1http://www.aygfsteel.com/Skynet/comments/commentRss/302901.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/302901.html q是一个很有用?公式比如Q用h费分值权?Q?产品兌分值权?{等


公式 

?http://www.wolframalpha.com 中表C?:
e = (1+1/n) ^n
a*e^(-(x-b)^2/c^2) 
a 峰值最大?
b 峰值x轴偏U量
c 弧度跨度


  =  1*e^(-(x-1)^2/1^2)




修改 峰?a = 2



q里 ?不一一展现 b 峰值x轴偏U量 Q?c 弧度跨度 ?大家可以 ?wolframalpha 自己d?br />


实例1 与时间有关的递减 Q?br />

import math
def gaussian(x,peak=1.0,axis=1.0,span=1.0):
    
return peak*math.e**(-(x-axis)**2/(span)**2 )


跨度 c 参?
= 1 : ?.5 附g急剧衰减
= 2 : 4
= 18 :30 # q个?nbsp;衰减l计 一个月 不错
= 55 :90 # 衰减l计 一个季?nbsp;不错


#单应?nbsp;
消费1ơ得峰??nbsp;览1ơ峰??nbsp;
l计某用户季度得?br /> 数据Q在?0天浏?0ơ,消费1?nbsp;Q前11天浏??nbsp;
d10 
= gaussian(10,span=55.0)
d11 
= gaussian(11,span=55.0)
print d10*10*2+d10*4*1+d11*5*2
#l果 33.0407089687


倒的高斯 Q 实例2  Q?br /> 公式 =

 
目的 与次数有关的产品分值化
#用户 ?nbsp;某?nbsp;分值化
#
 比如 某用?nbsp;用过某?nbsp;nơ,我希?nbsp;n 无限大是一?nbsp;渐进某个?nbsp;而不是和 n 无限递增?/span>
#
下面?nbsp;fun l果?nbsp; 1.6 ?nbsp;10 分值直接的区域, 也就?nbsp;传说中的 产品感兴?nbsp;“10分制” 易版   
def gs(x,peak=9.0,axis=-2.0,span=11.0):
    
return "%.4f" % (-1*peak*math.e**(-(x-axis)**2/(span)**2 )+peak+1)


>>> gs(1)
'1.6451'
>>> gs(2)
'2.1148'
>>> gs(3)
'2.6800'
>>> gs(4)
'3.3161'
>>> gs(5)
'3.9970'
>>> gs(6)
'4.6969'
>>> gs(60)
'10.0000'






]]>
跟我一起学 - 法D - 插入排序http://www.aygfsteel.com/Skynet/archive/2009/11/18/302864.html刘凯?/dc:creator>刘凯?/author>Wed, 18 Nov 2009 15:29:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/11/18/302864.htmlhttp://www.aygfsteel.com/Skynet/comments/302864.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/11/18/302864.html#Feedback0http://www.aygfsteel.com/Skynet/comments/commentRss/302864.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/302864.html#  插入排序                -         复杂?/span>
def insertion_sort(arr):             # 1
    for j in xrange( 1,len(arr) ):   # n-1
        key = arr[j]                 # n-1
        i=j-1                        # n-1
        while i>=and arr[i]> key : # n(n-1)/2
            arr[i+1= arr[i]        # n(n-1)/2
            i=i-1                    # n(n-1)/2
        arr[i+1= key               # n-1
        print arr                    # n-1

验证l果 :
>>> arr=[5,2,4,6,1,3]
>>> insertion_sort(arr)
[2, 5, 4, 6, 1, 3]
[2, 4, 5, 6, 1, 3]
[2, 4, 5, 6, 1, 3]
[1, 2, 4, 5, 6, 3]
[1, 2, 3, 4, 5, 6]





验证复杂度:
z = 5(n-1)+1+3n(n-1)/2
我们试数据 ?nbsp; n=6 
当数据极端情况就是需要全部重新排?
是 [6,5,4,3,2,1] 要排?[1,2,3,4,5,6] q样
>> z = 71

一U比较笨?验证Ҏ(gu) 供大家拍?:
def insertion_sort(arr):         
    ii
=0
    ii
+=1
    
for j in xrange( 1,len(arr) ):
        ii
+=1
        
        key 
= arr[j]            
        ii
+=1
        
        i
=j-1                
        ii
+=1
        
        
while i>=and arr[i]> key :    
            ii
+=1
            
            arr[i
+1= arr[i]    
            ii
+=1
            
            i
=i-1            
            ii
+=1
            
        arr[i
+1= key            
        ii
+=1
        
        
print arr            
        ii
+=1

    
print "----",str(ii)

>>> arr=[6,5,4,3,2,1]
>>> insertion_sort(arr)
[5, 6, 4, 3, 2, 1]
[4, 5, 6, 3, 2, 1]
[3, 4, 5, 6, 2, 1]
[2, 3, 4, 5, 6, 1]
[1, 2, 3, 4, 5, 6]
---- 71  #复杂度验证ؓ 71


|嗦?n(n-1)/2
极端情况?nbsp;
i=1 ; j 需要挪?1?br /> i=2 ; j 挪动 1+2?br /> i=3 ; j 挪动 1+2+3?br /> ....
i=n ; j 挪动 1+2....+n

我们又找?(1+n)+(2+n-1)+(3+n-2).... = (1+n)n/2
我们q??i 是从 2 ơ开始的 也就是说  (n-1)n/2 ?
def tn(ii):
    ti
=0
    
for i in xrange(ii) :
        
for j in xrange(i) :
            ti
+=1
    
print ti


print tn(2)  #1 = 1
print tn(3)  #3 = 1+2
print tn(4)  #6 = 1+2+3
..








]]>
法 复杂度分析图http://www.aygfsteel.com/Skynet/archive/2009/11/17/302742.html刘凯?/dc:creator>刘凯?/author>Tue, 17 Nov 2009 15:12:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/11/17/302742.htmlhttp://www.aygfsteel.com/Skynet/comments/302742.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/11/17/302742.html#Feedback0http://www.aygfsteel.com/Skynet/comments/commentRss/302742.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/302742.html Q如果大家有兴趣p我一?- 《算法导论?/strong>Q也望大家监督我能每天拿Z时和大家分享算?法代码我会量使用 py ?解决一些分析日志的应用上靠 Q其实,上面费劲?二叉hash 是ؓ了分析日志中 - 希望能实?多个大文件不需要合q就能根据某列排序输? 目前的解军_?find .. -exec cat {} \; | perl |sort 的笨Ҏ(gu) Q?

1. 法在计中的作?Q笔讎ͼ :
   法(algorithm):是定义良好的计过E,它取一个或一l作出,q生一个或一l自作ؓ输出?br />
一些函数运行?nbsp; # http://www.wolframalpha.com/ 函数都可在网站里q行
q里 n=一亿条数据 :
log_2(n)     30
n^0.5        31622
n            10^9
n*log_2(n)   2.9^10
n^2          10^18
n^3          10^27
2^n          无穷
n!           10^362880


需要知道的复杂?- 在某一个界点?合ƈ会别插入要快?br />   插入排序  复杂?n^2    http://www.wolframalpha.com/input/?i=n^2
  合ƈ排序  复杂?n*log_2(n)  http://www.wolframalpha.com/input/?i=n*log_2%28n%29+

|上的查扑ֈ的一些名Uͼ参?http://www.51testing.com/?uid-130868-action-viewspace-itemid-66729
1.1E_排序  非稳定排?-
  E_排序是所有相{的数经q某U排序方法后Q仍能保持它们在排序之前的相Ҏ(gu)序,。反之,是非稳定的排序?br /> 1.2内排?nbsp; 外排?/strong>
  在排序过E中Q所有需要排序的数都在内存,q在内存中调整它们的存储序Q称为内排序Q?在排序过E中Q只有部分数被调入内存,q借助内存调整数在外存中的存放序排序Ҏ(gu)UCؓ外排序?br /> 1.3法的时间复杂度  I间复杂?/strong>
  所谓算法的旉复杂度,是指执行法所需要的计算工作量?一个算法的I间复杂度,一般是指执行这个算法所需要的内存I间?br />

几种常见的算法复杂度Q?br /> 2.1冒排序 QBubble SortQ?nbsp;   O(n^2)
2.2选择排序 QSelection SortQ?O(n^2 )
2.3插入排序 QInsertion SortQ?O(n^2)
2.4堆排?nbsp;   O( nlog(n) )
2.5归ƈ排序  O( nlog_2(n)  )
2.6快速排?nbsp; 最?O( nlog_2(n) ) 最?O(n^2)




wiki 参?Qhttp://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95
I定?/strong>
  • 冒排序Qbubble sortQ??O(n2)
  • 插入排序 Qinsertion sortQ?O(n2)
  • a數排序 (counting sort) ?O(n+k); 需?O(n+k) 外I間
  • 合併排序 Qmerge sortQ?O(n log n); 需?O(n) 外I間
  • 原地合併排序 ?O(n2)
  • 二叉排序?/a>排序 QBinary tree sortQ??O(n log n)期望時間; O(n2)最壞時? 需?O(n) 外I間
  • ¿l(f)排序 (Pigeonhole sort) ?O(n+k); 需?O(k) 外I間
  • 基數排序 Qradix sortQ?O(n·k); 需?O(n) 外I間
  • [不穩?/span>
    • 選擇排序 Qselection sortQ?O(n2)
    • 希爾排序 Qshell sortQ?O(n log n) 如果使用最佳的珑֜版本
    • 堆排?/a> QheapsortQ?O(n log n)
    • 快速排?/a> QquicksortQ?O(n log n) 期望時間, O(n2) 最壞情? 於大的、亂怸列一般相信是最快的已知排序
    • [不實用的排序演算?/span>
      • Bogo排序 ?O(n × n!) 期望時間Q無H的最壞情況?/li>


      • ]]>法公式 - 囑Ş?http://www.aygfsteel.com/Skynet/archive/2009/11/05/301255.html刘凯?/dc:creator>刘凯?/author>Thu, 05 Nov 2009 05:09:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/11/05/301255.htmlhttp://www.aygfsteel.com/Skynet/comments/301255.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/11/05/301255.html#Feedback1http://www.aygfsteel.com/Skynet/comments/commentRss/301255.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/301255.html
         在定?数学公式的时?可能 希望有个直观?展现
        我们前一D|间用到的  { 推荐分?90 ?递减公式Q如果这东西早发?公式׃会错了!}
        目前我们递减的公?/div>
        y=-x/90+1
        y = -x/90+1
         
         在比如我们优化下  90天改?
        (y*10)^2=-x+90
        100 y^2 = 90-x
        2009-11-05





        ]]>apriori-实现http://www.aygfsteel.com/Skynet/archive/2009/09/01/293479.html刘凯?/dc:creator>刘凯?/author>Tue, 01 Sep 2009 09:43:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/09/01/293479.htmlhttp://www.aygfsteel.com/Skynet/comments/293479.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/09/01/293479.html#Feedback0http://www.aygfsteel.com/Skynet/comments/commentRss/293479.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/293479.html孙超
        讲解
        q就 把他?思想 M?




        ]]>apriori 法QpyQ?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/Skynet/archive/2009/08/31/293309.html</guid><wfw:comment>http://www.aygfsteel.com/Skynet/comments/293309.html</wfw:comment><comments>http://www.aygfsteel.com/Skynet/archive/2009/08/31/293309.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Skynet/comments/commentRss/293309.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/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);"> -*- coding: UTF8 -*-</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);"> sys<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);">sup_min </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);">])<br /> <br /> ss </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 /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 交易 数据 ?/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 />  </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 />  </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 />  </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 />  </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 />  </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);"> </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);">for</span><span style="color: rgb(0, 0, 0);"> arr </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> D : </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> 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 tid in open('BuyMusic.20090722.mob.prodIds').readlines()[:rows] :<br />     D.append(tid.split("\n")[0].split("\t")[1])<br /> <br /> <br /> print "d 文gl束 BuyMusic.20090722.mob.prodIds !"<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;攉</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">sup_data_map </span><span style="color: rgb(0, 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);">全局  最大频J项 攉</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);"> 遍历q程 临时 局?nbsp; 频繁?nbsp;攉</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">mapL </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);"> W一?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);"> find_frequent_1_itemset(I):<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> 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);"> </span><span style="color: rgb(0, 0, 255);">or</span><span style="color: rgb(0, 0, 0);"> 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);"> : </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">  <br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapL.has_key(I): 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 />     </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">: 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,[ I  </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> TID </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> D </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> I  </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> TID.split(ss) ])<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, 255);">def</span><span style="color: rgb(0, 0, 0);"> 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 />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k  </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> [k </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> map.items() </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> v</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">supmin] :<br />         </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> 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);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">W一?nbsp;{?nbsp;频繁?nbsp;l束!</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);"> mapL<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 装蝲 全局 频繁?nbsp;最大频J项</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);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> mapL.items() : <br />     sup_data_map[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />     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);"> 判定 是否 'BD' 属于  'BCD' ?nbsp;</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">isInTid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">lambda</span><span style="color: rgb(0, 0, 0);"> I,TID : len(I.split(ss)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> len([i </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> I </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> TID.split(ss)])<br /> <br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> l合  [A,B] + [A,C] = [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);"> comb(arr1,arr2):<br />     tmap</span><span style="color: rgb(0, 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);"> v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> arr1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">arr2 : 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);"> <br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> tmap.keys()<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> apriori q代核心</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);"> runL(mapL,dep):<br />     mapL2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {}<br />     C</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{}<br />     keys </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mapL.keys()<br />     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 />     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 />     </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Ҏ(gu) 上次  频繁?nbsp;Q生成本?nbsp;'可能频繁? 集合 </span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> ii </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> range(len(keys)) : <br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> jj </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> 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)) :<br />             keystr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">comb([ch </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> ch </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> keys[ii].split(ss)],[ch </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> ch </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> keys[jj].split(ss)])<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> len(keystr) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> dep : </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br />             keystr.sort()<br />             tk</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">ss.join(keystr)<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> tk </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> C : C[tk]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(keys[ii],keys[jj])<br /> <br />     </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">  '可能频繁? Ҏ(gu) 交易数据?nbsp; 计数</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> tk,z </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> C.items():<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> TID </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> D:<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isInTid(tk,TID) :<br />                 </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapL2.has_key(tk): 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 />                 </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">: 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 />     </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);">    remove_not_sup_min(mapL2)<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">  is_zsup.items() :<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k1,v1 </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> mapL2.items() :<br />             </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isInTid(k,k1) :<br />                 </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> is_zsup[k]<br />                 </span><span style="color: rgb(0, 0, 255);">break</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;,最大频J项  攉 </span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> mapL2.items() : <br />         sup_data_map[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />         is_zsup[k]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">v<br />     </span><span style="color: rgb(0, 0, 255);">print</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);">+</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;l束!</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);">return</span><span style="color: rgb(0, 0, 0);"> mapL2<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 真正 q行 </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);"> mapL :<br />     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 />     mapL </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> runL(mapL,ii)<br />     </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> mapL<br /> <br /> </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 全局  频繁?nbsp;?nbsp;去除 最大频J项</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);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> is_zsup.items() :<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> sup_data_map.has_key(k) : </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> sup_data_map[k]<br /> <br /> </span><span style="color: rgb(0, 0, 255);">print</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><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> sup_data_map<br /> </span><span style="color: rgb(0, 0, 255);">print</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);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">最大频J项</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);"> is_zsup<br /> </span><span style="color: rgb(0, 0, 255);">print</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);"> </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);"> k,v </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">  sup_data_map.items() :<br />     </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> k1,v1 </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> is_zsup.items() :<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isInTid(k,k1) :<br />             </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> k,</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);">,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);"> </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 /> l果:<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 /> W一?{?频繁?l束!<br /> {'A': 3, 'C': 4, 'B': 5, 'E': 3, 'D': 3}<br /> W??{?频繁?l束!<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 /> W??{?频繁?l束!<br /> {'A,B,D': 2, 'A,B,C': 3, 'B,C,D': 2, 'B,C,E': 2, 'A,C,D': 2}<br /> W??{?频繁?l束!<br /> {'A,B,C,D': 2}<br /> W??{?频繁?l束!<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 /> 最大频J项<br /> {'B,C,E': 2, 'A,B,C,D': 2}<br /> <br /> 可信?展现<br /> A -> A,B,C,D     66.7%<br /> C -> B,C,E     50.0%<br /> C -> A,B,C,D     50.0%<br /> B -> B,C,E     40.0%<br /> B -> A,B,C,D     40.0%<br /> E -> B,C,E     66.7%<br /> D -> A,B,C,D     66.7%<br /> C,D -> A,B,C,D     100.0%<br /> C,E -> B,C,E     100.0%<br /> A,D -> A,B,C,D     100.0%<br /> A,B -> A,B,C,D     66.7%<br /> A,C -> A,B,C,D     66.7%<br /> A,B,D -> A,B,C,D     100.0%<br /> B,C,D -> A,B,C,D     100.0%<br /> A,C,D -> A,B,C,D     100.0%<br /> B,E -> B,C,E     66.7%<br /> B,D -> A,B,C,D     66.7%<br /> B,C -> B,C,E     50.0%<br /> B,C -> A,B,C,D     50.0%<br /> A,B,C -> A,B,C,D     66.7%<br /> <br /> <img src ="http://www.aygfsteel.com/Skynet/aggbug/293309.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Skynet/" target="_blank">刘凯?/a> 2009-08-31 14:25 <a href="http://www.aygfsteel.com/Skynet/archive/2009/08/31/293309.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>k-means QpythonQ算?http://www.aygfsteel.com/Skynet/archive/2009/08/07/290242.html刘凯?/dc:creator>刘凯?/author>Fri, 07 Aug 2009 08:20:00 GMThttp://www.aygfsteel.com/Skynet/archive/2009/08/07/290242.htmlhttp://www.aygfsteel.com/Skynet/comments/290242.htmlhttp://www.aygfsteel.com/Skynet/archive/2009/08/07/290242.html#Feedback0http://www.aygfsteel.com/Skynet/comments/commentRss/290242.htmlhttp://www.aygfsteel.com/Skynet/services/trackbacks/290242.html ?http://www.daniweb.com/forums/thread31449.html
        什么都不说了,直接看代码吧?br /> 注解 应该写的比较详细


        # liukaiyi 
        # ?nbsp;k-means Q维度类?nbsp;- 数值Ş?nbsp;( 199 ?nbsp;23.13 

        import sys, math, random

        # -- cd '数据' 
        #
         ?nbsp;n-l度I间
        class Point:
            
        def __init__(self, coords, reference=None):
                self.coords 
        = coords
                self.n 
        = len(coords)
                self.reference 
        = reference
            
        def __repr__(self):
                
        return str(self.coords)

        # -- cd '聚集?nbsp;/ 聚类q_距离 ?nbsp;' 
        #
         -- ?nbsp;n-l度I间
        #
         -- k-means 核心c?/span>
        #
         -- 每次 聚集各点 围绕?nbsp;q行聚集 
        #
         -- q提供方?nbsp;?聚集后的计算中心点,同时记入 此次 中心?聚集各点q_距离)Qؓ下一ơ聚集提供中心点.
        class Cluster:
            
        def __init__(self, points):
                
        if len(points) == 0: raise Exception("ILLEGAL: EMPTY CLUSTER")
                self.points 
        = points
                self.n 
        = points[0].n
            
        for p in points:
                    
        if p.n != self.n: raise Exception("ILLEGAL: MULTISPACE CLUSTER")
                
        # ?nbsp;聚集各点?nbsp;q_?/span>
            self.centroid = self.calculateCentroid()
            
        def __repr__(self):
                
        return str(self.points)
            
            
        # 更新 中心点,q返?nbsp;原中心点 ?nbsp;C心点(聚集各点q_距离)距离  
            def update(self, points):
                old_centroid 
        = self.centroid
                self.points 
        = points
                self.centroid 
        = self.calculateCentroid()
                
        return getDistance(old_centroid, self.centroid)
            
            
        # 计算q_?nbsp;Q聚?攉各点Q离本类的中心点Q最q数?后生成新?nbsp;中心?nbsp;Q?/span>
            def calculateCentroid(self):
                centroid_coords 
        = []
                
        #  l度 q代
            for i in range(self.n):
                    centroid_coords.append(
        0.0)
                    
        # 攉各点 q代 
                for p in self.points:
                        centroid_coords[i] 
        = centroid_coords[i]+p.coords[i]
                    centroid_coords[i] 
        = centroid_coords[i]/len(self.points)
                
        return Point(centroid_coords)

        # -- q回Ҏ(gu) k-means 聚集形成?nbsp;数据?nbsp;
        def kmeans(points, k, cutoff):
            
        # Randomly sample k Points from the points list, build Clusters around them
            initial = random.sample(points, k)
            clusters 
        = []
            
        for p in initial: clusters.append(Cluster([p]))
            
        # q代 k-means 直到 每次q代 各收集点 别的 最?nbsp;不超q?nbsp;0.5 
            while True:
                
        #  k 个收?nbsp;数组
                lists = []
                
        for c in clusters: lists.append([])
            
        # q代 每个 数据?nbsp;Qƈ计算与每个中心点距离
            # q把数据Ҏ(gu)加入相应最短的中心Ҏ(gu)集数l中
            # 在P代中 smallest_distance 为每个点与各中心Ҏ(gu)短距?nbsp;参数Q请注意?/span>
                for p in points:
                    smallest_distance 
        = getDistance(p, clusters[0].centroid)
                    index 
        = 0
                    
        for i in range(len(clusters[1:])):
                        distance 
        = getDistance(p, clusters[i+1].centroid)
                        
        if distance < smallest_distance:
                            smallest_distance 
        = distance
                            index 
        = i+1
                    
        # d?nbsp;L短中心距ȝ 数组?/span>
                lists[index].append(p)
            
                
        # 聚集完,计算?nbsp;中心?/span>
            # q?nbsp;cluster.centroid 属性记入下 C心点Q下一?nbsp;聚集的中心点 Q?/span>
            # q?nbsp;计算与上一?nbsp;中心?nbsp;距离 Q如?nbsp;差值在 cutoff 0.5 以下 ,跛_q代 Q结束,q回最后一?nbsp;聚集集合Q?/span>
            biggest_shift = 0.0
                
        for i in range(len(clusters)):
                    shift 
        = clusters[i].update(lists[i])
                    biggest_shift 
        = max(biggest_shift, shift)
                
        if biggest_shift < cutoff: break
            
        return clusters


        # -- 得到Ƨ几里d距离两点之间 
        def getDistance(a, b):
            
        # Forbid measurements between Points in different spaces
            if a.n != b.n: raise Exception("ILLEGAL: NON-COMPARABLE POINTS")
            
        # Euclidean distance between a and b is sqrt(sum((a[i]-b[i])^2) for all i)
            ret = 0.0
            
        for i in range(a.n):
                ret 
        = ret+pow((a.coords[i]-b.coords[i]), 2)
            
        return math.sqrt(ret)

        # -- ?nbsp;n-l度 I间中创?nbsp;随机?/span>
        #
         -- 随机生成 试数据
        def makeRandomPoint(n, lower, upper):
            coords 
        = []
            
        for i in range(n): coords.append(random.uniform(lower, upper))
            
        return Point(coords)

        # main 
        def main(args):
            
        # 参数说明
            # num_points,    n,    k,      cutoff,         lower,        upper 
            # 随机数据数量 , l度, 聚集? 跛_q代最距?nbsp;,   l度数最大?l度数最?/span>
            num_points, n, k, cutoff, lower, upper = 10230.5-200200

            
        # ?nbsp;n-l度I间?nbsp;, 创徏 num_points 随机?/span>
            # 试数据生成 
            points = []
            
        for i in range(num_points): points.append(makeRandomPoint(n, lower, upper))

            
        # 使用 k-means 法Q来 聚集数据?nbsp;(法入口?
            clusters = kmeans(points, k, cutoff)

            
        print "\nPOINTS:"
            
        for p in points: print "P:", p
            
        print "\nCLUSTERS:"
            
        for c in clusters: print "C:", c
        if __name__ == "__main__": main(sys.argv)



        ]]>
        վ֩ģ壺 | | | | | ͼ| ũ| | ˮ| | ײ| | | º| | ʯ| | Т| ׿| | ̫| Ұ| | | | | | ˰| | | ΢ɽ| ٹ| | ֿ| ӡ| | DZ| Զ| º| ͺ| ̶|