锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲最大黄色网站,精品亚洲成A人无码成A在线观看 ,久久久久国产成人精品亚洲午夜 http://www.tkk7.com/czihong/category/52083.htmlzh-cnFri, 26 Apr 2013 13:07:47 GMTFri, 26 Apr 2013 13:07:47 GMT60涓鑷存ash綆楁硶 - consistent hashinghttp://www.tkk7.com/czihong/articles/398076.htmlChan ChenChan ChenFri, 19 Apr 2013 03:16:00 GMThttp://www.tkk7.com/czihong/articles/398076.htmlhttp://www.tkk7.com/czihong/comments/398076.htmlhttp://www.tkk7.com/czihong/articles/398076.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/398076.htmlhttp://www.tkk7.com/czihong/services/trackbacks/398076.htmlRefer to: http://blog.csdn.net/sparkliang/article/details/5279393

consistent hashing 綆楁硶鏃╁湪 1997 騫村氨鍦ㄨ鏂?/span> Consistent hashing and random trees 涓鎻愬嚭錛岀洰鍓嶅湪 cache 緋葷粺涓簲鐢ㄨ秺鏉ヨ秺騫挎硾錛?/span>

1 鍩烘湰鍦烘櫙

姣斿浣犳湁 N 涓?/span> cache 鏈嶅姟鍣紙鍚庨潰綆縐?/span> cache 錛夛紝閭d箞濡備綍灝嗕竴涓璞?/span> object 鏄犲皠鍒?/span> N 涓?/span> cache 涓婂憿錛屼綘寰堝彲鑳戒細(xì)閲囩敤綾諱技涓嬮潰鐨勯氱敤鏂規(guī)硶璁$畻 object 鐨?/span> hash 鍊鹼紝鐒跺悗鍧囧寑鐨勬槧灝勫埌鍒?/span> N 涓?/span> cache 錛?/span>

hash(object)%N

涓鍒囬兘榪愯姝e父錛屽啀鑰冭檻濡備笅鐨勪袱縐嶆儏鍐碉紱

涓涓?/span> cache 鏈嶅姟鍣?/span> m down 鎺変簡(jiǎn)錛堝湪瀹為檯搴旂敤涓繀欏昏鑰冭檻榪欑鎯呭喌錛夛紝榪欐牱鎵鏈夋槧灝勫埌 cache m 鐨勫璞¢兘浼?xì)澶辨晥锛屾庝箞鍔烇紝闇瑕佹妸 cache m 浠?/span> cache 涓Щ闄わ紝榪欐椂鍊?/span> cache 鏄?/span> N-1 鍙幫紝鏄犲皠鍏紡鍙樻垚浜?/span> hash(object)%(N-1) 錛?/span>

鐢變簬璁塊棶鍔犻噸錛岄渶瑕佹坊鍔?/span> cache 錛岃繖鏃跺?/span> cache 鏄?/span> N+1 鍙幫紝鏄犲皠鍏紡鍙樻垚浜?/span> hash(object)%(N+1) 錛?/span>

鍜?/span> 2 鎰忓懗鐫浠涔堬紵榪欐剰鍛崇潃紿佺劧涔嬮棿鍑犱箮鎵鏈夌殑 cache 閮藉け鏁堜簡(jiǎn)銆傚浜庢湇鍔″櫒鑰岃█錛岃繖鏄竴鍦虹伨闅撅紝媧按鑸殑璁塊棶閮戒細(xì)鐩存帴鍐插悜鍚庡彴鏈嶅姟鍣紱

鍐嶆潵鑰冭檻絎笁涓棶棰橈紝鐢變簬紜歡鑳藉姏瓚婃潵瓚婂己錛屼綘鍙兘鎯寵鍚庨潰娣誨姞鐨勮妭鐐瑰鍋氱偣媧伙紝鏄劇劧涓婇潰鐨?/span> hash 綆楁硶涔熷仛涓嶅埌銆?/span>

  鏈変粈涔堟柟娉曞彲浠ユ敼鍙樿繖涓姸鍐靛憿錛岃繖灝辨槸 consistent hashing...

2 hash 綆楁硶鍜屽崟璋冩?/h2>

銆銆 Hash 綆楁硶鐨勪竴涓 閲忔寚鏍囨槸鍗曡皟鎬э紙 Monotonicity 錛夛紝瀹氫箟濡備笅錛?/span>

銆銆鍗曡皟鎬ф槸鎸囧鏋滃凡緇忔湁涓浜涘唴瀹歸氳繃鍝堝笇鍒嗘淳鍒頒簡(jiǎn)鐩稿簲鐨勭紦鍐蹭腑錛屽張鏈夋柊鐨勭紦鍐插姞鍏ュ埌緋葷粺涓傚搱甯岀殑緇撴灉搴旇兘澶熶繚璇佸師鏈夊凡鍒嗛厤鐨勫唴瀹瑰彲浠ヨ鏄犲皠鍒版柊鐨勭紦鍐蹭腑鍘伙紝鑰屼笉浼?xì)琚槧灏勫埌鏃х殑缂撳啿闆嗗悎涓殑鍏朵粬缂撳啿鍖恒?/span>

瀹規(guī)槗鐪嬪埌錛屼笂闈㈢殑綆鍗?/span> hash 綆楁硶 hash(object)%N 闅句互婊¤凍鍗曡皟鎬ц姹傘?/span>

3 consistent hashing 綆楁硶鐨勫師鐞?/h2>

consistent hashing 鏄竴縐?/span> hash 綆楁硶錛岀畝鍗曠殑璇達(dá)紝鍦ㄧЩ闄?/span> / 娣誨姞涓涓?/span> cache 鏃訛紝瀹冭兘澶熷敖鍙兘灝忕殑鏀瑰彉宸插瓨鍦?/span> key 鏄犲皠鍏崇郴錛屽敖鍙兘鐨勬弧瓚沖崟璋冩х殑瑕佹眰銆?/span>

涓嬮潰灝辨潵鎸夌収 5 涓楠ょ畝鍗曡璁?/span> consistent hashing 綆楁硶鐨勫熀鏈師鐞嗐?/span>

3.1 鐜艦hash 絀洪棿

鑰冭檻閫氬父鐨?/span> hash 綆楁硶閮芥槸灝?/span> value 鏄犲皠鍒頒竴涓?/span> 32 涓虹殑 key 鍊鹼紝涔熷嵆鏄?/span> 0~2^32-1 嬈℃柟鐨勬暟鍊肩┖闂達(dá)紱鎴戜滑鍙互灝嗚繖涓┖闂存兂璞℃垚涓涓錛?/span> 0 錛夊熬錛?/span> 2^32-1 錛夌浉鎺ョ殑鍦嗙幆錛屽涓嬮潰鍥?/span> 1 鎵紺虹殑閭f牱銆?/span>

circle space

鍥?/span> 1 鐜艦 hash 絀洪棿

3.2 鎶婂璞℃槧灝勫埌hash 絀洪棿

鎺ヤ笅鏉ヨ冭檻 4 涓璞?/span> object1~object4 錛岄氳繃 hash 鍑芥暟璁$畻鍑虹殑 hash 鍊?/span> key 鍦ㄧ幆涓婄殑鍒嗗竷濡傚浘 2 鎵紺恒?/span>

hash(object1) = key1;

… …

hash(object4) = key4;

object

鍥?/span> 2 4 涓璞$殑 key 鍊煎垎甯?/span>

3.3 鎶奵ache 鏄犲皠鍒癶ash 絀洪棿

Consistent hashing 鐨勫熀鏈濇兂灝辨槸灝嗗璞″拰 cache 閮芥槧灝勫埌鍚屼竴涓?/span> hash 鏁板肩┖闂翠腑錛屽茍涓斾嬌鐢ㄧ浉鍚岀殑hash 綆楁硶銆?/span>

鍋囪褰撳墠鏈?/span> A,B 鍜?/span> C 鍏?/span> 3 鍙?/span> cache 錛岄偅涔堝叾鏄犲皠緇撴灉灝嗗鍥?/span> 3 鎵紺猴紝浠栦滑鍦?/span> hash 絀洪棿涓紝浠ュ搴旂殑 hash鍊兼帓鍒椼?/span>

hash(cache A) = key A;

… …

hash(cache C) = key C;

cache

鍥?/span> 3 cache 鍜屽璞$殑 key 鍊煎垎甯?/span>

 

璇村埌榪欓噷錛岄『渚挎彁涓涓?/span> cache 鐨?/span> hash 璁$畻錛屼竴鑸殑鏂規(guī)硶鍙互浣跨敤 cache 鏈哄櫒鐨?/span> IP 鍦板潃鎴栬呮満鍣ㄥ悕浣滀負(fù)hash 杈撳叆銆?/span>

3.4 鎶婂璞℃槧灝勫埌cache

鐜板湪 cache 鍜屽璞¢兘宸茬粡閫氳繃鍚屼竴涓?/span> hash 綆楁硶鏄犲皠鍒?/span> hash 鏁板肩┖闂翠腑浜?jiǎn)锛屾帴涓嬫潵瑕佽冭檻鐨勫氨鏄浣曞皢瀵硅薄鏄犲皠鍒?/span> cache 涓婇潰浜?jiǎn)銆?/span>

鍦ㄨ繖涓幆褰㈢┖闂翠腑錛屽鏋滄部鐫欏烘椂閽堟柟鍚戜粠瀵硅薄鐨?/span> key 鍊煎嚭鍙戯紝鐩村埌閬囪涓涓?/span> cache 錛岄偅涔堝氨灝嗚瀵硅薄瀛樺偍鍦ㄨ繖涓?/span> cache 涓婏紝鍥犱負(fù)瀵硅薄鍜?/span> cache 鐨?/span> hash 鍊兼槸鍥哄畾鐨勶紝鍥犳榪欎釜 cache 蹇呯劧鏄敮涓鍜岀‘瀹氱殑銆傝繖鏍蜂笉灝辨壘鍒頒簡(jiǎn)瀵硅薄鍜?/span> cache 鐨勬槧灝勬柟娉曚簡(jiǎn)鍚楋紵錛?/span>

