Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer 3 Michael Tsai 2011/3/11.

Similar presentations


Presentation on theme: "Divide and Conquer 3 Michael Tsai 2011/3/11."โ€” Presentation transcript:

1 Divide and Conquer 3 Michael Tsai 2011/3/11

2 Closest Pair of Points ๅ•้กŒ: ๅœจ๐‘›โ‰ฅ2ๅ€‹้ปžไธญ(้›†ๅˆP), ๆ‰พๅ‡บไธ€ๅฐ้ปžๅ…ถๅ…ฉ้ปžไน‹้–“็š„่ท้›ข็‚บๆœ€ๅฐ. ๆฏๅ€‹้ปž็‚บไธ€ไบŒ็ถญๅบงๆจ™.
่ท้›ข: ไปฅEuclidean distanceๅฎš็พฉ. ๐‘ 1 = ๐‘ฅ 1 , ๐‘ฆ 1 ๐‘ 2 = ๐‘ฅ 2 , ๐‘ฆ 2 ๐‘‘= ๐‘ฅ 1 โˆ’ ๐‘ฅ ๐‘ฆ 1 โˆ’ ๐‘ฆ 2 2 Qไธญๅฏ่ƒฝๆœ‰ไธ€ๆจฃ็š„ๅ…ฉ้ปž (ๅ‰‡ๅ…ถ่ท้›ข็‚บ0) Reference: (Textbook 33.4, p.1039)

3 Closest Pair of Points โ€“ ๆšดๅŠ›ๆณ•
็ฎ—ๅ‡บๆฏไธ€ๅฐ็š„่ท้›ข ๐‘› 2 =ฮ˜ ๐‘› 2

4 Closest Pair of Points โ€“ D&C
ๆ€่€ƒๆต็จ‹: ๅŽŸๆœฌๆ˜ฏฮ˜( ๐‘› 2 ), ฮ˜ ๐‘› log ๐‘› ไผผไนŽๆ˜ฏๅ€‹ไธ้Œฏ็š„็›ฎๆจ™ ๅคงๅˆ€ๅ…ˆๆ‹ฟไพ†็ ไธ€็ . ๅคงๆฆ‚ๆœƒ้•ท้€™ๆจฃ: ๐‘‡ ๐‘› =2๐‘‡ ๐‘› 2 +ฮ˜(??) ๅฏไปฅๅฐPไธญ็š„้ปžๆŽ’ๅบๅ—Ž? ้ธๆ“‡ไธ€: ้–‹ๅง‹็š„ๆ™‚ๅ€™ๅ…ˆๅ…จ้ƒจๆŽ’ไธ€ๆฌก ่Šฑฮ˜ ๐‘› log ๐‘› , ่ทŸ็›ฎๆจ™ไธ€ๆจฃโ€ฆok็š„! ้ธๆ“‡ไบŒ: ๆฏไธ€ๆฌก้ž่ฟดๅ‘ผๅซ้ƒฝๅฐ้ปžๆŽ’ๅบ ้‚ฃ้ž่ฟดๅผ่‡ณๅฐ‘่ฎŠๆˆ: ๐‘‡ ๐‘› =2๐‘‡ ๐‘› 2 +ฮ˜ ๐‘› log ๐‘› ๐‘‡ ๐‘› =ฮ˜(๐‘› log 2 ๐‘› ) (ไฝฟ็”จ่ชฒๆœฌ4.6-2 ่ฎŠๅฝขmaster theorem) not ok!

5 Closest Pair of Points โ€“ D&C
๐‘‡ ๐‘› =ฮ˜ ๐‘› log ๐‘› (ๆญฃๅฅฝๆ˜ฏๆˆ‘ๅ€‘่ฆ็š„) Algorithm็ต„ๆˆๅ…ƒ็ด : ไธ€้–‹ๅง‹ๅฏไปฅๅฐ้ปžๅ…ˆๆŽ’ๅบ ๅ…‰ๅŠไธ€้šป: Divide็š„ๆ™‚ๅ€™ๅˆ‡ๆˆๅ…ฉไปฝ็ญ‰ไปฝ Combine็š„ๆ™‚ๅ€™ๆœ€ๅคšๅฏไปฅ่Šฑฮ˜(๐‘›)็š„ๆ™‚้–“

