week1 Union-Find

Overview

We illustrate our basic approach to developing and analyzing algorithms by considering the dynamic connectivity problem.

We will learn the union-find data type and several implementations(quick find, quick union, weighted quick union, weighted quick union with path compression).Finally, we apply the union-find data type to the percolation problem from physical chemistry.

So what is dynamic connectivity problem?

Steps to developing a usable algorithms(scientific approach)

1.Model the problem, try to understand, basically, what are the main elements of the problem that need to be solved.

2.Find an algorithm to solve it.

3.Fast enough?Fits in memory?

4.If not, figure out why.

5.Find a way to address the problem

6.Iterate until satisfied.

dynamic connectivity

Given a set of N objects.
Union command:connect two objects.
Find/connected query:is there a path connecting two object?


image.png

image.png

image.png

In part two of the course, we'll consider algorithms that explicitly find paths(more work to do).

Modeling the objects

Applications involve manipulating objects of all types.

  1. Pixels in a digital video.
  2. Computers in a network.
  3. Friends in a social network.
  4. Transistors in a computer chip.
  5. Elements in a mathematical set.
  6. Variable names in Fortran Programs.
  7. Metallic sites in a computer system.

When programming, convenient to name objects 0 to N-1

  1. Use integers as array index.
  2. Suppress details not relevant to union-find.
    image.png

Modeling the connections

We assume "is connected to" is an equivalence relation:
Reflexive:p is connected to p.
Symmetirc:if p is connected to q, then q is connected to p.
Transitive:if p is connected to q and q is connected to r, then p is connected to r.

Connected components. Maximal set of objects that are mutually connected

image.png

Implementing the operations

Find query.Check if two objects are in the same component.
Union command.Replace components containing two objects with their union.

image.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市束昵,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌买雾,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件越锈,死亡現(xiàn)場離奇詭異宠进,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)绘梦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赴魁,“玉大人卸奉,你說我怎么就攤上這事∮庇” “怎么了榄棵?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長潘拱。 經(jīng)常有香客問我疹鳄,道長,這世上最難降的妖魔是什么芦岂? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任瘪弓,我火速辦了婚禮,結(jié)果婚禮上禽最,老公的妹妹穿的比我還像新娘腺怯。我一直安慰自己,他們只是感情好弛随,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布瓢喉。 她就那樣靜靜地躺著宁赤,像睡著了一般舀透。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上决左,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天愕够,我揣著相機(jī)與錄音,去河邊找鬼佛猛。 笑死惑芭,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的继找。 我是一名探鬼主播遂跟,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了幻锁?” 一聲冷哼從身側(cè)響起凯亮,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哄尔,沒想到半個(gè)月后假消,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡岭接,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年富拗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸣戴。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡啃沪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出窄锅,到底是詐尸還是另有隱情谅阿,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布酬滤,位于F島的核電站签餐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏盯串。R本人自食惡果不足惜氯檐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望体捏。 院中可真熱鬧冠摄,春花似錦、人聲如沸几缭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽年栓。三九已至拆挥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間某抓,已是汗流浹背纸兔。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留否副,地道東北人汉矿。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像备禀,于是被迫代替她去往敵國和親洲拇。 傳聞我的和親對象是個(gè)殘疾皇子奈揍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容