渚濈劧緇х畫(huà)涓婇潰鐨勪緥瀛愶紙鍙傝鍥?/span> 3 錛夛紝閭d箞鏍規(guī)嵁涓婇潰鐨勬柟娉曪紝瀵硅薄 object1 灝嗚瀛樺偍鍒?/span> cache A 涓婏紱 object2鍜?/span> object3 瀵瑰簲鍒?/span> cache C 錛?/span> object4 瀵瑰簲鍒?/span> cache B 錛?/span>

3.5 鑰冨療cache 鐨勫彉鍔?/h3>

鍓嶉潰璁茶繃錛岄氳繃 hash 鐒跺悗姹備綑鐨勬柟娉曞甫鏉ョ殑鏈澶ч棶棰樺氨鍦ㄤ簬涓嶈兘婊¤凍鍗曡皟鎬э紝褰?/span> cache 鏈夋墍鍙樺姩鏃訛紝cache 浼?xì)澶辨晥锛寴q涜屽鍚庡彴鏈嶅姟鍣ㄩ犳垚宸ㄥぇ鐨勫啿鍑伙紝鐜板湪灝辨潵鍒嗘瀽鍒嗘瀽 consistent hashing 綆楁硶銆?/span>

3.5.1 縐婚櫎 cache

鑰冭檻鍋囪 cache B 鎸傛帀浜?jiǎn)锛屾犚?guī)嵁涓婇潰璁插埌鐨勬槧灝勬柟娉曪紝榪欐椂鍙楀獎(jiǎng)鍝嶇殑灝嗕粎鏄偅浜涙部 cache B 閫嗘椂閽堥亶鍘嗙洿鍒頒笅涓涓?/span> cache 錛?/span> cache C 錛変箣闂寸殑瀵硅薄錛屼篃鍗蟲(chóng)槸鏈潵鏄犲皠鍒?/span> cache B 涓婄殑閭d簺瀵硅薄銆?/span>

鍥犳榪欓噷浠呴渶瑕佸彉鍔ㄥ璞?/span> object4 錛屽皢鍏墮噸鏂版槧灝勫埌 cache C 涓婂嵆鍙紱鍙傝鍥?/span> 4 銆?/span>

remove

鍥?/span> 4 Cache B 琚Щ闄ゅ悗鐨?/span> cache 鏄犲皠

3.5.2 娣誨姞 cache

鍐嶈冭檻娣誨姞涓鍙版柊鐨?/span> cache D 鐨勬儏鍐碉紝鍋囪鍦ㄨ繖涓幆褰?/span> hash 絀洪棿涓紝 cache D 琚槧灝勫湪瀵硅薄 object2 鍜?/span>object3 涔嬮棿銆傝繖鏃跺彈褰卞搷鐨勫皢浠呮槸閭d簺娌?/span> cache D 閫嗘椂閽堥亶鍘嗙洿鍒頒笅涓涓?/span> cache 錛?/span> cache B 錛変箣闂寸殑瀵硅薄錛堝畠浠槸涔熸湰鏉ユ槧灝勫埌 cache C 涓婂璞$殑涓閮ㄥ垎錛夛紝灝嗚繖浜涘璞¢噸鏂版槧灝勫埌 cache D 涓婂嵆鍙?/span>

 

鍥犳榪欓噷浠呴渶瑕佸彉鍔ㄥ璞?/span> object2 錛屽皢鍏墮噸鏂版槧灝勫埌 cache D 涓婏紱鍙傝鍥?/span> 5 銆?/span>

add

鍥?/span> 5 娣誨姞 cache D 鍚庣殑鏄犲皠鍏崇郴

4 铏氭嫙鑺傜偣

鑰冮噺 Hash 綆楁硶鐨勫彟涓涓寚鏍囨槸騫寵 鎬?/span> (Balance) 錛屽畾涔夊涓嬶細(xì)

騫寵 鎬?/span>

銆銆騫寵 鎬ф槸鎸囧搱甯岀殑緇撴灉鑳藉灝藉彲鑳藉垎甯冨埌鎵鏈夌殑緙撳啿涓幓錛岃繖鏍峰彲浠ヤ嬌寰楁墍鏈夌殑緙撳啿絀洪棿閮藉緱鍒板埄鐢ㄣ?/span>

hash 綆楁硶騫朵笉鏄繚璇佺粷瀵圭殑騫寵 錛屽鏋?/span> cache 杈冨皯鐨勮瘽錛屽璞″茍涓嶈兘琚潎鍖鐨勬槧灝勫埌 cache 涓婏紝姣斿鍦ㄤ笂闈㈢殑渚嬪瓙涓紝浠呴儴緗?/span> cache A 鍜?/span> cache C 鐨勬儏鍐典笅錛屽湪 4 涓璞′腑錛?/span> cache A 浠呭瓨鍌ㄤ簡(jiǎn) object1 錛岃?/span> cache C 鍒欏瓨鍌ㄤ簡(jiǎn) object2 銆?/span> object3 鍜?/span> object4 錛涘垎甯冩槸寰堜笉鍧囪 鐨勩?/span>

涓轟簡(jiǎn)瑙e喅榪欑鎯呭喌錛?/span> consistent hashing 寮曞叆浜?#8220;铏氭嫙鑺傜偣”鐨勬蹇碉紝瀹冨彲浠ュ涓嬪畾涔夛細(xì)

“铏氭嫙鑺傜偣”錛?/span> virtual node 錛夋槸瀹為檯鑺傜偣鍦?/span> hash 絀洪棿鐨勫鍒跺搧錛?/span> replica 錛夛紝涓瀹為檯涓妭鐐瑰搴斾簡(jiǎn)鑻ュ共涓?#8220;铏氭嫙鑺傜偣”錛岃繖涓搴斾釜鏁頒篃鎴愪負(fù)“澶嶅埗涓暟”錛?#8220;铏氭嫙鑺傜偣”鍦?/span> hash 絀洪棿涓互 hash 鍊兼帓鍒椼?/span>

浠嶄互浠呴儴緗?/span> cache A 鍜?/span> cache C 鐨勬儏鍐典負(fù)渚嬶紝鍦ㄥ浘 4 涓垜浠凡緇忕湅鍒幫紝 cache 鍒嗗竷騫朵笉鍧囧寑銆傜幇鍦ㄦ垜浠紩鍏ヨ櫄鎷熻妭鐐癸紝騫惰緗?#8220;澶嶅埗涓暟”涓?/span> 2 錛岃繖灝辨剰鍛崇潃涓鍏變細(xì)瀛樺湪 4 涓?#8220;铏氭嫙鑺傜偣”錛?/span> cache A1, cache A2 浠h〃浜?/span> cache A 錛?/span> cache C1, cache C2 浠h〃浜?/span> cache C 錛涘亣璁句竴縐嶆瘮杈冪悊鎯崇殑鎯呭喌錛屽弬瑙佸浘 6 銆?/span>

virtual nodes

鍥?/span> 6 寮曞叆“铏氭嫙鑺傜偣”鍚庣殑鏄犲皠鍏崇郴

 

姝ゆ椂錛屽璞″埌“铏氭嫙鑺傜偣”鐨勬槧灝勫叧緋諱負(fù)錛?/span>

objec1->cache A2 錛?/span> objec2->cache A1 錛?/span> objec3->cache C1 錛?/span> objec4->cache C2 錛?/span>

鍥犳瀵硅薄 object1 鍜?/span> object2 閮借鏄犲皠鍒頒簡(jiǎn) cache A 涓婏紝鑰?/span> object3 鍜?/span> object4 鏄犲皠鍒頒簡(jiǎn) cache C 涓婏紱騫寵 鎬ф湁浜?jiǎn)寰堝ぇ鎻愰珮銆?/span>

寮曞叆“铏氭嫙鑺傜偣”鍚庯紝鏄犲皠鍏崇郴灝變粠 { 瀵硅薄 -> 鑺傜偣 } 杞崲鍒頒簡(jiǎn) { 瀵硅薄 -> 铏氭嫙鑺傜偣 } 銆傛煡璇㈢墿浣撴墍鍦?/span> cache鏃剁殑鏄犲皠鍏崇郴濡傚浘 7 鎵紺恒?/span>

map

鍥?/span> 7 鏌ヨ瀵硅薄鎵鍦?/span> cache

 

“铏氭嫙鑺傜偣”鐨?/span> hash 璁$畻鍙互閲囩敤瀵瑰簲鑺傜偣鐨?/span> IP 鍦板潃鍔犳暟瀛楀悗緙鐨勬柟寮忋備緥濡傚亣璁?/span> cache A 鐨?/span> IP 鍦板潃涓?/span>202.168.14.241 銆?/span>

寮曞叆“铏氭嫙鑺傜偣”鍓嶏紝璁$畻 cache A 鐨?/span> hash 鍊鹼細(xì)

Hash(“202.168.14.241”);

寮曞叆“铏氭嫙鑺傜偣”鍚庯紝璁$畻“铏氭嫙鑺?#8221;鐐?/span> cache A1 鍜?/span> cache A2 鐨?/span> hash 鍊鹼細(xì)

Hash(“202.168.14.241#1”);  // cache A1

Hash(“202.168.14.241#2”);  // cache A2

5 灝忕粨

Consistent hashing 鐨勫熀鏈師鐞嗗氨鏄繖浜涳紝鍏蜂綋鐨勫垎甯冩х瓑鐞嗚鍒嗘瀽搴旇鏄緢澶嶆潅鐨勶紝涓嶈繃涓鑸篃鐢ㄤ笉鍒般?/span>

http://weblogs.java.net/blog/2007/11/27/consistent-hashing 涓婇潰鏈変竴涓?/span> java 鐗堟湰鐨勪緥瀛愶紝鍙互鍙傝冦?/span>

http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx 杞澆浜?jiǎn)涓涓?/span> PHP 鐗堢殑瀹炵幇浠g爜銆?/span>

http://www.codeproject.com/KB/recipes/lib-conhash.aspx C璇█鐗堟湰


 

涓浜涘弬鑰冭祫鏂欏湴鍧錛?/span>

http://portal.acm.org/citation.cfm?id=258660

http://en.wikipedia.org/wiki/Consistent_hashing

http://www.spiteful.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/

 http://weblogs.java.net/blog/2007/11/27/consistent-hashing

http://tech.idv2.com/2008/07/24/memcached-004/

http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx



Chan Chen 2013-04-19 11:16 鍙戣〃璇勮
]]>
Hacker News綆楁硶http://www.tkk7.com/czihong/articles/396897.htmlChan ChenChan ChenSat, 23 Mar 2013 05:35:00 GMThttp://www.tkk7.com/czihong/articles/396897.htmlhttp://www.tkk7.com/czihong/comments/396897.htmlhttp://www.tkk7.com/czihong/articles/396897.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/396897.htmlhttp://www.tkk7.com/czihong/services/trackbacks/396897.htmlHacker News鏄竴涓綉緇滅ぞ鍖猴紝鍙互寮犺創(chuàng)閾炬帴錛屾垨鑰呰璁烘煇涓富棰樸?/p>