6 Closest Pair of Points โ€“ D&C
ๅ…ˆๆƒณๆƒณRecursive case Divide: ๅŠƒไธ€ๆข็›ด็ทšl, ๆŠŠๆ‰€ๆœ‰็š„้ปžๅˆ†ๆˆๅ…ฉ้ƒจๅˆ† ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… , ไฝฟ ๐‘ƒ ๐ฟ = ๐‘ƒ 2 and ๐‘ƒ ๐‘… = ๐‘ƒ 2 , ๐‘ƒ ๐ฟ ไธญ็š„้ปž้ƒฝๅœจlไธŠๆˆ–ๅฎƒ็š„ๅทฆ้‚Š, ๐‘ƒ ๐‘… ็š„้ปž้ƒฝๅœจlไธŠๆˆ–ๅฎƒ็š„ๅณ้‚Š ้ž่ฟดๅ‘ผๅซๆœƒ่งฃๆฑบๆ‰พๅ‡บ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญ็š„closest pairs, ๅ‡่จญๆ‰พๅˆฐ็š„ๆœ€็Ÿญ่ท้›ขๅˆ†ๅˆฅ็‚บ ๐›ฟ ๐ฟ , ๐›ฟ ๐‘… , and ๐›ฟ=minโก( ๐›ฟ ๐ฟ , ๐›ฟ ๐‘… ) Combine: Closest Pairๆœ‰ไธ‰็จฎๅฏ่ƒฝ: ๐‘ƒ ๐ฟ ๆˆ– ๐‘ƒ ๐‘… ไธญๆ‰พๅˆฐ็š„ ๆˆ–่€…ๆ˜ฏไธ€ๅ€‹้ปžๅœจ ๐‘ƒ ๐ฟ ไธญ, ไธ€ๅ€‹้ปžๅœจ ๐‘ƒ ๐‘… ไธญ็š„, ่€Œๅ…ฉ้ปž่ท้›ขๅฐๆ–ผ๐›ฟ

7 Closest Pair of Points โ€“ D&C
ๆ€Ž้บผๆ‰พๅ‡บไธ€้ปžๅœจ ๐‘ƒ ๐ฟ , ไธ€้ปžๅœจ ๐‘ƒ ๐‘… , ่€Œๅ…ฉ้ปž่ท้›ข<๐›ฟ ? ๅช่€ƒๆ…ฎๅฏ่ƒฝ็š„้ปž: ๅœจ2๐›ฟ็ฏ„ๅœๅ…ง็š„้ปž line l ๐›ฟ ๐ฟ ๐›ฟ ๐‘… ๐›ฟ ๐›ฟ

8 Closest Pair of Points โ€“ D&C
ๅฏๆ˜ฏcombineๅช่ƒฝ่Šฑฮ˜(๐‘›)ๅ–” (ไธ่ƒฝๆšดๅŠ›ๆ‰พๆฏๅ€‹pair) ้—œ้ต: 2๐›ฟ้•ทๆข็ฏ„ๅœๅ…งไธ้œ€่ฆๆ‰พๆฏๅ€‹pair! ๆง‹ๆˆclosest pair็š„ๅ…ฉ้ปžไธ€ๅฎšๅœจ2๐›ฟร—๐›ฟ็š„้•ทๆ–นๅฝขๅ…ง ๐›ฟ ๐›ฟ ๐›ฟ

9 Closest Pair of Points โ€“ D&C
2๐›ฟร—๐›ฟ็š„้•ทๆ–นๅฝขๅ…งๆœ€ๅคšๅฏไปฅๆ”พไธ‹8ๅ€‹้ปž (้ปžๅ’Œ้ปžไน‹้–“็š„่ท้›ขไธๅฏไปฅๅฐๆ–ผ๐›ฟ, ๅฆๅ‰‡ๅฐฑ็Ÿ›็›พไบ†) ๅ› ๆญคๅฐๆฏๅ€‹ๅœจ2๐›ฟ้•ทๆขไธญ็š„้ปž, ๅฆ‚ๆžœ็…งYๆŽ’ๅบ้ŽๅพŒ, ๅช้œ€่ฆๆชขๆŸฅๅฎƒๅ’ŒๅฎƒๅพŒ้ขไธƒๅ€‹้ปž็š„่ท้›ข! ฮ˜(๐‘›) ๅ„2ๅ€‹้ปž, ไธ€ๅ€‹ๅœจ ๐‘ƒ ๐ฟ , ไธ€ๅ€‹ๅœจ ๐‘ƒ ๐‘… ๐›ฟ ๐›ฟ ๐›ฟ

