﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-welcome-随笔分类-Other</title><link>http://www.cppblog.com/stdyh/category/3709.html</link><description>vc java sdk</description><language>zh-cn</language><lastBuildDate>Thu, 31 Jan 2013 15:46:43 GMT</lastBuildDate><pubDate>Thu, 31 Jan 2013 15:46:43 GMT</pubDate><ttl>60</ttl><item><title>Bloom filter：大数据快速排除算法(zz)</title><link>http://www.cppblog.com/stdyh/archive/2013/01/25/197564.html</link><dc:creator>dyh</dc:creator><author>dyh</author><pubDate>Fri, 25 Jan 2013 08:15:00 GMT</pubDate><guid>http://www.cppblog.com/stdyh/archive/2013/01/25/197564.html</guid><wfw:comment>http://www.cppblog.com/stdyh/comments/197564.html</wfw:comment><comments>http://www.cppblog.com/stdyh/archive/2013/01/25/197564.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/stdyh/comments/commentRss/197564.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/stdyh/services/trackbacks/197564.html</trackback:ping><description><![CDATA[ZZ from <div>http://www.xiuwz.com/site/tech-bloom-filter/</div><br /><div><p style="text-indent: 2em;"><a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>是 由 Howard Bloom在 1970  年提出的一种多哈希函数映射的快速查找算法，该算法能够在非常快速的判定某个元素是否在一个集合之外。这种检测只会对在集合内的数据错判，而不会对不是集 合内的数据进行错判，这样每个检测请求返回有&#8220;在集合内（可能错误）&#8221;和&#8220;不在集合内（绝对不在集合内）&#8221;两种情况。目前<a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>在分布式系统中有着广泛的使用，比如说GFS/HDFS/Cassandra/<a title="BigTable" href="http://en.wikipedia.org/wiki/BigTable">Bigtable</a>/<a title="Squid" href="http://en.wikipedia.org/wiki/Squid_%28software%29">Squid</a>。</p> <p style="text-indent: 2em;"><a href="http://www.xiuwz.com/site/tech-bloom-filter"><img title="bloom filter" src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Bloom_filter.svg/360px-Bloom_filter.svg.png" alt="" height="129" width="360" /></a></p> <h3>实例</h3> <p style="text-indent: 2em;">为了说明<a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>存在的重要意义，举一个实例：</p> <p style="text-indent: 2em;">假设要你写一个网络蜘蛛（web crawler）。由于网络间的链接错综复杂，蜘蛛在网络间爬行很可能会形成&#8220;环&#8221;。为了避免形成&#8220;环&#8221;，就需要知道蜘蛛已经访问过那些URL。给一个URL，怎样知道蜘蛛是否已经访问过呢？稍微想想，就会有如下几种方案：</p> <ol><li>将访问过的URL保存到数据库。</li><li>用HashSet将访问过的URL保存起来。那只需接近O(1)的代价就可以查到一个URL是否被访问过了。</li><li>URL经过MD5或SHA-1等单向哈希后再保存到HashSet或数据库。</li><li>Bit-Map方法。建立一个BitSet，将每个URL经过一个哈希函数映射到某一位。</li></ol> <p style="text-indent: 2em;">方法1~3都是将访问过的URL完整保存，方法4则只标记URL的一个映射位。</p> <p style="text-indent: 2em;">以上方法在数据量较小的情况下都能完美解决问题，但是当数据量变得非常庞大时问题就来了。</p> <p style="text-indent: 2em;">方法1的缺点：数据量变得非常庞大后关系型数据库查询的效率会变得很低。而且每来一个URL就启动一次数据库查询是不是太小题大做了？</p> <p style="text-indent: 2em;">方法2的缺点：太消耗内存。随着URL的增多，占用的内存会越来越多。就算只有1亿个URL，每个URL只算50个字符，就需要5GB内存。</p> <p style="text-indent: 2em;">方法3：由于字符串经过MD5处理后的信息摘要长度只有128Bit，SHA-1处理后也只有160Bit，因此方法3比方法2节省了好几倍的内存。</p> <p style="text-indent: 2em;">方法4消耗内存是相对较少的，但缺点是单一哈希函数发生冲突的概率太高。还记得数据结构课上学过的Hash表冲突的各种解决方法么？若要降低冲突发生的概率到1%，就要将BitSet的长度设置为URL个数的100倍。</p> <p style="text-indent: 2em;">实质上上面的算法都忽略了一个重要的隐含条件：允许小概率的出错，不一定要100%准确！也就是说少量url实际上没有没网络蜘蛛访问，而将它们错判为已访问的代价是很小的&#8212;&#8212;大不了少抓几个网页呗。</p> <h3>Bloom Filter的算法</h3> <p style="text-indent: 2em;">下面引入本篇的主角&#8212;&#8212;<a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>。其实上面方法4的思想已经很接近<a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>了。方法四的致命缺点是冲突概率高，为了降低冲突的概念，<a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a><strong>使用了多个哈希函数，而不是一个</strong>。</p> <p style="text-indent: 2em;"><a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>采 用的是哈希函数的方法，将一个元素映射到一个 m 长度的阵列上的一个点，当这个点是 1  时，那么这个元素在集合内，反之则不在集合内。这个方法的缺点就是当检测的元素量很多时候可能有冲突，解决方法就是使用 k 个哈希 函数对应 k  个点，如果所有点都是 1 的话，那么元素在集合内，如果有 0 的话，元素则不再集合内。</p> <h3>Bloom filter 特点</h3> <p style="text-indent: 2em;"><a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>优点就是它的插入和查询时间都是常数，另外它查询元素却不保存元素本身，具有良好的安全性。它的缺点也是显而易见的，<strong>当插入的元素越多，错判&#8220;在集合内&#8221;的概率就越大了</strong>，另外 <a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a>也不能删除一个元素，因为多个元素哈希的结果可能在 <a title="Bloom filter" href="http://en.wikipedia.org/wiki/Bloom_filter">Bloom filter</a> 结构中占用的是同一个位，如果删除了一个比特位，可能会影响多个元素的检测。</p> <p style="text-indent: 2em;">其实要做到能够删除一个元素，就需要修改下算法，<strong>把bitmap修改成计数</strong>，这会带来另外一个缺点：<strong>内存浪费</strong>。</p></div><img src ="http://www.cppblog.com/stdyh/aggbug/197564.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/stdyh/" target="_blank">dyh</a> 2013-01-25 16:15 <a href="http://www.cppblog.com/stdyh/archive/2013/01/25/197564.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>可视化graph的工具: GraphViz</title><link>http://www.cppblog.com/stdyh/archive/2007/03/06/19321.html</link><dc:creator>dyh</dc:creator><author>dyh</author><pubDate>Tue, 06 Mar 2007 13:35:00 GMT</pubDate><guid>http://www.cppblog.com/stdyh/archive/2007/03/06/19321.html</guid><wfw:comment>http://www.cppblog.com/stdyh/comments/19321.html</wfw:comment><comments>http://www.cppblog.com/stdyh/archive/2007/03/06/19321.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/stdyh/comments/commentRss/19321.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/stdyh/services/trackbacks/19321.html</trackback:ping><description><![CDATA[
		<h4 class="TextColor1" id="subjcns!3BB36966ED98D3E5!393" style="MARGIN-BOTTOM: 0px">可视化graph的工具: GraphViz</h4>
		<div id="msgcns!3BB36966ED98D3E5!393">
				<div>这套工具可以把有向图(digraph)和无向图(graph)在平面内展现出来，方便观察。GraphViz使用DOT(一种图形描述语言)描述图，然后有解释工具dot生成图像文件。dot支持多种图像文件，包括非矢量的gif、矢量的ps、svg等约20多种格式。DOT语言也非常简单易学。举个例子：<br /><br />digraph G {<br /> size = "4,4"<br /> main [shape=box]; /* this is a comment */<br /> main -&gt; parse [weight=8];<br /> parse -&gt; execute;<br /> main -&gt; init [style=dotted];<br /> main -&gt; cleanup;<br /> execute -&gt; { make_string; printf}<br /> init -&gt; make_string;<br /> edge [color=red]; // so is this<br /> main -&gt; printf [style=bold, label="100 times"];<br /> node [shape=box, style=filled, color=".7.3 1.0"];<br /> execute -&gt; compare;<br />}<br /><br />存为test.dot，然后执行<br />  &gt; dot test.dot -Tpng -o test.png<br />就生成了graph的图像文件。很方便哦。</div>
				<div> </div>
				<div>实际上GraphViz还可以画出很多漂亮的“图”。比如ER图，hash table示意图。更多请参考：<a href="http://www.graphviz.org/Gallery.php"><font color="#956633">http://www.graphviz.org/Gallery.php</font></a>。以后可以考虑用GraphViz画一些示意图，既方便有专业，还很容易转成ps(eps)。继续研究研究。</div>
				<div> </div>
		</div>
		<table cellspacing="0" border="0">
				<tbody>
						<tr height="8">
								<td>
								</td>
						</tr>
						<tr>
								<td valign="top">
										<a href="http://tkfiles.storage.msn.com/x1piYkpqHC_35lu-E3rDrYKj8sE0KNXhelYTR4gjpTZgEk6QwWgpkh0Fe24z-FtOzNU2sxOrOR8J5CG6sLbUDp0kh7ItE5CU55kSJwIsLlpy0BLqEBpf0i4qA" target="_blank">
												<img src="http://tkfiles.storage.msn.com/x1piYkpqHC_35lu-E3rDrYKj8sE0KNXhelYSp4hB2Zb0ifB3JfOod3AyelkYRfZ5DUJKFqhBM2fZmmH-EBkrDbEfZ-12laDxtblfMv0-sii32w" border="0" />
										</a>
								</td>
								<td width="15">
								</td>
								<td valign="top">
										<a href="http://tkfiles.storage.msn.com/x1piYkpqHC_35lu-E3rDrYKj7s4IOzjQKttbac7GfZ8yoEcQ-G0L9CDNIsSrYIWQyUHhcSLj0QgtoszRbMTb7mAE5GOQPLU5GW--XjzhYbEbBbH2dDHSjuXtg" target="_blank">
												<img src="http://tkfiles.storage.msn.com/x1piYkpqHC_35lu-E3rDrYKj7s4IOzjQKttKmT907RtXsxSTAzEgPfOvoDziNMV977PgDFczKJnCoAOtk-SVIjKNv27zIwsLIv28VwiwBwQQiI" border="0" />
										</a>
								</td>
						</tr>
				</tbody>
		</table>
<img src ="http://www.cppblog.com/stdyh/aggbug/19321.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/stdyh/" target="_blank">dyh</a> 2007-03-06 21:35 <a href="http://www.cppblog.com/stdyh/archive/2007/03/06/19321.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>