姣忎釜甯栧瓙鍓嶉潰鏈変竴涓悜涓婄殑涓夎褰紝濡傛灉浣犺寰楄繖涓唴瀹瑰緢濂斤紝灝辯偣鍑諱竴涓嬶紝鎶曚笂涓紲ㄣ傛牴鎹緱紲ㄦ暟錛岀郴緇熻嚜鍔ㄧ粺璁″嚭鐑棬鏂囩珷鎺掕姒溿備絾鏄紝騫墮潪寰楃エ鏈澶氱殑鏂囩珷鎺掑湪絎竴浣嶏紝榪樿鑰冭檻鏃墮棿鍥犵礌錛屾柊鏂囩珷搴旇姣旀棫鏂囩珷鏇村鏄撳緱鍒板ソ鐨勬帓鍚嶃?/p>

Hacker News浣跨敤Paul Graham寮鍙戠殑Arc璇█緙栧啓錛屾簮鐮佸彲浠ヤ粠arclanguage.org涓嬭澆銆傚畠鐨勬帓鍚嶇畻娉曟槸榪欐牱瀹炵幇鐨勶細(xì)

灝嗕笂闈㈢殑浠g爜榪樺師涓烘暟瀛﹀叕寮忥細(xì)

鍏朵腑錛?/p>

銆銆P琛ㄧず甯栧瓙鐨勫緱紲ㄦ暟錛屽噺鍘?鏄負(fù)浜?jiǎn)蹇界暐鍙戝笘錆h鐨勬姇紲ㄣ?/p>

銆銆T琛ㄧず璺濈鍙戝笘鐨勬椂闂達(dá)紙鍗曚綅涓哄皬鏃訛級(jí)錛屽姞涓?鏄負(fù)浜?jiǎn)闃叉鏈鏂扮殑甯栧瓙瀵艱嚧鍒嗘瘝榪囧皬錛堜箣鎵浠ラ夋嫨2錛屽彲鑳芥槸鍥犱負(fù)浠庡師濮嬫枃绔犲嚭鐜板湪鍏朵粬緗戠珯錛屽埌杞創(chuàng)鑷矵acker News錛屽鉤鍧囬渶瑕佷袱涓皬鏃訛級(jí)銆?/p>

銆銆G琛ㄧず"閲嶅姏鍥犲瓙"錛坓ravityth power錛夛紝鍗沖皢甯栧瓙鎺掑悕寰(xiàn)涓嬫媺鐨勫姏閲忥紝榛樿鍊間負(fù)1.8錛屽悗鏂囦細(xì)璇︾粏璁ㄨ榪欎釜鍊箋?/p>

浠庤繖涓叕寮忔潵鐪嬶紝鍐沖畾甯栧瓙鎺掑悕鏈変笁涓洜绱狅細(xì)

絎竴涓洜绱犳槸寰楃エ鏁癙銆?/strong>

鍦ㄥ叾浠栨潯浠朵笉鍙樼殑鎯呭喌涓嬶紝寰楃エ瓚婂錛屾帓鍚嶈秺楂樸?/p>

浠?a target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">涓婂浘鍙互鐪嬪埌錛屾湁涓変釜鍚屾椂鍙戣〃鐨勫笘瀛愶紝寰楃エ鍒嗗埆涓?00紲ㄣ?0紲ㄥ拰30紲紙鍑?鍚庝負(fù)199銆?9鍜?9錛夛紝鍒嗗埆浠ラ粍鑹層佺傳鑹插拰钃濊壊琛ㄧず銆傚湪浠諱竴涓椂闂寸偣涓婏紝閮芥槸榛勮壊鏇茬嚎鍦ㄦ渶涓婃柟錛岃摑鑹叉洸綰垮湪鏈涓嬫柟銆?/p>

濡傛灉浣犱笉鎯寵"楂樼エ甯栧瓙"涓?浣庣エ甯栧瓙"鐨勫樊璺濊繃澶э紝鍙互鍦ㄥ緱紲ㄦ暟涓婂姞涓涓皬浜?鐨勬寚鏁幫紝姣斿(P-1)^0.8銆?/p>

絎簩涓洜绱犳槸璺濈鍙戝笘鐨勬椂闂碩銆?/strong>

鍦ㄥ叾浠栨潯浠朵笉鍙樼殑鎯呭喌涓嬶紝瓚婃槸鏂板彂琛ㄧ殑甯栧瓙錛屾帓鍚嶈秺楂樸傛垨鑰呰錛屼竴涓笘瀛愮殑鎺掑悕錛屼細(xì)闅忕潃鏃墮棿涓嶆柇涓嬮檷銆?/p>

浠庡墠涓寮犲浘鍙互鐪嬪埌錛岀粡榪?4灝忔椂涔嬪悗錛屾墍鏈夊笘瀛愮殑寰楀垎鍩烘湰涓婇兘灝忎簬1錛岃繖鎰忓懗鐫瀹冧滑閮藉皢璺屽埌鎺掕姒滅殑鏈熬錛屼繚璇佷簡(jiǎn)鎺掑悕鍓嶅垪鐨勯兘灝嗘槸杈冩柊鐨勫唴瀹廣?/p>

絎笁涓洜绱犳槸閲嶅姏鍥犲瓙G銆?/strong>

瀹冪殑鏁板煎ぇ灝忓喅瀹氫簡(jiǎn)鎺掑悕闅忔椂闂翠笅闄嶇殑閫熷害銆?/p>

浠?a target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: #112233;">涓婂浘鍙互鐪嬪埌錛屼笁鏍規(guī)洸綰跨殑鍏朵粬鍙傛暟閮戒竴鏍鳳紝G鐨勫煎垎鍒負(fù)1.5銆?.8鍜?.0銆侴鍊艱秺澶э紝鏇茬嚎瓚婇櫋宄紝鎺掑悕涓嬮檷寰楄秺蹇紝鎰忓懗鐫鎺掕姒滅殑鏇存柊閫熷害瓚婂揩銆?/p>

鐭ラ亾浜?jiǎn)绠楁硶鐨勬瀯鎴愬Q屽氨鍙互璋冩暣鍙傛暟鐨勫鹼紝浠ラ傜敤浣犺嚜宸辯殑搴旂敤紼嬪簭銆?/p>

Chan Chen 2013-03-23 13:35 鍙戣〃璇勮
]]>
Dynamic Programming: From novice to advancedhttp://www.tkk7.com/czihong/articles/386947.htmlChan ChenChan ChenTue, 04 Sep 2012 04:07:00 GMThttp://www.tkk7.com/czihong/articles/386947.htmlhttp://www.tkk7.com/czihong/comments/386947.htmlhttp://www.tkk7.com/czihong/articles/386947.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/386947.htmlhttp://www.tkk7.com/czihong/services/trackbacks/386947.html闃呰鍏ㄦ枃