10 Closest Pair of Points โ€“ D&C
Base case: ็•ถnโ‰ค3ๆ™‚, ๅ‰‡็›ดๆŽฅ็”จๆšดๅŠ›ๆณ•ๆ‰พๅ‡บclosest pair ๆœ€ๅคš็ฎ—ไธ‰ๆฌก่ท้›ข. ฮ˜(1)

11 Closest Pair of Points โ€“ D&C
Divide: ๅŠƒไธ€ๆข็›ด็ทšl, ๆŠŠๆ‰€ๆœ‰็š„้ปžๅˆ†ๆˆๅ…ฉ้ƒจๅˆ† ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… , ไฝฟ ๐‘ƒ ๐ฟ = ๐‘ƒ 2 and ๐‘ƒ ๐‘… = ๐‘ƒ 2 , ๐‘ƒ ๐ฟ ไธญ็š„้ปž้ƒฝๅœจl็š„ๅทฆ้‚Š, ๐‘ƒ ๐‘… ็š„้ปž้ƒฝๅœจlไธŠๆˆ–ๅฎƒ็š„ๅณ้‚Š ้ž่ฟดๅ‘ผๅซๆœƒ่งฃๆฑบๆ‰พๅ‡บ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญ็š„closest pairs, ๅ‡่จญๆ‰พๅˆฐ็š„ๆœ€็Ÿญ่ท้›ขๅˆ†ๅˆฅ็‚บ ๐›ฟ ๐ฟ , ๐›ฟ ๐‘… , and ๐›ฟ=minโก( ๐›ฟ ๐ฟ , ๐›ฟ ๐‘… ) Combine: ๅฐ‡ๅœจ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญไธๅœจ็›ด็ทšl็š„ๅทฆๅณ๐›ฟ่ท้›ขๅ…ง็š„้ปžๅŽป้™ค, ๅพ—ๅˆฐ ๐‘ƒ ๐ฟ โ€ฒๅŠ ๐‘ƒ ๐‘… โ€ฒ. ๅฐๆ–ผ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญ็š„้ปž, ๆชขๆŸฅๅฎƒๅ’ŒYๅบงๆจ™ๅœจๅฎƒๅพŒ้ข็š„ๅ…ซๅ€‹้ปž็š„่ท้›ขๆ˜ฏๅฆๅฐๆ–ผฮด. ่‹ฅๆ˜ฏ็š„่ฉฑๅ‰‡ๆญค็‚บclosest pair. ่‹ฅๅฆ็š„่ฉฑๅ‰‡้ž่ฟดๅ‘ผๅซๆ‰€ๅพ—ๅˆฐ็š„๐›ฟ่ท้›ข็š„pair็‚บclosest pair. ฮ˜(๐‘›) 2๐‘‡( ๐‘› 2 ) ฮ˜(๐‘›) ๐‘‡ ๐‘› =2๐‘‡ ๐‘› 2 +ฮ˜(๐‘›)