Chan Chen 2012-09-04 12:07 鍙戣〃璇勮
]]>
涓夎壊鏃?/title><link>http://www.tkk7.com/czihong/articles/386869.html</link><dc:creator>Chan Chen</dc:creator><author>Chan Chen</author><pubDate>Mon, 03 Sep 2012 06:47:00 GMT</pubDate><guid>http://www.tkk7.com/czihong/articles/386869.html</guid><wfw:comment>http://www.tkk7.com/czihong/comments/386869.html</wfw:comment><comments>http://www.tkk7.com/czihong/articles/386869.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/czihong/comments/commentRss/386869.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/czihong/services/trackbacks/386869.html</trackback:ping><description><![CDATA[<table border="0" cellpadding="0" cellspacing="0" style="font-family: serif; background-color: #ffffff; text-align: left; width: 946px; height: 32px; "><tbody><tr><td style="width: 676px; vertical-align: top; "><h2> 瑾槑</h2><small>涓夎壊鏃楃殑鍟忛鏈鏃╃敱E.W.Dijkstra鎵鎻愬嚭錛屼粬鎵浣跨敤鐨勭敤瑾炵偤Dutch Nation Flag(Dijkstra鐐鴻嵎铇漢)錛岃屽鏁哥殑浣滆呭墖浣跨敤Three-Color Flag渚嗙ū涔嬨?br /><br />鍋囪ō鏈変竴姊濈供瀛愶紝涓婇潰鏈夌磪銆佺櫧銆佽棈涓夌ó欏忚壊鐨勬棗瀛愶紝璧峰垵綣╁瓙涓婄殑鏃楀瓙欏忚壊涓︽矑鏈夐爢搴忥紝鎮(zhèn)ㄥ笇鏈涘皣涔嬪垎欏烇紝涓︽帓鍒楃偤钘嶃佺櫧銆佺磪鐨勯爢搴忥紝瑕佸浣曠Щ鍕曟鏁告墠鏈冩渶灝戯紝娉ㄦ剰鎮(zhèn)ㄥ彧鑳藉湪綣╁瓙涓婇茶閫欏嬪嫊浣滐紝鑰屼笖涓嬈″彧鑳借鎻涘叐鍊嬫棗瀛愩?/small> <br /><h2>瑙f硶</h2><small>鍦ㄤ竴姊濈供瀛愪笂縐誨嫊錛屽湪紼嬪紡涓篃灝辨剰鍛沖彧鑳戒嬌鐢ㄤ竴鍊嬮櫍鍒楋紝鑰屼笉浣跨敤鍏跺畠鐨勯櫍鍒椾締浣滆紨鍔╋紝鍟忛鐨勮В娉曞緢綈″柈錛屾?zhèn)ㄥ彲浠ヨ嚜宸辨儧_儚涓涓嬪湪縐誨嫊鏃楀瓙錛屽緸綣╁瓙闁嬮牠閫茶錛岄亣鍒拌棈鑹插線(xiàn)鍓嶇Щ錛岄亣鍒扮櫧鑹茬暀鍦ㄤ腑闁擄紝閬囧埌绱呰壊寰(xiàn)寰岀Щ錛屽涓嬫墍紺猴細(xì) </small><br /><div style="text-align: center; "><img alt="涓夎壊鏃? title="涓夎壊鏃? src="http://caterpillar.onlyfun.net/Gossip/AlgorithmGossip/images/threeColorsFlags-1.jpg" style="width: 504px; height: 208px; " /></div><small>鍙槸瑕佽畵縐誨嫊嬈℃暩鏈灝戠殑瑭憋紝灝辮鏈変簺鎶宸э細(xì)<br /></small><ol style="margin-top: 3px; margin-bottom: 3px; "><li><small>濡傛灉鍦栦腑W鎵鍦ㄧ殑浣嶇疆鐐虹櫧鑹詫紝鍓嘩+1錛岃〃紺烘湭铏曠悊鐨勯儴浠界Щ鑷寵嚦鐧借壊緹ょ祫銆?/small></li><li><small>濡傛灉W閮ㄤ喚鐐鴻棈鑹詫紝鍓嘊鑸嘩鐨勫厓绱犲皪瑾匡紝鑰孊鑸嘩蹇呴爤鍚?1錛岃〃紺哄叐鍊嬬兢緄勯兘澶氫簡(jiǎn)涓鍊嬪厓绱犮?/small></li><li><small>濡傛灉W鎵鍦ㄧ殑浣嶇疆鏄磪鑹詫紝鍓囧皣W鑸嘡浜ゆ彌錛屼絾R瑕佹笡1錛岃〃紺烘湭铏曠悊鐨勯儴浠芥笡1銆?/small></li></ol><small><br />娉ㄦ剰B銆乄銆丷涓︿笉鏄笁鑹叉棗鐨勫嬫暩錛屽畠鍊戝彧鏄竴鍊嬬Щ鍕曠殑鎸囨錛涗粈楹兼檪鍊欑Щ鍕曠祼鏉熷憿錛熶竴闁嬪鏅傛湭铏曠悊鐨凴鎸囨鏈冩槸絳夋柤鏃楀瓙鐨勭附鏁革紝鐣禦鐨勭儲(chǔ)寮曟暩娓涜嚦灝戞柤W鐨勭儲(chǔ)寮曟暩鏅傦紝琛ㄧず鎺ヤ笅渚嗙殑鏃楀瓙灝遍兘鏄磪鑹蹭簡(jiǎn)錛屾鏅傚氨鍙互緄愭潫縐誨嫊錛屽涓嬫墍紺猴細(xì) </small><br /><div style="text-align: center; "><img alt="涓夎壊鏃? title="涓夎壊鏃? src="http://caterpillar.onlyfun.net/Gossip/AlgorithmGossip/images/threeColorsFlags-2.jpg" style="width: 502px; height: 236px; " /></div><br /><small>濡傛灉鍙互浣跨敤闆欏悜閺堢祼鐨勮┍錛屽墖B鍙互鍐嶇渷鍘伙紝鍦╓閬炲鏅傦紝鑻ラ亣鍒拌棈鑹詫紝鍓囧皣钘嶈壊縐昏嚦閺堢祼鍓嶇涓擶+1錛岃嫢閬囧埌鐧借壊鍓嘩+1錛岃嫢閬囧埌绱呰壊錛屽墖灝囩磪鑹茬Щ鑷抽張緄愬熬绔笖R-1銆?br /><br /><div style="background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">import</span> java.io.BufferedReader;<br /><span style="color: #0000FF; ">import</span> java.io.IOException;<br /><span style="color: #0000FF; ">import</span> java.io.InputStreamReader;<br /><br /><span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">class</span> ThreeColorFlags {<br /><br />    <span style="color: #0000FF; ">private</span> <span style="color: #0000FF; ">void</span> swap(<span style="color: #0000FF; ">char</span>[] flags, <span style="color: #0000FF; ">int</span> x, <span style="color: #0000FF; ">int</span> y) {<br />        <span style="color: #0000FF; ">char</span> temp;<br />        temp = flags[x];<br />        flags[x] = flags[y];<br />        flags[y] = temp;<br />    }<br /><br />    <span style="color: #0000FF; ">public</span> String move(<span style="color: #0000FF; ">char</span>[] flags) {<br /><br />        <span style="color: #0000FF; ">int</span> bFlag = 0;<br />        <span style="color: #0000FF; ">int</span> wFlag = 0;<br />        <span style="color: #0000FF; ">int</span> rFlag = flags.length - 1;<br /><br />        <span style="color: #0000FF; ">while</span> (wFlag <= rFlag) {<br />            <span style="color: #0000FF; ">if</span> (flags[wFlag] == 'W') {<br />                wFlag++;<br />            } <span style="color: #0000FF; ">else</span> <span style="color: #0000FF; ">if</span> (flags[wFlag] == 'B') {<br />                swap(flags, wFlag, bFlag);<br />                wFlag++;<br />                bFlag++;<br />            }<span style="color: #0000FF; ">else</span>{<br />                <span style="color: #0000FF; ">while</span>(wFlag < rFlag && flags[rFlag] == 'R'){<br />                    rFlag--;<br />                }<br />                swap(flags, wFlag, rFlag);<br />                rFlag--;<br />            }<br />        }<br /><br />        <span style="color: #0000FF; ">return</span> <span style="color: #0000FF; ">new</span> String(flags);<br />    }<br />    <br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">void</span> main(String[] args) <span style="color: #0000FF; ">throws</span> IOException{<br />        BufferedReader buf;<br />        buf = <span style="color: #0000FF; ">new</span> BufferedReader(<span style="color: #0000FF; ">new</span> InputStreamReader(System.in));<br />        <br />        System.out.println("Type the sequence of three colors(BBWRWEBBWWRRRRWB): ");<br />        String flags = buf.readLine();<br />        <br />        ThreeColorFlags threeColorFlags = <span style="color: #0000FF; ">new</span> ThreeColorFlags();<br />        flags = threeColorFlags.move(flags.toUpperCase().toCharArray());<br />        <br />        System.out.println("After move: " + flags);<br />    }<br />}</div></small></td></tr></tbody></table><img src ="http://www.tkk7.com/czihong/aggbug/386869.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/czihong/" target="_blank">Chan Chen</a> 2012-09-03 14:47 <a href="http://www.tkk7.com/czihong/articles/386869.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Hanoi Tower 姹夎濉旂殑綆鍗曞垎鏋?Javahttp://www.tkk7.com/czihong/articles/386673.htmlChan ChenChan ChenFri, 31 Aug 2012 03:31:00 GMThttp://www.tkk7.com/czihong/articles/386673.htmlhttp://www.tkk7.com/czihong/comments/386673.htmlhttp://www.tkk7.com/czihong/articles/386673.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/386673.htmlhttp://www.tkk7.com/czihong/services/trackbacks/386673.html銆褰撶劧銆佽繖鏄竴涓粡鍏哥殑閫掑綊闂~
   銆鎯沖繀鏉ョ湅榪欑瘒鍗氭枃鐨勫悓瀛﹀姹夎濉斿簲璇ヤ笉浼?xì)闄岀敓浜?jiǎn)鍚э紝

銆銆鍐欒繖綃囧崥榪樻槸鏈夊垵琛風(fēng)殑錛?/p>

銆銆涔嬪墠瀛︽暟鎹粨鏋勭殑鏃跺欒嚜宸辯湅涔︺佷篃涓婄綉涓婃煡浜?jiǎn)寰堝璧勬枡锛岃祫鏂欓兘姣旇緝鏁c佽屼笖鎻忚堪鐨勪笉鏄緢娓呮錛屽浜庡綋鏃跺垰鍒?/p>

鎺ヨЕ綆楁硶鐨勬垜錛岃瀹屽叏鐞嗚В榪樻槸鏈変竴瀹氶毦搴︺備粖澶╁垰濂芥湁鏃墮棿灝辨暣鐞嗕簡(jiǎn)涓嬫濊礬銆侀噸鍐欏垎鏋愪簡(jiǎn)涓涓嬩箣鍓嶇殑鐤戞儜鐨勫湴鏂廣?/p>

娌℃湁閫忓交鐨勫湴鏂逛究閮借眮鐒跺紑鏈椾簡(jiǎn)銆傛墍浠ヨ揩涓嶅強(qiáng)寰呮妸鎴戠殑鎯蟲(chóng)硶璁板綍涓嬫潵錛屽拰澶у鍒嗕韓銆?/p>

銆銆濡傛灉浣犱篃鏄拰涔嬪墠鐨勬垜涓鏍峰hanoi tower娌¤兘瀹屽叏娑堝寲錛屾垨鑰呭垰鍒氭帴瑙︽眽璇哄錛岄偅甯屾湜鎴戠殑榪欑鐞嗚В鏂瑰紡鑳界粰浣犱簺

璁稿府鍔╋紝濡傛灉浣犺寰楀凡緇忓畬鍏ㄦ帉鎻$殑姣旇緝鐗㈤潬浜?jiǎn)锛岄偅涔熷彲浠ョ湅鐪嬪Q屾湁濂界殑idea鍙互涓璧峰垎浜紱姣曠珶浜ゆ祦璁ㄨ涔熸槸涓縐嶅緢濂界殑

瀛︿範(fàn)鏂瑰紡銆?/p>

銆銆濂戒簡(jiǎn)錛屽簾璇濅笉澶氳錛屽垏鍏ユ棰樸?/p>

鍏充簬姹夎濉旇搗婧愬晩銆佷紶璇村晩紲為┈鐨勫氨涓嶅暟鍡︿簡(jiǎn)錛屾垜浠洿鎺ュ垏鍏ユ棰橈細(xì)
闂鎻忚堪錛?/p>

銆銆鏈変竴涓⒌濉旓紝濉斿唴鏈変笁涓駭A銆丅銆丆錛孉搴т笂鏈夎騫蹭釜鐩樺瓙錛岀洏瀛愬ぇ灝忎笉絳夛紝澶х殑鍦ㄤ笅錛屽皬鐨勫湪涓婏紙濡傚浘錛夈?/p>

鎶婅繖浜涗釜鐩樺瓙浠嶢搴хЩ鍒癈搴э紝涓棿鍙互鍊熺敤B搴т絾姣忔鍙兘鍏佽縐誨姩涓涓洏瀛愶紝騫朵笖鍦ㄧЩ鍔ㄨ繃紼嬩腑錛?涓駭涓婄殑鐩?/p>

瀛愬緇堜繚鎸佸ぇ鐩樺湪涓嬶紝灝忕洏鍦ㄤ笂銆?/p>

鎻忚堪綆鍖栵細(xì)鎶夾鏌變笂鐨刵涓洏瀛愮Щ鍔ㄥ埌C鏌憋紝鍏朵腑鍙互鍊熺敤B鏌便?/p>

銆銆

銆銆鎴戜滑鐩存帴鍋囪鏈塶涓洏瀛愶細(xì)

銆銆鍏堟妸鐩樺瓙浠庡皬鍒板ぇ鏍囪涓?銆?銆?......n

銆銆鍏堢湅鍘熼棶棰樹(shù)笁涓煴瀛愮殑鐘舵侊細(xì)
鐘舵?銆銆A錛氭寜欏哄簭鍫嗘斁鐨刵涓洏瀛愩侭:絀虹殑銆侰錛氱┖鐨勩?/span>

銆銆鐩爣鏄鎶夾涓婄殑n涓洏瀛愮Щ鍔ㄥ埌C銆傚洜涓哄繀欏誨ぇ鐨勫湪涓嬪皬鐨勫湪涓婏紝鎵浠ユ渶緇堢粨鏋淐鐩樹(shù)笂鏈涓嬮潰鐨勫簲璇ユ槸鏍囧彿涓簄鐨勭洏瀛愶紝璇曟兂錛?/p>

瑕佸彇寰桝涓婄殑絎琻涓洏瀛愶紝灝辮鎶婂畠涓婇潰鐨刵-1涓洏瀛愭嬁寮鍚э紵鎷垮紑鏀懼湪鍝噷鍛紵鍏辨湁涓変釜鏌卞瓙錛欰鏄劇劧涓嶆槸銆佸鏋滄斁鍦–涓?/p>

浜?jiǎn)锛岄偅涔堟渶澶х殑鐩樺瓙灝辨病鍦版柟鏀撅紝闂榪樻槸娌″緱鍒拌В鍐熾傛墍浠ラ夋嫨B鏌便傚綋鐒?dòng)灱孊涓婇潰涔熸槸鎸夌収澶у湪涓嬪皬鍦ㄤ笂鐨勫師鍒欏爢鏀劇殑

錛堣浣忥細(xì)鍏堜笉瑕佺鍏蜂綋濡備綍縐誨姩錛屽彲浠ョ湅鎴愮敤涓涓嚱鏁板畬鎴愮Щ鍔紝鐜板湪涓嶇敤鍘昏冭檻鍑芥暟濡備綍瀹炵幇銆傝繖鐐瑰緢閲嶈錛夈?/strong>

銆銆寰堟槑鏄撅細(xì)涓婁竴姝ュ畬鎴愬悗涓変釜濉旂殑鐘舵侊細(xì)

鐘舵?錛?nbsp;銆銆A錛氬彧鏈夋渶澶х殑涓涓洏瀛愩侭錛氭湁鎸夎鍒欏爢鏀劇殑n-1涓洏瀛愩侰絀虹殑銆?/span>

銆銆涓婇潰鐨勫緢濂界悊瑙e惂錛屽ソ錛屽叾瀹炲埌榪欓噷灝卞凡緇忓畬鎴愪竴鍗婁簡(jiǎn)銆傦紙濡傛灉鍓嶉潰鐨勬病鎳傦紝璇烽噸鐪嬩竴閬嶃俻oint錛氫笉瑕佺濡備綍縐誨姩錛侊級(jí)

鎴戜滑緇х畫(huà)錛?/p>

銆銆榪欐椂鍊欙紝鍙互鐩存帴鎶夾涓婄殑鏈澶х洏縐誨姩鍒癈鐩橈紝縐誨姩鍚庣殑鐘舵侊細(xì)

涓棿鐘舵侊細(xì)銆銆A錛氱┖鐨勩侭錛歯-1涓洏瀛愩侰錛氭湁涓涓渶澶х洏錛堢n涓洏瀛愶級(jí)

銆銆瑕佹敞鎰忕殑涓鐐規(guī)槸錛氳繖鏃跺欑殑C鏌卞叾瀹炲彲浠ョ湅鍋氭槸絀虹殑銆傚洜涓哄墿涓嬬殑鎵鏈夌洏瀛愰兘姣斿畠?yōu)畯锛屽畠浠腑鐨勪恢M綍涓涓兘鍙互鏀懼湪涓婇潰錛屼篃灝辨槸銆銆銆銆銆銆    銆銆銆銆銆銆銆銆  C鏌變笂銆?/p>

銆銆鎵浠ョ幇鍦ㄤ笁涓煴瀛愮殑鐘舵侊細(xì)

涓棿鐘舵侊細(xì)銆銆A錛氱┖鐨勩侭錛歯-1涓洏瀛愩侰錛氱┖鐨?/span>

銆銆鎯充竴鎯籌紝鐜板湪鐨勯棶棰樺拰鍘熼棶棰樻湁浜涚浉浼間箣澶勪簡(jiǎn)鍚э紵銆傘傚浣曟洿鐩鎬技鍛紵銆傛樉鐒?dòng)灱屽彧瑕佸惂B涓婄殑n-1涓洏瀛愮Щ鍔ㄥ埌A錛屽緟瑙e喅鐨勯棶棰樺拰鍘熼棶棰樺氨鐩告瘮灝卞彧鏄妯″彉灝忎簡(jiǎn)

銆銆鐜板湪鑰冭檻濡備綍鎶夿涓婄殑n-1涓洏瀛愮Щ鍔ㄥ埌A涓婏紝鍏跺疄縐誨姩鏂規(guī)硶鍜屼笂鏂囦腑鐨勬妸n-1涓洏?shù)粠A縐誨姩鍒癇鏄竴鏍風(fēng)殑錛屽彧鏄煴瀛愮殑鍚嶇О鎹簡(jiǎn)涓嬭屽凡銆傘傦紙濡傛灉鍐欐垚鍑芥暟錛屽彧鏄弬鏁拌皟鐢ㄩ『搴忔敼鍙樿屽凡錛夈傘

銆銆鍋囪浣犲凡緇忓畬鎴愪笂涓姝ヤ簡(jiǎn)錛堝悓鏍風(fēng)殑錛屼笉瑕佽冭檻濡備綍鍘葷Щ鍔紝鍙鎯崇潃鐢ㄤ竴涓嚱鏁板疄鐜板氨濂斤級(jí)錛岃鐪嬬幇鍦ㄧ殑鐘舵侊細(xì)

鐘舵?錛氥A錛氭湁鎸夐『搴忓爢鏀劇殑n-1涓洏瀛愩侭錛氱┖鐨勩侰錛氭寜欏哄簭鍫嗘斁鐨勭n鐩樺瓙(鍙湅涓虹┖鏌?

灝卞湪鍒氭墠錛屾垜浠畬緹庣殑瀹屾垚浜?jiǎn)涓嬈¢掑綊銆傚鏋滄病鐪嬫噦璇蜂粠鏂扮湅涓閬嶏紝鍙互鐢ㄧ瑪鐢誨嚭涓変釜鐘舵併侀潤(rùn)涓嬪績(jī)鏉ユ參鎱㈡帹鐞嗐?/p>

鎴戜竴鍐嶅己璋冪殑錛氬綋瑕佹妸鏈澶х洏瀛愪笂闈㈢殑鎵鏈夌洏瀛愮Щ鍔ㄥ埌鍙︿竴涓┖鏌變笂鏃訛紝涓嶈鍏沖績(jī)鍏蜂綋濡備綍縐誨姩錛屽彧鐢ㄦ妸瀹冪湅鍋氫竴涓嚱鏁板彲浠ュ畬鎴愬嵆鍙紝涓嶇敤鍏沖績(jī)鍑芥暟鐨勫叿浣撳疄鐜般傚鏋滀綘鐨勬濊礬綰犵粨鍦ㄨ繖閲岋紝灝卞緢闅劇戶(hù)緇繁鍏ヤ簡(jiǎn)銆?/em>

鍒拌繖閲岋紝鍏跺疄 鍩烘湰鎬濊礬宸茬粡鐞嗘竻浜?jiǎn)銆傜姸鎬?鍜岀姸鎬?錛岄櫎浜?jiǎn)瑙勬ā鍙槥?錛屽叾瀹冩柟闈㈡病鏈変換浣曞尯鍒簡(jiǎn)銆傜劧鍚庡彧瑕佺敤鐩稿悓鐨勬濈淮鏂瑰紡錛屽氨鑳藉線(xiàn)涓嬫繁鍏ャ傘傘?/p>

 

濂戒簡(jiǎn)錛岀湅鐪嬪浣曠敤綆楁硶瀹炵幇鍚э細(xì)

瀹氫箟鍑芥暟Hanoi錛坅,b,c,n錛夎〃紺烘妸a涓婄殑n涓洏瀛愮Щ鍔ㄥ埌c涓婏紝鍏朵腑鍙互鐢ㄥ埌b銆?/p>

瀹氫箟鍑芥暟move(m,n)琛ㄧず鎶妋涓婄殑鐩樺瓙縐誨姩鍒皀涓?/p>

鎴戜滑闇瑕佽В鍐崇殑闂姝f槸銆銆Hanoi (a,b,c,n)   銆銆//涓婃枃涓殑鐘舵?

 