12 Closest Pair of Points โ€“ D&C
Divide: ๅŠƒไธ€ๆข็›ด็ทšl, ๆŠŠๆ‰€ๆœ‰็š„้ปžๅˆ†ๆˆๅ…ฉ้ƒจๅˆ† ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… , ไฝฟ ๐‘ƒ ๐ฟ = ๐‘ƒ 2 and ๐‘ƒ ๐‘… = ๐‘ƒ 2 , ๐‘ƒ ๐ฟ ไธญ็š„้ปž้ƒฝๅœจl็š„ๅทฆ้‚Š, ๐‘ƒ ๐‘… ็š„้ปž้ƒฝๅœจlไธŠๆˆ–ๅฎƒ็š„ๅณ้‚Š ้ž่ฟดๅ‘ผๅซๆœƒ่งฃๆฑบๆ‰พๅ‡บ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญ็š„closest pairs, ๅ‡่จญๆ‰พๅˆฐ็š„ๆœ€็Ÿญ่ท้›ขๅˆ†ๅˆฅ็‚บ ๐›ฟ ๐ฟ , ๐›ฟ ๐‘… , and ๐›ฟ=minโก( ๐›ฟ ๐ฟ , ๐›ฟ ๐‘… ) Combine: ๅฐ‡ๅœจ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญไธๅœจ็›ด็ทšl็š„ๅทฆๅณ๐›ฟ่ท้›ขๅ…ง็š„้ปžๅŽป้™ค, ๅพ—ๅˆฐ ๐‘ƒ ๐ฟ โ€ฒๅŠ ๐‘ƒ ๐‘… โ€ฒ. ๅฐๆ–ผ ๐‘ƒ ๐ฟ ๅŠ ๐‘ƒ ๐‘… ไธญ็š„้ปž, ๆชขๆŸฅๅฎƒๅ’ŒYๅบงๆจ™ๅœจๅฎƒๅพŒ้ข็š„ๅ…ซๅ€‹้ปž็š„่ท้›ขๆ˜ฏๅฆๅฐๆ–ผฮด. ่‹ฅๆ˜ฏ็š„่ฉฑๅ‰‡ๆญค็‚บclosest pair. ่‹ฅๅฆ็š„่ฉฑๅ‰‡้ž่ฟดๅ‘ผๅซๆ‰€ๅพ—ๅˆฐ็š„๐›ฟ่ท้›ข็š„pair็‚บclosest pair. ้œ€่ฆๆ‰€ๆœ‰Pไธญ็š„้ปž็…งxๅบงๆจ™ๆŽ’ๅบ ้œ€่ฆๆ‰€ๆœ‰Pไธญ็š„้ปž็…งyๅบงๆจ™ๆŽ’ๅบ ไฝ†ๆ˜ฏไธ่ƒฝๆฏๅ€‹้ž่ฟดๅ‘ผๅซ้ƒฝๆŽ’ๅบ! ๅฆ‚ไฝ•็”จไฝŽๆ–ผ๐šฏ ๐’ ๐’๐’๐’ˆ ๐’ ็š„ๆ™‚้–“ๅ–ๅพ—ๆŽ’ๅฅฝๅบ็š„้ปž?

13 Closest Pair of Points โ€“ D&C
ไพ็…งyๅบงๆจ™ๆŽ’ๅบ็š„P ไพ็…งxๅบงๆจ™ๆŽ’ๅบ็š„P ็…ง้ †ๅบๅˆ†ๆˆ ๐‘ƒ ๐ฟ , ๐‘ƒ ๐‘… ็š„่ฉฑๅช้œ€่ฆฮ˜ ๐‘› ไพ็…งxๅบงๆจ™ๆŽ’ๅบ็š„ ๐‘ƒ ๐ฟ ไพ็…งxๅบงๆจ™ๆŽ’ๅบ็š„ ๐‘ƒ ๐‘… ไพ็…งyๅบงๆจ™ๆŽ’ๅบ็š„ ๐‘ƒ ๐ฟ ไพ็…งyๅบงๆจ™ๆŽ’ๅบ็š„ ๐‘ƒ ๐‘…

14 Closest Pair of Points โ€“ D&C
ไธๅŒ…ๅซไธ€้–‹ๅง‹็š„sorting็š„่ฉฑ: ๐‘‡ ๐‘› =ฮ˜ ๐‘› log ๐‘› ๅŒ…ๅซๆ‰€ๆœ‰็š„่ฉฑ: ๐‘‡ โ€ฒ ๐‘› =ฮ˜ ๐‘› log ๐‘› +๐‘‡ ๐‘› =ฮ˜ ๐‘› log ๐‘› Sortingๆ‰€่Šฑๆ™‚้–“