1銆佹妸A涓婄殑n-1涓Щ鍔ㄥ埌B錛?nbsp; 銆銆Hanoi (a,c,b,n-1);       // 鎿嶄綔緇撴潫涓虹姸鎬?

2銆佹妸A涓婄殑澶х洏瀛愮Щ鍔ㄥ埌C         move(a,c)銆銆銆銆

3銆佹妸B涓婄殑n-1縐誨姩鍒癆銆銆銆銆銆Hanoi (b,c,a,n-1);銆銆//鎿嶄綔緇撴潫浣嶇姸鎬?(鍜岀姸鎬?鐩告瘮鍙槸瑙勬ā鍙樺皬)

 

濡傛灉鐜板湪榪樹(shù)笉鑳界悊瑙c佽鍥炶繃澶村啀鐪嬩竴閬嶃佹瘯绔熷鏋滄槸鍒濆鑰呬笉鏄緢瀹規(guī)槗灝辮兘鐞嗚В鐨勩傚彲浠ョ敤絎旇涓嬪嚑涓叧閿殑鐘舵侊紝騫朵笖鐪嬬湅浣犳湁娌℃湁鐪熸鐨勬姇鍏ュ幓鐪嬶紝鐙珛鍘繪濊冧簡(jiǎn)銆?/span>

 


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Hanoi {

    
public static void main(String[] args) throws IOException{
        
int n;
        BufferedReader buf;
        buf 
= new BufferedReader(new InputStreamReader(System.in));
        
        System.out.println(
"Please input the number of disk ");
        n 
= Integer.parseInt(buf.readLine());
        
        Hanoi hanoi 
= new Hanoi();
        hanoi.move(n,
'A','B','C');
    }
    
    
public void move(int n, char a, char b, char c){
        
if(n == 1){
            System.out.println(
"Disk " + n + " From " + a + " To " + c);
        }
        
else{
            move(n
-1,a,c,b);
            System.out.println(
"Disk " + n + " From " + a + " To " + c);
            move(n
-1,b,a,c);
        }
    }
}

 

浠ヤ笂銆佸鏋滄湁涓嶅鐨勫湴鏂廣佽繕甯屾湜鎮(zhèn)ㄨ兘鎸囧嚭銆?/p>

鎴戝閫掑綊鐨勪竴鐐圭悊瑙o細(xì)

瑙e喅瀹為檯闂鏃躲佷笉鑳藉お鍘誨叧蹇?jī)瀹炵幇鐨劸l嗚妭錛堝洜涓洪掑綊鐨勮繃紼嬫伆鎭版槸鎴戜滑瀹炵幇鐨勬柟娉曪級(jí)灝卞儚榪欎釜闂錛屽鍦ㄧ涓姝ュ氨榪囧鐨勭籂緇撲簬濡備綍鎶妌-1涓洏瀛愮Щ鍔ㄥ埌B涓娿侀偅涔堜綘鐨勬濊礬灝卞緢闅劇戶(hù)緇繁鍏ャ傚彧瑕佺湅鍋氭槸鐢ㄥ嚱鏁板疄鐜板氨濂斤紝濡傛灉浣犺兘鐪嬪嚭涓嶇鎬庝箞縐誨姩錛屽叾瀹炴湰璐ㄩ兘涓鏍風(fēng)殑鏃跺欙紝閭d箞灝辮兘杈冨揩鐨勫緱鍒扮粨鏋滀簡(jiǎn)銆傚氨鍍忚繖涓渚嬶紝瑕佹敞鎰忓埌鎴戜滑鍋氱殑鍏抽敭鍑犳閮藉彧鏄Щ鍔ㄧ殑欏哄簭鏈夋敼鍙橈紝鍏朵腑鐨勮鍒欐病鏈夋敼鍙橈紝濡?/p>

濡傛灉鐢ㄥ嚱鏁拌〃紺虹殑璇濓紝灝卞彧鏄弬鏁拌皟鐢ㄧ殑欏哄簭鏈夋墍涓嶅悓浜?jiǎn)銆傚湪閫掑綊鐨勮繍鐢ㄤ腑銆佷笉鐢ㄥ叧蹇?jī)姣忎竴姝ョ殑鍏蜂綋瀹炵幇 錛屽彧瑕佺湅鍋氱敤涓涓嚱鏁拌〃紺哄氨濂姐傚垎鏋愰棶棰樼殑鏃跺欙紝鏈濂界敾鍑?guó)檱忿q殑鎺ㄧ悊榪囩▼錛屽緱鍒版湁鏁堢殑鐘舵佸浘銆?/p>

鎬濊冮棶棰樿姹傛濊礬鐨勮繛璐с佸姏姹傚敖蹇繘鍏ョ姸鎬侊紝浜彈瀹屽叏鎶曞叆鍒頒竴浠朵簨涓殑緹庡鎰熻



Chan Chen 2012-08-31 11:31 鍙戣〃璇勮
]]>
Dijkstra's algorithmhttp://www.tkk7.com/czihong/articles/382323.htmlChan ChenChan ChenThu, 05 Jul 2012 22:33:00 GMThttp://www.tkk7.com/czihong/articles/382323.htmlhttp://www.tkk7.com/czihong/comments/382323.htmlhttp://www.tkk7.com/czihong/articles/382323.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/382323.htmlhttp://www.tkk7.com/czihong/services/trackbacks/382323.html

http://renaud.waldura.com/doc/java/dijkstra/ 

Dijkstra's algorithm is probably the best-known and thus most implemented shortest path algorithm. It is simple, easy to understand and implement, yet impressively efficient. By getting familiar with such a sharp tool, a developer can solve efficiently and elegantly problems that would be considered impossibly hard otherwise. Be my guest as I explore a possible implementation of Dijkstra's shortest path algorithm in Java.

What It Does

Dijkstra's algorithm, when applied to a graph, quickly finds the shortest path from a chosen source to a given destination. (The question "how quickly" is answered later in this article.) In fact, the algorithm is so powerful that it finds all shortest paths from the source to all destinations! This is known as the single-source shortest paths problem. In the process of finding all shortest paths to all destinations, Dijkstra's algorithm will also compute, as a side-effect if you will, a spanning tree for the graph. While an interesting result in itself, the spanning tree for a graph can be found using lighter (more efficient) methods than Dijkstra's.

How It Works

First let's start by defining the entities we use. The graph is made of vertices (or nodes, I'll use both words interchangeably), and edges which link vertices together. Edges are directed and have an associated distance, sometimes called the weight or the cost. The distance between the vertex u and the vertex v is noted [uv] and is always positive.

Dijkstra's algorithm partitions vertices in two distinct sets, the set of unsettled vertices and the set of settled vertices. Initially all vertices are unsettled, and the algorithm ends once all vertices are in the settled set. A vertex is considered settled, and moved from the unsettled set to the settled set, once its shortest distance from the source has been found.

We all know that algorithm + data structures = programs, in the famous words of Niklaus Wirth. The following data structures are used for this algorithm:

dstores the best estimate of the shortest distance from the source to each vertex
πstores the predecessor of each vertex on the shortest path from the source
Sthe set of settled vertices, the vertices whose shortest distances from the source have been found
Qthe set of unsettled vertices

With those definitions in place, a high-level description of the algorithm is deceptively simple. With s as the source vertex:

// initialize d to infinity, π and Q to empty d = ( ∞ ) π = () S = Q = ()  add s to Q d(s) = 0  
while Q is not empty {
      u = extract-minimum(Q)
      add u to S
      relax-neighbors(u)
 } 

Dead simple isn't it? The two procedures called from the main loop are defined below:

relax-neighbors(u) {
      for each vertex v adjacent to u, v not in S
      {
           if d(v) > d(u) + [u,v]
    // a shorter distance exists
           {
                d(v) = d(u) + [u,v]
                π(v) = u
                add v to Q
           }
      }
 }

  extract-minimum(Q) {
     find the smallest (as defined by d) vertex in Q
     remove it from Q and return it
 } 


An Example

So far I've listed the instructions that make up the algorithm. But to really understand it, let's follow the algorithm on an example. We shall run Dikjstra's shortest path algorithm on the following graph, starting at the source vertex a.

We start off by adding our source vertex a to the set QQ isn't empty, we extract its minimum, a again. We add a to S, then relax its neighbors. (I recommend you follow the algorithm in parallel with this explanation.)

Those neighbors, vertices adjacent to a, are b and c (in green above). We first compute the best distance estimate from a to bd(b) was initialized to infinity, therefore we do:

d(b) = d(a) + [a,b] = 0 + 4 = 4
π(b) is set to a, and we add b to Q. Similarily for c, we assign d(c) to 2, and π(c) to a. Nothing tremendously exciting so far.

The second time around, Q contains b and c. As seen above, c is the vertex with the current shortest distance of 2. It is extracted from the queue and added to S, the set of settled nodes. We then relax the neighbors of c, which are bd and a.

a is ignored because it is found in the settled set. But it gets interesting: the first pass of the algorithm had concluded that the shortest path from a to b was direct. Looking at c's neighbor b, we realize that:

d(b) = 4 > d(c) + [c,b] = 2 + 1 = 3
Ah-ah! We have found that a shorter path going through c exists between a and bd(b) is updated to 3, and π(b) updated to cb is added again to Q. The next adjacent vertex is d, which we haven't seen yet. d(d) is set to 7 and π(d) to c.

The unsettled vertex with the shortest distance is extracted from the queue, it is now b. We add it to the settled set and relax its neighbors c and d.

We skip c, it has already been settled. But a shorter path is found for d:

d(d) = 7 > d(b) + [b,d] = 3 + 1 = 4
Therefore we update d(d) to 4 and π(d) to b. We add d to the Q set.

At this point the only vertex left in the unsettled set is d, and all its neighbors are settled. The algorithm ends. The final results are displayed in red below:

  • π - the shortest path, in predecessor fashion
  • d - the shortest distance from the source for each vertex

This completes our description of Dijkstra's shortest path algorithm. Other shortest path algorithms exist (see the References section at the end of this article), but Dijkstra's is one of the simplest, while still offering good performance in most cases.

Implementing It in Java

The Java implementation is quite close to the high-level description we just walked through. For the purpose of this article, my Java implementation of Dijkstra's shortest path finds shortest routes between cities on a map. The RoutesMap object defines a weighted, oriented graph as defined in the introduction section of this article.

public interface RoutesMap {
     int getDistance(City start, City end);
     List<City> getDestinations(City city);
 } 

 

Data Structures

We've listed above the data structures used by the algorithm, let's now decide how we are going to implement them in Java.

S, the settled nodes set

This one is quite straightforward. The Java Collections feature the Set interface, and more precisely, the HashSet implementation which offers constant-time performance on the contains operation, the only one we need. This defines our first data structure.

private final Set<City> settledNodes = new HashSet<City>();
  private boolean isSettled(City v) {
     return settledNodes.contains(v);
 } 

 

Notice how my data structure is declared as an abstract type (Set) instead of a concrete type (HashSet). Doing so is a good software engineering practice, as it allows to change the actual type of the collection without any modification to the code that uses it.

d, the shortest distances list

As we've seen, one output of Dijkstra's algorithm is a list of shortest distances from the source node to all the other nodes in the graph. A straightforward way to implement this in Java is with a Map, used to keep the shortest distance value for every node. We also define two accessors for readability, and to encapsulate the default infinite distance.

private final Map<City, Integer> shortestDistances = new HashMap<City, Integer>();
  private void setShortestDistance(City city, int distance) {
     shortestDistances.put(city, distance);
 }

public int getShortestDistance(City city) {
     Integer d = shortestDistances.get(city);
     return (d == null) ? INFINITE_DISTANCE : d;

 

You may notice I declare this field final. This is a Java idiom used to flag aggregation relationships between objects. By marking a field final, I am able to convey that it is part of a aggregation relationship, enforced by the properties of final—the encapsulating class cannot exist without this field.

π, the predecessors tree

Another output of the algorithm is the predecessors tree, a tree spanning the graph which yields the actual shortest paths. Because this is the predecessors tree, the shortest paths are actually stored in reverse order, from destination to source. Reversing a given path is easy with Collections.reverse().

The predecessors tree stores a relationship between two nodes, namely a given node's predecessor in the spanning tree. Since this relationship is one-to-one, it is akin to amapping between nodes. Therefore it can be implemented with, again, a Map. We also define a pair of accessors for readability.

private final Map<City, City> predecessors = new HashMap<City, City>();
private void setPredecessor(City a, City b) {
     predecessors.put(a, b);
 }

public City getPredecessor(City city) {
     return predecessors.get(city);

 

Again I declare my data structure to be of the abstract type Map, instead of the concrete type HashMap. And tag it final as well.

Q, the unsettled nodes set

As seen in the previous section, a data structure central to Dijkstra's algorithm is the set of unsettled vertices Q. In Java programming terms, we need a structure able to store the nodes of our example graph, i.e. City objects. That structure is then looked up for the city with the current shortest distance given by d().

We could do this by using another Set of cities, and sort it according to d() to find the city with shortest distance every time we perform this operation. This isn't complicated, and we could leverage Collections.min() using a custom Comparator to compare the elements according to d().

But because we do this at every iteration, a smarter way would be to keep the set ordered at all times. That way all we need to do to get the city with the lowest distance is to get the first element in the set. New elements would need to be inserted in the right place, so that the set is always kept ordered.

A quick search through the Java collections API yields the PriorityQueue: it can sort elements according to a custom comparator, and provides constant-time access to the smallest element. This is precisely what we need, and we'll write a comparator to order cities (the set elements) according to the current shortest distance. Such a comparator is included below, along with the PriorityQueue definition. Also listed is the small method that extracts the node with the shortest distance.

private final Comparator<City> shortestDistanceComparator = new Comparator<City>()
     {
         public int compare(City left, City right)
         {
             int shortestDistanceLeft = getShortestDistance(left);
             int shortestDistanceRight = getShortestDistance(right);
              if (shortestDistanceLeft > shortestDistanceRight)
             {
                 return +1;
             }
             else if (shortestDistanceLeft < shortestDistanceRight)
             {
                 return -1;
             }
             else // equal
             {
                 return left.compareTo(right);
             }
         }
     };
  private final PriorityQueue<City> unsettledNodes = new PriorityQueue<City>(INITIAL_CAPACITY, shortestDistanceComparator);
  private City extractMin()
 {
     return unsettledNodes.poll();
 } 

One important note about the comparator: it is used by the PriorityQueue to determine both object ordering and identity. If the comparator returns that two elements are equal, the queue infers they are the same, and it stores only one instance of the element. To prevent losing nodes with equal shortest distances, we must compare the elements themselves (third block in the if statement above).

Having powerful, flexible data structures at our disposal is what makes Java such an enjoyable language (that, and garbage collection of course).

Putting It All Together

We have defined our data structures, we understand the algorithm, all that remains to do is implement it. As I mentioned earlier, my implementation is close to the high-level description given above. Note that when the only shortest path between two specific nodes is asked, the algorithm can be interrupted as soon as the destination node is reached.

public void execute(City start, City destination) {
     initDijkstra(start);
      while (!unsettledNodes.isEmpty())     {
         // get the node with the shortest distance
         City u = extractMin();
          // destination reached, stop
         if (u == destination) break;
          settledNodes.add(u);
          relaxNeighbors(u);
     }
 } 

 

The DijkstraEngine class implements this algorithm and brings it all together. See "Implementation Notes" below to download the source code.

A Word About Performance

The complexity of Dijkstra's algorithm depends heavily on the complexity of the priority queue Q. If this queue is implemented naively as I first introduced it (i.e. it is re-ordered at every iteration to find the mininum node), the algorithm performs in O(n2), where n is the number of nodes in the graph.

With a real priority queue kept ordered at all times, as we implemented it, the complexity averages O(n log m). The logarithm function stems from the collectionsPriorityQueue class, a heap implementation which performs in log(m).

Implementation Notes

The Java source code discussed in this article is available for download as a ZIP file. Extensive unit tests are provided and validate the correctness of the implementation. Some minimal Javadoc is also provided. As the code makes use of the assert facility and generics, it must be compiled with "javac -source 1.5"; the tests require junit.jar.I warmly recommend Eclipse for all Java development.

I've received a fair amount of e-mail about this article, which has become quite popular. I'm unfortunately unable to answer all your questions, and for this I apologize. Keep in mind this article (and the code) is meant as a starting point: the implementation discussed here is hopefully simple, correct, and relatively easy to understand, but is probably not suited to your specific problem. You must tailor it to your own domain.

My goal in writing this article was to share and teach a useful tool, striving for 1- simplicity and 2- correctness. I purposefully shied away from turning this exercise into a full-blown generic Java implementation. Readers after full-featured, industrial-strength Java implementations of Dijkstra's shortest path algorithm should look at the "Resources" section below.



Chan Chen 2012-07-06 06:33 鍙戣〃璇勮
]]>
Bag Queue and Stackhttp://www.tkk7.com/czihong/articles/382321.htmlChan ChenChan ChenThu, 05 Jul 2012 21:28:00 GMThttp://www.tkk7.com/czihong/articles/382321.htmlhttp://www.tkk7.com/czihong/comments/382321.htmlhttp://www.tkk7.com/czihong/articles/382321.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/382321.htmlhttp://www.tkk7.com/czihong/services/trackbacks/382321.htmlSeveral fundamental data types involve collections of objects. Specifically, the set of values is a collection of objects, and the operations revolve around adding, removing, or examining objects in the collection. In this section, we consider three such data types, known as the bag, the queue, and the stack. They differ in the specification of which object is to be removed or examined next.

APIs.

 We define the APIs for bags, queues, and stacks. Beyond the basics, these APIs reflect two Java features: generics and iterable collections.APIs for bag, queue, and stack

  • Generics. An essential characteristic of collection ADTs is that we should be able to use them for any type of data. A specific Java mechanism known as generics enables this capability. The notation <Item> after the class name in each of our APIs defines the name Item as a type parameter, a symbolic placeholder for some concrete type to be used by the client. You can read Stack<Item> as "stack of items." For example, you can write code such as
    Stack<String> stack = new Stack<String>(); 
    stack.push("Test");
    ...
    String next = stack.pop();
    to use a stack for String objects.

  • Autoboxing. Type parameters have to be instantiated as reference types, so Java automatically converts between a primitive type and its corresponding wrapper type in assignments, method arguments, and arithmetic/logic expressions. This conversion enables us to use generics with primitive types, as in the following code:
    Stack<Integer> stack = new Stack<Integer>(); stack.push(17);
    // auto-boxing (int -> Integer) int i = stack.pop();
    // auto-unboxing (Integer -> int)
    Automatically casting a primitive type to a wrapper type is known as autoboxing, and automatically casting a wrapper type to a primitive type is known as auto-unboxing.

  • Iterable collections. For many applications, the client's requirement is just to process each of the items in some way, or to iterate through the items in the collection. Java's foreach statement supports this paradigm. For example, suppose that collection is a Queue<Transaction>. Then, if the collection is iterable, the client can print a transaction list with a single statement:
    for (Transaction t : collection)
    StdOut.println(t);

  • Bags. A bag is a collection where removing items is not supported—its purpose is to provide clients with the ability to collect items and then to iterate through the collected items. Stats.java is a bag client that reads a sequence of real numbers from standard input and prints out their mean and standard deviation.

  • FIFO queues. A FIFO queue is a collection that is based on the first-in-first-out (FIFO) policy. The policy of doing tasks in the same order that they arrive server is one that we encounter frequently in everyday life: from people waiting in line at a theater, to cars waiting in line at a toll booth, to tasks waiting to be serviced by an application on your computer.

  • Pushdown stack. A pushdown stack is a collection that is based on the last-in-first-out (LIFO) policy. When you click a hyperlink, your browser displays the new page (and pushes onto a stack). You can keep clicking on hyperlinks to visit new pages, but you can always revisit the previous page by clicking the back button (popping it from the stack). Reverse.java is a stack client that reads a sequence of integers from standard input and prints them in reverse order.

  • Arithmetic expression evaluation. Evaluate.java is a stack client that evaluates fully parenthesized arithmetic expressions. It uses Dijkstra's 2-stack algorithm:
    • Push operands onto the operand stack.
    • Push operators onto the operator stack.
    • Ignore left parentheses.
    • On encountering a right parenthesis, pop an operator, pop the requisite number of operands, and push onto the operand stack the result of applying that operator to those operands.

    This code is a simple example of an interpreter.