15 ไผ‘ๆฏไธ€ไธ‹-็จ‹ๅผ่จญ่จˆๅธซๅธธ่ฆ‹่—‰ๅฃ by Santos Liu on Saturday, March 5, 2011 at 1:00am ็ฌฌ 20 ๅ๏ผš้€™ๅพˆๅฅ‡ๆ€ชๅ–”ใ€‚ ็ฌฌ 19 ๅ๏ผšไปฅๅ‰ๅพžไพ†ไธๆœƒ้€™ๆจฃๅ•Š๏ผ ็ฌฌ 18 ๅ๏ผšๆ˜จๅคฉๆ˜Žๆ˜Žๆœƒๅ‹•็š„ๅ•Š๏ผ ็ฌฌ 17 ๅ๏ผšๆ€Ž้บผๅฏ่ƒฝ~ ็ฌฌ 16 ๅ๏ผš้€™ไธ€ๅฎšๆ˜ฏๆฉŸๅ™จ็š„ๅ•้กŒใ€‚ ็ฌฌ 15 ๅ๏ผšไฝ ๅˆฐๅบ•ๆ˜ฏๆ‰“ไบ†ไป€้บผๆ‰่ฎ“็จ‹ๅผ็•ถๆŽ‰็š„๏ผŸ ็ฌฌ 14 ๅ๏ผšไธ€ๅฎšๆ˜ฏไฝ ็š„่ณ‡ๆ–™ๆœ‰ๅ•้กŒใ€‚ ็ฌฌ 13 ๅ๏ผšๆˆ‘ๅทฒ็ถ“ๅฅฝๅนพๅ€‹็ฆฎๆ‹œๆฒ’็ขฐ้‚ฃไธ€ๆฎต็จ‹ๅผไบ†ใ€‚ ็ฌฌ 12 ๅ๏ผšไฝ ไธ€ๅฎšๆ˜ฏ็”จๅˆฐ่ˆŠ็‰ˆไบ†ใ€‚ ็ฌฌ 11 ๅ๏ผšไธ€ๅฎšๆ˜ฏๅทงๅˆ๏ผ็‚บไป€้บผ้€™็จฎๅฃž้‹ๆฐฃๅช่ฎ“ไฝ ็ขฐไธŠใ€‚ ็ฌฌ 10 ๅ๏ผšๆˆ‘ไธๅฏ่ƒฝไป€้บผๅŠŸ่ƒฝ้ƒฝๆธฌ่ฉฆๅˆฐๅง๏ผŒๆœ‰ bug ๆ˜ฏๆญฃๅธธ็š„๏ผ ็ฌฌ 9 ๅ๏ผš้€™ๅ€‹ไธๅฏ่ƒฝๆ˜ฏ้‚ฃๅ€‹็š„ๅŽŸๅง‹็ขผ๏ผ ็ฌฌ 8 ๅ๏ผš้€™็จ‹ๅผๆ‡‰่ฉฒๆ˜ฏๆœƒๅ‹•็š„๏ผŒๅชๆ˜ฏๆˆ‘ๅฏซๅฅฝๅพŒ้‚„ๆฒ’ๅšๆธฌ่ฉฆใ€‚ ็ฌฌ 7 ๅ๏ผšๅฏๆƒก๏ผไธ€ๅฎšๆœ‰ไบบๆ”นไบ†ๆˆ‘็š„็จ‹ๅผใ€‚ ็ฌฌ 6 ๅ๏ผšไฝ ๆœ‰ๆชขๆŸฅ้Žไฝ ็š„้›ป่…ฆๆœ‰ๆฒ’ๆœ‰็—…ๆฏ’ๅ—Ž๏ผŸ ็ฌฌ 5 ๅ๏ผšๅ„˜็ฎก้€™ๅŠŸ่ƒฝ้‚„ไธ่ƒฝๅ‹•ๅ•ฆ๏ผŒไฝ ่ฆบๅพ—ไป–ๅฆ‚ไฝ•๏ผŸ ็ฌฌ 4 ๅ๏ผšๅœจไฝ ็š„็ณป็ตฑไธ่ƒฝ็”จ้‚ฃไธ€ๅ€‹็‰ˆๆœฌ็š„็จ‹ๅผๅ•ฆ๏ผ ็ฌฌ 3 ๅ๏ผšไฝ ๅนนๅ˜›่ฆ้‚ฃๆจฃๆ“ไฝœ๏ผŒ้ƒฝๆ˜ฏไฝ ็š„ๅ•้กŒใ€‚ ็ฌฌ 2 ๅ๏ผš็จ‹ๅผ็™ผ็”Ÿๅ•้กŒๆ™‚ไฝ ๅœจๅ“ช่ฃก๏ผŸ ็ฌฌ 1 ๅ๏ผšๅœจๆˆ‘็š„ๆฉŸๅ™จๆ˜Žๆ˜Žๅฐฑๅฏไปฅๅ‹•ๅ•Š๏ผ


Download ppt "Divide and Conquer 3 Michael Tsai 2011/3/11."

Similar presentations


Ads by Google