Array and resizing array implementations of collections.

  • Fixed-capacity stack of strings. FixedCapacityStackOfString.java implements a fixed-capacity stack of strings using an array.

  • Fixed-capacity generic stack. FixedCapacityStack.java implements a generic fixed-capacity stack.

  • Array resizing stack. ResizingArrayStack.java implements a generic stack using a resizing array. With a resizing array, we dynamically adjust the size of the array so that it is both sufficiently large to hold all of the items and not so large as to waste an excessive amount of space. Wedouble the size of the array in push() if it is full; we halve the size of the array in pop() if it is less than one-quarter full.

  • Array resizing queue. ResizingArrayQueue.java implements the queue API with a resizing array.

Linked lists.

 A linked list is a recursive data structure that is either empty (null) or a reference to a node having a generic item and a reference to a linked list. To implement a linked list, we start with a nested class that defines the node abstraction
private class Node {
Item item;
Node next;
}

  • Building a linked list. To build a linked list that contains the items tobe, and or, we create a Node for each item, set the item field in each of thbuilding a linked list

  • Insert at the beginning. The easiest place to insert a new node in a linked list is at the beginning.inserting a new node at the beginning of a linked list

  • Remove from the beginning. Removing the first node in a linked list is also easy.
    removing the first node in a linked list

  • Insert at the end. To insert a node at the end of a linked list, we maintain a link to the last node in the list.inserting a node at the end of a linked list

  • Traversal. The following is the idiom for traversing the nodes in a linked list.
    for (Node x = first; x != null; x = x.next) {
    // process x.item
    }

Linked-list implementations of collections.

  • Linked list implementation of a stack. Stack.java implements a generic stack using a linked list. It maintains the stack as a linked list, with the top of the stack at the beginning, referenced by an instance variable first. To push() an item, we add it to the beginning of the list; to pop()an item, we remove it from the beginning of the list.

  • Linked list implementation of a queue. Program Queue.java implements a generic FIFO queue using a linked list. It maintains the queue as a linked list in order from least recently to most recently added items, with the beginning of the queue referenced by an instance variable firstand the end of the queue referenced by an instance variable last. To enqueue() an item, we add it to the end of the list; to dequeue() an item, we remove it from the beginning of the list.

  • Linked list implementation of a bag. Program Bag.java implements a generic bag using a linked list. The implementation is the same as Stack.javaexcept for changing the name of push() to add() and removing pop().

Iteration.

 To consider the task of implementing iteration, we start with a snippet of client code that prints all of the items in a collection of strings, one per line:
Stack<String> collection = new Stack<String>(); 
...
for (String s : collection)
StdOut.println(s);
   ...
This foreach statement is shorthand for the following while statement:
Iterator<String> i = collection.iterator(); 
while (i.hasNext()) {
String s = i.next();
StdOut.println(s);
}
To implement iteration in a collection:

  • Include the following import statement so that our code can refer to Java's java.util.Iterator interface:
    import java.util.Iterator; 

  • Add the following to the class declaration, a promise to provide an iterator() method, as specified in the java.lang.Iterable interface:
    implements Iterable<Item> 

  • Implement a method iterator() that returns an object from a class that implements the Iterator interface:
    public Iterator<Item> iterator() {
    return new ListIterator();
    }

  • Implement a nested class that implements the Iterator interface by including the methods hasNext()next(), and remove(). We always use an empty method for the optional remove() method because interleaving iteration with operations that modify the data structure is best avoided.

    • The nested class ListIterator in Bag.java illustrates how to implement a class that implements the Iterator interface when the underlying data structure is a linked list.

    • The nested class ArrayIterator in ArrayResizingBag.java does the same when the underlying data structure is an array.


Autoboxing Q + A

Q. How does auto-boxing handle the following code fragment?

Integer a = null; int b = a; 

A. It results in a run-time error. Primitive type can store every value of their corresponding wrapper type except null.

Q. Why does the first group of statements print true, but the second false?

Integer a1 = 100;
Integer a2 = 100;
System.out.println(a1 == a2);
// true Integer b1 = new Integer(100);
Integer b2 = new Integer(100);
System.out.println(b1 == b2);
// false Integer c1 = 150;
Integer c2 = 150;
System.out.println(c1 == c2);
// false

A. The second prints false because b1 and b2 are references to different Integer objects. The first and third code fragments rely on autoboxing. Surprisingly the first prints true because values between -128 and 127 appear to refer to the same immutable Integer objects (Java's implementation of valueOf() retrieves a cached values if the integer is between -128 and 127), while Java constructs new objects for each integer outside this range.

Here is another Autoboxing.java anomaly.

Generics Q + A

Q. Are generics solely for auto-casting?

A. No, but we will use them only for "concrete parameterized types", where each data type is parameterized by a single type. The primary benefit is to discover type mismatch errors at compile-time instead of run-time. There are other more general (and more complicated) uses of generics, including wildcards. This generality is useful for handling subtypes and inheritance. For more information, see this Generics FAQ and this generics tutorial.

Q. Can concrete parameterized types be used in the same way as normal types?

A. Yes, with a few exceptions (array creation, exception handling, with instanceof, and in a class literal).

Q. Why do I get a "can't create an array of generics" error when I try to create an array of generics?

public class ResizingArrayStack<Item> {
Item[] a = new Item[1];
}

A. Unfortunately, creating arrays of generics is not possible in Java 1.5. The underlying cause is that arrays in Java are covariant, but generics are not. In other words, String[] is a subtype of Object[], but Stack<String> is not a subtype of Stack<Object>. To get around this defect, you need to perform an unchecked cast as in ResizingArrayStack.java.

Q. So, why are arrays covariant?

A. Many programmers (and programming language theorists) consider covariant arrays to be a serious defect in Java's type system: they incur unnecessary run-time performance overhead (for example, see ArrayStoreException) and can lead to subtle bugs. Covariant arrays were introduced in Java to circumvent the problem that Java didn't originally include generics in its design, e.g., to implement Arrays.sort(Comparable[]) and have it be callable with an input array of type String[].

Q. Can I create and return a new array of a parameterized type, e.g., to implement a toArray() method for a generic queue?

A. Not easily. You can do it using reflection provided that the client passes an object of the desired concrete type to toArray() This is the (awkward) approach taken by Java's Collection Framework.



Chan Chen 2012-07-06 05:28 鍙戣〃璇勮
]]>
Buffer http://www.tkk7.com/czihong/articles/382136.htmlChan ChenChan ChenTue, 03 Jul 2012 22:17:00 GMThttp://www.tkk7.com/czihong/articles/382136.htmlhttp://www.tkk7.com/czihong/comments/382136.htmlhttp://www.tkk7.com/czihong/articles/382136.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/382136.htmlhttp://www.tkk7.com/czihong/services/trackbacks/382136.html
class BinaryFileBuffer {
    public static int BUFFERSIZE = 2048;
    public BufferedReader fbr;
    public File originalfile;
    private String cache;
    private boolean empty;

    public BinaryFileBuffer(File f, Charset cs) throws IOException {
        originalfile = f;
        fbr = new BufferedReader(new InputStreamReader(new FileInputStream(f), cs), BUFFERSIZE);
        reload();
    }

    public boolean empty() {
        return empty;
    }

    private void reload() throws IOException {
        try {
            if ((this.cache = fbr.readLine()) == null) {
                empty = true;
                cache = null;
            } else {
                empty = false;
            }
        } catch (EOFException oef) {
            empty = true;
            cache = null;
        }
    }

    public void close() throws IOException {
        fbr.close();
    }

    public String peek() {
        if (empty())
            return null;
        return cache.toString();
    }

    public String pop() throws IOException {
        String answer = peek();
        reload();
        return answer;
    }

}


Chan Chen 2012-07-04 06:17 鍙戣〃璇勮
]]>
External Sortinghttp://www.tkk7.com/czihong/articles/382135.htmlChan ChenChan ChenTue, 03 Jul 2012 18:27:00 GMThttp://www.tkk7.com/czihong/articles/382135.htmlhttp://www.tkk7.com/czihong/comments/382135.htmlhttp://www.tkk7.com/czihong/articles/382135.html#Feedback0http://www.tkk7.com/czihong/comments/commentRss/382135.htmlhttp://www.tkk7.com/czihong/services/trackbacks/382135.html闃呰鍏ㄦ枃

Chan Chen 2012-07-04 02:27 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 亚洲国产精品自在自线观看| 国产精品视频免费一区二区| 亚洲精品国精品久久99热| 亚洲中文字幕久久精品无码VA | 亚洲AV无码国产精品色午友在线| 理论片在线观看免费| 日韩免费观看一级毛片看看| 中文无码亚洲精品字幕| 国产h视频在线观看网站免费| 亚洲成人激情在线| 毛片在线播放免费观看| 国产亚洲精品看片在线观看| 五月天婷婷免费视频| 免费观看日本污污ww网站一区| 亚洲精品国产摄像头| 成人au免费视频影院| 在线观看亚洲AV日韩A∨| 在线观看的免费网站| 亚洲an日韩专区在线| 色se01短视频永久免费| 亚洲jjzzjjzz在线播放| 噼里啪啦免费观看高清动漫4| 亚洲的天堂av无码| 亚欧免费视频一区二区三区| 亚洲精品日韩中文字幕久久久| 精品无码无人网站免费视频| 亚洲综合成人网在线观看| **俄罗斯毛片免费| 亚洲国产精品成人精品小说| 嘿嘿嘿视频免费网站在线观看 | 黑人粗长大战亚洲女2021国产精品成人免费视频| 久久WWW免费人成人片| 亚洲国产精品一区二区三区在线观看| 男女免费观看在线爽爽爽视频| 亚洲av无码一区二区三区天堂古代 | 四虎永久在线观看免费网站网址 | 亚洲国产专区一区| 一级一级一片免费高清| 国产亚洲情侣一区二区无| 两个人www免费高清视频| 亚洲AV午夜福利精品一区二区|