Вы находитесь на странице: 1из 187

퐤 ù‘ÚÈ÷¢

ü¨À€úõ ø ü€ê ùÀØÈ÷¢

ö€ä

ݵþ¤Úó üŸÂÏ §¤¢ ùøÛ

‘Þ€û¤ ¢‘µ¨

ý¢› Àƒa¨ š‘Ÿ ÀƒÞŸ Àƒa¨ µî¢

1384 ù‘õ ¤£


݃ŸÂó ßޟÂó ‚Üaó ÝƓ
a a

°ó‘Îõ ´¨Âúê

‚õÀÖõ

ü±÷‘¹õ ý‘û¢‘Þ÷ üêÂãõ 1 ÛÊê

1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ü±÷‘¹õ ý‘û¢‘Þ÷ .1

1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . big-O ¢‘Þ÷ 1.1

2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . big −Ω ¢‘Þ÷ 2.1

2 .......................................................... θ ¢‘Þ÷ 3.1

3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . small − o¢‘Þ÷ 4.1

3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . small − ω ¢‘Þ÷ 5.1

3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ý ƒð ÝÞþ Ãî‘õ ‚ƒÌì 6.1

5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‚ƒÌì À€ –‘±™ 7.1

üµÈ𥑓 ý‘û ݵþ ¤Úó 2 ÛÊê

9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . üµÈ𥑓 ý‘û ݵþ ¤ Úóø –„¢‘ãõ .2

9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . üµÈ𥑓 –„¢‘ãõ 1.2

10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‘û ‚Èþ ¤ ¢¤ õ ¤¢ ¶½“ 1.1.2

14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . üµÈ𥑓 ý‘û ݵþ ¤ Úó 2.2

26 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (Master Theorem) ü¨‘¨ ‚ƒÌì 3.2

35 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‘û ݵþ ¤ Úó Ãó‘÷ 4.2

37 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ý¥‘¨ °—Âõ ý‘û ݵþ ¤ Úó 5.2

37 . . . . . . . . . . . . . . . . . Selection Sort ü“‘¿µ÷ ý¥‘¨ °—Âõ ݵþ ¤ Úó 1.5.2

37 . . . . . . . . . . . . . . . . . . . . . . Bubble Sort ü“‘±Ÿ ý¥‘¨ °—Âõ ݵþ ¤ Úó 2.5.2


38 . . . . . . . . . . . . . . . . . . . Insertion Sort ü›¤¢ ý¥‘¨ °—Âõ ݵþ ¤ Úó 3.5.2

39 . . . . . . . . . . . . . . . . . . . . Pigeon hole Sort ý —±î ‚÷„ ý¥‘¨ °—Âõ 4.5.2

41 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binary Searchüþø¢ø¢ ý¹µÆ› 5.5.2

42 . . . . . . . . . . . . .Binary Insertion Sort ü›¤¢ üþø¢ø¢ ý¥‘¨ °—Âõ 6.5.2

43 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shell Sort ݵþ ¤ Úó 7.5.2

45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bucket Sort1 ݵþ ¤ Úó 8.5.2

45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bucket Sort2 ݵþ ¤ Úó 9.5.2

46 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bin Sort ݵþ ¤ Úó 10.5.2

47 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Counting Sort ݵþ ¤ Úó 11.5.2

47 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Radix sort ݵþ ¤ Úó 12.5.2

48 . . . . . . . . . . . . . . . . . . üµÈ𥑓 –„¢‘ãõ ( ö¢Âî ۟) ö¢Âî Trace ÛÞä 6.2

50 . . . . . . . . . . . . . . . . . . . . . . . . . Catalan Number ö„‘—‘î ¢Àä Â“ ý ¤Áð 7.2

‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý¤ø¢‘þ 3 ÛÊê

55 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ .3

56 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (Sparse array)§¤‘³¨ ‚þ¤ 1.3

57 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . üþø¢ø¢ ´¡¤¢ 2.3

59 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . max heap ´¡¤¢ 3.3

62 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ( ý‚Üޛø¢ ²ƒû) Binomial Heap 4.3

63 . . . . . . . . . . . . . . . . . . . . . . . . . ( ý ‚Üޛø¢ ´¡¤¢ )Binomial Tree 1.4.3

63 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Max Binomial Tree 2.4.3

64 . . . . . . . . . . . . . . . . . . . . . . . The Merge Of Max Binomial Trees 3.4.3

64 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binomial Heap 4.4.3

65 . . . . . . . . . . . . . . . . . . . . . . . . .Min Binomial Heap ýø ¤ “ –‘ƒÜÞä 5.4.3

66 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Max Binomial Heap 6.4.3

67 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . FIBONACCI HEAP 5.3

67 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fibonacci Tree 1.5.3


68 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Max Fibonacci Tree 2.5.3

68 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fibonacci Heap 3.5.3

68 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Max Fibonacci Heap 4.5.3

69 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 ö‘µ¡¤¢ 6.3

72 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 ´¡¤¢ ×þ ý¹µÆ› 1.6.3

73 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 ´¡¤¢ ×þ Û¡¢ ‚“ š¤¢ 2.6.3

77 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 ´¡¤¢ ×þ ¥ éÁŸ 3.6.3

83 . . . . . . . . . . . . . . 2-3 ´¡¤¢ ×þ ¥ éÁŸ ¢ÂØÜÞä ۃܽ— ø ‚þ ù— 4.6.3

84 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Red-Black ù‘ƒ¨ - ÃõÂì ´¡¤¢ 7.3

84 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ù‘ƒ¨ - ÃõÂì ´¡¤¢ «¡ 1.7.3

85 ................................... üþÀµ“ ý‘þ‘Ìì ø Óþ ¤‘ã— 2.7.3

86 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ö¤ ø¢ 3.7.3

87 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . š¤¢ 4.7.3

89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . éÁŸ 5.7.3

94 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (Disjoin sets)Ã¹õ ý‘û ‚äÞ¹õ 8.3

üÆþ÷ ݵþ ¤Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ 4 ÛÊê

97 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . üÆþ÷ ‚õ‘÷“ ý‘û ©ø ¤ ᐁ÷ .4

98 . . . . . . . . . . . . . . . . . . . . (Greedy Algorithms) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤ Úó 1.4

98 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ßÞê‘û ý¥‘¨ ù¢ÂÈê ݵþ ¤ Úó 1.1.4

101 . . . . . . . . . . . . . . . . . . . MST ñ‘Þƒ€ƒõ ý‘ª• ´¡¤¢ ý‘û ݵþ ¤ Úó 2.1.4

109 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Knapsack üµÈ• ‚óî ݵþ ¤ Úó 3.1.4

110 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DIJKSTRA ݵþ ¤ Úó 4.1.4

112 . . . . . . . . . .(timetable or scheduling) ýÀ€“ ö‘õ¥ ý‘û ݵþ ¤ Úó 5.1.4

117 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (devide and conquer) ۟ø ݃Æ֗ 2.4

117 ........................................ ï¤ Ã“ ¢Àä ’® 1.2.4

119 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . merge sort ݵþ ¤ Úó 2.2.4


121 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Quick Sort ݵþ ¤ Úó 3.2.4

126 . . . . . . . . . . . . . . . . (‘û Åþ —‘õ ’® ݵþ ¤ Úó) ߨÂµ¨ ݵþ ¤ Úó 4.2.4

128 . . . . . . . . . . . . . . . . . . . . . . Dynamic Programming ‘þ• üÆþ÷ ‚õ‘÷“ 3.4

n

128 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‚±¨‘½õ 1.3.4
k

130 .................................... ‘û ñ• ö¢Âî ¢Â¡ ‚ó‘bÆõ 2.3.4

131 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . {0, 1} üµÈ• ‚óî ‚ó‘bÆõ 3.3.4

131 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Floyd ݵþ ¤ Úó 4.3.4

133 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‘û Åþ —‘õ ýùƒ¹÷¥ ’® 5.3.4

135 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‚€ƒú“ üþø¢ø¢ ý¹µÆ› ´¡¤¢ 6.3.4

138 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . íµÈõ ‚µª¤ Âþ ¥ ßþ µð¤ Ó 7.3.4

140 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ü÷‘ú› –‘Ö“‘Æõ ý ‚ó‘bÆõ 8.3.4

142 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ¢Âð ù¤ ø¢ ùÀ€ªø Âê ‚ó‘bÆõ 9.3.4

143 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‘û ý¥‘“ ´€Þ÷¤ — 4.4

144 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B &T ( Back Tracking ) 5.4

144 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Âþ ¥ ø n‚ó‘bÆõ 1.5.4

146 .................................. ü÷µÜƒÞû ¤ ø¢ ßµê‘þ ‚ó‘bÆõ 2.5.4

147 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . m-coloring ‚ó‘bÆõ 3.5.4

148 . . . . . . . . . . . . . . . . . . . . . . . . . . . Branch and Bound (B&B) ׃€Ø— 6.4

‘û éÂð Çþ• 5 ÛÊê

149 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exploring graphs ‘û éÂð Çþ• .5

149 . . . . . . . . . . . . . . . . . . . . . . . . . . . (Depth First Search) DFS ݵþ ¤ Úó 1.5

152 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ‚µÈ• ‘“ ý¥‘¨ ù¢‘ƒ• 1.1.5

152 . . . . . . . . . . . . . . . . . . . . . . . . . . (Breath First Search) BFS ݵþ ¤ Úó 2.5

153 . . . . . . . . . . . . . . . . . . . . . . . . Topological Sort ý¦ ó•— ý¥‘¨ °—Âõ 3.5

154 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bellman Ford ݵþ ¤ Úó 4.5

155 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DAG ݵþ ¤ Úó 5.5


¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ 6 ÛÊê

157 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . LOOP INVARIANT1.6


173 . . . . . . . . . . . . . . . . . . . . . . . . . (Amortized Analysis) üî…úµ¨ Ãó‘÷ 2.6
173 . . . . . . . . . . . . . . . . . . . . . . . . . (Aggregate Analysis) üãÞ¹— Ãó‘÷ 1.2.6

176 . . . . . . . . . . . . . . . . . . . . . . . . (Accounting Method) ü“‘ÆŸ ©ø ¤ 2.2.6

178 . . . . . . . . . . . . . . . . . . . . . . . . . . (Potential Method) ۃÆ÷‘µ• ©ø ¤ 3.2.6


‚õÀÖõ

ü ŸÂ Ï §¤¢ ýùø à › , ´ ¨ ‚ µ êÂ ð ¤Â ì ‘ Þ ª ¤‘ ƒ µ ¡ ¤¢ ö € î Ý û ‚ î ýùø à ›

ùÀØÈ÷¢ —ƒ³õ‘î ü¨À€úõ ‚µª¤ ö‘þ¹È÷¢ ¥ üãޛ ͨ— ‚î Àª‘“ üõ ݵþ ¤ Úó

‚ ƒ ú —×—
ü ¨¤‘ ê ¤Ã ê ô ÷ Í ¨ — 1384 ñ‘ ¨ ù‘ õ ¤£ ¤¢ 퐤 ù‘ Ú È ÷¢ ü ¨À € ú õ ø ü € ê

. ´¨ ùÀª ‚µªÁð öÀ€õ ‚ì…ä ¤‘ƒµ¡ ¤¢ ø ´¨ ùÀþ¢Âð ßþøÀ—ø

ý ¤‘ Ø Þ û ùø à › ß þ ý‚ ƒ ú — ¤¢ ‚ î ü ÷Ã þ à ä ü õ‘ Þ — ¥ ¢¤¢ ‘ › ´ Þ Æ ì ß þ¤¢

ø ü € ê ùÀ Ø È ÷¢ 1382 ñ‘ ¨ ý¢ø ¤ ø  — ƒ ³ õ‘ î ü ¨À € ú õ ‚ µ ª¤ ö‘ þ ¹ È ÷¢ , À ÷ù¢ Þ ÷

ø ü÷‘Ìõ¤ ‚ÞÏ‘ê ‘û Ý÷‘¡ æþ ¤¢ ü“ –‘ÞŸ¥ ¥ «Ê¡ ‚“ , 퐤 ù‘ÚÈ÷¢ ü¨À€úõ

. ݪ‘“ ‚µª¢ ¤ ÂØȗ ñ‘Þî ö ÇþÂþø Âϑ¡ ‚“ ýÀޟ Âû¥

ý¢› Àƒ¨ š‘Ÿ ÀƒÞŸ


a

ü¨À€úõ ø ü€ê ùÀØÈ÷¢

퐤 ù‘ÚÈ÷¢

1384 ù‘õ ¤£


1 ÛÊê

ü±÷‘¹õ ý‘û¢‘Þ÷ üêÂãõ

ü±÷‘¹õ ý‘û¢‘Þ÷ .1
À €  ‘ þ ø¢ ùÀ ª ‚ µ êÂ ð ‚ Ñ ê‘ Ÿ ‘ þ øÂ › ö‘ õ¥ ö — ü õ ‘ ú ÷ Í ¨ — ‚ î ü Ž‘ û¤Ã “

. ݃þ‘Þ÷ ‚Æþ‘Öõ Ýû ‘“ ¤ ݵþ ¤ Úó

big-O ¢‘Þ÷ 1.1


:ù‘Ú÷ Àª‘“ f : N −→ R+ Âð

O(f (n)) = {g:N → R+ |∃c ∈ R+ , ∀ n ∈ N g(n) ≤ cf (n)}
 3
n n<1000
f (n) =

2n 2 n≥100

O(n2 ) = {n2 , n lg n, f (n), ....}

6 f1

f2 ∈ O(f1 )
...
... f2
...
...
. -
M

. ´¨ O(f1 ) ¥ Ýû ¥‘“ ¢ª “Â“ À¬ f2 Âð üµŸ Û±ì Ûت ¤¢


ü±÷‘¹õ ý‘û¢‘Þ÷ üêÂãõ .1 ÛÊê 2

6
f2 f1 ∈ O(f2 )

f1 f2 ∈ O(f1 )

‚î ýÝ ŕ, f2 ∈ O(f1 ) Ýþ ¤¢ ¢ª ’® °þ ® ×þ ¤¢ f1 Â𐠄‘“ Ûت ¤¢

. ´¨‚›¤¢ Ýúõ , ´Æƒ÷ c °þ ® ´¨ Ýúõ

ý„‘ “ öÂ î big-O ,ü êÂ Ê õ ‚ Ñ ê‘ Ÿ ýÂ “ f · õ ¢¤¢ ¤ „‘ “ öÂ î ô ú Ô õ ö‘ Þ û


… big-O
߃þ‘• ö ýÂ› ö‘õ¥ üþ„‘“ öÂî ‚î ´¨ ÀƒÔõ üÞµþ ¤ Úó ŕ .´¨ ‚Ñꑟ éÂÊõ

.Àª‘“

big − Ω ¢‘Þ÷ 2.1


:ù‘Ú÷ f : N −→ R+ Âð

Ω(f (n)) = {g : N → R+ |∃d > 0 ∀ n ∈ N f (n) ≤ dg(n)}

ƒƒç— ݃÷— üõ ¤ °þÂ® Âþ ¥ À÷¤À÷ ü—ø‘Ô— µ؝î ‘þ ýø‘Æõ µ؝î ‘¹€þ ¤¢

:݃û¢

f (n) < dg(n) =⇒ f (n) ≤ dg(n)


f (n) ≤ dg(n) =⇒ f (n) < d0 g(n)
: Ýþ ¤¢ ߃€»Þû

f2 (n) ∈ Ω(f1 (n)) ⇐⇒ f1 (n) ∈ O(f2 (n))

θ ¢‘Þ÷ 3.1
θ(f (n)) = O(f (n)) ∩ Ω(f (n)) : Ýþ ¤¢ù‘Ú÷ f : N −→ R+ Âð
: ñ‘·õ

O(n2 ) = {n2 , n2 + 4, nlogn, ...}



Ω(n2 ) = {2n2 , 2n2 + n, n3 + n2 , n4 , n2 logn, ...}
θ(n2 ) = {n2 , n2 + n, 2n2 , ...}

: ‚µØ÷

üþ„‘“ ߃þ‘• öÂî Àþ‘“ âìø ¤¢,¢Þ÷ ù¢‘Ôµ¨ Ω ¥ Àþ‘“ ݵþ ¤ Úó ýÀ“ Ƀ¿È— ýÂ“

. Ýþ ¤ ø ´¨¢ ‚“ ö ýÂ“
3 SM ALL − O¢‘Þ÷ .4.1

small − o¢‘Þ÷ 4.1


:ù‘Ú÷ f : N −→ R+ Âð
o(f (n)) = {g : N → R+ |∀ c > 0 ∀ n ∈ N g(n) ≤ cf (n)}

‚“ ‚î ´¨ ö ¤¢ ´ƒÞû ‘ú€—À÷¤À÷ ü—ø‘Ô— µ؝î ‘þ ýø‘Æõ µ؝î ‘¹€þ ¤¢

.À€î ëÀ¬ c °þ ® Âû ý¥

: ñ‘·õ

? n ∈ o(2n)‘þ 2n ∈ o(n): ´¨ ´¨¤¢ ë ê ý‘û –¤‘±ä ¥ ×þ ôÀî

c=1
2n ∈ o(n) ⇐⇒ ∀c > 0 2n ≤ cn F =⇒ 2n ∈

∀n∈N ⇐⇒ / o(n)

c= 13

n ∈ o(2n) ⇐⇒ ∀c > 0 ∀ n ∈ N n ≤ c2n ⇐⇒ / o(2n)
F =⇒ n ∈

small − ω ¢‘Þ÷ 5.1


:ù‘Ú÷ f : N −→ R+ Âð
ω(f (n)) = {g : N → R+ |∀ c > 0 ∀ n ∈ N g(n) ≥ cf (n)}

: Àƒû¢ ö‘È÷ :ñ‘·õ


T
ω(f (n)) o(f (n)) = ∅
T
´¨ ¢›õ ö‘€M1 ×þ d0 > 0 ýÂ“ ù‘Ú÷ g(n) ∈ ω(f (n)) o(f (n)) Âð :۟

: ‚î

∀n ≥ M1 g(n) ≥ d0 f (n) (g(n) ∈ ω(f (n)))


c0 = d20 ýÂ“
:‚î ´¨ ¢›õ ö‘€ M2 ×þ ñ‘Ÿ

∀n ≥ M2 g(n) ≤ c0 f (n) (g(n) ∈ o(f (n))) =⇒ g(n) ≤ d20 f (n) =⇒

2 f (n) ≥ d0 f (n) =⇒ d0 ≥ 2d0 =⇒ d0 ≤ 0


d0
∀n ≥ M ax{M1 , M2 }
.¢ª üõ ´“‘™ Ý؟ ßþ “‘€“, ÝþÀƒ¨¤ Ë쑀— ‚“ ŕ

ýƒð ÝÞþÃî‘õ ‚ƒÌì 6.1


1)f (n) + g(n) ∈ O(M AX{f (n), g(n)})
2)f (n) + g(n) ∈ Θ(M AX{f (n), g(n)})
: 1 –‘±™

f (n) ≤ M AX{f (n), g(n)} , g(n) ≤ M AX{f (n), g(n)} =⇒


f (n)+g(n) ≤ 2M AX{f (n), g(n)} =⇒ f (n)+g(n) ∈ O(M AX{f (n), g(n)})
ü±÷‘¹õ ý‘û¢‘Þ÷ üêÂãõ .1 ÛÊê 4

: 2 –‘±™

M AX{f (n), g(n)} ≤ f (n) + g(n) =⇒ f (n) + g(n) ∈ Ω(M AX{f (n), g(n)})
f (n) + g(n) ∈ O(M AX{f (n), g(n)}), f (n) + g(n) ∈ Ω(M AX{f (n), g(n)})
=⇒ f (n) + g(n) ∈ Θ(M AX{f (n), g(n)})
:ßþ Âޗ

. log n! ∈ θ(n log n)Àƒû¢ ö‘È÷


:۟

: ñø ù¤

(n! = 2πn) ـƒóµ¨ ñõÂê ¥ ù¢‘Ôµ¨ ‘“ –‘±™
( ne )n
√ √
n! = ( ne )n 2πn =⇒ log n! = log( ne )n + log 2πn

=⇒ log n! = n log ne + log 2πn

=⇒ log n! = n log n − log en + log 2πn

=⇒ log n! = n log n + log e2nπn
=⇒ log n! ∈ Θ(n log n)

: ôø¢ ù¤

.log n!∈ Ω(n log n) ø log n! ∈ O(nlog n) ݃þ‘Þ÷ –‘±™ Àþ‘“ ‘¹€þ ¤¢

logn! = logn + log(n − 1) + . . . + log 1 ≤ logn + logn + . . . + logn ≤ nlogn


| {z }
n
=⇒ logn! ∈ O(nlogn)
log n! = log n + log(n − 1) + . . . + log 1 ≥ n2 log n2 =⇒
log n! ≥ n2 log n − n2 log 2 ≥ 14 n log n =⇒ logn! ∈ Ω(nlogn)
=⇒ log n! ∈ Θ(n log n)
: ô¨ ù¤

: Àƒþ‘õÂê ´ì¢ Âþ ¥ ýø‘Æ— ‚“

(n!)2 = (1 × 2 × . . . × (n − 1)n)2 ⇒
Q
n
(n!)2 = (1 × 2 × . . . × (n − 1)n)(n(n − 1) × . . . × 1) = x(n − x + 1)
 x=1

 y = −x + (n + 1)x
 2
1 =⇒ y (n+1)2
x = n+ max =
 x = 1 ⇒ y = n, x = n ⇒4 y = n =⇒ y
 2
min = n
5 ‚ƒÌì À€ –‘±™ .7.1

(n + 1)2 n + 1 2n
n
Y n
Y
n ≤ (n!)2 ≤ =⇒ nn ≤ (n!)2 ≤ ( ) =⇒
x=1 x=1
4 2
n+1 n
nlogn ≤ 2logn! ≤ 2nlog ≤ 2nlogn =⇒ logn ≤ logn! ≤ nlogn
2 2
=⇒ log n! ∈ θ(n log n)

‚ƒÌì À€ –‘±™ 7.1


: ‚ƒÌì
: Ýþ ¤¢ o(f (n)) ýÂ“

o(f (n)) ⊆ O(f (n))\Ω(f (n))


o(f (n)) ⊆ O(f (n))\Θ(f (n))
:ñø ´ÞÆì ö‘û“

ü óø g(n) ∈ O(f (n)) Ý ƒ û¢ ü õ ö‘ È ÷ g(n) ∈ o(f (n)) Ý ƒ þ‘ Þ ÷ ü õ ­Â ê

. ´¨ g(n) ∈ O(f (n)) ݃û¢ üõ ö‘È÷ Àµ“. g(n) ∈


/ Ω(f (n))
ß þ ¤¢ ´ ¨ g(n) ∈ o(f (n)) ö  ù‘ Ú ÷ ´ ¨ c= 5 ‚ î Ý ƒ € î ü õ ­Â ê
´ ¨ g(n) ∈ O(f (n)) ŕ g(n) ≤ 5f (n) Ý þ ¤¢ n ≥ M1 , M1 × þ ýÂ “ –¤  ¬

À ª‘ “ g(n) ∈ Ω(f (n)) ‚ Ø € þ ­Â ê ‘ “ , g(n) ∈


/ Ω(f (n)) Ý ƒ û¢ ü õ ö‘ È ÷ ñ‘ Ÿ

¥ ø d0 f (n) ≤ g(n), n ≥ M1  û ýÂ “ ‚ î M1 × þÝ þ ¤¢d = d0 × þ ýÂ “ ù‘ Ú ÷


d0
n ≥ M2 ‚ î ¢¤¢ ¢ ›ø ö‘ €  M2c = 2 ýÂ “
×þ Å • g(n) ∈ o(f (n)) ü ê Ï

Ë쑀— ‚î d0 ≤ 0Ýþ ¤¢ n ≥ M AX{M1 , M2 } Âû ýÂ“ ŕ g(n) ≤ d20 f (n)Ýþ ¤¢


. ´¨

(A\B = A\(A ∩ B)):ôø¢ ´ÞÆì ö‘û“

o(f (n)) ⊆ O(f (n))\Ω(f (n)) = O(f (n))\(O(f (n))) ∩ Ω(f (n))
=⇒ o(f (n)) ⊆ O(f (n))\θ(f (n))

.¢ª ÛþÀ±— ýø‘Æ— ‚“À÷— üÞ÷ üÜî ´ó‘Ÿ ¤¢ ë ê ‚ƒÌì Àƒû¢ ö‘È÷ : ñ‘·õ

:۟

‚µª¢ ¤ Âþ ¥ ⓑ— Àƒ€î ­Âê, ݃û¢ üõ ö‘È÷ ¤ ၮõ ßþ ËÖ÷ ñ‘·õ ¥ ù¢‘Ôµ¨ ‘“

: ݃ª‘“
ü±÷‘¹õ ý‘û¢‘Þ÷ üêÂãõ .1 ÛÊê 6


12 ¢Âê ý‘û n
g(n)= n šø ¥ ý‘û n
: Ýþ ¤¢

M = 12 , n ≥ 13 ⇒ g(n) ≤ n =⇒ g(n) ∈ O(n)


: Ýþ ¤¢ ŕ , Àª‘“ g(n) ∈ Ω(n) ݃€î üõ ­Âê ñ‘Ÿ

0 0
∞ ∞ g(n)
g(n) ∈ Ω(n) ⇔ ∃c > ∀ n g(n) ≥ cn ⇔ ∃c > ∀n c ≥n⇔F
(À€µÆƒ÷ ¤¢ öÂî „‘“ ¥ ¢Âê ¢Àä) =⇒ g(n) 6∈ Ω(n)
Àª‘“ ¤Âì“ ‚Ф ßþ Àƒ€î ­Âê, ‚÷ ‘þ ´¨ ¤Âì“ g(n) ∈ o(n) ‚Ф ݃€ƒ±“ ñ‘Ÿ

: Ýþ ¤¢ ŕ

g(n) ∈ o(n) ⇐⇒ ∀c > 0 g(n) ≤ cn, c = 12 =⇒ g(n) ≤ n2 ⇐⇒ F



∀ n
=⇒ g(n) 6∈ o(n)
.¢ª ÛþÀ±— ýø‘Æõ ‚“À÷— üÞ÷ üÜî ´ó‘Ÿ ¤¢ ë ê ‚ƒÌì ¢ª üõ ùÀû‘Èõ ŕ

‚ î ü ó‘ Ÿ ¤¢ À € µ Æ û ýÀ 㠗ø ü ¨‘ Ø ã ÷ Ωø OÀ ƒ û¢ ö‘ È ÷ : ß þ Â Þ —

. À€ª‘“ üÞ÷ ü÷¤‘Ö—

: ۟

: ü¨‘Øã÷ ´ƒ¬‘¡ –‘±™

f (n) ∈ O(f (n)) ⇐⇒ ∃c > 0 ∀ n ∈ N f (n) ≤ 1 × f (n)


: ýÀ㗠´ƒ¬‘¡ –‘±™

‚ î ü þ‘ ¹ ÷ ¥ À ª‘ “ f (n) ∈ O(h(n)) ø g(n) ∈ O(f (n)) À ƒ € î ­Â ê

:ßþÂ“‘€“ g(n) ∈ O(f (n))


g(n) ≤ c1 f (n): Ýþ ¤¢ n ≥ M1 Âû ø M1 > 0 ×þ ø c1 ≥ 0 ×þ ýÂ“

Âû ø M2 > 0 × þ ø c2 ≥ 0 × þ ýÂ “ f (n) ∈ O(h(n)) ýÂ “ ° ƒ — — ß ƒ Þ û ‚ “ø


f (n) ≤ c2 h(n): Ýþ ¤¢ n ≥ M2
: Ýþ ¤¢ n≥M Âû ø M = M AX{M1 , M2 } ýÂ“ ñ‘Ÿ

 g(n) ≤ c1 f (n)
=⇒ g(n) ≤ c1 f (n) ≤ c1 c2 h(n) =⇒ g(n) ≤ ch(n)

f (n) ≤ c2 h(n)

=⇒ g(n) ∈ O(h(n))

.À÷ª üõ –‘±™ °ƒ—— ߃Þû ‚“ Ã÷ Ω ýÂ“ ´ƒ¬‘¡ ø¢ ßþ

: ü÷¤‘Ö— ´ƒ¬‘¡ ßµª¢ ôÀä ýÂ“ ËÖ÷ ñ‘·õ


7 ‚ƒÌì À€ –‘±™ .7.1

Àþ‘“ ‚¹ƒµ÷ ¤¢ n2 ∈ O(n) ݃þ‘Þ÷ üõ ­Âê , n2 ∈


/ O(n)‚î üó‘Ÿ ¤¢ n ∈ O(n2 )
:Ýþ ¤¢ n ≥ M1 , M1 ×þ ø c > 0 ×þ
n2 ≤ cn ⇒ n ≤ c ⇒ (.À÷¤À÷Âî „‘“ ¥ üãƒ±Ï ¢Àä)
. ´¨ ý¥ ¤ Ýû ‚Ф ×þ f ⓐ— ô‘Þ— ‚äÞ¹õ ýø ¤ θÀƒû¢ ö‘È÷ : ßþ Âޗ

: ۟

: Ýþ ¤¢ O, Ω ü¨‘Øã÷ ´ƒ¬‘¡ ßµª¢ Âϑ¡ ‚“

f (n) ∈ O(f (n)) , f (n) ∈ Ω(f (n)) ⇒ f (n) ∈ θ(f (n))


.Àª‘“ üõ ü¨‘Øã÷ ´ƒ¬‘¡ ý¤¢ θ ŕ

: Ýþ ¤¢ ŕ g(n) ∈ θ(f (n)) ݃€î üõ ­Âê Ã÷ ü÷¤‘Ö— ´ƒ¬‘¡ ü¨¤ “ ýÂ“

g(n) ∈ θ(f (n)) =⇒ g(n) ∈ O(f (n)), g(n) ∈ Ω(f (n)) =⇒ f (n) ∈ Ω(g(n)),
f (n) ∈ O(g(n)) =⇒ f (n) ∈ Ω(g(n)) ∩ Og(n)) =⇒ f (n) ∈ θ(g(n))
.Àª‘“ üõÃ÷ ü÷¤‘Ö— ´ƒ¬‘¡ ý¤¢ θ Àû¢ üõ ö‘È÷ ‚î

: Ýþ ¤¢ O, Ω ýÀ㗠´ƒ¬‘¡ ßµª¢ Âϑ¡ ‚“ ߃€»Þû

f (n) ∈ θ(g(n)) , g(n) ∈ θ(h(n)) =⇒


f (n) ∈ O(g(n)) , g(n) ∈ O(h(n)) ⇒ f (n) ∈ O(h(n))
f (n) ∈ Ω(g(n)) , g(n) ∈ Ω(h(n)) ⇒ f (n) ∈ Ω(h(n))
=⇒ f (n) ∈ θ(h(n))

´¨ ýÀ㗠ø ü÷¤‘Ö—, ü¨‘Øã÷ ´ƒ¬‘¡ ‚¨ Âû ý¤¢ θ Àª ùÀû‘Èõ ‚î ¤ Ï ö‘Þû

. ´¨ ý¥ ¤ Ýû ‚Ф ×þ f ⓐ— ô‘Þ— ‚äÞ¹õ ýø ¤ ŕ

: ݃ª‘“ ‚µª¢ Âð : ßþ Âޗ

L= limn→∞ fg(n)
(n)
, f, g : N −→ R +

: Àƒþ‘Þ÷ –‘±™

.f (n) ∈ o(g(n)) ù‘Ú÷ L = 0 ‚»÷‘€ (1


.g(n) ∈ o(f (n)) ù‘Ú÷ L = ∞ ‚»÷‘€ (2
.g(n) ∈ θ(f (n)) ù‘Ú÷ 0 < L < ∞ ‚»÷‘€ (3
: ‚µØ÷
ü±÷‘¹õ ý‘û¢‘Þ÷ üêÂãõ .1 ÛÊê 8

¢¤ õ ¤¢ ü óø ´ ¨ ý¥ ¤ Ý û §… î × þ Â þ¥ ¢ø ¤ ü õ ¤‘ Ø “ ´ ¨¤¢ θ ¢¤ õ ¤¢ ýø‘ Æ — ¢‘ Þ ÷

.Àª‘“ üÞ÷ ´¨¤¢ O, o, Ω, ω

: ñ‘·õ

:Àƒ€î ¢¤ ‘þ ø –‘±™ ¤ Âþ ¥ ý‘û‘䢐

• f (n) ∈ O(g(n)) =⇒ 2f (n) ∈ O(2g(n) )

݃þ‘Þ÷ üõ ¢¤ ¤ ‘䢐 ßþ ËÖ÷ ñ‘·õ‘“ : ۟

2 ) =⇒ 2 ∈ O(2log2 ) =⇒ nn ∈ O(n!)
n n!
n logn2 ∈ O(logn! n log2

:ÂÚþ¢ ËÖ÷ ñ‘·õ ‘þ ø ´¨ Ë쑀— ‚î


2
2 logn2 ∈ O(logn2 ) =⇒ 2 log n
2 ∈ O(2log2 ) =⇒ n2 ∈ O(n)
n

• f (n) ∈ O(g(n)) =⇒ (f (n))k ∈ O(g(n)k )

:۟

‚“ ݃÷— üõ ßþÂ“‘€“ Ýþ ƒð üõ ÂÑ÷ ¤¢ ×þ ¥ µð¤ Ó ¤ gø f ‘û ݵþ ¤ Úó ۃܽ— ¤¢)

(. ´Æƒ÷ ´¨¤¢ Ýþ ƒړ ×þ ¥ µð¤ Ó ü÷‘õ¥ ÂÑ÷ ¥ Âð üóø݃÷‘¨Â“ ö—

f (n) ≤ cg(n) =⇒ (f (n))k ≤ ck (g(n)k ) =⇒ (f (n))k ∈ O(g(n)k )

• f (n) ∈ O(f 2 (n))

:۟

f (n) = n1 =⇒ f 2 (n) = n12 =⇒ f (n) 6∈ O(f 2 (n))


2 ÛÊê

üµÈ𥑓 ý‘û ݵþ ¤ Úó

üµÈ𥑓 ý‘û ݵþ ¤Úóø –„¢‘ãõ .2

üµÈ𥑓 –„¢‘ãõ 1.2


À€ ‘þ ×þ ‚“ ‚µÆ“ø ö ‚Üޛ Âû ‚î ´¨ ý‚ó¢‘ãõ üµÈ𥑓 ý‚ó¢‘ãõ ×þ ¥ ¤ Ñ€õ

ùÀª Óþ Âã— Û±ì ¥ ‚ƒóø ¤ÀÖõ À€ ‘þ ×þ ýÂ“ ùø…ä ‚“ ø ´¨ ö Û±ì ‘õ ý‚Üޛ

. ´¨

‚î Ýþ ¥¢Â•üõ üփ֟ °þÂ® ‘“ ùƒçµõ ×þ üµÈ𥑓 –„¢‘ãõ ۟ ‚“ ´ÞÆì ßþ ¤¢

:À€€î üõ ëÀ¬ Âþ ¥ üõÞä Ûت ¤¢

a0 tn + a1 tn−1 + · · · + ak tn−k = f (n) ai ∈ R, f : N −→ R≥0 , k ∈ N

n  ƒ ç µ õ × þ Í Ö ê ø ´ ¨ ´ “‘ ™ k ,À € µ Æ û É ¿ È õ Û ± ì ¥ t0 , · · · , tk−1 ° þÂ ®

. À€þð ´“‘™ °þÂ® ‘“ üµÈ𥑓 ‚ó¢‘ãõ „‘“ ‚ó¢‘ãõ ‚“ .Ýþ ¤¢

­Âê Àµ“ ¤¢ .݃€î üõ ù¢‘Ôµ¨ ‚Ê¿Èõ ‚ó¢‘ãõ ¥ „Þãõ –„¢‘ãõ ßþ ۟ ýÂ“

‚ó¢‘ãõ ۟ ßþÂ“ ‘€“ ,f (n) = 0 ü€ãþ Àª‘“ ßÚÞû á÷ ¥ üµÈ𥑓 ‚ó¢‘ãõ ݃€î üõ
:Ýþ ¤¢ ¤ Âþ ¥ ´“‘™ °þÂ® ‘“ üµÈ𥑓

a0 tn + a1 tn−1 + · · · + ak tn−k = 0

xi ‚“ ¤ ti ‚Üޛ Âû.݃Æþ÷ üõ Âþ ¥ –¤ ¬ ‚“ ¤ ‚Ê¿Èõ ‚ó¢‘ãõ ‚ó¢‘ãõ ßþ ۟ ýÂ“

:ßþÂ“‘€“ .݃€î üõ ÛþÀ±—


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 10

a0 xn +a1 xn−1 +· · ·+ak xn−k = 0 ⇒ xn−k (a0 xk + a1 xk−1 + · · · + ak ) = 0


| {z }

(ÂÆÔõ) ‚Ê¿Èõ ‚ó¢‘ãõ

. ݃€î üõ É¿Èõ ¤ ‚Ê¿Èõ ý‚ó¢‘ãõ ý‘û ‚Èþ ¤ ų¨

‘û ‚Èþ ¤ ¢¤õ ¤¢ ¶½“ 1.1.2

’› –¤ ¬ ßþ ¤¢Àª‘“ r1 , . . . , rk Ãþ‘Þµõ ø¢ ‚“ ø¢ üփ֟ ‚Èþ ¤ k ý¤¢ ‚ó¢‘ãõ(1


:´¨ Âþ ¥ ôÂê ‚“ ‚ó¢‘ãõ

tn = c1 (r1 )n + c2 (r2 )n + . . . + ck (rk )n

‚Èþ ¤ ñ‘·õ ýÂ“ ,Àª‘“ ý ¤Âؗ ü¡Â“ ñ‘Ÿ ßƒä ¤¢ø üփ֟ ý‘û‚Èþ ¤ ý¤¢ ‚ó¢‘ãõ(2

Àì‘ê üóø ´¨ (x − rp )m ¤ µî‘ê ý¤¢ –¤‘±ä ü€ãþ) Àª‘“ m üþ‘µÆ“ ¥ ý ¤Âؗ rp
m+1
: ´¨ Âþ ¥ ôÂê ‚“ ‚ó¢‘ãõ ’› –¤ ¬ ßþ ¤¢ (´¨(x − rp ) ¤ µî‘ê

tn = c1 (r1 )n + c2 (r2 )n + . . . + cp0 (rp )n + cp1 n(rp )n +

. . . + cpm−1 nm−1 (rp )n + . . . + ct (rt )n

¤ øõ¢ ‚ƒÌì ¥ù¢‘Ôµ¨‘“ ,Àª‘“üփ֟ ƒè ø üõûõ ý‘û ‚Èþ ¤ ý¤¢ ‚ó¢‘ãõ Âð(3

ö— üõ ùÀª ‚µÔð ‚Ê¿Èõ ‚ó¢‘ãõ üփ֟ ý‘û ‚Èþ ¤ ¢¤ õ ¤¢ ‚î ‚ ö‚ƒ±ª ‘fփì¢

.¢Âî ‚±¨‘½õ ¤ ’›

: ñ‘·õ

:Àƒ€î ۟¤ Âþ ¥ üµÈ𥑓 –„¢‘ãõ


n n≤1
g(n) =

5g(n − 1) − 6g(n − 2) else
: ۟

g(n) − 5g(n − 1) + 6g(n − 2) = 0 =⇒ x2 − 5x + 6 = 0


⇒ x1 = 3, x2 = 2 ⇒ g(n) = c1 3n + c2 2n
g(0) = 0, g(1) = 1 ⇒ c1 = 1 , c2 = −1 =⇒ g(n) = 3n − 2n
11 üµÈ𥑓 –„¢‘ãõ .1.2


 2tn−1 − tn−2
tn = t0 = 1

t1 = 3
: ۟

tn − 2tn−1 + tn−2 = 0 =⇒ x2 − 2x + 1 = 0 ⇒ x1 = x2 = 1
⇒ tn = c1 + c2 n t0 = 1 , t1 = 3 ⇒ c1 = 1, c2 = 2 =⇒ tn = 1 + 2n

 2tn−1 + 1
tn = t0 = 0

t1 = 1
: ۟

tn = 2tn−1 + 1 , tn−1 = 2tn−2 + 1 ⇒ tn − tn−1 = 2tn−1 − 2tn−2


⇒ tn − 3tn−1 + 2tn−2 = 0 ⇒ x2 − 3x + 2 = 0
⇒ x1 = 1, x2 = 2 , t0 = 0, t1 = 1 =⇒ tn = 2n − 1

:´“‘™ °þÂ®‘“ ßÚÞûƒè üµÈ𥑓 –„¢‘ãõ ۟

¤À€€î üõ ëÀ¬ Âþ ¥ üõÞä Ûت ¤¢ ‚î ´“‘™ °þÂ®‘“ ßÚÞûƒè üµÈ𥑓 –„¢‘ãõ

: Àþ ƒړ ÂÑ÷ ¤¢

a0 tn + a1 tn−1 + a2 tn−2 + . . . + ak tn−k =

bn1 p1 (n) + bn2 p2 (n) + . . . + bnm pm (n)

( .À€µÆû ´“‘™ ø üփ֟ ¢À䐑û ai , bj ø À€µÆû di ‚›¤¢ ¥ ý‚Üޛ À€ pi (n) )

: ݃Æþ÷ üõ Âþ ¥ –¤ ¬ ‚“ ¤ ‚Ê¿Èõ ‚ó¢‘ãõ –„¢‘ãõ ¥ ‚µ¨¢ ßþ ýÂ“

(a0 xk + a1 xk−1 + · · · + ak )(x − b1 )d1 +1 (x − b2 )d2 +1 · · · (x − bm )dm +1 = 0

‘“ ‚ó¢‘ãõ ý‘û ‚Èþ ¤ ‚±¨‘½õ ¥ ŕ .݃€î üõ ‚±¨‘½õ ¤ ‚ó¢‘ãõ ý‘û ‚Èþ ¤ ų¨

üõÞä ’› ö— üõ Àª ‚µÔð ßÚÞû –„¢‘ãõ ¢¤ õ ¤¢ ‚»÷ ‚“‘Èõ ‘f Öƒì¢ üªø ¤

.¢Âî ‚±¨‘½õ ¤ ‚ó¢‘ãõ

:Ýþ ¤¢ ¤ Âþ ¥ ý‚ó¢‘ãõ ñ‘·õ ýÂ“

tn − 7tn−1 + 12tn−2 = 2n + n + 3n (n + 1)
: Ýþ ¤ ø üõ ´¨À“ ¤ ‚Ê¿Èõ ý‚ó¢‘ãõ

(x2 − 7x + 12)(x − 2)0+1 (x − 1)1+1 (x − 3)1+1 = 0


⇒ (x − 3)(x − 4)(x − 2)(x − 1)2 (x − 3)2 = 0
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 12

⇒ r1 = 1, r2 = 1, r3 = 2, r4 = 3, r5 = 3, r6 = 3, r7 = 4
⇒ tn = c1 (1)n + c2 n(1)n + c3 (2)n + c4 (3)n + c5 n(3)n + c6 n2 (3)n + c7 (4)n
: ñ‘·õ

:Àƒ€î ۟ ¤ Âþ ¥ üµÈ𥑓 ‚ó¢‘ãõ


1 n=1
t(n) =
2t( n2 ) + n o.w

:۟

. ݃€î üõ ۟ Âþ ¥ ƒçµõ ƒƒç— ¥ ù¢‘Ôµ¨ ‘“ ¤ ñ‘·õ ßþ

n = 2k ⇒ t(n) = t(2k )
t(2k ) = 2t( 22 ) + 2k = 2t(2k−1 ) + 2k
k

g(k) = t(n)
⇒ g(k) = 2g(k − 1) + 2k ⇒ (x − 2)(x − 2) = 0 ⇒ r1 = 2, r2 = 2
=⇒ g(k) = c1 2k + c2 k 2k 2k = n =⇒ log2 n = k
t(n) = c1 n + c2 n logn2
t(1) = c1 = 1 t(2) = 2t(1) + 2 = 4 = 2c1 + 2c2 ⇒ c2 = 1
=⇒ t(n) = n + n logn2

: ñ‘·õ

:Àƒ€î ۟ ¤ Âþ ¥ üµÈ𥑓 ‚ó¢‘ãõ



 1 n=1
3
2 n=2
T (n) =


3 n 1 n 1
2T(2) − 2T(4) − n o.w

:۟

: Ýþ ¤¢ ƒçµõ ƒƒç— ¥ ù¢‘Ôµ¨ ‘“ ¥‘“

n = 2k =⇒ T (2k ) = 32 T (2k−1 ) − 12 T (2k−2 ) − 21k =⇒


g(k) = 32 g(k − 1) − 12 g(k − 2) − ( 12 )k =⇒ (x2 − 32 x − 12 )(x − 12 ) = 0⇒
r1 = r2 = 12 , r3 = 1 =⇒ g(k) = c1 + c2 ( 12 )k + c3 k( 12 )k =⇒
13 üµÈ𥑓 –„¢‘ãõ .1.2


 c1 + c2 = 1
T (n) = c1 + c2 n1 + c3 lgnn , c1 + c22 + c23 = 32

c1 + c42 + c23 = 32

=⇒ T (n) = 1 + lg n
n

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ : ñ‘·õ


t(n) + nt(n − 1) = 2n!
t(0) = 1

:۟

t(n−1)
t(n) + nt(n − 1) = 2n! =⇒ t(n)
n! + (n−1)!
=2

t(n)
n! = g(n)

g(n) + g(n − 1) = 2 =⇒ (x − 1)(x + 1) = 0


=⇒ r1 = −1, r2 = 1 =⇒ g(n) = c1 (−1)n + c2 (1)n
t(n) = −21 n!(−1)n + 32 n!

: ñ‘·õ

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ

 a n=0

b n=1
T (n) =


 1+T (n−1)
T (n−2)
o.w

: ۟

T (0) = a T (1) = b T (2) = 1+b


a T (3) = 1+a+b
ab T (4) = a+1
b
T (5) = a T (6) = b T (7) = 1+b
a =⇒ T (n) = T (n mod 5) n>4

: ñ‘·õ

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 14

 1
 4−t(n− 1) n>1
t(n) =

1 n=1

:۟

t(2) = 13 , t(3) = 11
3 , t(4) = 11 =⇒ t =
41 n
pn
qn

= t(n + 1) = 4qnq−p =⇒ qn+1 = 4qn − pn


pn+1 n
qn+1 n

pn+1 = qn =⇒ pn = qn−1

qn+1 = 4qn − qn−1 =⇒ (x2 − 4x + 1) = 0 =⇒ x = 2 ± 3
√ √
=⇒ qn = c1 (2 + 3)n + c2 (2 − 3)n q2 = 3, q3 = 11

üµÈ𥑓 ý‘û ݵþ ¤Úó 2.2


ù¢‘ƒ• ¤ üþÂÖµ¨ ¤‘µ¡‘¨ ‚î ¢¤¢ ¢›ø ö‘Øõ ßþ üÆþ÷ ‚õ‘÷“ ý‘û ö‘“ ¥ ü¡Â“ ¤¢

‘þ ×þ ýÂ“ ü—‘õÀÖõ ۟ ù¤ ×þ ‚î ´¨ ö üþÂÖµ¨ ¤‘µ¡‘¨ ¥ ¤ Ñ€õ ,݃€î ý¥‘¨

‚µª¢ üþÂÖµ¨ ‚Ф ×þ ùø…ä ‚“ øÀª‘“ É¿Èõ Û±ì ¥ ‚ó‘bÆõ ¥ ‚ƒóø ´ƒã®ø À€

‚Ø÷ ü€ãþ Àþ‘Þ÷ ͱ—Âõ ö Û±ì‘õ ý‚Üޛ À€ ‘þ ×þ‚“ ¤ ƒ¡ ‚Üޛ À÷µ“ ‚î ݃ª‘“

‚ ã ¨ — ¤ Û Ÿ ù¤ ß þ Ý ƒ ÷ µ “ ¢ ª ‚ µ Æ ÷¢  ƒ ¡ ý‚ Ü ŸÂ õ ¥ Û ± ì ‘ — ‚ ó‘b Æ õ Û Ÿ ù¤  ð

.݃“‘þ ´¨¢ ‚ÜŸÂõ ßþ ¤¢ ۟ ù¤ ×þ ‚“ø ݃û¢


15 üµÈ𥑓 ý‘û ݵþ ¤Úó .2.2

:ý÷‘û ý‘û šÂ“ ‚ó‘bÆõ


¥ üØþ ýø ¤ ,À÷ ùÀª ý ¤Áð ù¤‘Þª 3ø2ø1 ý‘û ù¤‘Þª ‚“ ‚܃õ ‚¨ Àƒ€î ­Âê

‚ î À ÷ ‚ µ ê 𠤐 ì ý ¤  Ï ô i‚ Ü ƒ õ ýø ¤ × Æ þ¢ n ß þ ,¢¤¢ ¤Â ì × Æ þ¢ n ‘û ‚܃õ

ßþ ‚î ´¨ ö éÀû .´¨ ‚µêÂÚ÷ ¤Âì µ؝î ×Æþ¢ ýø ¤ ý µð¤ Ó ×Æþ¢ ÃðÂû

ùÀ ÷‘ Þ ƒ ì‘ “ ü Ø Þ î ‚ Ü ƒ õ ¥ ù¢‘ Ô µ ¨ ‘ “ ¤‘ î ß þ ‚ î ¢Â î Û Ö µ € õ ô j‚܃õ ‚“ ¤ × Æ þ¢ n
¤‘“ Âû ø ¢Âƒð üÞ÷¤Âì µ؝î ×Æþ¢ ýø ¤ µð¤ Ó ×Æþ¢ ÃðÂû ø ¢Âƒð üõ –¤ ¬

.¢Èƒõ ‘¹“‘› ×Æþ¢ ×þ ‘ú€—

i −→ j i + j + x = 1 + 2 + 3 −→ x = 6 − (i + j)

j 6-(i+j)
i

void hanoi(int n,int i,int j)


{
if(n > 0) {
hanoi(n-1,i,6-(i+j)) ;
cout  i  ”→”  j ;
hanoi(n-1,6-(i+j),j);
}
}

T (0) = 0
T (n) = T (n − 1) + 1 + T (n − 1) =⇒
T (n) = 2 T (n − 1) + 1 ⇒ T (n) = 2n − 1
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 16

:ßþ Âޗ

öµ÷ ¤ üØÆþ¢ ºƒû ý÷‘û šÂ“ ‚ó‘b Æõ ¤¢ ö ‚܃¨ø ‚“ ‚î ÀƒÆþ€“ üÞµþ ¤ Úó

¢À 㠗 ‚ î ü µ È ð¥‘ “ ‚ Î “¤) ¢Â î Û Ö µ € õ Å Ø ã ó‘ “ ‘ þ ô j ‚ Ü ƒ õ ‚ “ ô i ‚ Ü ƒ õ ¥ ‘f Þ ƒ Ö µ Æ õ

(.Àƒ€î ‚±¨‘½õ Àû¢ üõ ¤ ô j ‚܃õ ‚“ ×Æþ¢ n ñ‘Öµ÷ ýÂ“ ô¥„ ý‘û ´îŸ

: ۟

Void Hanoi (int i , int j)


{
if (n > 0)
{
Hanoi(n-1, i , j);
cout i  ” → ”  6 − (i + j);
Hanoi(n-1, j , i);
cout 6 − (i + j)  ” → ”  j;
Hanoi(n-1, i , j);
}
}


 T ( 0) = 0
T ( 1) = 2 ⇒ T (n) = 3n − 1

T (n) = 3 T (n − 1) + 2

: ñ‘·õ

¤ ¤÷ ßþ ý‘û ‚÷‘¡ ¥ ü¡Â“ ݃û¡ üõ ‚î Ýþ ¤¢ n ñÏ ‚“ ý ¤÷ Àƒ€î ­Âê

–¤ ¬ ßþ¤¢ .À€ª‘±÷ Ýû ¤ ø‘¹õ ýÀƒÔ¨ ‚÷‘¡ ‚¨ ºƒû ‚î ݃€î ýÃõ Ù÷¤ ý ¤ Ï

‚“ ¤ À€î üõ ¢‘¹þ ¤ ¢ÊÖõ ßþ ýÂ“ ßØÞõ ý‘û ´ó‘Ÿ ô‘Þ— ‚î üµÈ𥑓 ݵþ ¤ Úó

‚“ ¢ÊÖõ ßþ ýÂ“ ¤ ßØÞõ –„‘Ÿ ¢À㗠‚î üµÈ𥑓 ‚Ф ߃€»Þû .Àþ ¤ ø ´¨¢

Ù÷¤ ‘“ ø À÷ Ù÷¤ ÀƒÔ¨ ‚÷‘¡ n ‚î ´¨ ö ­Âê).Àþ ¤ ø ´¨À“ Ã÷ ¤Àû¢ üõ ‘õ

(.݃€î ýÃõ Ù÷¤ ݃û¡ üõ ù‘ƒ¨

:۟
17 üµÈ𥑓 ý‘û ݵþ ¤Úó .2.2

a
a[1] a[2] ............. a[n]

ù‘ƒ¨ a[3]

ù‘ƒ¨ a[2]
ÀƒÔ¨ a[3]
a[1] ù‘ƒ¨ a[3]
ÀƒÔ¨ a[2]
ÀƒÔ¨ a[3] ù‘ƒ¨

Void Coloring (int n){


if (n == 1){
printf(’◦’); printf(’•’);}
else if (n == 2) {
printf(’◦◦’); printf(’◦•’); printf(’•◦’); printf(’••’); }
else if (n == 3) {
printf(’◦••’); printf(’•◦•’); printf(’••◦’); printf(’◦◦•’);
printf(’◦ • ◦’); printf(’• ◦ ◦’); printf(’• • •’); }
else {
•.Coloring(n-1);
◦•.Coloring(n-2);
◦ ◦ •.Coloring(n-3);
}
}
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 18





2 n=1
4 n=2
a(n) =



7 n=3
1 × a(n − 1) + 1 × a(n − 2) + 1 × a(n − 3) else

: ñ‘·õ

ÝÞþ Ãî‘õ‘û‚Æþ‘Öõ ¢À㗠ÛìÀŸ ‘“ ݃û¡ üõ.Ýþ ¤¢ ¤‘ƒµ¡ ¤¢ n ñÏ ‚“ ý ‚þ¤

¢À 㠗 Û ìÀ Ÿ ‚ î À ƒ û¢ ‚ Ž¤ ¤ ü µ È ð¥‘ “ ‚ Î “¤ . Ý þ ¤ ø ´ ¨À “ ¤ ‚ þ¤ Ý Þ ƒ ÷ ü õ ø

.À€î ‚±¨‘½õ ¤ ‘û‚Æþ‘Öõ

: ۟

ö— üõ ŕ ù¢Þ÷ À› ¤ ‚þ¤ ‚÷‘¡ ø¢ ݃÷— üõ Âþ ¥ Ûت Փ‘ÎõÀª‘“ šø ¥ n Âð

ø Ýþ ¤¢¥‘ƒ÷ ñø ‚÷‘¡ø¢ Ýރ÷ üõ ø ÝÞþ Ãî‘õ ö¢¤ ø ´¨À“ ýÂ“ ‚Æþ‘Öõ ×þ ‚î ´Ôð

ø¢ Ýރ÷ üõ ‘“ ¤ ¢Àä n-2 Ýރ÷ üõ ø ñø ¢Àä ø¢ ÝÞþ Ãî‘õ‘“ ¤ ¢Àä n-2 ÝÞþ Ãî‘õ Àþ‘“
.
1 ´¨ ¥‘ƒ÷ ‚Æþ‘Öõ ‚¨ ‘fãޛ ŕ. ݃€î ‚Æþ‘Öõ ñø ¢Àä

| {z } | {z }
2 n−2

: Ýþ ¤¢ ŕ


1 n=2
T (n) = =⇒ T (n) = 32 n − 2

T (n − 2) + 3 n = 2k

ùÀ ÷‘ õü ì‘ “ ý‚ ÷‘ ¡ n-1 Å • ù¢ Þ ÷ À › ¤ ‚ þ¤ ý‚ ÷‘ ¡ × þ À ª‘ “ ¢Â ê n  ð ø

‚ “‚ ÷‘ ¡ n-1 ß þ ýÂ “ ¤ ‘ û ‚ Æ þ‘ Ö õ ¢À 㠗 Û ± ì ý‚ Î “¤ Õ “‘ Î õ ø ´ ¨ šø ¥ ý¢À ä

߃“ üþ‘ú÷ Ýރ÷ üõø ÝÞþ Ãî‘õ ö¢Þ÷ Àƒ• ýÂ“ Ã÷ ‚Æþ‘Öõ ø¢ ų¨, Ýþ ¤ ø üõ ´¨¢

( 32 (n − 1) − 2) + 2‘fãޛ ŕ, Ýþ ¤¢ ¥‘ƒ÷‚÷‘¡ n-1 Ýރ÷ üõø ÝÞþ Ãî‘õ ø ñø ý‚÷‘¡
: Ýþ ¤¢ Ûî ¤¢ ŕ. ´¨ ¥‘ƒ÷¢Âê n ýÂ“ ‚Æþ‘Öõ

2T ( n2 ) ù‘Ú÷ , ݃€î ÓÊ÷ ¤ ‚þ¤ ݃÷— üõÀª‘“ šø ¥ n Âð) 1


‚Æþ‘Öõ ø¢ ø Ýþ ¤¢ ‚Æþ‘Öõ
ö‘Þû ‚î. Ýþ ¤¢ ´ó‘Ÿ T (n) = 2T ( n 2 ) + 2 Ûî ¤¢ ŕ . ùø Âð Âû Ýރ÷ üõ ø ÝÞþÃî‘õ ýÂ“
(.Àû¢ üõ ‘õ ‚“ ¤ T (n) = 32 n − 2
19 üµÈ𥑓 ý‘û ݵþ ¤Úó .2.2



 0 n=1



T (n) = 3 (n − 1) n = 2k + 1
 2



3
2n − 2 n = 2k

: AVL ´¡¤¢
1 ùÂð Âû ´¨¤ ø ² ´¡¤¢ Âþ ¥ á‘Ô—¤ 酵¡ ‚î üŽø¢ø¢ ý¹µÆ› ´¡¤¢

ùÂð ö— üõ O(log n)‘“ ‚Æþ‘Öõ ýÂ“ ŕ . ´¨ log n ¥´¡¤¢ ßþ á‘Ô—¤ . Àª‘“

. ¢Âî ´þ Ãþø ¤ ‘û

´ ¨ ° Æ Â “ ý¤¢ ö ù ð  û ‚ î ü Žø¢ø¢ ´ ¡¤¢ : ü Žø¢ø¢ ý ¹ µ Æ › ´ ¡¤¢

. Àª‘“ ´¨¤ À÷¥ Âê ýø‘Æõ µ؝î ² À÷¥ Âê ‚Øþ ¤ Î“

: ñ‘·õ

‘õ ‚“ ¤ 4 á‘Ô—¤ ‚“ AVL ´¡¤¢ ×þ ý‘û ùÂ𠢐À㗠ÛìÀŸ ‚î üµÈ𥑓 ‚Ф

? ´¨ ôÀî ÀûÀ“

: ۟

 þ ¥ á‘ Ô —¤ ø n-1 , n á‘ Ô —¤ ‘ “ AVL ´ ¡¤¢ ´ ¨¤ ´ Þ ¨ ´ ¡¤¢  þ ¥ á‘ Ô —¤

. ´¨ n-2 ² ´Þ¨ ´¡¤¢

T(n-2) T(n-1)
T(0) T(1) T(2) T(3) T(n)





1 n=0
2 n=1
T (n) =



4 n=2
T (n − 1) + T (n − 2) + 1 o.w
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 20

ý ‚ Î “¤ T (n) = T (n − 2) + T (n − 1) + 1 ‚ ó‘ ± ÷¢ ø ü ‘ ÷ ± ƒ ê ‚ ó‘ ± ÷¢ ß ƒ “

:´¨ ¤Âì“ Âþ ¥ –¤ Ê“

1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
1 , 2 , 4 , 7 , 12 , 20 , 33 , 54
T (n) = f (n + 2) + (−1)
f (n) = f (n − 1) + f (n − 2) f (0) = 1 , f (1) = 1

. Àƒ€î ۟ ¤ Âþ¥ ÍþÂª ‘“ü‘÷±ƒê ‚ó¢‘ãõ

f (n) = f (n − 1) + f (n − 2) f (0) = 0, f (1) = 1


√ √
x2 − x − 1 = 0 ⇒ r1 = 1+2 5 , r2 = 1−2 5
√ √
f (n) = c1 ( 1+2 5 )n + c2 ( 1−2 5 )n
√ √
⇒ f (n) = √15 ( 1+2 5 )n − √15 ( 1−2 5 )n

: AVL ´¡¤¢ ¤¢ «‘¡ ýùÂð ´þ Ãþø ýÂ“ ‚Æþ‘Öõ ·îÀŸ


1 1 + 5 h+2
n ≥ T (h) = f (h + 2) − 1 =⇒ n ≥ √ ( ) − 1 =⇒
5 2

n+1 ≥ √ (
1 1 + 5 h+2
) =⇒ log(n+
1
1) ≥ log 5 +(h + 2) =⇒ √

5 2 ϕ ϕ

1 √
h + 2 ≤ log(n+ 1) − log 5 =⇒ h ∈ O(log 5(n+1) ) =⇒ h ∈ O(logn )

ϕ ϕ ϕ 2

‘þ

1 √
1) (n+1) (n+1)
h + 2 ≤ log(n+ × log2ϕ + logϕ 5 = 1.44 log2 +1.67

ϕ − logϕ 5 = log2
(n+1)
=⇒ h ≤ 1.44 log2 −0.33 =⇒ h ∈ O(logn
2)

logn2 ü€ãþ ´¡¤¢ á‘Ô—¤ ù¥À÷ ‚“ ùÂð ×þ ´þ Ãþø ýÂ“ ‚Æþ‘Öõ ·îÀŸ ßþÂ“‘€“

. ´¨
21 üµÈ𥑓 ý‘û ݵþ ¤Úó .2.2

: ñ‘·õ

‚±¨‘½õ ÀûÀ“ ‘õ ‚“ ¤ Ãþ‘Þµõ ÀƒÜî n ‘“ üŽø¢ø¢ ý¹µÆ› ý‘úµ¡¤¢ ¢À㗠‚î ý‚Ф


. Àƒ€î

(c1 < c2 < · · · < cn Ãþ‘Þµõ ø¢ ‚“ ø¢ ÀƒÜî n)

ck
c1 < c2 < ... < ck−1 ck+1 < ... < cn

T (k − 1) T (n − k)

T (1) = 1 T (2) = 2

T (n) =
Pn 1 2n 
T (k − 1) × T (n − k) = n+ (ö„‘—‘î ¢Àä)
k=1 1 n
: ñ‘·õ

À€ ‚“ . ݃€î ‚±¨‘½õ ¤ A1 , A2 , · · · , An Åþ —‘õ n ’ÂÌܬ‘Ÿ ݃û¡ üõ

? ¢Âî ý ¤Áðõ÷Â• ’® ´ú› ¤ ‘û Åþ —‘õ ßþ ö— üõ Õþ ÂÏ

: ۟

T ( 0) = 0
T ( 1) = 1 (A)
T ( 2) = 1 (AB)
T ( 3) = 2 A(BC), (AB)C
T ( 4) = 5 A(BCD), (AB)(CD), (ABC)D
⇒ T (4) = T (1).T (3) + T (2)T (2) + T (3).T (1)
..
.
(A1 · · · Am ) (Am+1 · · · An ) ⇒
| {z }| {z }
m n−m

X1  
T (n) =
n−
1 2n − 2
T (m)T (n − m) =
n n−1
m=1
:ßþ Âޗ

.Àƒþ‘Þ÷ –‘±™ Àóõ ⓑ— ¥ ù¢‘Ôµ¨ ‘“ ¤ Û±ì ñ‘·õ ø¢ ý ‚Ф ø¢

:ñ‘·õ
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 22

¤¢ ùÀª Âû‘Ò ý‘û ÂÔ¬ ¢À㗠‚î ´¨ ±µãõ ü÷‘õ¥ n ñÏ ‚“ 10 ý‘€±õ ¤¢ ýÀî

¤ n ñÏ ‚“ ±µãõ ý‘û Àî Ûî ¢À㗠‚î ÀƒÆþ€“ üµÈ𥑓 ‚Ф ×þ Àª‘“ šø ¥ ö

.Àþ‘Þ÷ ‚±¨‘½õ

:۟

n ñÏ ‚“ ±µãõ ý‘ûÀî ô‘Þ— ¢À㗠= a(n)


n-1 ñÏ ‚“ ±µãõ ý‘ûÀî ô‘Þ— ¢À㗠= a(n − 1)
(.Àª‘±÷ ÂÔ¬ ñø ‚÷‘¡ ‚î üµó‘Ÿ ¤¢)

´ó‘Ÿ 9 a(n − 1)
z }| {z }| {
1,2,...,9

.´¨ ¢Âê Çþ‘û ÂÔ¬ ¢À㗠‚î n-1 ñÏ ‚“ üþ‘û ‚µª¤ ô‘Þ— ¢À㗠= b(n − 1)
(.Àª‘“ ÂÔ¬ ñø ‚÷‘¡ ‚î üµó‘Ÿ ¤¢)

b(n − 1)
z }| {
0

=⇒ a(n) = 9a(n − 1) + b(n − 1)

1 2 n−1
10 10 ......... 10
(n-1) ñÏ ‚“ ‘û ‚µª¤ ô‘Þ— ¢Àã—
= a(n-1)+b(n-1)=10n−1

a(n − 1) + b(n − 1) = 10n−1 ⇒ b(n − 1) = 10n−1 − a(n − 1)


=⇒ a(n) = 9a(n − 1) + 10n−1 − a(n − 1) = 8a(n − 1) + 10n−1

: ñ‘·õ

¢¤ ø ´¨À“ Í¡ n ‘“ ö— üõ ‚î ¤ üŸ÷ ÝÞþ Ãî‘õ ‚î ÀƒÆþ €“ üµÈ𥑓 ‚Ф

.Àû¢ ö‘È÷
23 üµÈ𥑓 ý‘û ݵþ ¤Úó .2.2

: ۟

1
T(1)=2
2

1
T(2)=4
2 3
4

1 4
3 T(3)=7
2
5 6
7

T (1) = 2, T (2) = 4, T (3) = 7, T (4) = 11, . . .

ôn Í¡ ö¢Âî ‚ê ‘® ýÂ“ ŕ. ¢Âî ¢‘¹þ ö— üõ ‚ƒŸ‘÷ T (n − 1) Í¡ n − 1‘“
‚ ¹ ƒ µ ÷ ¤¢ Å • ¢ ª ü õ ‚ ê‘ ® À þÀ › ‚ ƒ Ÿ‘ ÷ n ‚ î ¢Â î â Î ì ¤ Û ± ì Í ¡ n − 1À þ‘ “
.´ª¢ ݃û¡ ‚ƒŸ‘÷ T (n) = T (n − 1) + n

n(n + 1)
T (n) = T (n − 1) + n =⇒ T (n) = +1
2

: ñ‘·õ

¢Àä ßþ ö— üõ Õþ ÂÏ À€ ‚“ Ýþù¢Âî ´ê‘þ ¤¢ ¤ n ≥ 2 üãƒ±Ï ¢Àä Àƒ€î ­Âê

?¢Âî ¥Âê 2 ýø‘Æõ µð¤ Ó üãƒ±Ï ¢Àä áÞ¹õ ‚“ ¤ üヱÏ


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 24

: ۟

T ( 2) = 1 : 2
T ( 3) = 1 : 3
T ( 4) = 2 : 2 + 2, 4
T ( 5) = 3 : 2 + 3, 3 + 2, 5
T ( 6) = 5 : 2 + 2 + 2, 2 + 4, 3 + 3, 4 + 2, 6
‚ê‘® ¤ " 2+ ' –¤‘±ä ô k − 2 ¢Àä ý‘û¥Âê ‚“ Àþ‘“ üãƒ±Ï ¢Àä ߃õ k ýÂ“

: ݃€î ‚ê‘® ÀŸø ×þ , ¢Àä ߃õ k − 1 ý‘û¥Âê ñø ý‘ûÀ÷ø âޛ ‚“ ø ݃€î

T (5) : {2 + (3)}, {(2 + 1) + 2}, {(4 + 1)}


| {z } | {z }
3+2 5

T ( 6) : {2+(2+2)}, {2+(4)}, {(2 + 1) + 3}, {(3 + 1) + 2}, {(5 + 1)}


| {z } | {z } | {z }
3+3 4+2 6

:Ýþ ¤¢ ‚¹ƒµ÷ ¤¢



1 n=2
T(n)=

1 n=3
T (n − 1) + T (n − 2) o.w

: ñ‘·õ

×þ ø¢ ‘þ ü󐁵õ ÂÔ¬ ø¢ ‚î ´¨ ±µãõ ü÷‘õ¥ 0,1,2 ô‘줐 Ûõ‘ª n ñÏ ‚“ ý ‚µª¤

¤ n ñÏ ‚“ ±µãõ ý‘û ‚µª¤ ¢À㗠‚î ÀƒÆþ€“ üµÈ𥑓 ‚Ф ,Àª‘“ ‚µªÀ÷ ü󐁵õ

.Àû¢ ö‘È÷

: ’›

.n ñÏ ‚“ ±µãõ ý‘ûÀî ô‘Þ— : αn


.À÷ª áø ª ×þ ‘“ ‚î n ñÏ ‚“ ±µãõ ý‘ûÀî ô‘Þ— : an
.À÷ª áø ª ÂÔ¬ ‘“ ‚î n ñÏ ‚“ ±µãõ ý‘ûÀî ô‘Þ— : bn
.À÷ª áø ª ø¢ ‘“ ‚î n ñÏ ‚“ ±µãõ ý‘ûÀî ô‘Þ— : cn

αn = an + bn + cn

an = bn−1 + cn−1 , bn = an−1 + cn−1 , cn = an−1 + bn−1 + cn−1

⇒ cn = αn−1 ⇒ cn−1 = αn−2

=⇒ αn = an + bn + cn = 2an−1 + 2bn−1 + 3cn−1 =


25 üµÈ𥑓 ý‘û ݵþ ¤Úó .2.2

2(an−1 + bn−1 + cn−1 ) + cn−1 = 2αn−1 + cn−1


=⇒ αn = 2αn−1 + αn−2

: ñ‘·õ

éÂÏ ‚“ À÷— üõ ÍÖê ‚î ´¨ ‚½Ô¬ ßþ ¤¢ üªõ .Ýþ ¤¢ n×m ‚½Ô¬ ×þ

ý‘ û ƒ Æ õ ô‘ Þ — ‚ î À ƒ Æ þ € “ ¤ ü µ È ð¥‘ “ ‚ Î “¤ .À € î ´  ´ ¨¤ éÂ Ï ‚ “ ‘ þ ø „‘ “

.Àû¢ ‚Ž ¤ ¤ B ÀÊÖõ ‚“ A bÀ±õ ¥ ©õ ´îŸ ýÂ“ ßØÞõ

: ۟

T(n,m-1)
6
c B



 d - T(n-1,m)




n







| {z }
A
m
©õ Àª‘“ m ñøÀ› ý‘ú÷ µ¨ ¢À㗠ø n ñøÀ› ý‘ûÂΨ ¢À㗠Â𐠄‘“ Ûت ¤¢

T (n, m − 1) ´ ó‘ Ÿ ß þ ¤¢ ‚ î ¢¤Á Ú “ c ‚ Î Ö ÷¥ À þ‘ “ ‘ þ B ‚ Î Ö ÷ ‚ “ öÀ ƒ ¨¤ ý Â “

ƒÆõ T (n − 1, m) ´ó‘Ÿ ßþ ¤¢ ‚î À€î ¤ ±ä d ‚ÎÖ÷ ¥ Àþ‘“ ‘þ ø ¢¤¢ ¢›ø ƒÆõ

: Ýþ ¤¢ ‚¹ƒµ÷ ¤¢ .¢¤¢ ¢›ø

T (m, n) = 1 × T (n, m − 1) + T (n − 1, m) × 1 , T (n, 0) = T (0, m) = 0


 
m+n
T (m, n) =
n, m
:ßþ Âޗ

݃þ‘Þ÷ üõ ݃Æ֗ ýø‘Æõ ´ÞÆì n ‚“ ¤ ᅮ , ᅮ„ ýø‘Ƶõ ¶Ü·õ ×þ ¤¢

Àþ ¤ ø ´¨¢ ‚“ üµÈ𥑓 ý‚Ф, ݃þ‘Þ÷ üõ Û¬ø Ýû ‚“ ¤ ‘ú÷ ‘û âÜ® –¥õ ‚“ ø

.Àû¢ ´¨¢ ‚“¤ ùÀª ‚µ¡‘¨ ý‘û ᅮ„ ý¥µõ ô‘Þ— ‚î

: ßþ Âޗ

éÂ Ï ‚ “ ‘ ú € — ‚ î ¢¤¢ ¢ ›ø ‚ ½ Ô ¬ ß þ ¤¢ ý ¤‘ õ .Ý þ ¤¢ ¤‘ ƒ µ ¡ ¤¢ n*n ý ‚ ½ Ô ¬

üµìø ø ù¢Þ÷ ´îŸ ‚“ áø ª ‚½Ô¬ ý„‘“ ¥ ¤‘õ ßþ.À€î üõ ´îŸ ´¨¤ ‘þ ߃þ‘•
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 26

‚“ À÷— üõ ‚î üþ‘úµó‘Ÿ ¢À㗠.À€î üõ ´îŸ ߃þ‘• éÂÏ ‚“ À¨¤ üõ ÂÎì ‚“ ‚î

?Àþ ¤ ø ´¨À“ ¤À¨Â“ ۓ‘Öõ ý‚ÎÖ÷

: ßþ Âޗ

ý‘ û ¶ Ü · õ ‚ “¤ ö ö — ü õ ´ ó‘ Ÿ À €  ‚ “ .Ý þ ¤¢ ¤‘ ƒ µ ¡ ¤¢ ’À ½ õ ü ã Ü ® n ×þ

?¢Âî ¥Âê Óܵ¿õ

:ßþ Âޗ

.Àƒ“‘ƒ“ üµÈ𥑓 ø ý €þ‘“ ©ø ¤ ø¢ ‚“ ¤ ýÌä n ‚äÞ¹õ ×þ ý‘û ‚äÞ¹õ Âþ ¥

 ð Ý þ  ƒ ð ü õ Â Ñ ÷ ¤¢ ´ ƒ “ × þ  Ì ä  û ý¥ ‚ “ ,ý  € þ‘ “ ©ø ¤ ¤¢ : ü þ‘ Þ € û¤

üõ ÂÑ÷ ¤¢ 0 ¤ ö Àª‘±÷ ‚äÞ¹õ Âþ ¥ Ìä Âð ø 1 ¤ ö Àª‘“ ‚äÞ¹õ Âþ ¥ Ìä

.Ýþ ƒð

1 2 3 4
0 0 0 0 {}

0 0 0 1 {4}

0 0 1 0 {3}

¤¢ ø Ý ƒ Æ þ ÷ ü õ ¤‘ “ø¢ ¤ ý Ì ä n-1 ý‘ û ‚ ä Þ ¹ õ  þ ¥ à ƒ ÷ ü µ È ð¥‘ “ ©ø ¤ ¤¢

.݃€î üõ ‚ê‘® ¤ Ìä ߃õ n ôø¢ Óþ¢¤

:ßþ Âޗ

n2 ‘ — 1 ¢À ä ö ¤¢ ‚ î ö ü þø¢‘ › ⠓  õ ø ‚ µ ê 𠐤 n ¢À ä ‚ î À ƒ Æ þ € “ ý ‚ õ‘ ÷ “

.ÀûÀ“ ‘õ ‚“ ¤ À÷ ùÀª ùÀƒ

:ßþ Âޗ

.Àƒþ‘Þ÷ ý¥‘¨ ù¢‘ƒ• ¤ tic tac toe ý¥‘“

(Master Theorem) ü¨‘¨ ‚ƒÌì 3.2


ù‘Ú÷ f : N → R+ , b > 1 , a ≥ 1 ö ¤¢ ‚î T (n) = a T ( nb ) + f (n) Âð

: ´¨ ¤Âì“ Âþ ¥ ¸þ‘µ÷

ù‘ Ú ÷ À ª‘ “ ¤Â 젓  > 0 × þ ýÂ “ f (n) ∈ O(nlog


a
b − )  ð (1
27 (MASTER THEOREM) ü¨‘¨ ‚ƒÌì .3.2

a
.T (n) ∈ Θ(nlog b ) ‚î ¢ª üõ ‚¹ƒµ÷

T (n) ∈ Θ(nlog b (log n)k+1 ) (2


a a
. ù‘Ú÷ Àª‘“ f (n) ∈ Θ(nlog b (log n)k ) Âð

0 1 0 (3
a
× þ ¥ < δ < ,  > × þ ýÂ “ ø f (n) ∈ Ω(nlog b + )  ð

. T (n) ∈ Θ(f (n)) ù‘Ú÷ a f ( nb ) < δ f (n) ݃ª‘“ ‚µª¢ À㓠‚“ üþ‘›

¤Â 젓 „‘ “ ý‚ ¹ ƒ µ ÷ ¥‘ “ ¢Â ƒ 𠤐 ì à ƒ ÷ b nb c ‘ þ d nb e ,
n
b ý‘ › ‚ “  𐠂 µ ± ó)
( . ´¨

:–‘±™
.´¨ ´ÞÆì ø¢ Ûõ‘ª –‘±™

ö —  “Â “ ý‘ û n ýø ¤ T(n) ⠓‘ — ‚ î ù¢‘ ¨ ­Â ê × þ ´ ½ —¤ ‚ ƒ Ì ì ´ Þ Æ ì ß ƒ óø

ÀûÀƒõ ö‘È÷ ´ÞÆì ߃õø¢øÀ€î üõ Ãó‘÷ n = 1, b, b2 , .. ü€ãþ b > 1¥ ¼ƒ½¬ ý‘û
‘ ê ¬ ø À € î À ƒ • ‚ ã ¨ — ü 㠃 ± Ï ´ ± · õ ¢À ä ‚ Þ û ‚ “ À ÷ — ü õ à ƒ ó‘ ÷ ß þ ¤  Î  ‚ î

.¢ø ¤ üõ ¤‘î ‚“ ‚ÜÿÆõ ÓÖ¨ø Óî ü¨¤ “ ýÂ“ ü®‘þ ¤ ý‘ú؃€Ø—

݃þ‘Þ÷ üõ ù¢‘¨ ¤ ­Âê ý ‚ƒÌì –‘±™ ýÂ“ üµìø ݃ª‘“ °ìÂõ Àþ‘“ ‘õ ñ‘Ÿ ßþ ‘“

¤¢ ¤ T(n) À€÷‘õ ü㓑— ñ‘·õ ö€ä ‚“. À÷‘¨Â“ ÍÜè ¸þ‘µ÷ ‚“ ¤ ‘õ Àþ‘±÷ ý¥‘¨ ù¢‘¨ ßþ

üµ÷‘Þ® ºƒû –¤ ¬ ßþ ¤¢, ´¨ n “Â“ ´¨ 2 ¥ üþ‘û ö— n üµìø ‚î Àþ ƒړ ÂÑ÷
:¢Þ÷ Óþ Â㗠–¤ ¬ ßþ ‚“ ¤ T(n) ⓑ— ö— üõ Âþ ¥ , T(n)∈O(n) ‚î ´Æƒ÷


n n=1,2,3,..
T(n)=
n2 otherwise

. T(n)∈O(n2 ) ‚î üó‘Ÿ ¤¢

: ¼ƒ½¬ ý‘û ö— ýÂ“ –‘±™


.´¨Âþ ¥ üµÈ𥑓 ‚Ф Ãó‘÷ ü¨‘¨ ‚ƒÌì –‘±™´ÞÆì ߃óø

T (n) = aT ( nb ) + f (n)
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 28

b ö¢ “ ¼ ƒ ½ ¬ ‚ “ ü õÃ ó‚ î ´ ¨ b> 1¥ ü ½ ƒ ½ ¬ ö — n‚ î ­Â ê ß þ ´ ½ —


üµÈ𥑓 ý ‚Ф ۟ ý‚ó‘b Æõ Ýó ߃óø .¢¢Âð üõ ö‘ƒ“ Ýó ‚¨ ¤¢ Ãó‘÷ ßþ.´Æƒ÷

. À € î ü õ ×  î ‘ Þ Ú ƒ ¨ × þ Û õ‘ ª –¤‘ ± ä × þ ýø‘ Æ — ý‚ ó‘b Æ õ ‚ “¤ ¤  îÁ õ

ýÂ “ ü Ü ± ì Ý óø¢¥ Ý ó ß ƒ õ ¨ øÀ þ‘ Þ ÷ ü õ ß ƒ ƒ 㠗 ¤‘ Þ Ú ƒ ¨ ýù¢øÀ ½ õ Ý ó ß ƒ õø¢

.Àþ‘Þ÷ üõ ù¢‘Ôµ¨ , ´¨ b¥ ü½ƒ½¬ ý‘û ö— n ‚Ø€þ ­Âꑓ ü¨‘¨ ý‚ƒÌì –‘±™

: 1Ýó
ý‘ ûö — ¥ ü Ô € õ‘ ÷ ü 㠓‘ — f (n) ø À € ª‘ “ ´ “‘ ™ ¢À ä ø¢ b> 1ø a ≥ 1 À ƒ € î ­Â ê
üõ Óþ Â㗠Âþ ¥ –¤ ¬ ‚“ b ¼ƒ½¬ ý‘û ö— ýø ¤ ¤ T(n)ⓑ— .Àª‘“ b¥ ü½ƒ½¬

. ݃€î


 θ(1) n=1
T(n)=

aT ( nb ) + f (n) n = bi

. ´¨ üµ±·õ ¼ƒ½¬ ¢Àä i ‚î


: ݃€î üõ ´“‘™ ñ‘Ÿ

logn− 1
X b

T (n) = θ(n loga


b )+ aj f (n/bj ) (1)
j=0

: –‘±™
ø ´ ¨f (n) ©¥ ¤ ý¤¢ ö ý‚ È þ ¤ ‚ î Ý ƒ € î ü õ ù¢‘ Ô µ ¨ ü µ È ð¥‘ “ ´ ¡¤¢ ¥ ‘ õ

ß þ ¥ ôÀ î û .À € µ Æ û f (n/b) ©¥ ¤ ý¤¢ ôÀ î  û ‚ î ,À ª‘ “ ü õ À ÷¥  ê a ý¤¢

‚ È þ ¤ ¥ 2 ‚ Ü ¬‘ ê ‘ “ ù ð a2 ‘ õ Å •, ´ ¨ f (n/b2 ) ©¥ ¤‘ “ À ÷¥  ê aý¤¢ ‘ û À ÷¥  ê

j
ý¤¢ ôÀ î  û ‚ î ¢¤¢ ¢ ›ø ‚ È þ ¤ ¥ j ‚ Ü ¬‘ ê ‘ “ ù ð a ¢À 㠗 ü Ü î ¤  Ï ‚ “ . Ý þ ¤¢

T (1) = Θ(1)
j
 û ø ´ ¨  “Â “ ´ ¡¤¢ ß þ   û ©¥ ¤.À ª‘ “ ü õ f ( nb ) ©¥ ¤

loga n
݃û¡ ï“ n b ¢À㗠ø logb á‘Ô—¤ ‘“ üµ¡¤¢ ŕ.¢¤¢ ¤Âì lognb ÕÞä ¤¢ ï“
. ´ª¢
29 (MASTER THEOREM) ü¨‘¨ ‚ƒÌì .3.2

f(n) - f(n)
6 a

 ? j
f(n/b) f(n/b) f(n/b) af(n/b)
a a a
lognb f (n/b2 ) ................................................................... f (n/b2 ) a2 f (n/b2 )
a a a a a a a a a
.. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
a
?Θ(1)Θ(1)Θ(1) Θ(1)Θ(1)Θ(1)Θ(1)Θ(1)Θ(1)Θ(1) Θ(nlogb )
| {z }
a
nlogb

ý‘û ï“ ©¥ ¤ ø ´¡¤¢ ¥ ¼Î¨ Âû ©¥ ¤ ö¢Âî âޛ ‘“ ¤ (1) ‚Ф ݃÷— üõ ‘õ

j Û·õ ¼Î¨ Âû ©¥ ¤ , ´¨ ùÀª ù¢¢ ö‘È÷ Ûت ¤¢ ‚î ‚÷Ú÷‘Þû. ݃€î ‚±¨‘½õ ö

‚Þû áÞ¹õ ßþÂ“‘€“øÀª‘“ üõ aj f (n/bj ) –¤ ¬ ‚“ ´¨üÜ¡¢ ý‘û ùÂð ý¤¢ ‚î

: ´¨ Âþ ¥ –¤ ¬ ‚“ ‘û ¼Î¨ üÜ¡¢ ý‘û ùÂð

logb n−1
X
aj f (n/bj )
j=0

a
´¨¢ ‚“ (1) ‚Ф °ƒ—— ßþ ‚“. Àª‘“ üõ Θ(nlogb ) “Â“ Ã÷ ‘û ï“ ô‘Þ— ©¥ ¤ø

.Àþ üõ

‚¨ üµÈ𥑓 ´¡¤¢ °ÆŸ “ ‚Ø€þ öø¢Þ÷ ù¤‘ª üÔþ ÂÒ ‚µØ÷ ‚“ Àþ‘“ ´ÞÆì ßþ ¤¢

:Àª‘“ Âþ ¥ ¢¤ õ ‚¨ ‘“ Փ‘Îõ °ƒ—— ‚“ À÷— üõ ü¨‘¨ ý‚ƒÌì ¤¢ ùÀª ù¢Â“ ô‘÷ ¢¤ õ

üÜ¡¢ ý‘û ùÂð ø ‚Èþ ¤ ü€ãþ , Àª‘“ ‚µÆ“ø‘û ï“ ©¥ ¤ ‚“ ´¡¤¢ Ûî ©¥ ¤ (1

.À€ª‘“ ‚µªÀ÷ ü÷À€ ƒ™‘—

. Àª‘“ ùÀª Ç¿• ´¡¤¢ žÎ¨ ¤¢ ýø‘Æõ ‘f±þ Â֗ –¤ ¬‚“ ´¡¤¢ Ûî ©¥ ¤ (2

üÜ¡¢ ý‘û ùÂð ø ‘û ï“ øÀª‘“ ‚µª¢ üڵƓ ‚Èþ ¤ ©¥ ¤ ‚“ ´¡¤¢ Ûî ©¥ ¤ (3

. À€ª‘“ ‚µªÀ÷ ü÷À€ ƒ™‘—

: 2Ýó
ý‘ ûö — ¥ ü Ô € õ‘ ÷ ü 㠓‘ — f (n) ø À € ª‘ “ ´ “‘ ™ ¢À ä ø¢ b> 1ø a ≥ 1 À ƒ € î ­Â ê
: ݃ª‘“ ‚µª¢Âþ ¥ –¤ ¬ ‚“ ¤ g(n) ⓑ— ø .Àª‘“ b¥ ü½ƒ½¬
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 30

logn− 1
Xb

g(n) = aj f (n/bj ) (2)


j=0 : ´ª¢ ݃û¡ ù‘Ú÷

Ý þ ¤¢ ,  > 0 ´ ± · õ ´ “‘ ™  þ¢‘ Ö õ ýÂ “ , f (n) = O(nlogb − )Â


a
ð(1

loga
. g(n) = O(n b )
a a
. g(n) = Θ(nlogb . log n) ù‘Ú÷ f (n) = Θ(nlogb )Âð(2

Ý ƒ ª‘ “ ‚ µ ª¢ n≥b ´ “‘ ™  þ¢‘ Ö õ ô‘ Þ — ø c< 1´ “‘ ™  þ¢‘ Ö õ ¥ ü Ì ã “ ýÂ “  ð(3


.g(n) = Θ(f (n)) ù‘Ú÷ af (n/b) ≤ cf (n)

:–‘±™
a
Ý þ ¤¢ ‚ ¹ ƒ µ ÷ ¤¢ Å • f (n) = O(nlogb − ) Ý þ ¤¢ ñø¢¤  õ –‘ ± ™ ýÂ “ •
n j loga
b −
Àþ üõ ´¨À“ Âþ ¥ ‚Ф (2)‚Ф ¤¢ ý ¤ÁÚþ‘› ‘“ ‚î f ( j ) = O((n/b ) )
b
:

 1 
logn−  n logab −
X b

g(n) = O  aj  (3)
bj
j=0

: Ýþ ¤¢ ü¨À€û ý‘û ý ¨ ¥ ù¢‘Ôµ¨‘“ ߃€»Þû

1 1
logn−
X b  n logab − X  ab j
logn−
b
j loga
b −
a =n a
bj blogb
j=0 j=0

logn− 1
X b
loga
b −
j
=n (b )
j=0
 
b logb − 1
n
loga
b −
=n
b − 1
 
a n − 1
= nlogb − .
b − 1

–¤  ¬ ‚ “ ¤  ¡ –¤‘ ± ä ö — ü õ. À € µ Æ û ´ “‘ ™ ø b ‚ î ý¤ Ï ‚ “

loga
b −
a
.´ª÷ n O(n ) = O(nlogb )
31 (MASTER THEOREM) ü¨‘¨ ‚ƒÌì .3.2

:Àþ üõ ´¨À“ Âþ ¥ ‚Ф (3) ‚Ф ¤¢ –¤‘±ä ßþ ö¢¢ ¤Âì ‘“

a
g(n) = O(nlogb )

.¢ª üõ –‘±™ ñø ¢¤ õ °ƒ—— ßþÀ“

a
Ýþ ¤¢ f (n) = Θ(nlogb ) ý‚Ф ‘“ ôø¢ ¢¤ õ ýÂ“ •
a
f (n/bj ) = Θ((n/bj )logb )

:Àþ üõ ´¨À“ (4)‚Ф (2)‚Ф ¤¢ ý ¤ÁÚþ‘› ‘“ ø

 1 
logn−  n logab
bX
g(n) = Θ  aj  (4)
bj
j=0

: Ýþ ¤¢ ͓ø ¤ ý¥‘¨ ù¢‘¨ ‘“ ¥‘“

1 1
logn−
X b  n logab logn−
X b  j
j loga a
a =n b
a
bj blogb
j=0 j=0

logn− 1
X b

=n loga
b 1
j=0

a
= nlogb lognb

: Ýþ ¤¢ (4) ‚Ф ¤¢ –¤‘±ä ßþ ö¢Âî ߃È÷‘› ‘“ ‚î

a
g(n) = Θ(nlogb lognb ) =⇒

a
g(n) = Θ(nlogb log n)

.Àª ´“‘™ Ã÷ ôø¢ ´ó‘Ÿ ‚î

ý‘û ö— ýÂ“ ‚î Ýþ ƒړ ‚¹ƒµ÷ ݃÷— üõ (2) ‚Ф ¥ ù¢‘Ôµ¨ ‘“ ô¨ ¢¤ õ ¤¢ •
ýÂ “ af (n/b) ≤ cf (n) ‚ î ­Â ê ß þ ‘ “ . g(n) = Ω(f (n)) Ý þ ¤¢, b ¼ƒ½¬

. f (n/b) ≤ (c/a)f (n) Ýþ ¤¢ n ≥ b Âþ¢‘Öõ ô‘Þ— ø c<1 ´“‘™ Âþ¢‘Öõ üÌã“

f (n/b) ≤ (c/a)f (n) ⇒ f (n/bj ) ≤ (c/a)j f (n) ⇒ aj f (n/bj ) ≤ (cj f (n))


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 32

: Ýþ ¤¢ (2) ‚Ф ¤¢ ý ¤‘ص¨¢ üÞî ‘“ ñ‘Ÿ

logn− 1
X b

g(n) = aj f (n/bj )
j=0

logn− 1
X b

≤ cj f (n)
j=0


X
≤ f (n) cj
j=0
 
1
= f (n)
1−C

= O(f (n)).

Ý þ ¤¢ , b ¼ ƒ ½ ¬ ý‘ û ö — ýÂ “ Å •.´ ¨ ´ “‘ ™ c ö ¤¢ ‚ î

ß þÀ “ .g(n) = Θ(f (n)) ß þÂ “‘ € “ g(n) = Ω(f (n)), g(n) = O(f (n))
.Àª –‘±™ Ã÷ 2 Ýó ô¨ ¢¤ õ °ƒ——

3 Ýó
ý‘ûö— ¥ üԀõ‘÷ ü㓑— f (n) ø À€ª‘“ ´“‘™ ¢Àä ø¢ b > 1ø a ≥ 1 Àƒ€î ­Âê

üõ Óþ Â㗠Âþ ¥ –¤ ¬ ‚“ b ¼ƒ½¬ ý‘û ö— ýø ¤ ¤ T(n)ⓑ— .Àª‘“ b¥ ü½ƒ½¬

. ݃€î


 θ(1) n=1
T(n)=

aT ( nb ) + f (n) n = bi

: ´ª¢ ݃û¡ –¤ ¬ ßþ ¤¢. ´¨ üµ±·õ ¼ƒ½¬ ¢Àä i ‚î

ù‘ Ú ÷,  > 0´ “‘ ™  þ¢‘ Ö õ ü Ì ã “ ýÂ “ f (n) = O(nlogb − )


a
 ð (1

loga
.T (n) = Θ(n b )Ýþ ¤¢
a a
.T (n) = Θ(nlogb log n)) ù‘Ú÷ f (n) = Θ(nlogb )Âð (2
33 (MASTER THEOREM) ü¨‘¨ ‚ƒÌì .3.2

0 ´ “‘ ™  þ¢‘ Ö õ ü Ì ã “ ýÂ “, f (n) = Ω(nlog + )  ð (3


a
ü Ì ã “ ýÂ “  ðø > b

Ýþ ¤¢ ù‘Ú÷ af (n/b) ≤ cf (n) ݃ª‘“ ‚µª¢ n ï¤ Ã“ Âþ¢‘Öõ ø c < 1 ´“‘™ Âþ¢‘Öõ

.T (n) = Θ(f (n))

:–‘±™
:݃€î üõ ù¢‘Ôµ¨ 1 Ýó ¥ (1)‚Ф ü“‘þ ¥ ¤ ýÂ“ 2 Ýó ¢¤õ ¥ ‘õ

: Ýþ ¤¢ ñø ¢¤ õ ýÂ“

a a a
T (n) = Θ(nlogb ) + O(nlogb ) = Θ(nlogb ),

: 2 ¢¤ õ ýÂ“ ø

a a a
T (n) = Θ(nlogb ) + Θ(nlogb log n) = Θ(nlogb log n),

: 3 ¢¤ õ ýÂ“ ø

a
T (n) = Θ(nlogb ) + Θ(f (n)) = Θ(f (n)),

:Âþ ¥

loga+
f (n) = Ω(n b )
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 34

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ : ñ‘·õ



1 √ n=1
T(n)=
3 T ( n2 ) + n else

(‚ƒÌì ñø ´ÞÆì §‘¨ “): ۟


√ 1 3
f (n) = n= n2 ∈ O(nlog 2 − ) a=3, b=2
3
⇒ T (n) ∈ Θ(nlog 2 )

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ : ñ‘·õ



0 n=1
T(n)=
2T ( n2 ) + n − 1 else

(‚ƒÌì ôø¢ ´ÞÆì §‘¨ “): ۟

T (n) = 2 T ( 2 ) + n − 1
n

f (n) = n − 1 ∈ Θ(n) a=2, b=2


2
Θ(n) = Θ(nlog 2 (log n)0 ) ⇒ T (n) ∈ Θ(n(log n)0+1 )
⇒ T (n) ∈ Θ(n log n)

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ : ñ‘·õ



T (n) =
1 n=1
3 T ( n2 ) + n2 else

(‚ƒÌì ô¨ ´ÞÆì §‘¨ “): ۟


3
f (n) = n2 ∈ Ω(nlog 2 + ) a=3, b=2
a = 3 ( 2 )2 ≤ δ n2 ⇒ δ = 45
f ( nb ) n

⇒ T (n) ∈ Θ(n2 )

.Àƒ€î ۟ ¤ Âþ ¥ ‚ó¢‘ãõ : ñ‘·õ



c n=1
T (n) =
2 T ( n2 ) + log n! else

: ۟

f (n) = log n! ∈ Θ(n logn) a=b=2


2
Θ(n logn) = Θ(nlog 2 (logn))
⇒ T (n) ∈ Θ(n (logn)1+1 ) ⇒ T (n) ∈ Θ(n (logn)2 )
35 ‘û ݵþ ¤Úó Ãó‘÷ .4.2

‘û ݵþ ¤Úó Ãó‘÷ 4.2


üþ‘þ‘• Û¬

ø¢ ‚“ ¤ ‚õ‘÷“ ‚ãÎì ×þ ‘þ ݵþ ¤ Úó ×þ Âð.Àª‘“ üõ üþ‘þ‘• Û¬ , Û¬ ߃óø

Â› ýÂ“ ¤ t2 ö‘õ¥ ý ÂÚþ¢ ø t1 ö‘õ¥ üØþ ‚î ù¢Þ÷ ý¥‘¨ ù¢‘ƒ• Óܵ¿õ Ûت

¤¢ ý ƒ™‘b — ݵþ ¤ Úó ý¥‘¨ ù¢‘ƒ• ‚î ü€ãõ ßþÀ“.t1 ∈ θ(t2 ) ù‘Ú÷ À€ª‘“ ‚µª¢ ¥‘ƒ÷

.¢¤À÷ ݵþ ¤ Úó üð¤ Ó ý‚±—Âõ

ý ¤Á𠰃—— Û¬

¥‘ƒ÷ Â› ýÂ“ ¤ t2 ö‘õ¥ ý ÂÚþ¢ ø t1 ö‘õ¥ üØþ ‚î À€ª‘“ ‚õ‘÷“ ‚Ø— ø¢ p2 , p1 Âð
¤¢ ,À€ª‘“ ‚µªÀ÷ Ýû “ ü±÷‘› ƒ™‘— ºƒû ‚õ‘÷“ ‚Ø— ø¢ ßþ ö “ ùø…ä ,Àª‘“ ‚µª¢

. ´¨ t1 + t2 “Â“ p1 , p2 ü󐁵õ ýÂ› ýÂ“ ô¥„ ö‘õ¥ ´ó‘Ÿ ßþ

t1 + t2 ∈ θ(max{t1 , t2 })

׃ޗ –¤ µ¨¢ Û¬

݃Æ֗ µ؝î ýÃ› ‚“ ¤Ãê ´¿¨ ¼Î¨ ¤¢ ‚î ´¨ ý ¤ µ¨¢ ׃ޗ ¤ µ¨¢ Âû

ü—‘õÀÖõ –¤ µ¨¢ ߃€»Þû ø .´¨ θ(1) ¥ –¤ µ¨¢ ßþ ýÂ› ö‘õ¥ ,¢ª üÞ÷

θ(1) ù¥À÷ ‚“ ü÷‘õ¥‚î À€þ üõ ¢›“ ׃ޗ –¤ µ¨¢ üû‘€µõ ¢À㗠°ƒî— ¥

.À÷¤¢ ¥‘ƒ÷

: ü÷‘õ¥ Ãó‘÷

:while ‚Öܟ Ãó‘÷

i←1
while (i <= m)
p(i)
i←i+1
„‘ “ öÂ î × þ Ý ƒ û ¡ ü õ ,À ª‘ “ Ý µ þ ¤  Ú ó ýÂ › ýÂ “ ô¥„ ö‘ õ¥ L À ƒ € î ­Â ê

1: ݃€î üõ ÛÞä °ƒ—— ßþ ‚“ .݃“‘ƒ“ ݵþ ¤ Úó ýÂ“

L ≤ O(1) + i←1 ýÂ“

O((m + 1) × 1) + ‚Æþ‘Öõ ýÂ“

O(mt) + ‘û p(i) ô‘¹÷ ýÂ“

O(m) + i ← i + 1 ýÂ› ýÂ“


×þ ¥ ‚µ±ó).p(i) ∈ O(t) ü€ãþ.Àª‘“ t, p(i) Ãþ‘Þµõ ý‘ûÂ› ýÂ“ üþ„‘“ öÂî ×þ Àƒ€î ­Âê 1
(À㓠‚“ üþ‘›
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 36

O(m) while ‚“ goto ÛÞä ýÂ“

⇒ L < O(2 + 3m + mt) ⇒ L ∈ O(max{1, m, mt})


ôÀî ºƒû ö— üÞ÷ øÀ€µÆû Ýúõ ý ƒð ÝÞþ Ãî‘õ ßþ Û ‚¨ Âû‚î ¢Þ÷ ‚›— Àþ‘“

.¢Âî éÁŸ ¤

öÂ î × þ i  û ýÂ “ –¤  ¬ ö ¤¢ À ª‘ “ ¢‘ þ ¥ ‘ û p(i) ýÂ › ü ÷‘ õ¥ ü ðÀ € îÂ •  ð

Â› ö‘õ¥ –¤ ¬ ö ¤¢ ݃õ‘€“ ti ¤ üþ„‘“ öÂî ßþ Âð ,ù¢Âî ‚±¨‘½õ¤ p(i) üþ„‘“

:´¨ Âþ ¥ –¤ ¬ ‚“

m
!
X
L ∈ O max{1, m, ti }
i=1

:for ‚Öܟ Ãó‘÷

ö‘õ¥ ßþÂ“‘€“ ,¢ª üõ ý¥‘¨ù¢‘ƒ• while À€÷‘õ ¤Ãê ´¿¨ ¼Î¨ ¤¢ for ¤ µ¨¢ Âû
.´¨ while ‚Öܟ ýÂ› ö‘õ¥ Û·õ for ‚Öܟ ýÂ›
for i ← 1 to n − 1 do
for j ← i + 1 to n do
write(’*’);

n
X
ti = (1) = n − (i + 1) + 1 = n − i
j=i+1

X1
n− X1
n−
(n − 1)n (n − 1)n
L∈ ti = (n − i) = n(n − 1) − = ∈ O(n2 )
i=1 i=1
2 2
Âû ‚î , Ûت ¤¢ üܬ ÂÎì ý„‘“ ý‘û ù¤‘µ¨ ¢À㗠‚î ´¨ ßþ Û·õ ၮõ ßþ

n2 −n
.À€µÆû
2 “Â“ ‚î. Ýþ ¤‘Þȓ ¤ À€µÆû O(1) ¥ ôÀî

1 2 ................. n
1 .*....*. *
2. *......
... *....
... ....
... ....
... ....
... ....
. .*...
n ....
*.

n2 −n
2 ∈ O(n2 )
37 ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó .5.2

ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó 5.2


Selection Sort ü“‘¿µ÷ ý¥‘¨ °—Âõ ݵþ ¤Úó 1.5.2

procedure Selection Sort( T [ 1· · ·n ])


fori ← 1 to n-1 do
 
minx ← T[i]
O(1) Ω(1)
minj ← i
for j← i+1 to n do
 
 if (T[j] < minx)then 
O(1) minx ← T[j] Ω(1)
 
minj ← j
 
T[minj] ← T[i]
O(1) Ω(1)
T[i] ← minx
: ݵþ ¤ Úó öÂî ‚±¨‘½õ


 P1
n− P
n


 L ≤ (O( 1 ) + O(1)) ∈ O(n2 )
i=1 j=i+1
⇒ L ∈ Θ(n2 )
 P1
n− P
n


 L> (Ω(1) + Ω(1)) ∈ Ω(n2 )
i=1 j=i+1

.¢¤À÷ ‘û ý¢ø ¤ ø ‘“ üϑ±—¤ Â›ö‘õ¥ ý¥‘¨ù¢‘ƒ• á÷ ßþ¤¢

Bubble Sort ü“‘±Ÿ ý¥‘¨ °—Âõ ݵþ ¤Úó 2.5.2

Procedere Bubble Sort(T[1..n])


for i ← 1 to n-1 do
for j ← i+1 to n do
if(T [j] < T [i]) then
swap(T [i], T [j])

°—Âõ¤¢ ‚î üó‘Ÿ ¤¢ ´¨ n − 1 ‚ȃÞû ü“‘¿µ÷ ý¥‘¨ °—Âõ¤¢ ‘û üþ‘¹“‘›¢Àã—


n(n−1)
öÂ î ß þÂ “‘ € “ .´ ¨
2 ´ ó‘ Ÿ ß þ  —À “ ¤¢ ø 0 ´ ó‘ Ÿ ß þ  µ ú “ ¤¢ ü “‘ ± Ÿ ý¥‘ ¨

. ´¨ Ω(1) ö ߃þ‘• öÂî ø O(n2 ) ݵþ ¤ Úó ßþ ý„‘“


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 38

Insertion Sort ü›¤¢ ý¥‘¨ °—Âõ ݵþ ¤Úó 3.5.2

Procedere Insertion Sort(T[1..n])


for i← 2 to n do
x← T [i]
j← i-1
while (j > 0 && T [j] > x)
T[j+1] ← T[j]
j−−
T[j+1] ← x
Ω(n) ¥ ݵþ ¤ Úó ߃þ‘• öÂî Àª‘“ false ‚ȃÞû while ¯Âª Âð ݵþ ¤ Úó ßþ ¤¢
P
n
ü—¤ ¬ ‚“ ŕ. Àª‘“ üõ (O(1) + O(i)) ∈ O(n2 ) –¤ ¬ ‚“ ö ý„‘“ öÂî ø
i=2
´ó‘Ÿ ßþ µú“ ¤¢ ¤ ݵþ ¤ Úó ßþ ¤ Ñ€õ ßþÀ“. ¢¤¢ üڵƓ ý¢ø ¤ ø á÷ ‚“ ‚î ´¨

. ݃þ‘Þ÷ üõ ü¨¤ “ ͨµõ ´ó‘Ÿ ø ´ó‘Ÿ ßþ —À“

(Best Case Analysis) ´ó‘Ÿ ßþ µú“ Ãó‘÷

Ìä Âû ¢ø ¤ ø ‘“ ´ó‘Ÿ ßþ ¤¢ ,Àª‘“ °—Âõ ‚þ¤ ‚î Àû¢ üõ  ¤ ü÷‘õ¥ ´ó‘Ÿ ßþ µú“

¤¢ ¢ª Â› Àþ‘±÷ while ‚Öܟ ü€ãþ .¢ª š¤¢ ´ƒãìõ ö‘Þû ¤¢ Ìä ö Àþ‘“ ÀþÀ›

Ý µ þ ¤  Ú ó ýÂ › ö‘ õ¥ ‚ ¹ ƒ µ ÷ ¤¢ ,´ ¨ O(1) ¥ while ¯Â ª ‚ Æ þ‘ Ö õ ö‘ õ¥ ´ ó‘ Ÿ ß þ

ß þ ýÂ “ ß ƒ þ‘ • öÂ î × þ ü êÂ Ï ¥ .¢ ª ü õ Â › ¤‘ “ n-1 Â þ ¥ ¢ “ À û ¡ O(n) ¥

.Àª‘“ üõ θ(n) ¥ Â› ö‘õ¥ ‚¹ƒµ÷ ¤¢ ,´¨ ù¢“ Ω(n) ݵþ ¤ Úó

(Worst Case Analysis) ´ó‘Ÿ ßþ —À“ Ãó‘÷

ÀþÀ› ÌäÂû ¢ø ¤ ø ‘“ –¤ ¬ ßþ ¤¢ À€ª‘“ ùÀª °—Âõ üóø Ã÷ –¤ ¬ ‚“ ‘û ù¢¢ Âð

while ¤ µ¨¢ ýÂ“ ö‘õ¥ Ω(i) ù¥À÷ ‚“ ‚¹ƒµ÷ ¤¢ .´¨ ¥‘ƒ÷ ‚Æþ‘Öõ i (ôi Ìä Û·õ)

ü êÂ Ï ¥ ø ¢ “ À û ¡ Ω(n2 ) Ý µ þ ¤  Ú ó ß þ ýÂ › ýÂ “ ô¥„ ö‘ õ¥ Å •.Ý þ ¤¢ ¥‘ ƒ ÷

¥ Ý µ þ ¤  Ú ó ýÂ › ö‘ õ¥ ‚ ¹ ƒ µ ÷ ¤¢ ´ ¨ O(n2 ) Ý µ þ ¤  Ú ó ýÂ › ýÂ “ ü þ„‘ “ Ó Ö ¨

.¢“ Àû¡ θ(n2 )


39 ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó .5.2

(Average Case Analysis)ͨµõ ´ó‘Ÿ Ãó‘÷

üþ‘ú÷ ´ƒãìõ i i-1 i-2 ..... 1


‘û ‚Æþ‘Öõ ¢À㗠1 2 3 ..... i

P
E(x) = x × f (x) ü®‘þ ¤ Àƒõ
x∈X

.Àµê üõ ‚÷‘¡ ×þ ¤¢ x ‚î üó‘ÞµŸ: f (x) = 1i


T[1..i] ‚þ¤Âþ ¥ ¤¢ °¨‘€õ ´ƒãìõ ¤¢ ô i ÂÊ€ä š¤¢ ýÂ“ ô¥„ ö‘õ¥ ͨµõ : ci

ci =
i
X

1=1X i
1 i(i + 1) = i + 1
k= ×
k=1
i i
k=1
i 2 2
:‘“ ´¨ “Â“ ý¥‘¨ °—Âõ ݵþ ¤ Úó ýÂ› ýÂ“ ô¥„ ö‘õ¥ ͨµõ ‚¹ƒµ÷ ¤¢

i+1 1X 1 n(n + 1)
n
X n
X n n
X
ci = = ( i+ 1) = ( − 1 + n − 1) = θ(n2 )
i=2 i=2
2 2 i=2 i=2 2 2
´ ¨  µ ú “ ‘ û sort ‚ ƒ Ö “ ¥ insertion sort ¤‘ µ ê¤ ´ ¨ Ý î ‘ û ù¢¢ ¢À 㠗 ‚ î ü õ‘ Ú € û

üÞ÷ øÀµê üõ ë‘Ô— Âþ¢ ü܃¡ ´ó‘Ÿ ßþ µú“ ö Àª‘“ ¢‘þ ¥ ‘û ù¢¢ ¢À㗠üµìø üóø

.Àª‘“ °¨‘€õ À÷—

. ´¨
1 n!
n! ´ó‘Ÿ ßþ µú“ ‚î ´¨‘û ´ó‘Ÿ Ûî

Pigeon hole Sort ý—±î ‚÷„ ý¥‘¨ °—Âõ 4.5.2

u ü€ãþ °—Âõ‘÷ ý‚þ¤ Âʀä ßþ µð¤ Ó ýù¥À÷ ‚“ ý ‚þ¤ Àµ“ ý¥‘¨ °—Âõ ßþ ¤¢

¤¢ ¤ °—Âõ‘÷ ý‚þ¤ ¤¢ Âʀä Âû ü÷øÂê âìø ¤¢ ‚þ¤ ßþ ‚î ,¢ª üõ ‚µêÂð ÂÑ÷ ¤¢

üÆþ÷ ¥‘“ °—Âõ –¤ ¬ ‚“ ¤ ‚ƒóø ý‚þ¤ ‚þ¤ ßþ ýø ¤ ¥ ų¨.¢¤¢ üõ ù‘Ú÷ ¢¡

ö‘Þû ü€ãþ ‘û ü÷øÂê âޛø ´¨‘û ü÷øÂê “ ü€µ±õ ý¥‘¨ °—Âõ ßþ ŕ. À€î üõ

. ´¨‘û ù¢¢ Ûî ¢À㗠“Â“ u ¬‘€ä áÞ¹õ


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 40

Procedure pigeon hole sort(T[1..n])


let m = max{T [i]} T [i] ∈ Z >0 , 1≤ i ≤ n
array u[1..m]
for i← 1 to m do // θ(m)
u[i] ← 0
for i ← 1 to n do // θ(n)
k← T[i]
u[k]++
k← 1
for i← 1 to m do
while u[i] 6= 0 do
T[k]← i
u[i] − −
k++
: ݵþ ¤ Úó Ãó‘÷
P
n
Â› ¤‘“ m+1 ù¥À÷ ‚“ for ý‚Öܟ ø ¤‘“ (u[i] + 1) ù¥À÷ ‚“ for ‚Öܟ ¤¢ while
i=1
: Ýþ ¤¢ ݵþ ¤ Úó ¥ ´ÞÆì ßþ ýÂ“ ßþÂ“‘€“ ,¢¢Âð üõ
P
m P
m P
m
(u[i] + 1) = u[i] + 1=n+m
i=1 i=1 i=1
.Àª‘“ üõ θ(n + m)¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥ ßþÂ“‘€“

:Àƒþ‘Þ÷ ´ì¢ Âþ ¥ ñ‘·õ ‚“

T 2 1 3 2 1 2 5 3 4 2 5 °—Âõ‘÷ ‚þ¤

1 2 3 4 5
u 0 0 0 0 0 m=5
1 2 3 4 5
u 2 4 2 1 2

T 1 1 2 2 2 2 3 3 4 5 5 ùÀª °—Âõ ‚þ¤

| {z } | {z }| {z } | {z }
1 ‘— ø¢ 2 ‘— ¤‘ú 3 ‘— ø¢ 5 ‘— ø¢
?
4 üØþ
41 ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó .5.2

Binary Searchüþø¢ø¢ ý¹µÆ› 5.5.2

Binary Search (A, temp){


left = 0;
right = lenght(A)-1;
while (left< right) do
middle= (left+right) / 2;
if (temp > A[middle])
left= middle + 1;
else
right= middle-1;
return left;}
ö‘Øõ öÀªÀƒ• –¤ ¬ ¤¢. Àþ‘Þ÷ üõ ¹µÆ› A ý‚þ¤ ¤¢ ¤ temp ݵþ ¤ Úó ßþ

.À÷¢Âð üõ “ ¤ Àª‘“ ´¨ ¤Âì temp ‚î üþ‘› öÀÈ÷ Àƒ• –¤ ¬ ¤¢ ø ö

:Binary Search ýÂ› ö‘õ¥ ‚±¨‘½õ

i ← lef t, j ← right, i = 0, j = n − 1
.¢ª ´ê‘þ ö ¤¢ temp ´¨ ¤Âì ‚î ´¨ ý‚þ¤ ñÏ d

d=j −i+1=n−1−0+1 =n k = middle = i+j


2

¤Á𠥐 ŕ ,À€ª‘“ if ¤ µ¨¢ ¥ Û±ì middle, right, lef t Âþ¢‘Öõ k, j, i Àƒ€î ­Âê
 þ¢‘ Ö õ ýÂ “ ° ƒ — — ‚ “ ¤ ˆ ĵ, î þ¢‘ Ö õ ø
d, À û¢  ¤ ´ ¨ ß Ø Þ õ ´ ó‘ Ÿ ø¢ if ¤  µ ¨¢ ¥

.Ýþ ƒð üõ ÂÑ÷ ¤¢ ÀþÀ› ý‚þ¤ Âþ ¥ ñÏ ø right, left

• temp ≥ A[middle] ⇒ î = k+ 1, ĵ = j, dˆ = ĵ − î+ 1 = j −k = j −( i+j


2 )=
j−i+1
j−i
2< = d ⇒ dˆ < d
2 2 2

• else ⇒ ĵ = k − 1, î = i, dˆ = ĵ − î + 1 = k − 1 − i + 1 = i+j j−i


2 −i = 2 <
j−i+1
= d ⇒ dˆ < d
2 2 2

.¢¢Âð üõ ÓÊ÷ ‚þ¤ ñÏ ¤‘“ Âû ¤¢ ŕ


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 42

: Ýþ ¤¢ šø ¡ ¯Âª ýÂ“ ñ‘Ÿ

i≥j ⇒j −i≤0⇒j −i+1≤1⇒d≤1


d0 = n d1 < d20 = n2 d2 < d21 = n4 · · ·
dk ≤ 1 ⇒ 2nk ≤ 1 ⇒ n ≤ 2k ⇒ logn2 ≤ k
⇒ k = dlogn2 e ⇒ O (logn2 ) üþ„‘“ öÂî

Binary Insertion Sort ü›¤¢ üþø¢ø¢ ý¥‘¨ °—Âõ 6.5.2

Binary Insertion Sort(A[1...n]){


for (i = 1; i < n; i + +){
temp= A[i];
left=1;
right=; i
Binary Search algorithm // for array A , temp=A[i], left=1 , right=i
for (j = i; j > lef t; j − −)
swap(A[j-1],A[j]);
}
}

worst case best case average case

insertion sort O(n2 ) O(n2 ) O(n2 )


Binary insertion sort O(n2 ) O(nlog n) O(n2 )

: ´ó‘Ÿ ßþ µú“ Ãó‘÷


P1
n−
i+1 P
n
n+1
• blog2 c = blog2 c ' (n + 1)blog2
i
c+2 blog(n+1)c+1
+2
i=1 i=2

∈ O(nlogn) or

 
P
n
i P
n
i Rn x

• blog2 c ∈ O log2 ∈O 2 log2 dx ∈ O(nlogn)
i=2 i=2
43 ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó .5.2

Shell Sort ݵþ ¤Úó 7.5.2

void Shell-Sort(int gap,int A[1...n]){

while((gap/=2)≥1){

for(i=0;i < length(A);i++){

int j=i;

while((j ≥ gap) && (A[j-gap] > A[j])){

swap(A[j - gap],A[j])

j -=gap;

Å ³ ¨ , Ý þ  ƒ ð ü õ Â Ñ ÷ ¤¢ ‚ þ¤ ñ Ï ù¥À ÷ ‚ “ ý gap À µ “ Ý µ þ ¤  Ú ó ß þ ¤¢

.À÷¢Âð üõ ‚Æþ‘Öõ Ýû ‘“ ‚ܬ‘ê ßþ ‘“ ¬‘€ä ø ¢¢Âð üõ ÓÊ÷ ¤ ±ä ¤‘“ Âû ¤¢ gap ßþ

:Àƒþ‘õÂê ´ì¢ ¢¤ õ ßþ ¤¢ üó‘·õ ‚“

–¤ ¬ ¤¢ ø À÷ª üõ ‚Æþ‘Öõ Ýû ‘“ 4 ‚ܬ‘ê ‘“ ¬‘€ä ‚þ¤ ¨ ¥ Àµ“ ñ‘·õ ßþ ¤¢

.À÷ª üõ ­ä Ýû ‘“ 1ø 4 ø Ýû ‘“ 6 ø 7 ý‘› ŕ ,À÷¢Âð üõ ‘¹“‘› Ýû ‘“ ôø Ãó

Ýû ‘“ 2 ý‚ܬ‘ê ‘“ ñø ¥ ‚þ¤ ¬‘€ä ù¤‘“ø¢ Àþ‘“ ø ùÀª ÓÊ÷ ‚ܬ‘ê ñÏ ų¨

Ýû ‘“ 8 ø 6 ų¨ ,À÷ª üõ ‘¹“‘› Ýû ‘“ 1ø 2 Àµ“ ´ÞÆì ßþ ¤¢, À÷ª ‚Æþ‘Öõ

‘ —  ¡ ü ó ø 5 ‘ “ 2 ° ƒ — — ß ƒ Þ û ‚ “ ø ¢Â þÁ • ü Þ ÷ –¤  ¬ ü þ‘ ¹ “‘ › ø ùÀ ª ‚ Æ þ‘ Ö õ

´ ó‘ Ÿ ß þ ¤¢ ‚ î Ý þ¢Â ð ü õ š¤‘ ¡ Ý µ þ ¤  Ú ó ¥ ø ¢ ª ü õ  µ Þ î × þ ¥ ‚ Ü ¬‘ ê ‚ Ø € þ

. ´¨ ùÀõ ¤¢ ùÀª °—Âõ –¤ ¬ ‚“ ‚þ¤


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 44

Len=8=gap

2 7 4 8 5 6 1 10 gap=4
| {z }
| {z }
| {z }
| {z }

2 6 1 8 5 7 4 10 gap=2
| {z } | {z } | {z }
| {z } | {z } | {z }
| {z } | {z }
| {z } | {z }
| {z }
| {z }

1 6 2 7 4 8 5 10 gap=1
|{z} |{z}
2 6|{z} |{z}
4 7|{z} |{z}
|{z} 5 8 |{z}
4 6 |{z}
|{z} 5 7
5 6

1 2 4 5 6 7 8 10 gap= 21

. ¢È÷ Â› ôø¢ while ‚î ´¨ üõ‘Ú€û ö‘õ¥ ßþ µú“

best case worst case average case

shell-sort n log n n1.5 n1.25

üÞ÷ ýÀ÷ƒ• ´Æƒó ‘“ ¤ ݵþ ¤ Úó ßþ.À÷¤¢ üڵƓ gap ‚“ ùÀõ ´¨À“ ¢Àä

. ¢Þ÷ ý¥‘¨ ù¢‘ƒ• ö—


45 ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó .5.2

Bucket Sort1 ݵþ ¤Úó 8.5.2

unsighned const m=max


void Bucket-Sort1(int A[ ],int n)
int buckets[m];
for (int i=0;i<m;i++)
buckets[i]=0;
for(i=0;i< n;i++)
++buckets[A[i]];
for(int j=0,i=0;j< m;j++)
P1
m−
for(int k=buckets[j];k>0;- - k) // buckets[j] = n
j=1
A[i++]=j;
O(n+m) ¥ à ƒ ÷ ö ýÂ › ö‘ õ¥ ø ´ ¨ Pigeon hole sort ö‘ Þ û Ý µ þ ¤  Ú ó ß þ

.Àª‘“ üõ

2 3 4 5 1 2 1

0 2 2 1 1 1

1 1 2 2 3 4 5

Bucket Sort2 ݵþ ¤Úó 9.5.2

ý‘ û ù¢¢ ý¥‘ ¨ ° — õ ýÂ “ ‚ î ´ ¨ Bucket-Sort ¥ Â Ú þ¢ ü ä ÷ Ý µ þ ¤  Ú ó ß þ

. ´¨ °¨‘€õ,À÷ùÀª âìø [0, 1) ù¥‘“ ¤¢ ‚î üꢑʗ

Bucket-Sort2(A)
n ← [A]
for i ← 1 to n do
insert A[i] into list B[bnA[i]c]
for i ← 0 to n-1 do
sort list B[i] with insertion sort
concatenate the list B[0],B[1],...,B[n-1] together in order
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 46

A B

1 .78 0
- .12 - .17
2 .17 - .23 - .26
- .21
3 .39 - .39
4 .26
5 .72
6 .94 - .68
7 .21 - .72 - .78
8 .12
9 .23
9 - .94
10 .68

Bin Sort ݵþ ¤Úó 10.5.2

for(int i=0;i<m;i++) //O(m)


bin [i]= -1;
for(int i=0;i < n;i++)
bin[a[i]]=a[i]; //O(n)
j=0;
for(int i=0;i < m;i++){
if(bin[i]!= -1) //O(n+m)
a[j]=b[i];
j++;}
ݵþ ¤ Úó ßþ ¤¢ ‚î ¢Þ÷ ‚›— Àþ‘“. ´¨ O(m + n) ¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥

Àþ‘“ ݃ª‘“ ‚µª¢ Ýû ý ¤Âؗ ¬‘€ä ݃û¿“ Âð, À€ª‘“ À€÷— üÞ÷ ý ¤Âؗ ¬‘€ä

. Ýþ ƒړ ÂÑ÷ ¤¢ ýÀ÷ƒ• ´Æƒó ×þ ‚þ¤ ý‚÷‘¡ Âû ýÂ“

a: 3 7 2 8 1

bin: -1 1 2 3 -1 -1 -1 7 8

a: 1 2 3 7 8
47 ý¥‘¨ °—Âõ ý‘û ݵþ ¤Úó .5.2

Counting Sort ݵþ ¤Úó 11.5.2

counting-sort
for i←0 to k do k is maximum of elements
c[i]←0
for j← 1 to n do
c[a[j]]←c[a[j]]+1;
for i← 2 to k do
c[i] ← c[i]+c[i-1];
for j ← n downto 1 do
b[c[a[j]]] ← a[j];
c[a[j]] ← c[a[j]] -1 ;

.Àª‘“ üõ θ(n + k) ¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥

¥‘ƒ÷ ‚Æþ‘Öõ ¢À㗠log n! Ûì ÀŸ ‚“ ‚Æþ‘Öõ“ ü€µ±õ ý¥‘¨ °—Âõ ݵþ ¤ Úó Âû : ‚µØ÷

. ¢¤¢ ¥‘ƒ÷ ‚Æþ‘Öõ dlog n!e ÛìÀŸ ´ó‘Ÿ ßþ —À“ ¤¢. ¢¤¢

dlog n!e ∈ Ω(n log n)


ß þ ‘ õ ,À ª‘ “ ü õ θ(n log n)¥ ´ ó‘ Ÿ ß þ  —À “ ¤¢ ý¢ø ¤ ø ၠ÷ ¥ Û Ö µ Æ õ Å •

.¢ÂþÁ³“ –¤ ¬ Ã÷ θ(n log n)¥ µÞî ü÷‘õ¥ ¤¢ À÷— üõ ݵþ ¤ Úó

Radix sort ݵþ ¤Úó 12.5.2

stable á÷ ¥ ݵþ ¤ Úó ßþ.¢ÂþÁ• üÞ÷ –¤ ¬ ý ‚Æþ‘Öõ ¬‘€ä ߃“ ݵþ ¤ Úó ßþ ¤¢

¬‘€ä ų¨. ݃þ‘Þ÷ üõ °—Âõ ö‘Øþ °ƒ—— ‚“ ¤ ¬‘€ä Àµ“ ©ø ¤ ßþ¤¢ .´¨ sort
üõ °—Âõ ö‘Úû¢ °ƒ—— ‚“ ¤ ¬‘€ä ñ‘Ÿ . ݃þ‘Þ÷ üõ üܬ ‚þ¤ ¢¤ø °ƒ—— ‚“ ¤

݃û¢ üõ ‚õ¢ °ƒ—— ߃Þû ‚“ø ݃þ‘Þ÷ üõ ‚þ¤ ¢¤ø ø ݃þ‘Þ÷ üõ ´Æƒó ¢¤ø ø ݃þ‘Þ÷

·îÀŸ α(n) ‚î Àª‘“ üõ O(nα(n)) ¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥.¢¢Â𠰗Âõ ‚þ¤ ‘—

. ´¨ ¢Àä n ߃“ ùÀª Âû‘Ò ô‘줐 ¢Àã—

: Àƒ€î ´ì¢ Âþ ¥ ñ‘·õ ‚“


üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 48

25 13 43 124 313 513 5 1 999 96 205

0
1 - 531 - 1
2
3 - 13 - 43 - 313
4 - 124
5 - 25 - 5 - 205
6 - 96
7
8
9 - 999

. ݃þ‘Þ÷ üõ üܬ ‚þ¤ ¢¤ø Âþ ¥ °ƒ—— ‚“ ¤ ¬‘€ä ñ‘Ÿ

531,1,13,43,313,24,25,5,205,96,999
. Ýþ ¥¢Â• üõ ý¥‘¨ °—Âõ ý‚õ¢ ‚“ ø

üµÈ𥑓 –„¢‘ãõ ( ö¢Âî ۟) ö¢Âî Trace ÛÞä 6.2



T, (n) = T ( n5 ) + T ( 710n ) + θ(n)

T, (1) = C
.Àª‘“ üõ θ(n) ö ‚Èþ ¤ ‚î Ýþ ¥‘¨ üõ ´È𥑓 ´¡¤¢ ×þ ¤ Ñ€õ ßþ ýÂ“

θ(n)

O( n5 ) O( 710n )

n
O( 25 ) O( 750n ) O( 750n ) O( 49 n
100 )

–¤ ¬ ‚“ ‘û ´¡¤¢ Âþ ¥ ‚Þû ü€ãþ ´Æƒ÷ ùÀ÷ø ¤ ߃þ‘• ö ¤‘Öµõ ´¡¤¢ „‘“ ´¡¤¢

’‘ÆŸ ¤ O Àþ‘“ Àª‘±÷ ö ¤‘Öµõ ´¡¤¢ Âð â쐁õ ßþ ¤¢ .À€þ üÞ÷ ߃þ‘• ö‘ÆØþ

. ݃€î
49 üµÈ𥑓 –„¢‘ãõ ( ö¢Âî ۟) ö¢Âî TRACE ÛÞä .6.2

 i  i
7 10
n ≤ 1 =⇒ n ≤ =⇒ logn10 ≤ i =⇒ i = dlogn10 e =⇒
10 7 7 7

dlogn10 e  logn10 +1
1− 9
X7  9
i
10
7
T (n) ≤ n=
i=0
10 9
1 − 10
1 10
53 > =⇒ logn 1 > logn10 =⇒ 3 logn5 > logn10 =⇒
7 53 7 7

dlogn10 e
X7  i 3X 5 
logn i
T (n) ≤
9 n≤
9 n
i=0
10 i=0
10

.Àƒ€î ۟ ¤ Âþ ¥ üµÈ𥑓 ‚ó¢‘ãõ

• T (n) = T ( n3 ) + T ( 23n ) + θ(n)

θ(n)

θ( n3 ) θ( 23n )

θ( n9 ) θ( 29n ) θ( 29n ) θ( 49n )

 i  i
2 3 l m
n ≤ 1 =⇒ n ≤ =⇒ logn3 ≤ 1 =⇒ i = logn3
3 2 2 2

l m
logn3
X2
=⇒ T (n) ≤ n ≤ O(n logn3 )
2
i=0
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 50

Catalan Number ö„‘—‘î ¢Àä Â“ ý¤Áð 7.2

:1 ùÂð n ‘“ üþø¢ø¢ ö‘µ¡¤¢ ¢À㗠‚±¨‘½õ

‘ ¹ € þ ¤¢.¢¤À ÷ ý ¤ ø¢ ‘ þ ‚ ì Ïø ´ ¨ À € ± Þ û ‚ î , ¨ü “ ´ ¨ ü êÂ ð ´ ¡¤¢ × þ

. ݃€î üõ ü¨¤ “ ¤ ¤¢ ‚Èþ ¤ üþø¢ø¢ ö‘µ¡¤¢

Ýþ ùÀƒÈî ùÂþ¢ ö¤ ø¢ ‚î ü¨¤ ‘ú÷¤¢ ‚î ݃€ƒ“ üõ üþø¢ø¢ ´¡¤¢ø¢ 1 Ûت ¤¢

( ‚¡‘ª ô‘÷ ‚“) ñ‘þø¢Â·îÀŸ §¤ Âû ¥ ‚Ø€þ Ûƒó¢ ‚“ ¤ ‘úµ¡¤¢ ßþ.´¨ ‚Èþ ¤ éÂãõ

.À€õ‘÷ üõ üþø¢ø¢,À÷ùÀª ݨ¤ ߃þ‘• ´Þ¨ ‚“

¥ ‚î ü³ ‚¡‘ª ‚î‘€ãõ ßþÀ“À÷ °—Âõ,¤¢ ‚Èþ ¤ üþø¢ø¢ ö‘µ¡¤¢ ßþ «Ê¿“

ÂÑ÷ ¤¢ Àþüõ ߃þ‘• ‚“ §¤ ö‘Þû¥ ‚îüµ¨¤ ‚¡‘ª ¥ –ø‘ÔµõÀþüõ ߃þ‘• ‚“ §¤

ß Ø Þ õ ¤¢‚ È þ ¤ ° — õ ü þø¢ø¢ ´ ¡¤¢ ¸ € •,§¤ ‚ ¨ ¢ ›ø ´ ó‘ Ÿ ¤¢.¢ ª ü õ ‚ µ ê ð

×þ ý¤¢Â¡ üþø¢ø¢ ´¡¤¢ ¤‘ú,Àª‘±÷ Ýúõ °ƒ——Âð) ùÀª ù¢¢ ö‘È÷ ø¢ Ûت¤¢

(.À÷¤‘µ¡‘¨

n≥ 0,§¤ n ‚“ ¯“ Âõ ¤¢ ‚Èþ ¤ °—Âõ üþø¢ø¢ ö‘µ¡¤¢ ¢Àã—,bn ©¤‘Þª‘õ éÀû
´ ¨¢ ‚ “ ýÂ “ À ª‘ “ ´ ¨¢ ¤¢ 0 ≤ i ≤ n ý¥ ‚ “ bi  þ¢‘ Ö õ ‚ Ø € þ ­Â ê ‘ “. ´ ¨

Âþ ¥ ‚¨ Ûت Û·õ ‚î ݃€î üõ ‚›— ø ’‘¿µ÷ ‚Èþ ¤ ö€ä ‚“ ¤ ü¨¤,bn+1 ö¢¤ ø

‚Èþ ¤ °—Âõ üþø¢ø¢)ý‘úµ¡¤¢ À€þ üõ ߃þ‘• ‚Èþ ¤ ´¨¤ ø ² ¥ ‚î üþ‘û¤‘µ¡‘¨

 þ ¥ ¤  µ Ø  î ý‘ ú µ ¡¤¢ ß þ.´ ¨ n‘ ú ÷ ý‘ ú ¨¤ ¢À 㠗 ‚ î À € µ Æ û ý  µ Ø  î (¤¢

üú— ´¡¤¢ Âþ ¥,ßØÞõ ý‘úµ¡¤¢Âþ ¥ ßþ ‚Üޛ ¥.À€õ‘÷ üõ ­ø ÂÔõ ´¡¤¢ ý‘úµ¡¤¢

b0 = 1 Ýþ ¤¢ ö ýÂ“ ‚î ´¨

ýÀó‘Þþ Â𠂵ÆÆ𠖑ƒ®‘þ ¤ ’‘µî ¥ ‚µêÂð“ –‘±™ 1


51 CATALAN NUMBER ö„‘—‘î ¢Àä Â“ ý¤Áð .7.2

1 Ûت

1 2 3 4 5

2 Ûت

3 Ûت

ýÀ€“ ‚Ö±Ï ´¡¤¢ Âþ ¥ ø¢ ßþ ¤¢ ö— üõ ¤ §¤ n ‚÷Ú ‚î ݃€î üõ ü¨¤ “ ׀þ

.¢Âî

‚îÀû¢ üõ ‚¹ƒµ÷ Âõ ßþ.´¨¤ ´Þ¨ ¤¢ §¤ n,¢¤¢¤Âì ² ´Þ¨ ¤¢ §¤ 0 (1)
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 52

.À÷b0 bn “Â“À÷ª üõ ù¢ÂÞª bn+1 ¤¢ ‚î üþ‘û¤‘µ¡‘¨Âþ ¥ ‚Þû

´ ¡¤¢ b1 bn−1 ‚ î,´ ¨¤ ´ Þ ¨¤¢ §¤ n − 1,¢¤¢ ¤Â ì ²  ´ Þ ¨ ¤¢ §¤ 1 (2)
.Àû¢ üõ ´¨¢ ‚“ §¤ n + 1‘“,¤¢ ‚Èþ ¤ °—Âõ üþ‘—ø¢
.
.
.

´ ó‘ Ÿ ß þ ¤¢ ‚ î À ÷¤¢ ¤Â ì ´ ¨¤ ´ Þ ¨ ¤¢ §¤ n − i,²  ´ Þ ¨ ¤¢ §¤ i (i+1)


.´¨ bi bn−i “Â“ bn+1
.
.
.

–¤ ¬ ßþ ¤¢,´Æƒ÷ ´¨¤ ´Þ¨¤¢ ü¨¤ ºƒû ø ´¨ ² ´Þ¨ ¤¢ §¤ n (n+1)
. ´¨‘úµ¡¤¢ ¥ bn b0 “Â“ bn+1

n ≥ 0Âþ¢‘Öõ ‚Þû ý¥ ‚“,ßþÂ“‘€“

bn+1 = b0 bn + b1 bn−1 + b2 bn−2 + · · · + bn−1 b1 + bn b0


X ∞
X
bn+1 xn+1 = (b0 bn + b1 bn−1 + · · · + bn−1 b1 + bn b0 )xn+1 (1)

n=0 n=0

P

ù‘Ú÷ , Àª‘“ a0 , a1 , a2 , · · · ‚ó‘±÷¢ ýÂ“ Àóaõ ⓑ— f (x) = ai xi Â𐠂î ݃÷¢ üõ
i=0
ü€ãþ , ©¢¡ ‘“ a0 , a1 , · · · ‚ó‘±÷¢ Ç»ƒ• , [f (x)]2

a0 a0 , a0 a1 + a1 a0 , a0 a2 + a1 a1 + a2 a0 , · · ·

, a0 an + a1 an−1 + a2 an−2 + · · · + an−1 a1 + an a0

P

b0 , b1 , b2 , · · · À aó õ ⠓‘ — f (x) = bn xn À ƒ € î ­Â ê × € þ .À € î ü õ À ƒ ó — ¤
n=0
–¤ ¬ ‚“ ¤ (1) ‚ó¢‘ãõ .´¨


X
(f (x) − b0 ) = x (b0 bn + b1 bn−1 + · · · + bn b0 )xn = x[f (x)]2
n=0
53 CATALAN NUMBER ö„‘—‘î ¢Àä Â“ ý¤Áð .7.2

. ݃Æþ÷ üõ

: À÷‘¨¤ üõ Âþ ¥ ôø¢ ‚›¤¢ ‚ó¢‘ãõ ‚“ ¤ ‘õ ‚ó¢‘ãõ ßþ

x[f (x)]2 − f (x) + 1 = 0

ŕ

[1 ± 1 − 4x]
f (x) =
(2x)
‘õ

     
1 − 4x = (1 − 4x)1/2 = 1/2 1/2 1/2 (−4x)2 + · · ·
p
+ (−4x) +
0 1 2

: ‘“ ´¨ “Â“ n ≥ 1, xn °þ ® ö ¤¢ ‚î

 
1/2 (−4)n =
(1/2)((1/2) − 1)((1/2) − 2) · · · ((1/2) − n + 1)
(−4)n
n n!
(1/2)(1/2)(3/2) · · · ((2n − 3)/2)
= (−1)n−1 (−4)n
n!
(−1)2n (1)(3) · · · (2n − 3)
=
n!
(−1)2n (n!)(1)(3) · · · (2n − 3)(2n − 1)
=
(n!)(n!)(2n − 1)
 
(−1)(2)(4) · · · (2n)(1)(3) · · · (2n − 1) (−1) 2n
= =
(2n − 1)(n!)(n!) (2n − 1) n

ýÂ“ üԀõ ý Âþ¢‘Öõ , –¤ ¬ ßþ Âƒè ¤¢ ; ݃€î üõ ’‘¿µ÷ ¤ üԀõ ñ‘Øþ¢¤ f (x)¤¢
ŕ ; ݃€î üõ Àƒ• ‘û bn

" "   ##
1 X∞
1 2n
f (x) = 1− 1− xn
2x n=1
(2n − 1) n

¤¢ xn+1 °þ ® ÓÊ÷ , f (x) ¤¢ xn °þ ®, bn ø

 

X 1 2n xn
(2n − 1) n
n=1
üµÈ𥑓 ý‘û ݵþ ¤ Úó .2 ÛÊê 54

‚î üÞÆì ‚“ ,´¨

    
1 1 2(n + 1) (2n)! 1 2n
bn = = =
2 2(n + 1) − 1 (n + 1) (n + 1)!(n!) (n + 1) n

(1894 − 1814) ö„‘ —‘ î ö¦ ¤ ø ü Ø þÄ Ü “ öÀ ƒ ®‘ þ ¤ ‚ “ ’ Æ € õ ‚ î ¤ bn ¢À ä

Û ¡¢ ý‘ ú û¤ ¢À 㠗 ß ƒ ƒ 㠗 ¤¢ ¤ ‘ ú ÷ ö„‘ —‘ î .À € õ‘ ÷ ü õ ö„‘ —‘ î ¢À ä , À ÷

ö„‘ —‘ î ñø ¢À ä ´ Ô û . ´ ¨ ù¢Â “ ¤‘ î ‚ “ x1 x2 x3 · · · xn –¤‘ ± ä ß µ ªÁ ðà µ ÷Â •

.À€µÆû b6 = 132 ø b5 = 42, b4 = 14, b3 = 5, b2 = 2, b1 = 1, b0 = 1


3 ÛÊê

ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ

‘û

‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ .3

ù ƒ ¡£ ‚ Ñ ê‘ Ÿ ¥ ü µ Þ Æ ì ¤¢ ü µ Æ þ‘ “ ü —‘ ä… Ï ‚ ÷ ð  û ‚ î ü þ‘ ¹ ÷ ¥

üÔܵ¿õ ñ‘ت ‚“ ‚Ñꑟ ¤¢ À€÷— üõ, À€µÆƒ÷ á÷ ×þ ¥ ‘û ù¢¢ ‘fõø Ãó ‚Ø€þ ø ¢¢Âð

‚›— ‘“ ýù¢¢ Âû ùø…ä ‚“ ø .À÷ª üÈîø üÔܵ¿õ ñ‘ت ‚“ ߃€»Þû ø À÷ª ùƒ¡£

. Àû¢ «‘ʵ¡ ¢¡ ‚“ ¤ ‚Ñꑟ ¥ ü—ø‘Ôµõ ñÏ ´¨ ßØÞõ ö üð¤ Ó öÃƒõ ‚“

ü ê ã õ ¤ Ó Ü µ ¿ õ ᐁ ÷ Ý þ ¤¢ ‚ î ýù¢¢ ¤‘ µ ¡‘ ¨ ‚ “ ‚ › — ‘ “ Ý ƒ µ Æ û ¤  ± ¹ õ ß þÂ “‘ € “

°îÂõ ø ù¢‘¨ ýù¢¢ á÷ Ûت ø¢ ‚“ ݃÷— üõ ¤ ýù¢¢ ᐁ÷ üÜî¤ Ï ‚“ . ݃þ‘Þ÷

. ݃þ‘Þ÷ ýÀ€“ ‚µ¨¢

üÆþ÷ ‚õ‘÷ “ ý‘û ö‘“ ¥ ‚î À€µÆû üþ‘û ù¢¢ ᐁ÷ üޗ ‘þ ù¢‘¨ ý ù¢¢ á÷

.ùƒè ø char, bool ,int:À€÷‘õ ,Àû¢ üõ ¤Âì “ ¤‘î ¤‘ƒµ¡ ¤¢ ø ù¢Þ÷ Óþ Â㗠¤ ö‘÷

‚õ‘÷“ ‚“ ¤ ö‘÷Â“ ¤‘î ¢¡ ø ¢ª üõ ‚µ¡‘¨ Åþ÷ ‚õ‘÷“ ͨ— °îÂõ ýù¢¢ á÷

. ùƒè ø , struct , union : À€÷‘õ.À€î üõ üêÂãõ

‘f Ö Î € õ ý‘ û ‚ Ñ ê‘ Ÿ ¥ ý¢À 㠗 ¥ ‚ î ´ ¨ ù¢¢ ö‘ Þ µ ¡‘ ¨ ၠ÷ ß þ  — ù¢‘ ¨ : ‚ þ¤

:À€µÆû ö‘ÆØþ ñÏ ø á÷ ÂÑ÷ ¥ ‚î À€î üõ ù¢‘Ôµ¨ Ýû ¤ ø‘¹õ

x : array [’a’..’z’] of integer


int x[26];
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 56

(Sparse array)§¤‘³¨ ‚þ¤ 1.3


ý ¤Á𠤐ÀÖõ ýù½÷ üóø ¢ª ý ¤Á𠤐ÀÖõ ´¨ ¤Âì ‚î Ýþ ¤¢ ý ‚þ¤ ݃€î ­Âê

ø À÷ª üÞ÷ ý ¤Á𠤐ÀÖõ ‚þ¤ ý‘û ‚÷‘¡ ô‘Þ— ‘f õø Ãó ü€ãþ , ´¨ §¤‘³¨ Ûت ‚“

ý ¤Á𠤐ÀÖõ ¤ Ñ€õ ßþ ýÂ“ .À÷ª üõ ý ¤Áð¤ÀÖõ «‘¡ ý‘û‚÷‘¡ ¥ ü¡Â“ ÍÖê

´Æƒ÷ ¤Âì ø ´¨ ï¤ Ã“ ‚þ¤ ñÏ ö , ´Æƒ÷ üÖ΀õ ÂÔ¬ ¤ÀÖõ ‘“ ‚þ¤ ö¢Âî

‚ þ¤ ø¢ ¥ Ý þ ¤‘ ‘ ÷ ¤  Ñ € õ ß þ ýÂ “ . À ÷ ª ü È îø ùÀ € þ ¤¢ ‚ þ¤ ý‘ û ‚ ÷‘ ¡ ô‘ Þ —

‘û ‚þ¤ ßþ ¯‘±—¤ µú“ í¤¢ ýÂ“. ݃þ‘Þ÷ ù¢‘Ôµ¨ üܬ ý‚þ¤ ‘“ ñÏ Ýû üØÞî

:Àƒ€î ´ì¢ Âþ ¥ ñ‘·õ ‚“

1 2 3 4 5 6 7 8
T 3 11 13

1  29 U 3 4 5 6 7 8
a 4 8 2
?

j z
b 3 1 2
1 2 3 4 5 6 7 8

, T ü€ãþ üܬ ý‚þ¤ ý ¤Á𠤐ÀÖõ ¤‘“ Âû ¤¢. ݃€î üõ ÂÔ¬ ¤ ctr ƒçµõ ¤ÀÖõ Àµ“
݃û¢ üõ ¤Âì T ¥ ý ‚÷‘¡ ÅþÀ÷ “Â“ ¤ a[ctr] ø ݃€î üõ ‚ê‘® ctr ‚“ ÀŸø ×þ

ßþ ‚“ . ݃û¢ üõ ¤Âì ctr “Â“ ¤ b[ ÅþÀ÷] ߃€»Þû ø ´¨ ùÀª ý ¤Áð¤ÀÖõ ‚î

.¢ÂþÁ• üõ ô‘¹÷ ý ¤Áð¤ÀÖõ °ƒ——

ctr=0 →ctr=ctr+1=1 T[4]=11 → a[ctr]=a[1]=4 b[4]=1


ctr=1 →ctr=ctr+1=2 T[8]=13 → a[ctr]=a[2]=8 b[8]=2
ctr=2 →ctr=ctr+1=3 T[2]=3 → a[ctr]=a[3]=2 b[2]=3
Âþ ¥ ¯Âª ø¢ Àþ‘“ ‚÷ ‘þ Ýþ ù¢Âî ý ¤Áð ÅþÀ÷ ‘õ ¤ ý ‚÷‘¡ ݃÷À“ ݃û¿“ Âð ñ‘Ÿ

:À€ª‘“ ¤Âì“

1 ≤ b[i] ≤ ctr , a[b[i]] = i


57 üþø¢ø¢ ´¡¤¢ .2.3

. ´¨ ÂÔ¬ ctr ‚î ´¨ ¼®ø ݃ª‘“ ù¢ÂØ÷ üû¢ §¤¢ ýÝ Âð

ý ¤Áð¤ÀÖõ ‘õ ¤ T ô 8 ý‚÷‘¡ Û±ì ý‘û ‚þ¤ ¤¢ ݃÷À“ ݃û¡ üõ ñ‘·õ ö€ä ‚“

. ‚÷ ‘þ Ýþ ù¢Âî

b[8]=2 , 1≤ b[8]≤ 3 ⇒ ݃ª‘“ ù¢Âî üû¢ ¤ÀÖõ ‘õ ¤ ‚÷‘¡ ßþ ´¨ ßØÞõ

a[b[8]]=a[2]=8 ⇒ Ýþ ù¢Âî üû¢ ¤ÀÖõ ‘õ ¤ ‚÷‘¡ ßþ

üþø¢ø¢ ´¡¤¢ 2.3


.¢¤¢ À÷¥ Âê ø¢ ·îÀŸ öùÂð Âû ‚î ´¨ ý ¤¢ ‚Èþ ¤ ´¡¤¢ üþø¢ø¢ ´¡¤¢

.Àµê üõ ë‘Ô— ÂÔ¬ ¼Î¨ ¤¢ ö ý‘úð“ ô‘Þ— ‚î ´¨ ý €þ‘“ ´¡¤¢ ×þ :• ´¡¤¢

×þ á‘Ô—¤ . ´¨ ï“ ‘— ùÂð ö ¥ ƒÆõ ßþ µð¤ Ó ñÏ : ´¡¤¢ ¥ ùÂð ×þ á‘Ô—¤

.´¨ ‚Èþ ¤ á‘Ô—¤ , ´¡ ¤¢

ý ³¨ ý‘úó‘þ ¢À㗠) ‚Èþ ¤ ‘— ùÂð ö ‚ܬ‘ê ¥ ´Æ—¤‘±ä : (Depth)ùÂð ×þ ÕÞä

. (ùÂð ö ‘— ùÀª

ö á‘ Ô —¤ Û ®‘ Ô — ‘ “ ´ ¨  “Â “ ù ð ö ¼ Î ¨ ù ð  û ýÂ “ :(Level)ù ð × þ ¼ Î ¨

.ùÂð ö ÕÞä ¥ ´¡¤¢

: ñ‘·õ

ø¢ žÍ¨
a
ÂÔ¬ á‘Ô—¤

N
×þ á‘Ô—¤
b f ×þ žÍ¨

c d e ÂÔ¬ žÍ¨

ùÂð á‘Ô—¤ ÕÞä žÍ¨

a 2 0 2
b 1 1 1
c 0 2 0
d 0 2 0
e 0 2 0
f 0 1 1

ø¢ ý¤¢‘f Ö ƒ ì¢ ö ü Ü ¡¢ ý‘ û ù ð ô‘ Þ — ù ð n‘ “ ü þø¢ø¢ ´ ¡¤¢ × þ ¤¢ :ñ‘ · õ

ý‘û ùÂð ÕÞä áÞ¹õ I(T) ø ´¡¤¢ ý‘û ï“ ÕÞä áÞ¹õ E(T).À€ª‘“ üõÀ÷¥ Âê
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 58

ö üµÈ𥑓 ‚Ф ù‘Ú÷ Àª‘“ T(n)= E(T)-I(T)Âð .Àª‘“ üõ ´¡¤¢ ßþ üÜ¡¢

? ´¨ ôÀî

6 6

h
h+1

T(n) T(n+2)

E(T ) = E(T 0 ) − h + h + 1 + h + 1

I(T ) = I(T 0 ) + h

⇒ E(T ) − I(T ) = E(T 0 ) − I(T 0 ) + 2 ⇒ T (n + 2) = T (n) + 2

ø¢ ‘f Ö ƒ ì¢ ‘ þ À ÷¥ Â ê º ƒ û ö ù ð  û ‚ î ´ ¨ ü þø¢ø¢ ´ ¡¤¢ : Û õ‘ î ü þø¢ø¢ ´ ¡¤¢

.¢¤¢ À÷¥ Âê

‚î ݃õ‘÷ üõ ü¨‘¨ Ûõ‘î ´¡¤¢ ¤ üþø¢ø¢ ´¡¤¢ : ü¨‘¨ Ûõ‘î üþø¢ø¢ ´¡¤¢

) × þ ¼ Î ¨ ¥ ü þ‘ ûù ð Â Ú õÀ € ª‘ “À ÷¥  ê ø¢ ý¤¢ ‘f Ö ƒ ì¢ ö ü Ü ¡¢ ý‘ û ù ð ô‘ Þ —

üµÆþ‘“ À÷¥ Âê ö Àª‘“ ‚µª¢À÷¥ Âê ×þ ¼Î¨ ßþ¤¢ ýùÂð Âð .(¡ ‚“ üØþ ¼Î¨

À÷ùÀõ ö ² ´Þ¨ ¤¢ ‚î ö ¼Î¨ Ýû ý‘û ùÂð ô‘Þ— ùø…ä ‚“ø Àª‘“ ²À÷¥ Âê

¤Âì ´¨¤ ´Þ¨ ¤¢ ‚î ö ¼Î¨ Ýû ý‘û ùÂð ô‘Þ— ø À€ª‘“ ‚µª¢ À÷¥ Âê ø¢ Àþ‘“ ‘fփì¢

Ýû ô‘Þ— Àª‘“ ‚µªÀ÷ À÷¥ Âê ý ùÂð Âð ߃€»Þû ,À€ª‘“ ‚µªÀ÷ À÷¥ Âê üµÆþ‘“ À÷‚µêÂð

¼Î¨ ¤¢ ‘û ï“ ô‘Þ— ´¡¤¢ ßþ¤¢ .À€ª‘“ À÷¥ Âê öøÀ“ Àþ‘“ ö ´¨¤ ý‘û ü½Î¨

. ´¨ ö¥µõ ‚ȃÞû ´¡¤¢ ŕ À€û¢ üõ  ¤ ×þ ‘þ ÂÔ¬

: ü¨‘¨ Ûõ‘î üþø¢ø¢ ´¡¤¢ ¥ ý ÂÚþ¢ Óþ Âã—

ø Ýþ ƒړ ÂÑ÷ ¤¢ ‘û ï“ éÂÏ ‚“‚Èþ ¤ ¥ ü®Âê ¤ Ï ‚“ ¤ • üþø¢ø¢ ´¡¤¢ ×þ Âð

¥  ð , Ý ƒ û¢ ´ ± Æ ÷ ù ð  û ‚ “ ¤ ý¢ ã ¬ ý‘ û ù¤‘ Þ ª ´ ¨¤ ‚ “ ²  ¥ ¼ Î ¨Â û ¤¢


59 MAX HEAP ´¡¤¢ .3.3

ü¨‘¨ Ûõ‘î ¤ Àþ üõ ´¨¢ ‚“ ‚î üµ¡¤¢ , ݃€î éÁŸ ¤ ‘û ùÂð À㓠‚“ ý ù¤‘Þª

.À€þð

À㓠‚“ 6 ¥ éÁŸ
- 1
1

3 2 3
2

4 5 6 7 4 5

ü¨‘¨ Ûõ‘î üþø¢ø¢ ´¡¤¢


8 9 10 11 12 13 14 15

. ´¨ ü¨‘¨ Ûõ‘î üþø¢ø¢ ´¡¤¢ ,Ûõ‘î üþø¢ø¢ ´¡¤¢ ¥ ¤ Ñ€õ °µî ü¡Â“ ¤¢

max heap ´¡¤¢ 3.3


°ÆÂ“ ‚î ù¢¤ ¡ °ÆÂ“ ý¢Àä ‘“ ö ùÂð Âû ‚î ´¨ ü¨‘¨ Ûõ‘î üþø¢ø¢ ´¡¤¢

ÀƒÜî üóø ´Æƒ÷ ¢Âê ‚“ Âʽ€õ °ÆÂ“). ´Æƒ÷ µ؝î Ç÷À÷¥ Âê °ÆÂ“ ¥ ¤À•

‚“. ´¨ ‚þ¤ heap ý¥‘¨ ù¢‘ƒ• ýÂ“ ù¢¢ ö‘Þµ¡‘¨ ßþ µú“ (. ´¨ ¢Âê ‚“ Âʽ€õ

ÝÞþ Ãî‘õ Âʀä‚ȃÞû .À€µÆû 2i , 2i+1 ý‘û ‚÷‘¡ ¤¢ ô i Âʀä öÀ÷¥ Âê ‚î ý ¤ Ï

,Ã÷ Ýރ€ƒõ Âʀä ö¢Âî Àƒ• . ´¨ θ(1)¥ ÝÞþ Ãî‘õ ö¢Âî Àƒ• ŕ , ´¨ ‚þ¤ ¨

.¢Âî ם ¤ ‘û ï“ Àþ‘“ ‘ú€— ö


n
1 · · · b n2 c b c + 1···n
| 2 {z }
dn
2e
. (´¨ d n2 e ‘û ‚Æþ‘Öõ ¢Àã—)
sift-up –‘ ƒ Ü Þ ä.À € µ Æû sift-downø (perculate)sift-up, heap ¤¢ Ý ú õ –‘ ƒ Ü Þ ä ø¢

ý¢À ä ‚ “  µ Þ î ý¢À ä ¥ ù ð × þ ° Æ Â “ ‚ î ¢Â ƒ ð ü õ –¤  ¬ ø ´ ¨ ô¥„ ü ÷‘ õ¥

µÞî ý¢Àä ßþ ÃÚþ‘› µȃ“ ý¢Àä ‚î ü÷‘õ¥ sift-down –‘ƒÜÞäøÀ“‘þ ƒƒç— µȃ“

.¢¢Âð
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 60

procedure alter − heap (T[1..n],i,v)


{ T[1..n] is a heap ,the value of T[i] is set to v and the heap
property is re-established we suppose the 1 ≤ i ≤ n }
x ← T[i]
T[i] ← v
if v < x then sift-down (T,i)
else percolate (T,i)
.....................................................................
procedure sif t − down (T[1..n],i)
{ this procedure sifts node i down so as to re-establish the
heap property in T[1..n] we suppose that T would be a heap if T[i]
were sufficiently large we also suppose that 1 ≤ i ≤ n }
k←i
repeat
j ← k {find the larger of node j }
if (2j≤ n and T[2j]> T[k])
k ← 2j
if (2j < n and T[2j+1]> T[k])
k ← 2j+1
exchange T[j] and T[k]
{if j == k then the node has arrived at its final position }
until j = k //O(logn2 )
: ù‘Ú÷ Àª‘“ h, heap á‘Ô—¤ø Àª‘“ n , heap ý‘û ùÂ𠢐À㗠Àƒ€î ­Âê

h+1
20 + 21 +....+2h−1 < n ≤ 20 + 21 +....+2h ⇒ 22−−11 < n ≤ 2 2−1−1
h

• n≤ 2h+1 -1 =⇒ n+1≤ 2h+1 =⇒ lg(n + 1) ≤ h + 1


⇒ h ≥ lg(n + 1) − 1 =⇒ h=dlg(n + 1) − 1e=blg nc

• n>2h -1⇒ n+1 > 2h ⇒ h < lg n + 1 ⇒h-1 < lg(n + 1)-1 ≤ h

.´¨ O(logn2 ) ¥Ýµþ ¤ Úó ö‘õ¥ ŕ


61 MAX HEAP ´¡¤¢ .3.3

procedure percolate(T[1..n],i)
{we suppose that T would be a heap if T[i] were sufficiently small,
we also suppose that 1≤i≤ n the parametr n is not used here}
k←i
repeat
j←k
if ((j>1) && ( T[ 2j ] < T[k]) )
k← 2j
exchange T[j] and T[k]
until j=k; //O(log n)
........................................................
f unction f ind − max( T[1..n] )
{ returns the largest element of the heap T[1..n]}
return T[1]; //θ(1)
........................................................
procedure delete − max(T[1..n])
Delete the root}
T[1]←T[n]
sift-Down(T[1..n-1],1) //O(log n)

ÛÞä ø ¢Âƒð üõ ¤Âì Âʀä ߃óø ý‘› ‚“ Âʀä ßþ ¡ .´¨ üÖ΀õ éÁŸ, éÁŸ

.¢ª üõ ô‘¹÷ sift-Down


procedure insert − N ode(T[1..n],v)
T[n+1] ← v
percolate(T[1..n+1],n+1) {O(lg n)}

: heap ´¡¤¢ ´¡‘¨

P rocedure slow − M akeHeap(T[1..n])


{this procedure makes the array T[1..n] into a heap}
for i ← 2 to n do
percolate(T[1..n],i) // O(n log n)
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 62

P rocedure M akeHeap(T[1..n])

For i ← b n2 c DownTo 1 Do

SiftDown(T, i) //O(n)

Heap À ÷ — ü õ O(n) ‚ ± — õ ‘ “ n ñ Ï ‚ “ ù ¿ ó¢ ‚ þ¤ × þ ¥„‘ “ Ý µ þ ¤  Ú ó : ‚ ƒ Ì ì

.¢¥‘Æ“

: ö‘û“

r+1 ·îÀŸ ž®ø ‚“ r ¼Î¨ ¤¢ ùÂð ×þ ýÂ“ SiftDown ݵþ ¤ Úó ¤¢ Repeat ‚Öܟ
´¡¤¢ ßþ á‘Ô—¤ Âð ñ‘Ÿ (.©¢¡ ‘“ Ýû ¤‘“ ×þ ø Àþüõ ߃þ‘• ¤‘“ r) ¢¤¢ ǡ
n
 þ ¥ ñ õÂ ê ¥ ô¥„ ý‘ û ´  Û î ¢À 㠗 Ý þ  ƒ Ú “ Â Ñ ÷ ¤¢ ¤ K = blog2 c ¤ Heap
:¢Èƒõ ‚±¨‘½õ

.For ‚Öܟ ¤¢ SiftDown ¤¢ Repeat ‚Öܟ ý‘û ǡ ¢À㗠:t

t ≤ 2 × 2k−1 + 3 × 2k−2 + . . . + (k + 1) × 20 ⇒

t ≤ −2k + 2k + 2 × 2k−1 + 3 × 2k−2 + . . . + (k + 1) × 20 ⇒

t < −2k + 2k+1 (2−1 + 2 × 2−2 + 3 × 2−3 + . . .) ⇒


P

t < −2k + 2k+1 n( 12 )n < −2k + 2k+1 × 2 ⇒ t < 2k (22 − 1) ⇒
n=1

⇒ t < 3 × 2k ⇒ t < 3n ⇒ t ∈ O(n) K = blogn2 c ⇒ 2k ' n

1 1
1−x = 1 + x + x + . . . 2 = 1 + 2x + 3x + . . .
2 ⇒ (1−x) 2

P

x
⇒ (1−x) 2 = x + 2x2 + 3x3 + . . . x
⇒ (1−x) 2 = nxn
n=1
P
∞ 1
⇒ n( 12 )n = 2
(1− 12 )2
=2
n=1

( ý‚Üޛø¢ ²ƒû) Binomial Heap 4.3

.´¨ ý‚Üޛø¢ ý‘û ´¡¤¢ ¥ Ûڀ› ×þ Binomial Heap ×þ


ùÂð n2 ýÂÚþ¢ø ùÂð n1 üØþ ‚î. Ýþ ¤¢ ¥‘ƒ÷ heap ø¢ ô‘袐 ‚“ heap ×þ ßµ¡‘¨ ýÂ“ : ‚›—)

(.Àª‘“ üõ O(n1 + n2 ) heap ø¢ ßþ ô‘袐 ö‘õ¥.¢¤¢


63 ( ý‚Üޛø¢ ²ƒû) BINOMIAL HEAP .4.3

( ý ‚Üޛø¢ ´¡¤¢ )Binomial Tree 1.4.3

¢¡ Bn−1 ‚Èþ ¤ ‚“ á÷ ßþ ¤¢.¢ª üõ Óþ Â㗠üµÈ𥑓 –¤ ¬ ‚“ ´¡¤¢ á÷ ßþ

.À€€î üõ ‚ê‘® ´¨¤ À÷¥ Âê ö€ä ‚“¤ Bn−1

1 1 1 1
1 2 3
1 3

1
B0 B1 B2 B3



 n=0
Bn =


. ݃þ‘Þ÷ üõ ‚ê‘® ¤ Bn−1 ¢¡ Bn−1 ‚“

.´¨ (x + y)n ÍƓ °þÂ® Âþ ¥ ;¢ª üõ ùÀƒõ‘÷ ý‚Üޛø¢ ´¡¤¢,´¡¤¢ ßþ

. 2n ‘“ ´¨ “Â“ Bn ý‘û ùÂ𠢐À㗠•

. 2n−1 ‘“ ´¨ “Â“ Bn ý‘û ï“¢À㗠•

. ´¨ 2n−1 “Â“ Bn üÜ¡¢ ý‘û ùÂð¢À㗠•



À÷¤À÷ üìÂê ÕÞä ‘þ ¼Î¨) .
kn ‘“ ´¨ “Â“ ô k ¼Î¨ ¤¢ Bn ý‘û ùÂ𠢐À㗠•
( ´¨ ö ¤‘Öµõ ´¡¤¢ Âþ ¥

. ´¨ µȃ“ ÂÚþ¢ ý‘ûùÂð‚Þû ¥ ùÂð ßþ ‚›¤¢ , ´¨ nÃ÷ Bn ‚Èþ ¤ ‚›¤¢ •

Max Binomial Tree 2.4.3

¤À • ù
b  ð ¢À ä ‚ î,À ª‘ “ ù¢¤  ¡ ° Æ Â “ ý¢À ä ‘ “ ý‚ Ü Þ ›ø¢ ´ ¡¤¢ × þ ù ð  û  ð

.À€þð Max Binamial Tree ×þ ¤ öÀª‘±÷ Ç÷À÷¥ Âê ¥ µ؝î


‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 64

The Merge Of Max Binomial Trees 3.4.3

ù½÷. À€û¢ üõ ¤ Bn+1 ,Max Binomial Tree Bn0 ø Max Binomial Tree Bn ø¢

ö ‚ È þ ¤ ° Æ Â “ ¢À ä ‚ î ü µ ¡¤¢ , ´ ¡¤¢ ø¢  û ß ƒ “‚ î ´ ¨ ü Ü Ø ª ‚ “ merge


.Àª Àû¡ ÂÚþ¢ ´¡¤¢ ´¨¤ À÷¥ Âê ´¨ µ؝î

.´¨ ´¨¤ À÷¥ Âê min Âʀä ø ¢¤¢ ¢›ø ‚Æþ‘Öõ ÍÖê . ´¨ θ(1)¥ merge ÛÞä

:Àƒþ‘õÂê ´ì¢ Âþ ¥ ñ‘·õ ‚“

8 9

7 6 2 7 6 4
+
3 1 2 4 3 2

.5 1

7 6 4 8

⇒ 3 7 6 2
4 2

1 3 1 2

.5

Binomial Heap 4.4.3

. ´¨‘û Binomial Tree ¥ ý ‚äÞ¹õ H ,Binomial Heap ×þ


65 ( ý‚Üޛø¢ ²ƒû) BINOMIAL HEAP .4.3

Binomial-Heap

Head[H]
- 10 - 1 - 6

12 25 8 14 29

18 11 17 38

27

¤ Âþ ¥ «¡ ‚î Àƒþ‘Þ÷ üõ ùÀû‘Èõ ¤ Min Binomial Heap ×þ „‘“ Ûت ¤¢

:¢¤¢

ü€ãþ ,´¨¤¢ ¤ Ýރ€ƒõ Heap ´¡¤¢ ×þ «¡ Binomial Tree Âû .1

1 .´¨ ©¤À• ÀƒÜî ýø‘Æõ µð¤ Ó Node Âû ÀƒÜî

¢¤¢ ¢ ›ø H ¤¢ Binomial Tree ×þ k ü Ô € õ‘ ÷ ¼ ƒ ½ ¬ ¢À ä  û ýÂ “ .2

,n ‘ “ Binomial Heap × þ ‚ î ´ ¨‘ € ã õ ß þÀ “ ´ ƒ ¬‘ ¡ ß þ .´ ¨ k ö ý‚ ›¤¢ ‚ î

.ùÀª ۃØȗ Binomial Tree ,blg nc + 1 ¥ ·îÀŸ Node

Min Binomial Heap ýø ¤ “ –‘ƒÜÞä 5.4.3

.¢¤À÷ ýÌä ºƒû ø ´¨ üó‘¡ ‚î ¢¥‘¨ üõ Heap ´¡¤¢ ×þ : Make-Heap() •

.À€î üõ ‚ê‘® H ´¡¤¢ ‚“ ¤ x , Node : Insert(H, x) •

ÀƒÜî ßþ µÞî ‚î À÷¢Âð üõ“ H ´¡¤¢ ¥ ý Node ‚“ ý Âð ù¤‘ª : Minimum(H) •


.Àª‘“ ‚µª¢ ¤

Delete ¤ À ª‘ “ ‚ µ ª¢ ¤ ¤À Ö õ ß þ  µ Þ î ‚ î Heap ¥ ýNode : Extract-Min(H) •


.À÷¢Âð üõ“ ¤ Node ¢¡ ø À€î üõ

–¤¬ ‚“ Ýû ø ¢Þ÷ Óþ Â㗠Ýރ€ƒõ Heap –¤ ¬ ‚“ ö— üõ Ýû ¤‘û Heap Àƒª‘“ ‚µª¢ ‚›— 1
.¢¤À÷ ü÷À€ –ø‘Ô— ‘û ö ýø ¤ “ –‘ƒÜÞä –¤ ¬ Âû ¤¢ ÝÞþ Ãî‘õ Heap
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 66

ý‘ û Node ‚ Þ û Û õ‘ ª ‚ î À ÷¢Â ð ü õ “ ¤ ýHeap ´ ¡¤¢ : Union(H1 , H2 ) •


.¢Â±ƒõ ߃“ ¥ ¤ H2 ø H1 ,Heap ý‘û ´¡¤¢

‚î Àû¢ üõ ¤ k ÀþÀ› ¤ÀÖõ H ´¡¤¢ ¥ x ,Node ‚“ : Decrease-Key(H, x, k) •


(x > k) .´¨ ö üÜ±ì ¤ÀÖõ ¥ µÞî ÀþÀ› ¤ÀÖõ ßþ

.À€î üõ éÁŸ H ´¡¤¢ ¥ ¤ x ,Node : Delete(H, x) •

Max Binomial Heap 6.4.3

‚Èþ ¤ ‚î ;´¨ ÝÞþ Ãî‘õ ý ‚Üޛ ø¢ ö‘µ¡¤¢ ¥ üÜڀ› Max Binomial Heap ×þ

ÇÜê ×þ ùø…ä ‚“ ø À€Üʵõ Ýû ‚“ üþ‘û Âð ù¤‘ª Õþ ÂÏ ¥ ‘û ´¡¤¢ ßþ ô‘Þ— ý‘û

Ûڀ› ý‘û ‚Èþ ¤ Âþ‘¨ ߃“ ö °ÆÂ“ ¢Àä ‚î ¢¤¢ ¢›ø ´¡¤¢ ¥ ü ‚Èþ ¤ ‚“ ¢¥

:Âþ ¥ ñ‘·õ À€÷‘õ.Àª‘“ ÝÞþ Ãî‘õ


4 ........ 9 ..................... 7

3 2 5 6 4

1 5 2 3

ü õ ùÀ û‘ È õ ¤ Max Binomial Heap ø¢ merge –‘ ƒ Ü Þ ä ¥ ‚ ÷ Þ ÷ × þ  þ ¥ ¤¢

.´¨ Ýî ‘û ‚Èþ ¤ ¢À㗠Âþ ¥,´¨ θ(1)¥Ãƒ÷ ݵþ ¤ Úó ßþ ö‘õ¥ .Àƒþ‘Þ÷
67 FIBONACCI HEAP .5.3

R ?
8 ......... 6 4 .......... 9 ............. 7

4 3 4 3 2 5 6 4
+
2 1 2 1 5 2 3

1 2

?
8 ......... 9 ............. 7

4 3 2 5 6 4 6

⇒ 4 3 4
1 5 2 3
2 1 2
2
1

.Àƒ€î °ƒî— ¤ ÀþÀ› ø ÝþÀì ý €ÈØþ¢ ø¢‘û Binomial Heap ͨ—: ßþ Âޗ

FIBONACCI HEAP 5.3

Fibonacci Tree 1.5.3

:¢ª üõ Óþ Â㗠üµÈ𥑓 –¤ ¬ ‚“ ´¡¤¢ á÷ ßþ:Óþ Âã—



 n=0









Fn = n=1










 ݃€î üõ ‚ê‘® ´¨¤ ´Þ¨ À÷¥ Âê ö€ä ‚“ ¤ Fn−2 , Fn−1 ‚Èþ ¤ ‚“ else
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 68

.Àƒ€î ‚ê‘® ´¨¤ À÷¥ Âê ö€ä ‚“ ¤ Fn−2 ,Fn−1 ‚Èþ ¤ ‚“ ´¡¤¢ á÷ ßþ ¤¢

F0: F1: F2: F3 :

F4 :

Max Fibonacci Tree 2.5.3

° Æ Â “ ¥ ¤À • ° Æ Â “ ø ´ ¨ ° Æ Â “ ý¤¢ öù ð  û ‚ î ´ ¨ ü ‘ ÷ ± ƒ ê ´ ¡¤¢

.Àª‘“üÞ÷ µ؝î‘û À÷¥ Âê

: ݃€î ý ¤Áðù¤‘Þª °ƒ—— ßþ ‚“ ¤ ü‘÷±ƒê ý ¨ Âð

f0 = 0 , f1 = 1 , f2 = 1 , f3 = 2, ...
‘“ ´ ¨  “Â “ Fn ý‘ û  ¢À 㠗 ø fn+2 ‘ “ ´ ¨  “Â “ Fn ý‘ ûù 𠢐À 㠗 ù‘ Ú ÷

. (n ≥ 0) fn ‘“ ´¨ “Â“ Fn üÜ¡¢ ý‘ûùÂ𠢐À㗠. fn+1

Fibonacci Heap 3.5.3

ö‘µ¡¤¢ ßþ ý‘û ‚Èþ ¤ ‚î ´¨‘û Fibonacci Tree¥ üÜڀ› Fibonacci Heap ×þ

. À€Üʵõ Ýû ‚“ ý Âð ù¤‘ª Õþ ÂÏ ¥

Max Fibonacci Heap 4.5.3

Max Binomial Heap Õ “‘ Î õ ‚ î ´ ¨ Max Fibonacci Tree ö‘ µ ¡¤¢ ¥ ü Ü Ú € ›

ø ´¨ Fi+2 Û¬‘Ÿ ø ¢ÂƒÚƒõ –¤ ¬ Fi+1 ø Fi ߃“ ö¢Âî Merge ÛÞä üóø ´¨

ŕ .¢Âî ‚ê‘® Fi+1 ´¨¤ À÷¥ Âê ö€ä ‚“ ¤ Fi Àþ‘“ ‘ú÷ ö¢Âî Merge ¤¢ ‘¹€þ ¤¢
69 2-3 ö‘µ¡¤¢ .6.3

ýø‘Æõ ‘þ µð¤ Ó Fi+1 ‚Èþ ¤ ¢Àä ‘þ ,Àµê ë‘Ô— ´¨ ßØÞõ ´ó‘Ÿ ø¢ ö¢Âî ‚ê‘® ¥

Û Þ ä × þ À þ‘ “ –¤  Ê € þ  ƒ è ¤¢ ‘ þ ø ´ ¨ ùÀ ª ô‘ Þ — ¤‘ î ‚ î ´ ¨ Fi ‚ È þ ¤ ¢À ä

üڵƓ Fi ‚“ ö‘õ¥ ŕ . ݃ª‘“ ‚µª¢ Fi ´Þ¨ ‚“ Fi+1 ´¡¤¢ ‚Èþ ¤ ¥ SiftDown


.´¨ ´¡¤¢ á‘Ô—¤ ݵþ ¤‘Úó “Â“ ·îÀŸ ø ¢¤¢

Àª¤ Âþ ¥ ´¨ âþ ¨ ü܃¡ ÅþÀ÷ ‚“ ´±Æ÷ ü‘÷±ƒê ´¡¤¢ ý‘û ï“ ÇþÃê

‘“ ´¨ “Â“ ø À€î üõ Àª¤ üõ¤ ø ü“¡ ‚“ á‘Ô—¤ üóø ´¨ ¢‘þ ¥ ü‘÷±ƒê ⓑ—

.´¨ lg∗ ÀŸ ¤¢ ø ¢¤À÷ ‚€þ Ãû ¤‘ƒÆ“ SiftDown ÛÞä ‚¹ƒµ÷ ¤¢ Height(Fn ) = d n2 e


.ÀûÀ“ ¤ ü‘÷±ƒê ý‘û ´¡¤¢ ‚î ÀƒÆþ€“ ý ‚õ‘÷“ : ßþ Âޗ

2-3 ö‘µ¡¤¢ 6.3


:
2 Óþ Âã—

.Àª‘“ üõ Âþ ¥ ý‘û üðÄþø ý¤¢ ‚î Àª‘“ üõ ¹µÆ› ´¡¤¢ ×þ 2-3 ´¡¤¢ ×þ

× þ ø Â Ê € ä × þ ý¤¢ 2-node × þ.´ ¨ 3-node ‘þ 2-node ‘ þ ü Ü ¡¢ ù ð  û •


.Àª‘“ üõ Âʀä ø¢ ý¤¢ 3-node

.À€ª‘“ 2-node ×þ öÀ÷¥ Âê ùÀ€û¢ ö‘È÷ MiddleChil ø LeftChild Àƒ€î ­Âê •


ô‘ Þ — .À € ª‘ “ ö À ƒ Ü î dataL.key ø ù ð ß þ Â Ê € ä dataL À ƒ € î ­Â ê ß ƒ € » Þ û

dataL.key ¥  µ Þ î À ƒ Ü î ý¤¢ LeftChild ‚ È þ ¤ ‘ “ 2-3 ´ ¡¤¢  þ ¥ ¤¢  ¬‘ € ä

MiddleChild ‚ È þ ¤ ‘ “ 2-3 ´ ¡¤¢  þ ¥ ¤¢  ¬‘ € ä ü õ‘ Þ — ‚ î ü ó‘ Ÿ ¤¢ ,À € µ Æ û

.Àª‘“ üõ dataL.key ¥ µð¤ Ó ÀƒÜî ý¤¢

3-node × þ ùÀ € û¢ ö‘ È ÷ LeftChild ,MiddleChild,RightChild À ƒ € î ­Â ê •


.À€ª‘“ ùÂð ßþ Âʀä ø¢ dataL,dataR Àƒ€î ­Âê .À€µÆû

 þ ¥ ¤¢ ‘ û À ƒ Ü î ‚ Þ û ß ƒ € » Þ û ø ù¢ “ dataL.key < dataR.keyù‘ Ú ÷

 þ ¥ ¤¢ ý‘ û À ƒ Ü î ô‘ Þ — dataL.key¥ µÞî LeftChild ‚ È þ ¤ ‘ “ 2-3 ´ ¡ ¤¢

ø dataL.key ¥ µð¤ Ó ø dataR.key¥ µÞî MiddleChild ‚Èþ ¤ ‘“ 2-3 ´¡¤¢

.À€µÆû dataR.key ¥ µð¤ Ó RightChild ‚Èþ ¤ ‘“2-3 ´¡¤¢ Âþ ¥ ¤¢ ‘û ÀƒÜî ô‘Þ—

.À÷¤¢ ¤Âì ¼Î¨ ×þ ¤¢ ü›¤‘¡ ý‘û ùÂð ô‘Þ— •

FUNDAMENTALS OF DATA STRUCTURES IN C++, ’‘µî ¥ ‚µêÂð“ 2


Ellis Horowitz,Sartaj Sahni,Dinesh Mehta
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 70

.´¨ ùÀª ‚Ž¤ 1 Ûت ¤¢ 2-3 ´¡¤¢ ¥ üó‘·õ

A
40

B C
10 20 80

2-3 ´¡¤¢ ¥ üó‘·õ 1 Ûت

¤¢ .À÷ª üÞ÷ ù¢¢ Çþ‘Þ÷ —ƒ³õ‘î ¤¢ üØþ Ãê ø üãìø ¤ Ï ‚“ ü›¤‘¡ ý‘û ùÂð

.¢Âƒð üõ ¤Âì ÂÔ¬ ‘“ “Â“ ü›¤‘¡ ùÂð Âû ¤à‘€µõ À÷¥ Âê ù¢¢ Ìä ­ä

3h
− 1ø 2h − 1 ߃“ Âʀä n ø h á‘Ô—¤ ‘“ 2-3 ´¡¤¢ ×þ ¤¢ ¬‘€ä ßþ ¢Àã—
üÜ¡¢ ùÂð Âû ‚î ü÷‘õ¥ ñø ‚÷Âî ‚î Àƒ€î ‚›— , °ÜÎõ ßþ ùÀû‘Èõ ýÂ“ . ´¨

‚ î ü ÷‘ õ¥ ôø¢ ‚ ÷Â î ‚ î ü ó‘ Ÿ ¤¢ ¢¢Â ð ü õ ñ‘ Þ ä , ¢Â ƒ ð ü õ ¤Â ì 2-node × þ ¤¢

, ´ƒã®ø ø¢ ßþ.Àª Àû¡ ñ‘Þä , ¢¢Âð üõ âìø 3-node ×þ ¤¢ üÜ¡¢ ùÂð Âû

3-node ø 2-node ý¢À㗠ý¤¢ 2-3 ´¡¤¢ ×þ.À€€î üõ ‚Ž¤ ¤ ë ê ‚÷Âî ø¢

.¢“ Àû¡

h=1 X 20
h = 2 .. X X .. 21
...
..... ...
.. ...
h=h 2h−1
2-node
l m
n = 20 + 21 + · · · + 2h−1 ⇒ n = 22−−11 = 2h − 1 ⇒ h = log2n+1
h
71 2-3 ö‘µ¡¤¢ .6.3

h=1 X X 2 × 30
h=2 X .X X .X X .. X 2 × 31
.. .. ...
..... ..
.. ...
. ...
... ..
. ..
h=h 2 × 3h−1
3-node

n = 2 × 30 + 2 × 31 + · · · + 2 × 3h−1 ⇒ n = 2×(33−1−1) = 3h − 1 ⇒ h =
h

l m
logn+ 1
3
: ßþÂ“‘€“
l m l m
logn+ 1 < h < logn+1
3 2

: ‘û §…î ¥ ù¢‘Ôµ¨ ‘“ 2-3 ´¡¤¢ ×þ Çþ‘Þ÷

templete<class KeyT ype >class Two3;//forward declaration

templete< class KeyT ype >

class Two3Node{

friend class Two3 < KeyT ype >;

private :

Element< KeyT ype > dataL,dataR;

Two3Node *LeftChild, *MiddleChild, *RightChild;};

templete< class KeyT ype >

class Two3{

public:

Two3(KeyT ype max,Two3Node< KeyT ype > *init=0)

:MAXKEY(max), root(init){};//constractor

Boolean Insert(const Element< KeyT ype > &);


‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 72

Boolean Delete (const Element< KeyT ype > &);

Tow3Node< KeyT ype > *Search(const Element < KeyT ype >&);

private:

Tow3Node< KeyT ype > *root;

KeyT ype MAXKEY;

};

ø Àª‘±÷ M AXKEY ÀƒÜî ý¤¢ ý ±µãõ ÂÊ€ä ºƒû ‚î ݃€î üõ ­Âê ‘¹€þ ¤¢

‘ú€— . ´¨ dataR.key = M AXKEY ý¤¢ 2−node ×þ ‚î ݃€îüõ ¢¢¤Âì

À÷¥ Âê ø¢ ‚“ MiddleChild ø LeftChild ø ¢ªüõ ùƒ¡£ dataL ¤¢ ùÂð ßþ Âʀä

.¢¢ ´±Æ÷ ù¿ó¢ ¤ÀÖõ Âû ö— üõ RightChild ù¢¢ Ìä ‚“ .À€€î üõ ù¤‘ª ö

2-3 ´¡¤¢ ×þ ý¹µÆ› 1.6.3

ⓑ— ö¢¤ ø ´¨¢ ‚“ ýÂ“ ¤ üŽø¢ø¢ ý¹µÆ› ö‘µ¡¤¢ ݵþ ¤ Úó ö— üõ ü𢑨 ‚“

.¢¢ ݃Þ㗠À€î üõ ¹µÆ› ¤ xÀƒÜî ‘“ Âʀä ýø‘Ÿ ùÂð ‚îTow3::Search ý¹µÆ›
ý‘ûÀƒÜî ‘“ ¤ x ÀƒÜî ×þ ‚î À€î üõ ù¢‘Ôµ¨ compare ô‘÷ ‚“ ü㓑— ¥ ¹µÆ› ⓑ—

3,2,1  þ¢‘ Ö õ ° ƒ — — ‚ “ ⠓‘ — ß þ.À € îü õ ‚ Æ þ‘ Ö õ p À € ÷‘ õ É ¿ È õ ù ð × þ ¤¢ â ìø

¥  µ ð¤ à “ ,ôø¢ ø ñø ý‘ ûÀ ƒ Ü î ß ƒ “ , ñø À ƒ Ü î ¥  µ Þ î x ‘ þ ‚ î ß þ ‚ “ ‚ µ Æ “ ¤ 4‘ þ


‚Öܟ ¤Âؗ ¢Àã—.Àû¢ üõ ´Èð“ , Àª‘“ p¤¢ ý‘ûÀƒÜî ¥ üØþ ‘“ “Â“ ‘þ ôø¢ ÀƒÜî

ùÂð n ý¤¢ ´¡¤¢ Âð ßþÂ“‘€“ . Àª‘“ üõ 2-3´¡¤¢ á‘Ô—¤ ‚“ ¢øÀ½õ for ý‘û

.¢“ Àû¡ O(log n) “Â“ Tow3::Search ⓑ— üðÀƒ»ƒ• ù‘Ú÷ ,Àª‘“

templete< class KeyT ype >


Tow3Node< KeyT ype > *Tow3< KeyT ype >::
Search(const Element< KeyT ype >& x)
//If the element x is not in the tree,then return 0,Otherwise
//return a pointer to the node that contains this element.
{
for(Tow3Node< KeyT ype > *p=root;p;)
73 2-3 ö‘µ¡¤¢ .6.3

switch(p→ compare(x)){
case 1 :p=p→ LeftChild;break;
case 2 :p=p→ MiddleChild;break;
case 3 :p=p→RightChild;break;
case 4 :return p ;// x is one of the keys in p
}
}

2-3 ´¡¤¢ ×þ Û¡¢ ‚“ š¤¢ 2.6.3

‚“ 70 ÂÊ€ä š¤¢ ñ‘·õ ýÂ“ .Àª‘“ üõ ù¢‘¨ ‘f µ±Æ÷ 2-3 ´¡¤¢ ×þ Û¡¢ ‚“ š¤¢

ßþ ßµê‘þ ýÂ“ ô¥„ ý¹µÆ› Àµ“ ¤¢.Àþ ƒړ ÂÑ÷ ¤¢ ¤ 1 Ûت 2-3 ´¡¤¢ Û¡¢

f ±ì ÀƒÜî Âð .݃û¢üõ ô‘¹÷ ¤ ÀƒÜî


‘ûÀƒÜî ô‘Þ— ‚Ø€þ Âϑ¿“ ù‘Ú÷ Àª‘“ ´¡¤¢ ¤¢ …

üÞ÷ ô‘¹÷ ø ùÀª ø “ø ¤ ´Æت ‘“ š¤¢ ÛÞä À€µÆû ¢Âê ‚“ Âʽ€õ 2-3 ´¡¤¢ ¤¢

Û ¡¢ ‚ “ ¤ ö Á ó ¢¤À ÷ ¢ ›ø 2-3 ´ ¡¤¢ ¤¢ 70 Â Ê € ä ö  ñ‘ · õ ß þ ¤¢.¢Â ƒ ð

݃÷À“ , 70 Âʀä ý¹µÆ› ñ…¡ ¤¢ ´¨ ô¥„ ¤‘î ßþ ýÂ“. ݃€î üõ š¤¢ ´¡¤¢

´ ¡¤¢ ¤¢ ‚ î ¤ ýÀ ƒ Ü î ù‘ ð û À ƒ ª‘ “ ‚ µ ª¢ ‚ › — . Ý þ ª ü õ ø  “ø ¤ ù ð ôÀ î ‘ “ ‚ î

¢Âê ‚“ Âʽ€õ ï“ ùÂð ×þ ‘“ ¹µÆ› , ݃û¢ ¤Âì ¹µÆ› ¢¤ õ ¢¤À÷ ¢›ø 2-3

¢“ ݃û¡ ‚›õ ö ‘“ 70 Âʀä ý¹µÆ› üÏ ¤¢ ‚î üð“ ùÂð .Àª Àû¡ ø “ø ¤

ÀþÀ› Âʀä, ´¨ Âʀä ×þ ý¤¢ ‘ú€— ùÂð ßþ ‚î ‘¹÷ ¥. ´¨ 80 ÀƒÜî ‘“ C ùÂð

ù¢¢ ö‘È÷ (Óó) ´ÞÆì 2 Ûت¤¢ Û¬‘Ÿ ´¡¤¢ .¢Âî š¤¢ ‚ÎÖ÷ ßþ ¤¢ ö— üõ ¤

. ´¨ ùÀª

¤‘“ ßþ . ݃€î š¤¢ ´¡¤¢ ¤¢ 30 ÀƒÜî ‘“ x Âʀä ݃û¡ üõ Àƒ€î ­Âê ñ‘Ÿ

´¨ ô¥„ Áó ù¢“ 3-node ×þ B ‚î ‘¹÷ ¥ .ÝþÈƒõ ø “ø ¤ B ï“ ùÂ𠑓 ¹µÆ›

Â Ê € ä ø¢ ß ƒ “ ¥ ‚ î ´ ¨ ý Â Ê € ä Û õ‘ ª D . Ý ƒ € î ¢‘ ¹ þ ¤ D ô‘ ÷ ‚ “ ýÀ þÀ › ù ð

Àû¡ ¤Âì B ¤¢ ÀƒÜî ßþ µ؝î ‘“ Âʀä .´¨¤¢ ¤ ÀƒÜî ßþ µð¤ Ó xø B ¤¢ ¢›õ

Àû¡ š¤¢ B ¥ A ¤À• ùÂ𠤢 D‚“ ý Âð ù¤‘ª ‘“ ùÂÞû ͨµõ ÀƒÜî ‘“ Âʀä ø ´ª¢
. ´¨ ùÀª ‚Ž¤ (’)´ÞÆì 2 Ûت ¤¢ Û¬‘Ÿ ´¡¤¢.Àª
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 74

A A
20 40 40

D C B C
B
10 30 70 80 10 20 70 80

30 š¤¢(’) 70 š¤¢(Óó)

1 Ûت2-3 ´¡¤¢ öø ¤¢ ‚“ š¤¢-2Ûت

Ûت 2-3 ´¡¤¢ Û¡¢ ‚“ ¤ 60 ÀƒÜî ‘“ xÂʀä ý ¤ÁÚþ‘› , ñ‘·õ ßþ ¡ ö€ä ‚“

üõ –‘ì…õ C ùÂ𠤢 60 ý¹µÆ› üÏ ¤¢ ï“ ùÂð .Àþ ƒړ ÂÑ÷ ¤¢ (’) ´ÞÆì 2

ýø‘ Ÿ ù ð ß þ .¢¢Â ð ü õ ¢‘ ¹ þ E À þÀ › ù ð × þ ´ ¨ 3-node ×þ C ö .¢¢Â ð

ÀƒÜî ßþ µ؝î ‘“ ý Âʀä ýø‘Ÿ Ã÷ CùÂð .(80) ´¨ ÀƒÜî ßþ µð¤ Ó ‘“ ý Âʀä

‚“ , E ÀþÀ› ùÂ𠂓 ý Âð ù¤‘ª ‘“ ùÂÞû (70) ͨµõ ÀƒÜî ‘“ Âʀä.(60)“ Àû¡

´¨ 3-node ×þ A ‚î ‘¹÷ ¥ f¢À¹õ .¢¢Âð üõ ý ¤ÁÚþ‘› C ¥ A ¤À• ùÂð Û¡¢

.¢ª üõ ¢‘¹þ 20, 40, 70 ߃“ ¤¢ ÀƒÜî ßþ µð¤ Ó ‘“ ý Âʀä ýø‘Ÿ F ÀþÀ› ùÂð

ö€ä ‚“ °ƒ—— ‚“ D ø B . ´¨ ÀƒÜî ßþ µ؝î ‘“ ý Âʀä ýø‘Ÿ A , Û±ì À€÷‘õ

‚“ F öÀ÷¥ Âê ö€ä ‚“ Ã÷ E ø C °ƒ—— ‚“ ,À÷‘õ üõ ü쑓 A ͨø ø ² öÀ÷¥ Âê

G À þÀ › ù ð × þ , ¢¤À ÷ ý ¤À • A ö  . À ÷ ª ü õ ý ¤Á Ú þ‘ › ¤À • ù ð ß þ Û ¡¢

ù¤‘ª ‘“ ùÂÞû 40 ÀƒÜî ‘“ ý Âʀä ýø‘Ÿ ùÂð ßþ .¢ª üõ ¢‘¹þ 2-3´¡¤¢ ýÂ“

¤¢ ÀþÀ› 2-3 ´¡¤¢ ,¢“ Àû¡ F ‚“ ü÷‘ƒõ À÷¥ Âê Âð ù¤‘ª ø A ‚“ ² À÷¥ Âê Âð

.´¨ ùÀª ‚Ž¤ 3 Ûت

G
40

A F
20 70

B D C E
10 30 60 80

(’) ´ÞÆì 2 Ûت2-3 ´¡¤¢ Û¡¢ ‚“60 š¤¢-3Ûت


75 2-3 ö‘µ¡¤¢ .6.3

,݃€î ‚ê‘® p À€÷‘õ 3-node ×þ Û¡¢ ‚“ ¤ ý Âʀä ݃ª‘“ ‚µ¨¡ ‚î ö‘õ¥ Âû

. À€þð üõ ‘û ùÂð ݃Æ֗ ¤‘î ßþ ‚“ ‚î . ¢Âî ݃û¡ ¢‘¹þ ¤ q À€÷‘õ ýÀþÀ› ùÂð

üõ Âþ ¥ –¤ ¬ ‚“ š¤¢ ⓑ— . ´¨ ùÀª ݃Æ֗ ü÷‘ƒõÂʀä ø q,p ‚“ p ݃þð üõ

.Àª‘“

templete< class KeyT ype >


Boolean Tow3< KeyT ype >::Insert(const Element< KeyT ype >& y)
//Insert the element y into the 2-3 tree only if it does not already
// contain an element with the same key.
{
Tow3Node< KeyT ype > *p;
Element < KeyT ype > x=y;
if(x.key>=MAXKEY) return FALSE; //invalid key
if(!root){NewRoot(x,0); return TRUE;} //empty 2-3 tree
if(!(p=FindNode(x))){
insertionError();
return FALSE;}//key already in 2-3 tree
for(Tow3Node< KeyT ype > *a=0; ; )
if(p→dataR.key==MAXKEY){// p is a 2-node
p→PutIn(x,a);
return TRUE; }
else{// p is a 3-node
Tow3Node< KeyT ype > *olda=a;
a=new (Tow3Node< KeyT ype >);
x=Split(p,x,olda,a);
if(root==p){ //root has been split
NewRoot(x,a);
return TRUE; }
else p=p→ parent();
}//end of p is a 3-node and for loop
}//end of Insert
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 76

 “ ⠓ — ß þ ¥ ôÀ î  û ‚ î ý ‚ Ô ƒ Òø .À € î ü õ ù¢‘ Ô µ ¨ ⠓‘ — ß þÀ €  ¥ ⠓‘ — ß þ

:Àª‘“ üõ Âþ ¥ –¤ ¬ ‚“ , À÷¤¢ ùÀúä

( ´¨ ùÀª ÂÑ÷ é¬ ü𢑨 ýÂ“ < KeyT ype > ýÚó µõ¤‘• ¥ )

: void Tow3::NewRoot(const Element& x,Tow3Node *a) •


ü÷‘õ¥ ‘— . À€î ƒƒç— Àþ‘“ 2-3 ´¡¤¢ ‚Èþ ¤ ‚î ¢ª üõ ü÷¡Âê ü÷‘õ¥ ⓑ— ßþ

(new root) ÀþÀ› ‚Èþ ¤ öø ¤¢ ‚“ x ¢ÂԀõ ÂÊ€ä ¢¥‘¨ üõ ¤ Middle Child, a ‚î

À þÀ › ‚ È þ ¤ ( ²  À ÷¥  ê) LeftChild ‚ “ Û þÀ ± — à ƒ ÷ ü Þ þÀ ì ‚ È þ ¤ .¢ ª ü õ š¤¢

.Àª Àû¡

:Tow3Node* Tow3::Find(const Element & x) •


´¡¤¢ ×þ ë ê ⓑ— .´¨ (1 ‚õ‘÷“) Tow3::Search ùÀª ž…¬ ‚¿Æ÷ ⓑ— ßþ
.Àû¢ üõ ¤Âì ¹µÆ› ¢¤ õ x.key ÀƒÜî ‘“ ý ÂÊ€ä ¤ ÌŸ ýÂ“ ¤ üú— ƒè 2-3

ßþ Âƒè ¤¢ .¢ª üõ ù¢¢ ´Èð“ ÂÔ¬ ¤ÀÖõ ù‘Ú÷ Àª‘“ ‚µª¢ ¢›ø ÀƒÜî ßþ Âð

ⓑ— .À÷¢Âð üõ “ ¤ ¹µÆ› ßþ ¤¢ ùÀª –‘ì…õ ï“ ùÂ𠂓 ý Âð ù¤‘ª –¤ ¬

FindNode ⠓‘ — Í ¨ — ‚ î ý  ð ù¤‘ ª ù ƒ ¡£ ýÂ “ ýÂ “ p  ƒ ç µ õ ¥ Tow3::Insert


ý ù¢¢ ö‘Þµ¡‘¨ ߃€»Þû FindNode . À€î üõ ù¢‘Ôµ¨ , ¢ª üõ ù¢¢ ´Èð“

ß þ . ¢¥‘ ¨ ü õ ¤¢‘ ì (‚ È þ ¤) root ‘— p ¥ ´ È ð¥‘ “ ‚ “ ¤ ‘ õ ‚ î À € î ü õ ¢‘ ¹ þ ¤

‚“ . Àª‘“ p‘— root ƒÆõ ¤¢ ¢›õ ý‘û ùÂ𠥐 üµÆƒó À÷— üõ ý ù¢¢ ö‘Þµ¡‘¨

ùÂð Àóø ‚“ §Âµ¨¢ , ùÂð ×þ ݃Æ֗ ¥ ŕ Âþ ¥ ´¨ ¥‘ƒ÷ ý ù¢¢ ö‘Þµ¡‘¨ ߃€

. ´¨ ý ¤ ø ® ùÀª ݃Æ֗

:void InsertionError() •
š¤¢ ö ¤¢ ´¨ ´¡¤¢ ¥ ý ÂÊ€ä ‘“ ýø‘Æõ ©ÀƒÜî ‘“ ¤ ý Âʀä ݃û¿“ üµìø

. ¢Âî Àû¡ ô…ä ¤ ‘Ρ ⓑ— ßþ . ¢¢ Àû¡  ¤ üþ‘Ρ ݃€î

: void Tow3Node::PutIn(const Element& x,Tow3Node *a) •


, ´¨ Âʀä ×þ ý¤¢ ‘fãìø ‚î (this)ùÂð Û¡¢ ‚“ x ÂÊ€ä š¤¢ ýÂ“ ⓑ— ßþ ¥

¤¢ ø ݃û¢ üõ ¤Âì x ´¨¤ ´Þ¨ ¤¢ ‘f Öƒì¢ ¤ a ´¡¤¢ Âþ ¥ ¤¢ . ݃€î üõ ù¢‘Ôµ¨

üܱì Âþ¢‘Öõ ùÀª Middle Child ýø‘Æõ a ù‘Ú÷ ¢¢Âð dataL “Â“ x Â𐠂¹ƒµ÷

x Âð . À÷ª üõ RightChild ø dataR “Â“ °ƒ—— ‚“ MiddleChild ø dataL


.Àª Àû¡ RightChild ‚“ ÛþÀ±— a ù‘Ú÷ ¢¢Âð dataR ‚“ ÛþÀ±—

:Eelement& Tow3::Split(Tow3Node* p,Element& x,Tow3Node *olda,*a) •


Âþ ¥ žÂª ‚“ Âʀä ø¢ Ûõ‘ª Àµ“ ‚î (p) À€÷‘õ Tow3Node ×þ ýø ¤ “ ⓑ— ßþ
77 2-3 ö‘µ¡¤¢ .6.3

ýø‘ Ÿ À € î ü õ ù¤‘ ª ö ‚ “ a ‚ î ùÀ ª ¢‘ ¹ þ ü ó‘ ¡ ù ð.À € î ü õ Û Þ ä À ª‘ “ ü õ

Âʀä .Àª‘“ üõ x ø p ¤¢ ¢›õ ‚ƒóø Âʀä ø¢ ö‘ƒõ ¥ ÀƒÜî ßþ µð¤ Ó ‘“ ý Âʀä

öÀ÷¥ Âê Âð ù¤‘ª ‚¨ .Àª‘“ üõ p ¤¢ ùÀ÷‘õ ü쑓 ÂÊ€ä ‘ú€— , ÀƒÜî ßþ µ؝î ý¤¢
¤ À÷ª Óþ Â㗠aøp ¤¢ Àþ‘“ ‚î ýÀ÷¥ Âê ù¢¢ Ìä ¤‘ú olda Âð ù¤‘ª ø p üܬ

. À÷¢Âð üõ “ ¤ ‚÷‘ƒõ ÀƒÜî ‘“ Âʀä ⓑ— ßþ . À€€î üõ ñ‘窐

Ã÷ a ø ¢ ª š¤¢ p ¤¢ À û ¡ ü õ ‚ î ´ ¨ ý Â Ê € ä ùÀ € û¢ ö‘ È ÷ x , Insert ⠓‘ — ¤¢

. ´¨ ùÀª ¢‘¹þ for ‚Öܟ ¤Âؗ ßþ ¡ ¤¢ ü𥑗 ‚“ ‚î ´¨ ý ùÂð ùÀ€û¢ ö‘È÷

‚“ üڵƓ ùÀª é¬ Ûî ö‘õ¥ ‚î ݃€î üõ ùÀû‘Èõ üðÀƒ»ƒ• ۃܽ— ø ‚þ ù— ýÂ“

Âʀä n ‘“ 2-3 ´¡¤¢ ×þ Û¡¢ ‚“ ý ¤ÁÚþ‘› ßþÂ“‘€“ . ¢¤¢ 2-3 ´¡¤¢ ÕÞä

.¢Âƒð üõ –¤ ¬ O(log n) ‘“ “Â“ ü÷‘õ¥ ¤¢

2-3 ´¡¤¢ ×þ ¥ éÁŸ 3.6.3

 ð . ´ Æ ƒ ÷ ý ¤Á Ú þ‘ › ¥  — Û Ø È õ ü õ ú Ô õ à‘ ½ ó ‚ “ 2-3 ´ ¡¤¢ × þ ¥ éÁ Ÿ

Âʀä ×þ ‚“ ùÀª éÁŸ ùÂð ÛþÀ±— ‘“ ù‘Ú÷ Àª‘±÷ ï“ ùÂð ‚î ݃€î éÁŸ ¤ ý Âʀä

ñ‘·õ ýÂ“ . ݃û¢ üõ ô‘¹÷ ¤ éÁŸ ÛÞä ï“ éÁŸ ø ´¨ ï“ ùÂ𠤢 ‚î °¨‘€õ

éÁŸ ¤ ¢¤¢ ¤Âì (Óó) ´ÞÆì 4 Ûت ‚Èþ ¤ ¤¢ ‚î 50 ÀƒÜî ‘“ Âʀä ݃û¿“ Âð

60 ÀƒÜî ‘“ ý ÂÊ€ä ‘þ 20 ÀƒÜî ýø‘Ÿ ý ÂÊ€ä ‘“ ´¨ ßØÞõ Âʀä ßþ ù‘Ú÷ ݃€î

ÂÊ€ä ¥ ݃÷— üõ üÜî ´ó‘Ÿ ¤¢ . À÷¤¢ ¤Âì ï“ ùÂ𠤢 ‘ú€þ ø¢ Âû ø ¢ª Ëþã—

âìø ÀƒÜî ßþ µ؝î‘“ ÂÊ€ä ‘þ ø ² ´Þ¨ ¤¢ âìø ´¡¤¢ Âþ ¥ ¤¢ ÀƒÜî ßþ µð¤ Ó ‘“

.݃€î ù¢‘Ôµ¨ ,¢ª éÁŸ Àþ‘“ ‚î ý ÂÊ€ä ´¨¤ ´Þ¨ ´¡¤¢ Âþ ¥ ¤¢

ýø ¤ “ ¤ ¶½“ ßþ . Ýþ ƒð üõ ÂÑ÷ ¤¢ ¤ ï“ ùÂð ×þ ¥ éÁŸ ‘ú€— ‚¹ƒµ÷ ¤¢

ÍÖê 70 ÀƒÜî ‘“ Âʀä éÁŸ ýÂ“ . ݃û¢ üõ ‚õ¢ (Óó) ´ÞÆì 4 Ûت ´¡¤¢

¤‘ î ‚ ¹ ƒ µ ÷ . Ý ƒ û¢ ü õ ¤Â ì dataR.key ¤¢ ¤ MAXKEY , C ù 𠤢 ‚ î ´ ¨ ü ê‘ î

Û Ø ª 2-3 ´ ¡¤¢ ¥ 90 À ƒ Ü î ‘ “ éÁ ¡ ýÂ “ . ´ ¨ ùÀ ª ù¢¤ ø (’) 4 Û Ø ª ¤¢

Ã÷ D ù 𠤢 ø Ý ƒ € î ü õ Û Ö µ € õ dataL ‘ “ ¤ dataR ´ Æ þ‘ “ ü õ (’) ´ Þ Æ ì 4

ß þ ‚ ¹ ƒ µ ÷. (dataR.key=MAXKEY) Ý ƒ û¢ ü õ ¤Â ì MAXKEY ‘ “ ¤ dataR.key


. ´¨ ùÀª ‚Ž¤ (”)´ÞÆì 4 Ûت 2-3 ´¡¤¢ ¤¢ ¤‘î
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 78

A
50 80

B C D
10 20 60 70 90 95

‚ƒóø2-3 ´¡¤¢(Óó)

A
50 80

B C D
10 20 60 90 95

70 ùÂð éÁŸ ( ’)

A
50 80

B C D
10 20 60 95

90 ùÂð éÁŸ(”)

” ø ’ ø Óó ý‘û ´ÞÆì , 4 Ûت

C ùÂð ‚î ¢ª üõ °›õ Âõ ßþ. Àþ ƒړ ÂÑ÷ ¤¢ ¤ 60 ÀƒÜî ‘“ Âʀä éÁŸ ñ‘Ÿ

üõ ø ¤ ßþ ¥ , ´¨ 3-node ×þ B ü€ãþ C ùÂ𠲝 ¢ÃÞû ‚î ‘¹÷ ¥ . ¢¢Âð üú—

¤ 50 À ƒ Ü î ø ù¢¢ ñ‘ Ö µ ÷ , A ,¤À • ù ð dataL ö‘ Ø õ ‚ “ ¤ 20 À ƒ Ü î ‘ “ Â Ê € ä Ý ƒ ÷ —

´¡¤¢ B ¤¢ dataR.key=MAXKEY ý ¤Âì“ ¥ À㓠݃û¢ ñ‘Öµ÷ C ùÂ𠂓 ¤À• ¥

ßþ . ¢¢Âð üõ ÛþÀ±— ,ùÀª ‚Ž¤ (–) ´ÞÆì 4 Ûت ¤¢ ‚î ‚»÷ –¤ ¬ ‚“ 2-3
79 2-3 ö‘µ¡¤¢ .6.3

‚î üõ‘Ú€û . ¢ª üõ ùÀƒõ‘÷ (rotation) ǡ ‘û ù¢¢ üþ‘¹“‘› ø ñ‘Öµ÷ À€þÂê

60 éÁŸ ‚î üõ‘Ú€û À€÷‘õ . ¢ª üõ üú— D ùÂð ,¢¢Âð üõ éÁŸ 95 ÀƒÜî ‘“ Âʀä

2-node ×þ C ü€ãþ ² ´Þ¨ ¢ÃÞû ‚î Â Àª‘“ üÞ÷ ßØÞõ rotation Àª ô‘¹÷

݃€î üõ éÁŸ ¤ D ùÂð ø ù¢Âî ÛÖµ€õ C ² ¢ÃÞû Û¡¢ ‚“ ¤ 80 ¤‘“ ßþ . ´¨

ü õ éÁ Ÿ ù ð × þ ° ƒ  ¤¢.¢ ª ü õ ‚ µ Ô ð (combine) ° ƒ  À € þÂ ê ß þ ‚ “ .

95 Âʀä éÁŸ .¢¢Âð üÞ÷ éÁŸ ýùÂð ‚÷Ú»ƒû ǡ ¤¢ ‚î ü—¤ ¬ ¤¢ ¢ª

50 À ƒ Ü î ‘ “ Â Ê € ä éÁ Ÿ .¢¢Â ð ü õ (˜) ´ Þ Æ ì 4 Û Ø ª 2-3 ´ ¡¤¢ ‚ “ ü ú µ € õ

ñ‘ Ÿ .À ª À û ¡ (š) ´ Þ Æ ì 4 Û Ø ª 2-3 ´ ¡¤¢ ‚ Ž¤ ° › õ à ƒ ÷ ´ ¡¤¢ ß þ ¥

B ùÂ𠑗 ¢ª üõ °›õ ‚î Àþ ƒړ ÂÑ÷ ¤¢ ´¡¤¢ ßþ ¥ ¤ 10 ÀƒÜî ‘“ Âʀä éÁŸ

2-node ×þ C ü€ãþ B ´¨¤ ´Þ¨ ¢ÃÞû ‘þ ‚î ݃€î üõ ü¨¤ “ ö€î .¢¢Âð üú—

‚î ‚»÷ ‘“ ‚“‘Èõ üȡ ݃÷— üõ Àª‘“ 3-node ×þ C Âð .3-node ×þ ‘þ ´¨

ÛÞä ×þ ù‘Ú÷ Àª‘“ 2-node ×þ Âð ø ݃û¢ ô‘¹÷, ´êÂ𠖤 ¬ 60 éÁŸ ýÂ“

éÁŸ ´ƒã®ø ‚“‘Èõ , ´¨ 2-node ×þ C ö ‘¹€þ ¤¢ .¢Âƒð üõ –¤ ¬ °ƒî—

ù ð ø ùÀ ª Û Ö µ € õ B ‚ “ 80 ø 20 ý‘ ûÀ ƒ Ü î ‘ “  ¬‘ € ä ¤‘ “ ß þ .Ý ƒ € î ü õ Û Þ ä 95

‚ ÷ Ú » ƒ û ý¤¢ A ù ð ¤À • ‚ î À ª ü õ ° › õ ‚ ó‘b Æ õ ß þ À € Â û.¢¢Â ð ü õ éÁ Ÿ C


Ýþ¢¢ ô‘¹÷ C ýÂ“ ‚î ‚»÷ À€÷‘õ ݃µÆ÷— üõ ¢±÷ ‚Èþ ¤ ¤À• Âð .Àª‘±÷ ý Âʀä

¤¢ ø(60 Â Ê € ä éÁ Ÿ) Ý ƒ û¢ ¤Â ì ´ Æ — ø ü ¨¤  “ ¢¤  õ ¤ ö ´ ¨¤ ø ²  ¢Ã Þ û ,

éÁŸ ü𢑨 ‚“ , ´¨ ‚Èþ ¤ A ö .Àþ¢Âð üõ üú— (95Âʀä éÁŸ) D ‚¹ƒµ÷

.((œ) ´ÞÆì 4 Ûت) Àª Àû¡ ÀþÀ› ‚Èþ ¤ B ø ùÀª


‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 80

A
20 80
B C D
10 50 95

60 éÁŸ(–)

A
20
B C
10 50 80

95 éÁŸ(˜)

A
20

B C
10 80

50 éÁŸ(š)

B
20 80

10 éÁŸ(œ)

4 Ûت

2-3 ´¡¤¢ ×þ ï“ ùÂ𥐠éÁŸ ۟Âõ


Âʀä éÁŸ ¥ ŕ ǵƒã®ø ö¢Âî ÅØã€õ ýÂ“ ôø Ãó –¤ ¬ ¤¢ ¤ p ùÂð :1 ‚ÜŸÂõ

. Àƒû¢ ƒƒç— ÂÑ÷ ¢¤ õ

:2 ‚ÜŸÂõ

for(;p has zero element && p!=root;p=r){


left r be the parent of p and let q be the left or right sibling of p ;
if(q is a 3-node) perform a rotation
else perform a combine;}
81 2-3 ö‘µ¡¤¢ .6.3

p ²  ´ Þ ¨ À ÷¥  ê . À ª‘ “ ‚ È þ ¤ À þ‘ “ p ù‘ Ú ÷,¢¤À ÷ ý Â Ê € ä p  ð :3 ‚ Ü ŸÂ õ

.¢ª üõ éÁŸ p ùÂð ø ùÀª ‚Èþ ¤ ‚“ ÛþÀ±—

¤À• ü÷‘ƒõ ‘þ ø ´¨¤ , ² À÷¥ Âê p ‚î ßþ ‚“ ‚µÆ“ ǡ ×þ ýÂ“ ´ƒã®ø ‚¨

¤ qù‘Ú÷ ,Àª‘“ r ² ´Þ¨ À÷¥ Âê p Âð . Àþ üõ ¢›ø ‚“ ƒ¡‘þ Àª‘“ r ü€ãþ ¢¡

ßµêÂð ÂÑ÷ ¤¢ öøÀ“ ‚î Àƒª‘“ ‚µª¢ ‚›— . Àƒ€î ­Âê p ² ´Þ¨ À÷¥ Âê ö€ä ‚“

ǡ ‚¨ . ´¨ ùÀª Óþ Â㗠üµ¨¤¢ ‚“ q, ´¨ 3-node‘þ 2-node ×þ r ‚î ßþ

Ìä ‚î Àû¢ üõ ö‘È÷ ? ´õ…ä . ´¨ ùÀª ‚Ž¤ 5 Ûت ¤¢ ý ¤¢Þ÷ –¤ ¬ ‚“

ü€ãþ)À€µÆû ‘û ùÂð öÀ÷¥ Âê ùÀ€û¢ ö‘È÷ d ø c,b,a. ¢¤À÷ üµƒÞû ¢‘þ ¥ ‚ρ“ Âõ ù¢¢

.( ö‘µ¡¤¢ Âþ ¥ ý‘û ‚Èþ ¤

r r
X ? Y ?
p q - p q
Y Z X Z

a b c d a b c d

Àª‘“ üõ r ² À÷¥ Âê p (Óó)


r r
Z ? Y ?
q p - q p
X Y X Z

a b c d a b c d
Àª‘“ üõ r ü÷‘ƒõ À÷¥ Âê p (’)

r r
W Z W Y
-
a q p
p a q
X Y X Z

b c d e b c d e
Àª‘“ üõ r ´¨¤ À÷¥ Âê p (”)

2-3 ´¡¤¢ ¤¢ ǡ ýÂ“ Óܵ¿õ ´ƒã®ø ‚¨-5 Ûت


‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 82

6 Ûت ¤¢ ´¨ r ² ´Þ¨ À÷¥ Âê p ‚î üµìø combine –‘ƒÜÞä ´ƒã®ø ø¢

. ´¨ ùÀª ù¢¢ ö‘È÷

r r
x
p q - p
y x y

a b c a b c
( Óó)

r r
x z z
p q - p
y d x y d

a c a b c
b
( ’ )

6 Ûت

–¤ ¬ ‚“ 2-3 ´¡¤¢ ¤¢ ï“ ùÂð ×þ éÁŸ ۟Âõ ¥ ñø ý‚ÜŸÂõ Àî ‚±ª

:Àª‘“ üõ Âþ ¥

template < class KeyT ype >


Two3< KeyT ype >::DeleteKey(Two3Node< KeyT ype > *p,
const Element< KeyT ype >& x)
//Key x.Key is to be deleted from the leaf node p.
{
if( x.Key==p→dataL.Key) // first element
if(p→dataR.Key!=MAXKEY )
{ // p is a 3-node
p →dataL = p → dataR;
p → dataR.Key =MAXKEY;}
else p →dataL.Key=MAXKEY; // p is a 2-node
else p → dataR.Key =MAXKEY;} // delete second element
83 2-3 ö‘µ¡¤¢ .6.3

‚“ ´¨ r ² À÷¥ Âê p üµìø Combineø Rotation ý‘û –‘ƒÜÞä Àî ߃€»Þû

:Àª‘“ üõ Âþ ¥ –¤ ¬

//Rotation when p is the left child of r and q is the middle child of r.


p → dataL= r → dataL;
p → MiddleChild = q → LeftChild;
r → dataL = q → dataL;
q → dataL = q → dataR;
q → LeftChild = q → MiddleChild;
q → MiddleChild = q → RightChild;
q → dataR.Key = MAXKEY;
...............................................................
//Combine when p is the left child of r and q is the right sibling of p.
p → dataL= r → dataL;
p → dataR= q → dataL;
p → MiddleChild = q → LeftChild;
q → RightChild = q → MiddleChild;
if(r → dataR.Key = MAXKEY)// r was a 2-node
r → dataL.Key = MAXKEY;
else {
r → dataL= r → dataR;
r → dataR.Key = MAXKEY ;
r → MiddleChild = r → RightChild;
}
2-3 ´¡¤¢ ×þ ¥ éÁŸ ¢ÂØÜÞä ۃܽ— ø ‚þù— 4.6.3

O(1) ‘“ “Â“ ü÷‘õ¥ ¤¢ üþ‘ú€— ‚“ ǡ ‘þ °ƒî— ¢ÂØÜÞä ×þ ‚î ´¨ É¿Èõ

×þ Âð. ¢¢Âð üõ Ûõ‘î éÁŸ ÛÞä ,¢ª ô‘¹÷ ǡ ×þ Âð . ¢Âƒð üõ ô‘¹÷

ßþÂ“‘€“ . ¢ª üõ ÛÖµ€õ —„‘“ ¼Î¨ ×þ ‚“ 2-3 ´¡¤¢ ¤¢ p ,¢Âƒð –¤ ¬ °ƒî—

2-3 ´¡¤¢ á‘Ô—¤¥ À÷ƒ𠖤 ¬ À€÷— üõ éÁŸ ×þ ñ…¡ ¤¢ ‚î ü—‘±ƒî— ¢Àã—

ü÷‘õ¥ ‚“ Âʀä n ‘“ 2-3 ´¡¤¢ ×þ ¥ éÁŸ ÛÞä ‚¹ƒµ÷ ¤¢ ø ¢Âî Àû¿÷ ¥ ø‘¹—

.¢¤¢ ¥‘ƒ÷ O(log n) ‘“ “Â“


‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 84

Red-Black ù‘ƒ¨ - ÃõÂì ´¡¤¢ 7.3


3 :Àª‘“ üõ Âþ ¥ –¤ ¬ ‚“ ù‘ƒ¨-ÃõÂì ´¡¤¢ ×þ

ù‘ƒ¨ - ÃõÂì ´¡¤¢ «¡ 1.7.3

. ´¨ ù‘ƒ¨ ‘þ ø ÃõÂì ‘þ ùÂð Âû •

. ´¨ ù‘ƒ¨ nil ï“ Âû •

À ÷ — ü Þ ÷ à õ ì À ÷¥  ê,à õ ì ù ð × þ ¤¢ ) .À € µ Æ û ù‘ ƒ ¨ , à õ ì ù ð × þ À ÷¥  ê ø¢ •
(Àª‘“

¢À 㠗 Û õ‘ ª ( Ý ƒ Ö µ Æ õ À ÷¥ Â ê ‘f õø Ã ó ‚ ÷) À ÷¥  ê  ‚ “ ù ð × þ ¥ ù¢‘ ¨  ƒ Æ õ  û •
. ´¨ ù‘ƒ¨ ùÂð ü÷‘ÆØþ

(Àª‘“ üÞ÷ ü¨‘¨ ¯ø ª ¥ ¯Âª ßþ). ´¨ ù‘ƒ¨ ´¡¤¢ ‚Èþ ¤ •

:Âþ ¥ ñ‘·õ À€÷‘õ

26

17 41
14 21 30 47
10 16 19 23
28 38 NIL NIL

NIL NIL
7 12 15 15 NIL NIL NIL
NIL
35 39
NIL NIL
3 NIL
NIL NIL NIL NIL
NIL NIL NIL NIL

NIL NIL

.Àª‘“ üõ ü¨Àì ÀÞ½õ Âµî¢ ùø Û ¥ ‚µêÂð“ ´ÞÆì ßþ °ó‘Îõ 3


85 RED-BLACK ù‘ƒ¨ - ÃõÂì ´¡¤¢ .7.3

üþÀµ“ ý‘þ‘Ìì ø Óþ ¤‘ã— 2.7.3

ùÂ𠢐À㗠‘“ ´¨ “Â“ bh(X)‘þ Black-Height(x) Óþ Â㗠“‘€“ :Black-Height(x)


4
. À÷¥ Âê ï“ ×þ ‘— x ¥ ù‘ƒ¨ ý‘û

: uncle(x)
if parent[x]=right[parent[parent[x]]] then
uncle[x]:=left[parent[parent[x]]]
else uncle[x]:=right[parent[parent[x]]]

 “Â “ , ´ ¨ ü Ü ¡¢ ù ð n ý¤¢ ‚ î RB ´ ¡¤¢ × þ á‘ Ô —¤  · îÀ Ÿ : 1 ‚ƒÌì


1)
. ´¨ 2 log(n+
2
ù¿ó¢ ‚Èþ ¤ ‚“ ´¡¤¢ Âþ ¥ ×þ ‚î ݃û¢ üõ ö‘È÷ Àµ“ ë ê ‚ƒÌì –‘±™ ýÂ“: –‘±™

á‘Ô—¤ ýø ¤ “ ÂÖµ¨ ¥ –‘±™ ýÂ“ . ´¨ üÜ¡¢ ùÂð 2bh(x) − 1 ý¤¢ ÛìÀŸ x
.¢ª üõ ù¢‘Ôµ¨ ´¡¤¢ ¤¢ x üÜ¡¢ ùÂð

: DZÂÖµ¨ ‚þ‘•

‚“ ´¡¤¢ Âþ ¥ ßþÂ“‘€“. ´¨ nil ‚¹ƒµ÷ ¤¢ ø ï“ ‘fÞµŸ x , Àª‘“ ÂÔ¬ x á‘Ô—¤ Âð

.¢¤¢ üÜ¡¢ ùÂð 20 − 1 = 0 ü€ãþ 2bh(x) − 1 ÛìÀŸ x ‚Èþ ¤


: üŽ ÂÖµ¨ ô‘ð

À÷¥ Âê ø¢ ý¤¢ x ø Àª‘“ üõ ´±·õ ý¢Àä bh(x) . Àþ ƒړ ÂÑ÷ ¤¢ ¤ x üÜ¡¢ ùÂð
‘ú÷ Black - Height, ù‘ƒ¨ ‘þ À€ª‘“ ÃõÂì ‚Ø÷ °ÆŸ “ öÀ÷¥ Âê ¥ ôÀî Âû. Àª‘“ üõ

x ¢¡ ¥ µÞî üä‘Ô—¤ x À÷¥ Âê Âû ‚Ø÷ ´Üä ‚“ . ¢“ Àû¡ bh(x)-1 ‘þ bh(x) “Â“

×þ ‚Èþ ¤ ‚“ ´¡¤¢ Âþ ¥ Âû ‚î Ýþ ƒړ ‚¹ƒµ÷ ÂÖµ¨ ­Âê ¥ ù¢‘Ôµ¨ ‘“ ݃÷— üõ ¢¤¢

ÛìÀŸ x ‚ 2bh(x)−1 − 1 ÛìÀŸ x À÷¥ Âê


b Èþ ¤ ‚“ ´¡¤¢ Âþ ¥ ßþÂ“‘€“. ¢¤¢ üÜ¡¢ ùÂð

. ´ª¢ Àû¡ üÜ¡¢ ùÂð 2 − 1 + 2bh(x)−1 − 1 + 1 = 2bh(x) − 1


bh(x)−1

¤¢ öøÀ“) ‘û ùÂ𠥐 üރ÷ ÛìÀŸ h á‘Ô—¤ ‚“ ´¡¤¢ ¤¢ ‚î ݃÷¢ üõ ÂÚþ¢ éÂÏ ¥

Âƒè ¤¢ Âþ ¥ . À€µÆû ù‘ƒ¨ , ï“ ‚“ ‚Èþ ¤ ¥ ù¢‘¨ ƒÆõ Âû ýø ¤ “ ( ‚Èþ ¤ ßµª¢ ÂÑ÷

Black-Height ‚¹ƒµ÷ ¤¢. Àª Àû¡ ËÖ÷ ´¡¤¢ «¡ ¥ 3 ´ƒ¬‘¡ –¤ ¬ ßþ

h
.¢“ Àû¡ ÛìÀŸ ‚Èþ ¤
2
(n+1)
n ≥ 2 − 1 =⇒ 2 ≤ n + 1 =⇒ h ≤ 2 log2
h h
2 2

. ´¨ O(log n) ,RB ´¡¤¢ á‘Ô—¤ : ‚¹ƒµ÷

. Ýþ ¤‘Þª üÞ÷ Â÷ ø ´¨ ƒ™‘b— ü“ x ¢¡ Ù÷¤ 4


‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 86

ö¤ø¢ 3.7.3

¤ ±¹õ ÍþÂª üÌ㓠¤¢ éÁŸ ø š¤¢ –‘ƒÜÞä ¥ À㓠RB ´¡¤¢ «¡ ü“‘þ ¥‘“ ýÂ“
‘û Âð ù¤‘ª ¤‘µ¡‘¨ ¤¢ ü—Âƒƒç— ‘þ ݃€î ­ä ¤ ‘û ùÂ𠥐 üÌ㓠Ù÷¤ ‚î Àª ݃û¡

ü õ Ó þ  㠗 ( ¢Â Ú ³  ø ¢Â Ú µ ¨¤ ) ö¤ ø¢ Û Þ ä ø¢ ¤  Ñ € õ ß ƒ Þ û ‚ “ . Ý ƒ þ‘ Þ ÷ ñ‘ Þ ä

×þ «¡ ¤¢ üó‘ت ºƒû ÛÞä ø¢ ßþ ‚î ´¨ ¼®ø Âþ ¥ Ûت ‚“ ‚›— ‘“ . ݃þ‘Þ÷

. ¢Âî Àû¿÷ ¢‘¹þ üþø¢ø¢ ´¡¤¢

À÷¥ Âê ‚î ݃þ‘Þ÷ üõ ­Âê ¢ª üõ ô‘¹÷ ¢ÂÚ³ ö¤ ø¢ ×þ ‚î üõ‘Ú€û : ‚µØ÷

. Àª‘±÷ nil ÂÑ÷ ¢¤ õ ùÂð ´¨¤

ƒƒç— ö¤ ø¢ ×þ ™ ¤¢ ‘û Âð ù¤‘ª ‘ú€— Âþ ¥ Àª‘“ O(1) ¥ ö¤ ø¢ ‚î ´¨ ¼®ø

. À÷‘õ üõ ƒƒç— öøÀ“ Ã› Âþ‘¨ ø À€þ‘Þ÷ üõ

Right-Rotate(T,y)
-
y x
Left-Rotate(T,x)

x α y
γ

α β β γ

Left-Rotate(T,x)
1. y ← right[x] // SET y
2. right[x] ← left[y] // Turn y’s left subtree into x’s right subtree
3. if left[y] 6= NIL then
4. p[left[y]] ← x
5. p[y] ← p[x] // Link x’s parent to y
6. if p[x] =Nil then
7. root[T] ← y
8. else if x= left[p[x]]then
9. left[p[x]] ← y
10. else right[p[x]] ←y
11. left[y] ← x // Put x on y’s left
12. p[x]← y
87 RED-BLACK ù‘ƒ¨ - ÃõÂì ´¡¤¢ .7.3

š¤¢ 4.7.3
ü õ à õÂ ì ¤ x ¤‘ î ß þ ýÂ “ Ý ƒ þ‘ Þ ÷ ü õ š¤¢ ¤ x ü Žø¢ø¢ ´ ¡¤¢ ¤¢ š¤¢ §‘ ¨  “

Àª‘“ ù‘ƒ¨ x ¤À• Âð . À÷‘õ üõ ´“‘™ ‘û ùÂð ô‘Þ— ýÂ“


5 bh ‚î ´¨ ¼®ø , ݃þ‘Þ÷

¤¢ . Ý þø ¤ ü õ y=uncle[x] åÂ ¨ ‚ “ À ª‘ “ à õ ì x ¤À •  𐠑 õ À ¨¤ ü õ ö‘ þ‘ • ‚ “ š¤¢

´¡¤¢ ‚“ ¯“ Âõ 4 ´ƒ¬‘¡ Ðԟ ‘“ ‚ÜŸÂõ Âû ¤¢ ‚î ´Æ€þ üܬ ùÀþ ýÀ㓠´ÞÆì

á‘ Ô —¤ ýÂ “ ¤ À ª‘ “ ü õ 3 ´ ƒ ¬‘ ¡ ý ¤Â 젓 ôÀ ä ‚ “ ¯ “  õ ‚ î ¢ › õ ñ‘ Ø ª RB


ýÂ“ ¤ ¤‘î ßþ ø ݃þ‘Þ÷ ñ‘¨¤ —„‘“ –‘ä‘Ô—¤ ‚“ ¤ ÛØÈõ ø ݃þ‘Þ÷ ۟ ´¡¤¢ üÜãê

ø ¢ª ¤Âì“ x ¤À• ö¢“ ù‘ƒ¨ ¯Âª ‘þ ݃¨Â“ ‚Èþ ¤ ‚“ ‘— ݃û¢ ‚õ¢ ¤ÀÖ÷ —„‘“ žÎ¨

¼ƒ½Ê— ‚“ ´¨ ÃõÂì x ¤À• ‚î ü÷‘õ¥ ‘ú€— ´¡¤¢ ‚î Àª‘“ üõ ôÜãõ . ݃¨Â“ ‘úµ÷ ‚“

.¢¤¢ ¥‘ƒ÷

Ç ª ´ ¨ Parent[Parent[x]] ´ ¨¤ ‘ þ ²  À ÷¥  ê Parent[x] ‚ Ø ÷ °ÆŸ “

:
6 ´¨ ö ¤‘Öµõ Âþ ¥ ´ó‘Ÿ ‚¨ ‘“ ÂÚþ¢ ´ó‘Ÿ ‚¨ ‚î Àû¢ üõ  ¤ –ø‘Ôµõ ´ó‘Ÿ

. ´¨ ÃõÂì y : ñø ´ó‘Ÿ

. ´¨ ©¤À• ´¨¤ À÷¥ Âê xø ù‘ƒ¨ y : ôø¢ ´ó‘Ÿ

. ´¨ ©¤À• ² À÷¥ Âê xø ù‘ƒ¨ y : ô¨ ´ó‘Ÿ

üõ ù‘ƒ¨ ´¡¤¢ ‚Èþ ¤ 5 ´ƒ¬‘¡ Ðԟ ýÂ“ RB-insert ýÂ› ö‘þ‘• ¤¢ : ‚µØ÷

new x C . ¢ª
C
-
y A y
A D D

α Bx δ ε α B δ ε

β γ β γ

new x C
C
-
B Dy B D

x A γ A γ δ ε
δ ε

α β α β

ñø ´ó‘Ÿ

Black-Height 5
¢ª üõ ߃ƒã— y=uncle[x] Ù÷¤ §‘¨ “ –„‘Ÿ ßþ 6
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 88

C ôø¢ ´ó‘Ÿ C
y -
y
A δ B δ
α Bx x A ε
β ε
α β

ô¨ ´ó‘Ÿ

?
B

x A C

α εβ δ

RB-Insert(T,x)
1. Tree-Insert(T,x)
2. color[x]← RED
3. while x6=root[T] and color[p[x]]=RED do
4. if p[x] =left[p[p[x]]] then
5. y ← right[p[p[x]]]
6. if color[y] =RED then
7. color[p[x]] ← BLACK // Case 1
8. color[y] ← BLACK // Case 1
9. color[p[p[x]]] ← RED // Case 1
10. x ← p[p[x]] // Case 1
11. else if x = right[p[x]] then
12. x ← p[x] // Case 2
13. LEFT-Rotate(T,x) // Case 2
14. color[p[x]] ← BLACK // Case 3
15. color[p[x]] ← RED // Case 3
16. RIGHT-Rotate(T,p[p[x]]) // Case 3
17. else(same as then clause with ”right” and ”left” exchanged)
18. color[root[t]]←BLACK
89 RED-BLACK ù‘ƒ¨ - ÃõÂì ´¡¤¢ .7.3

11 11
y
2 14 case 1- 2 14
x
1 7 15 1 7 15
5 8 5 8
x 4 4

case 2
?
11
7 y
x 14
2 11 case 3 7
x 2 8 15
1 5 14
1 5
4 15
4

ùÂð ×þ š¤¢

éÁŸ 5.7.3

ô‘÷ ‚“ üµÒ‘ÔŸ ùÂð ×þ RB-Delete ý¥‘¨ ù¢‘ƒ• ¤¢ ý¥ Âõ ÍþÂª ý¥‘¨ ù¢‘¨ ýÂ“

‚î ´¨ üþ‘û ‚Ôóõ ö‘Þû ýø‘Ÿ ùÂð ßþ . ݃þ‘Þ÷ üõ Óþ Â㗠T ´¡¤¢ ýÂ“ nil[T]
Âþ‘¨ ø Àª‘“ üõ ù‘ƒ¨ DZüª ßþ Ù÷¤ ‚Ôóõ . ¢¤¢ ¢›ø ´¡¤¢ üóÞãõ ý‘û ùÂ𠤢

. À÷ƒړ ù¿ó¢ Âþ¢‘Öõ À€÷— üõ ‘û ‚Ôóõ

üóÞãõ ùÂð ×þ À€÷‘õ nil À÷¥ Âê ×þ ‘“ ݃÷— üõ üµÑꑽõ DZ üª ßþ ¥ ù¢‘Ôµ¨ ‘“


DZ üª ßþ ‚“ ¯“ Âõ Parent ‚Ôóõ ù¢‘Ôµ¨) ݃þ‘Þ÷ ¤‘µê¤ Àª‘“ üõ x ©¤À• ‚î ´¡¤¢

(. Àª‘“ üõ while ‚Öܟ ýÂ› ߃óø ø RB-Delete-Fixup ü÷¡ Âê ¤¢ üµÑꑽõ

ßþ ¤¢) ݃û¢ ù¤‘ª nil[T] ‚“ ¤ nil ý‘ûÂð ù¤‘ª ô‘Þ— RB ´¡¤¢ ¤¢ Àþ‘“ ßþÂ“‘€“

¤¢ ýù¢‘Ôµ¨ ۓ‘ì ø «‘¡ ¤ÀÖõ ¥ €û DZ üª ßþ ‘“ Âґ€µõ Parent ‚Ôóõ , ‚ÜŸÂõ

(.¢¤À÷ ¢¡

,  ¹ µ Æ › ü Žø¢ø¢ ´ ¡¤¢ ¥ éÁ Ÿ À € ÷‘ õ ü ªø ¤ ‚ “ À µ “ Â Ê € ä × þ éÁ Ÿ ýÂ “

Ý ƒ þ‘ Þ ÷ ´ ì¢ À þ‘ “. Ý ƒ þ‘ Þ ÷ ü õ ¼ ƒ ½ Ê — ¤ ´ ¡¤¢ Å ³ ¨ ø éÁ Ÿ ¤ Â Ñ ÷ ¢¤  õ Â Ê € ä

ý¹µÆ› ´¡¤¢ ¤¢ ¢¡ ý‘µÞû ‘“ RB ´¡¤¢ ¥ éÁŸ ‚“ ¯“ Âõ –¤ µ¨¢ ‚±ª ‚î

üõ ù¤‘ª nil[T] ‚“ nil ý‘ûÂð ù¤‘ª RB ´¡¤¢ ¤¢ ö ´Æƒ÷ ö‘ÆØþ ‘f Öƒì¢ üŽø¢ø¢
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 90

. ¢ª üõ üû¢ ¤ÀÖõ üϪ ºƒû öøÀ“ 7 Í¡ ¤¢ Parent[x] ‚Ôóõ ߃€»Þû ø Àþ‘Þ÷

. ´ ê ð À û ¡ ¤À Ö õ nil[T].Parent, À ª‘ “ nil  × þ x ‚ î Ý û ü —¤  ¬ ¤¢ Â þ ¥

16 ¯Î¡ ¤¢ ´¡¤¢ , Àª‘“ ù‘ƒ¨ y Ù÷¤ ‚î ü—¤ ¬ ¤¢ , ùÀª Âî£ Âƒƒç— ø¢ “ ùø…ä

RB ´¡¤¢ ¤¢ 4 ´ƒ¬‘¡ ‘— ¢ª üõ ¼ƒ½Ê— RB-Delete-Fixup ü÷¡Âê ‘“ 17 ø

. ¢ª ü“‘þ ¥‘“

ù ð ö ¢ ¡ ‘ þ ¢ ª ü ÷ ¡Â ê T ´ ¡¤¢ ýø ¤  “ z ùÂ ð ‘ “ RB-Delete  ð : ‚ µ Ø ÷

¤ µ¨¢ ôø¢ ´ó‘Ÿ ¤¢ . ¢ª üõ éÁŸ ö (Successor) ýÀ㓠ÂÊ€ä ‘þ ¢ª üõ éÁŸ

. Àª Àû¡ Â› Ã÷ Key[z]:=Key[Succ(z)]


. ݃õ‘÷ üõ y ¤ ùÀª éÁŸ ùÂð : ¢¢¤Âì

¤±ä y ¥ ‚ µ ªÁ 𠤢 ‚ î ý  ƒ Æ õ ¢ ª ü õ ° › õ ö ¥ éÁ Ÿ , À ª‘ “ ù‘ ƒ ¨ y  ð

ßµê¤ ßƒ“ ¥ ¶ä‘“ ßþ ø Àª‘“ ‚µª¢ ‘û ƒÆõ Âþ‘¨ ¥ µÞî ù‘ƒ¨ ùÂð ×þ , ù¢Âî üõ

ùÂð ݃÷µ“ ‚Ø€þ ­Âê ‘“ ¤ ÛØÈõ ßþ ݃÷— üõ ‘õ . ¢ª üõ ´¡¤¢ ¤¢ 4 ´ƒ¬‘¡

×þ ¤ ( ´¨ ù¢“ y öÀª éÁŸ ¥ Û±ì y ùÂð ´¨¤ ‘þ ² À÷¥ Âê ´ÖƒÖŸ ¤¢ ‚î) x
ùÂð ø¢ ¥ ¤ ±ä ý‘€ãõ ‚“ x ùÂ𠥐 ¤ ±ä ü€ãþ . ݃þ‘Þ÷ ۟ , ݃€î ù‘ƒ¨ — ‚ê‘® ¤‘“

x ü€ãþ ©À÷¥ Âê ‚“ ¤ ö Ù÷¤ ݃€î üõ éÁŸ ¤ y ùÂð ‚î ü÷‘õ¥ ßþÂ“‘€“ . Àª‘“ ù‘ƒ¨

¥ ©¢¡) . Àª‘“ ùÀª ù‘ƒ¨ ¤‘“ø¢ x ¢¤¢ ö‘Øõ ‚î ´¨ ßþ ÛØÈõ ‘ú€— . ݃µ¨Âê üõ

. ¢ª üõ ´¡¤¢ ¤¢ 1 ´ƒ¬‘¡ ßµê¤ ßƒ“ ¥ ¶ä‘“ ßþ ø ( Àª‘“ ù‘ƒ¨ Û±ì

´¡¤¢ ¤¢ —„‘“ –‘ä‘Ô—¤ éÂÏ ‚“ ¤ üꑮ ù‘ƒ¨ Ù÷¤ ×þ ‚î ´¨ ßþ ‘õ éÀû

: ݃¨Â“ Âþ ¥ ý‘û ´ó‘Ÿ ¥ üØþ ‚“ ‘— ݃µ¨Âԓ

. ݃þ‘Þ÷ üõ ù‘ƒ¨ ¤ ö ‘õ ´ó‘Ÿ ßþ ¤¢ ‚î Àþ‘Þ÷ ù¤‘ª ÃõÂì ùÂð ×þ ‚“ x ( Óó

. ´¡À÷ ¤ ø¢ ö— üõ ¤ üꑮ ù‘ƒ¨ , ´ó‘Ÿ ßþ ¤¢ ‚î À€î ù¤‘ª ‚Èþ ¤ ‚“ x ( ’

. ¢Þ÷ ‘ƒŸ ¤ 1 ´ƒ¬‘¡ öµ“ ¢À¹õ ýÃõ Ù÷¤ ø °¨‘€õ ý‘û ö¤ ø¢ ‘“ ( ”

4 ‘“ ´¨ ßØÞõ ݃¨Â“ „‘“ ´ƒã®ø ‚¨ ¥ üØþ ‚“ ‚î ÝþÈ÷ Õêõ ‚ÜŸÂõ ×þ ¤¢ Âð

Àª‘“ ´¨¤ ‘þ ² À÷¥ Âê x ‚Ø€þ °ÆŸ “ ´ÖƒÖŸ ¤¢) Ýþª ø ¤ ‚“ ø ¤ –ø‘Ôµõ ´ó‘Ÿ

: ‘û ¢¢¤Âì (. Àþ üõ ¢›ø ‚“ ö ¤‘Öµõ ø¢ ‚“ ø¢ ´ƒã®ø 8

. ¢¤¢ ù‘ƒ¨ °Æ “ ø¢ ‚î ´¨ ý ùÂð x ‚ÜŸÂõ Âû ¤¢ •

. ¢“ Àû¡ w=sibling(x) •

ߪø ¤ ý µÆ ý‘û ùÂð ø ÃõÂì ý µÆ ý‘û ùÂð , ù‘ƒ¨ Ù÷¤ ùƒ— ý‘û ùÂð •
. c’,c À€÷‘õ ´¨ ôÜãõ‘÷ Ù÷¤ ‘“ ùÂð ÂÚ÷ ‘þ‘Þ÷
91 RED-BLACK ù‘ƒ¨ - ÃõÂì ´¡¤¢ .7.3

: ´¨ Âþ ¥ –¤ ¬ ‚“ Àþ ¢›ø ‚“ ´¨ ßØÞõ ‚î üþ‘û ´ó‘Ÿ

( ¢¤¢ ù‘ƒ¨ öÀ÷¥ Âê ßþÂ“‘€“ ) ´¨ ÃõÂì w(1

. ´¨ ù‘ƒ¨ ©À÷¥ Âê ø¢ ø w(2

. ´¨ ù‘ƒ¨ ö ´¨¤ À÷¥ Âê ø ÃõÂì ö ² À÷¥ Âê ø ù‘ƒ¨ w(3

. ´¨ ÃõÂì ö ´¨¤ À÷¥ Âê ø ù‘ƒ¨ w(4

.... ....
.. ..
B D
case -
1
(a) x A B E
D
α β x ε ξ
A new x C
C E
α β γ δ
γ δ ε ξ
... ....
... .
B c new x . c
B
case -
2
(b) x A
A D D
α β α β
C E C E
γ δ ε ξ γ δ ε ξ
... ....
... .. c
Bc B
case 3- new w
x A w xA C
(c) D
β α β γ
α D
C E
γ δ ε ξ δ
E
... .... ε ξ
... .. c
c D
B
x case -
4
(d) w E
A D B
α β c’ ε ξ
C A C
E
γ ε ξ α β γ δ
δ
new x =root[T]
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 92

ö¤ ø¢ ô‘¹÷ ø ÂÚþÀØþ ‘“ B ø D ý‘û ùÂð Ù÷¤ Ëþã— ‘“ 1 ´ó‘Ÿ : ñø ´ó‘Ÿ (1

. ¢ª üõ ÛþÀ±— 4 ‘þ 3 ‘þ ø 2 ý‘û ´ó‘Ÿ ¥ üØþ ‚“ ¢Â𠲝

à õÂ ì ‘ “ ´ ¨ ùÀ ª ù¢¢ Ç þ‘ Þ ÷ x  ð ù¤‘ ª ‘ “ ‚ î ü ê‘ ® ù‘ ƒ ¨ Ù ÷¤ : ôø¢ ´ ó‘ Ÿ (2

Õ þ Â Ï ¥  ð . ¢ ª ü õ Û Ö µ € õ  —„‘ “ á‘ Ô —¤ × þ ‚ “ B ‚“ x ö¢¢ ù¤‘ ª ø D ö¢Â î

. Àƒ¨¤ Àû¡ ö‘þ‘• ‚“ ‚Öܟ ´¨ ÃõÂì c ö ݃ª‘“ ùÀª 2 ´ó‘Ÿ ¢¤ø ñø ´ó‘Ÿ

ö¤ ø¢ × þ ô‘ ¹ ÷ ø D ø C ý‘ û ù ð Ù ÷¤ Ë þ 㠗 ‘ “ 3 ´ ó‘ Ÿ : ô ¨ ´ ó‘ Ÿ (3

. ¢ª üõ ÛþÀ±— 4 ´ó‘Ÿ ‚“ ¢ÂÚµ¨¤

­ä ‘“ ö— üõ ¤ ´¨ ùÀª ù¢¢ Çþ‘Þ÷ x ‘“ ‚î üꑮ ù‘ƒ¨ Ù÷¤ 4 ´ó‘Ÿ ¤¢ (4

¢¥ ‚ Þ Î ó ´ ¡¤¢ « ¡ ‚ “ ‚ Ø ÷ öøÀ “ ¢Â ð ²  ö¤ ø¢ × þ ø Ù ÷¤ À €  ö¢Â î

. À¨¤ üõ ö‘þ‘• ‚“ ‚ÜŸ Âõ ßþ ¤¢ ‚Öܟ ø ¢Þ÷ éÁŸ

. ´¨ O(lg n) ¥ Ýû ݵþ ¤ Úó ßþ ö‘õ¥

RB-Delete(T,z)
1. if left[z]=nil[T] or right[z]=nil[T] then
2. y←z
3. else y ← Tree-Successor(z)
4. if left[y] 6= nil[T] then
5. x ← left[y]
6. else x ← right[y]
7. p[x] ← p[y]
8. if p[y] = nil[t] then
9. root[T] ← x
10. else if y = left [p[y]] then
11. left[p[y]] ← x
12. else right[p[y]]← x
13. if y 6= z then
14. key[z]← key[y]
15. // if y has other fields , copy them , too
16. if color[y] = BLACK then
93 RED-BLACK ù‘ƒ¨ - ÃõÂì ´¡¤¢ .7.3

17. RB-Delete-Fixup(T,x)
18. return y

RB-Delete-Fixup(T,x)
1. while x 6= root[T] and color[x] =BLACK do
2. if( x = left[p[x]]) then
3. w ← right[p[x]]
4. if( color[w]=RED) then
5. color[w] ← BLACK //case1
6. color[p[x]] ← RED //case1
7. LEFT-Rotate(T,p[x]) //case1
8. w ← right[p[x]] //case1
9. if(color[left[w]]=BLACk and color[right[w]]=BLACK) then//case1
10. color[w]=RED //case2
11. x ← p[x] //case2
12. else if color[right[w]]=BLACK then
13. color[left[w]] = BLACK //case3
14. color[w] ← RED //case3
15. RIGHT-Rotate(T,w) //case3
16. w←right[p[x]] //case3
17. color[w]←color[p[x]] //case4
18. color[p[x]]←BLACK //case4
19. color[right[w]] ← BLACK //case4
20. LEFT-Rotate(T,p[x]) //case4
21. x←root[T] //case4
22. else(same as then clause with ”right” and ”left” exchanged)
23. color[x]← BLACK
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 94

(Disjoin sets)Ã¹õ ý‘û ‚äÞ¹õ 8.3


ၠ÷ ß þ  —ù¢‘ ¨.À € õ‘ ÷ ü õ Ã ¹ õ À ª‘ “ ü ú — ø¢ ö íÂ µ ª ù‘ ð û ¤ AøB ‚ ä Þ ¹ õ ø¢

°ÆÂ“ nø ...ø2ø1 ý‘û ù¤‘Þª‘“ ‚î ýÌä n‚äÞ¹õ ý¥‘¨ ù¢‘ƒ• ýÂ“ ýù¢¢

.´¨‚þ¤¥ù¢‘Ôµ¨À÷ù¢¤ ¡

Ã¹õ ý‘û ‚µ¨¢ ‚“ ‚î ¤ nø...ø2ø1 ‚äÞ¹õ¬‘€ä ö— üõ ü𢑨 ‚“ ‚þ¤ ×þ‘“

ö Ýރ÷ üõ Âʀä ô‘÷ ‘“ ¤ ‚äÞ¹õ Âþ ¥ Âû ¤ Ñ€õ ßþ ýÂ“ .¢¢ Çþ‘Þ÷À÷ùÀª ¥Âê

ü õ ¤Â ì Ý Þ ƒ ÷ ü õ Â Ê € ä ß ƒ Þ û ‘ “  “Â “ ¤ ¤À • Â Ê € ä ö  “ ùø… ä ø ù¢ Þ ÷ É ¿ È õ

. ݃þ‘Þ÷ üõ üû¢¤ÀÖõ ö ý‘û ÅþÀ÷Âþ¢‘Öõ ‘“¤ set ‚þ¤ Ã÷ Àµ“ ¤¢. ݃û¢

set={1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
set
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1={1 , 5 } 2={2 , 4 , 7 , 10 } 3={3 , 6 , 8 , 9 }

1 2 3

5 4 7 10 6 8 9

. ݃û¢ üõ ¤Âì set ‚þ¤ ¤¢ ¤ ©¤À• ùÂð ¢Àä Âʀä Âû ýÂ“ ų¨

set
1 2 3 2 1 3 2 3 3 2
1 2 3 4 5 6 7 8 9 10

function find1(x)
{ find the lable of the set containing x}
return set[x]

ݵþ ¤ Úó ßþ. À÷¢Âð üõ “¤ ¢¤¢ ¤Âì ö ¤¢ x ‚î ý‚äÞ¹õ °ÆÂ“ find1 ⓑ—
. ´¨ θ(1) ¥

ö‘õ¥.Àþ‘Þ÷ üõ ô‘袐 Ýû ‘“ ¤ À€µÆû bø a Ûõ‘ª ‚î ý‚äÞ¹õ ø¢ merge1 ⓑ—


. ´¨ θ(n) ¥ ⓑ— ßþ
95 (DISJOIN SETS)Ã¹õ ý‘û ‚äÞ¹õ .8.3

function merge1(a,b)
{merge the sets labled a and b }
i←min(a,b)
j←max(a,b)
fork←1 to n do
if set[k] = j then set[k]← i

merge2 ø find2 ⓐ— ø ݃þ‘Þ÷ üõ °ƒî— ý ÂÚþ¢ –¤ ¬ ‚“ ¤ ‚äÞ¹õ ø¢ ñ‘Ÿ

: ݃€î üõ Óþ Â㗠Âþ ¥ –¤ ¬ ‚“¤

function find2(x)
r ←x
while(set[r]6= r) do
r←set[r]
return r

function merge2(a,b)
if(a<b) then set[b]← a
else set[a]← b

merge2 ⓑ— ø ´¨üΡ·îÀŸ á‘Ô—¤ ´ó‘Ÿ ßþ —À“ ¤¢Âþ ¥ ´¨ O(n) ¥find2 ⓑ—
. ´¨ — ‚€ƒú“ üܱì ݵþ ¤ Úó ‚“ ´±Æ÷ ‚î ´¨ θ(1) ¥

ݵþ ¤ Úó ¤¢ ´¨ µ؝î ö °ÆÂ“ ‚î ´¨ ýÀƒÜî ‚Èþ ¤ ÀƒÜî üÜ±ì ´¡¤¢ ø¢ ¤¢

ö öÀª ï¤ Ã“ ¥ø¢Âî ¤‘î á‘Ô—¤ ýø ¤ Àþ‘“ ŕ ´¨ ý µȃ“ ö‘õ¥ ý¤¢ find ôø¢

.¢Âî ý ƒðÜ›

¤ ´ þ óø ø Ý þ  ƒ ð ü õ Â Ñ ÷ ¤¢ ü ä‘ Ô —¤ ´ ¡¤¢ ¥ ù ð  û ýÂ “ ¤  Ñ € õ ß þ ýÂ “

. ÀƒÜî ö¢“ םî ‚÷ ݃û¢üõ ¤Âì á‘Ô—¤

: ݃þ‘Þ÷ üõ Óþ Â㗠Âþ ¥ –¤ ¬ ‚“ ¤ find3 ø merge3 ⓐ— –¤ ¬ ßþ ¤¢

function merge3(a,b)
if(height[a] = height[b])
‘û ù¢¢ ö‘Þµ¡‘¨ ¥ ü¡Â“ ý ¤ ø¢‘þ .3 ÛÊê 96

set[b]← a
height[a] ++
r← a
else
if(height[a]>height[b]) then
set[b]← a
r←a
else
set[a] ← b
r←b
return r

function find3(x)
r←x
while(set[r]6=r) do
r← set[r]
i←x
while i6= r do
j ← set[i]
set[i] ← r
i←j
return r
.´¨ÂµÞî Ã÷ log n¥Ýµþ ¤ Úó ßþ ö‘õ¥
4 ÛÊê

Óܵ¿õ ý‘û ©ø ¤ üêÂãõ

üÆþ÷ ݵþ ¤ Úó

üÆþ÷ ‚õ‘÷“ ý‘û ©ø ¤ ᐁ÷ .4

ù¤‘ ª  þ ¥ ¢¤ õ ‚ “ ö — ü õ ü Æ þ ÷ ‚ õ‘ ÷ “ ý‘ û × ƒ € Ø — ᐁ ÷ ¥

:¢Þ÷

‚÷‘Êþ Ÿ ׃€Ø— •

۟ ø ݃Æ֗ ׃€Ø— •

‘þ• üÆþ÷‚õ‘÷“ ׃€Ø— •

°Öä ‚“ ´È𥑓 ׃€Ø— •

ÀŸ ø ‚¡‘ª ׃€Ø— •

‘û ý¥‘“ ´€Þ÷¤ — ׃€Ø— •

üó‘ÞµŸ ý‘û ݵþ ¤ Úó •

ý¥õ ý‘û ݵþ ¤ Úó •


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 98

(Greedy Algorithms) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó 1.4


.À€µÆû üÆþ÷ ݵþ ¤ Úó ý‘û ©ø ¤ ßþ — ù¢‘¨ âìø ¤¢ø ‚µ¨¢ ߃óø ‘û ݵþ ¤ Úó ßþ

©¥ ¤  • ñ‘ Ÿ ß ƒ ä ¤¢ ø ß þ  — ´ Ÿ¤ ¢ ª ü õ ü ã ¨ ù¤ Þ û Û Ÿ ýÂ “ ©ø ¤ ß þ ¤¢

۟ ýÂ“ ©ø ¤ ×þ âìø ¤¢ ‚÷‘Êþ Ÿ ׃€Ø—.¢ª ’‘¿µ÷ ô‘ð Âû ¤¢ ۟ ù¤ ßþ —

©ø ¤ ßþ ¤¢ ‚î ´Æ÷ ©ø ¤ ßþ ¤¢ ´ƒÞû ΑŸ ‚µØ÷ ‘õ .´¨ ý¥‘¨ ‚€ƒú“ ێ‘Æõ

‚“ ´¨ ßØÞõ Âʀä ßþ — ©¥ ¤ • ’‘¿µ÷ ‚î ¢¤À÷ ¢›ø ၮõ ßþ ‚èÀè¢ ÃðÂû

.¢ª üúµ€õ ´Æ“ ߓ ‚“ ‘þ ø ‚ûÂƒ“ ‚“ ¢ª ¹€õ ۟ ×þ‚“ ‚Ø÷ ý‘›

:´¨Âþ ¥ Control Abstraction Փ‘Îõ ‚÷‘Êþ Ÿ ݵþ ¤ Úó ×þ ¤‘µ¡‘¨

Fuction Greedy(C):set //solution set


{ C is a set of condidates}
S ← ∅ { S ⊆ C is a set of solution ,if it is possible }
{ Greedy loop}
While (C 6= ∅ & not Solutions(S))
x ← Select(C)
C←C\{x}
if feasible ( S ∪ { x }) then
S←S∪{x}
if (solution(S)) then return S
else ” there are no solutions.”
‚î ¤‘“ Âû ¢ª üõ ‚ê‘® ‚äÞ¹õ ‚“ ’› ¢›ø –¤ ¬ ¤¢ ´¨ üú—Àµ“¤¢ S
éÁŸ C ‚äÞ¹õ ¥ ‘f—Àä‘ì À€î üõ ’‘¿µ÷ ¤ C ‚äÞ¹õ ¥ Âʀä ×þ Select ⓑ—

‚“C ‚äÞ¹õ ¥ Âʀä ö¢Âî ‚ê‘® ‘þ ‚î ´Æ€þ feasible ⓑ— ‚ÔƒÒø .¢ª üõ

solution , feasible ø Select ⓐ— ‚ó‘bÆõ á÷ ‚“ ‚µÆ“.ƒ¡ ‘þ ´¨ ßØÞõ S ‚äÞ¹õ

. À€µÆû ƒƒç— ۓ‘ì

ßÞê‘û ý¥‘¨ ù¢ÂÈê ݵþ ¤Úó 1.1.4

ý¢ø ¤ ø ‚ õ‘ ÷ “ ©ø ¤ ß þ¤¢ . ´ ¨ ß Þ ê‘ û ©ø ¤ ‘ û Û þ‘ ê ý¥‘ ¨ ù¢Â È ê ©ø ¤ × þ

¤¢ ‚µê¤ ¤‘î ‚“ ý‘û ‚Æþ÷ ¥ ×þ Âû Å÷‘îÂê ų¨ ¢ª üõ ´ê‘þ ¤¢ ‚Æþ÷ ‚“ ‚Æþ÷

‘û ‚Æþ÷ ü÷øÂê ¥ ù¢‘Ôµ¨ ‘“ ݃û¢ üõ ¤Âì üóøÀ› ¤¢ ¤ ö ø¢¢Âð üõ ‚±¨‘½õ ßµõ

:݃€î üõ ‘€“ Âþ ¥ žÂª ‚“ üŽø¢ø¢ ´¡¤¢ ×þ


99 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

ý¤¢ ‚ î ý ù ð ø¢ Å ³ ¨, Ý þ ¥‘ ¨ ü õ ¤ ´ ¡¤¢ ¥  × þ ‚ Æ þ ÷  û ý¥ ‚ “

Ýþ ¥‘¨ üõ ¤À• ùÂð ×þ ùÂð ø¢ ö ýÂ“ ø ù¢Âî °ƒî— Ýû ‘“ ¤ À€µÆû ý µÞî Å÷‘îÂê

ø¢ ߃“ .´¨ ö ùÀ÷¥‘¨ ùÂð ø¢ ý‘û Å÷‘îÂê áÞ¹õ “Â“ ¤À• ùÂð ßþ Å÷‘îÂê ‚î

‘— ݃û¢ üõ ‚õ¢ ¤ À÷ø ¤ ßþ À÷ ùÀÈ÷ ‚µêÂð ý¥‘“ ‚“ ñ‘½“ ‘— ‚î üþ‘û ùÂð ø ƒ¡ ùÂð

“Â“ ‘û ï“ ý‘û Å÷‘îÂê áÞ¹õ ‘“ Àþ‘“ ‚Èþ ¤ Å÷‘îÂê.Àþ ´¨¢ ‚“ ´¡¤¢‚Èþ ¤

ÂÑ÷ ¤¢ ×þ ‘þ ÂÔ¬ °ÆÂ“ ×þ ´¡¤¢ ñ‘þ Âû ýÂ“ Àª ‚µ¡‘¨ ´¡¤¢ ‚î ñ‘Ÿ ,Àª‘“

ÂÔ¬ °Æ “ ¤ ² ´Þ¨ ñ‘þ Âû ø ×þ °ÆÂ“ ¤ ´¨¤ ´Þ¨ ñ‘þ Âû. Ýþ ƒð üõ

üõ ´îŸ ‘û ï“ ´Þ¨ ‚“ ‚Èþ ¤ ´Þ¨ ¥ ‚î ý ƒÆõ üÏ ¤¢ °ƒ—— ßþÀ“ .݃÷¥ üõ

ö‘“ Âґ€µõ Àî ‚µª¤ ö ‚î Àõ Àû¡ ´¨À“ ï“ ‘“ Âґ€µõ ý €þ‘“ ‚Èþ ¤ ×þ, ݃€î

. ´¨ ï“

¥ Ý þ ¤¢ ¤‘ ƒ µ ¡ ¤¢ ü þ‘ ± Ô ó 頟7000 Û õ‘ ª ü € µ õ À ƒ € î ­Â ê ñ‘ · õ ýÂ “

1100, D頟 1800 ,C頟 900, A頟 1200, B 頟 1800 ¢À 㠗 ß þ

ù¢ÂÈê ýÂ“ ô¥„ ý‘û ´ƒ“ ¢À㗠ÛìÀŸ ݃û¡ üõ. Ýþ ¤¢ MéŸ 200ø EéŸ
. Ýþ ¤ ø ´¨¢ ‚“ ¤ ßµõ ßþý¥‘¨

7000
1

4000
0 0

2200
1
0 1
3000 1100
0 1
1 0

1200 1800 900 1100 1800 200


A=00 B=01 C=1000 E=101 D=11 M=1001
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 100

‘“ ö— üõ °ƒ—— ßþÀ“.¢¤¢ ý µ؝î Àî , ¢¤¢ ý —„‘“ Å÷‘îÂê ‚î ý Âʀä

´ó‘Ÿ ¤¢ µ‘î Âû ñ‘·õ ßþ ¤¢ .¢¢ Çþ‘Þ÷ ¤ –‘ä…ϐ Ûî , ‚Ñꑟ ¥ ý µÞî ¤ÀÖõ

‚ “  ð ý¥‘ ¨ ù¢Â È ê ¥ Å • ‚ î ü ó‘ Ÿ ¤¢,À þ‘ Þ ÷ ü õ ñ‘ ç ª ¤ ‚ Ñ ê‘ Ÿ ¥ ´ ƒ “8 ý¢‘ ä

ñ‘窐 ¤ ´ƒ“ 4·îÀŸ µ‘î Âû ݃û¢ ¤Âì ¤ ©Âґ€µõ üþø¢ø¢ Àî µ‘î Âû ý‘›

:Àþüõ ´¨¢ ‚“ Âþ ¥ –¤ ¬ ‚“ ñ‘·õ ßþ ¤¢ ùÀª éÂÊõ ý‘û ´ƒ“ Ûî.Àþ‘Þ÷ üõ

2 ×1200 +2 ×1800+4×900+3×1100+2×1800+4×200=17300
.Àª‘“ üõ 7000×8=56000 ü€ãþ ö ý¢‘ä ´ó‘Ÿ ¥ µÞî ¤‘ƒÆ“ ‚î

ý‘ › ‚ “ ¤‘ î ß þ ýÂ “, Ý ƒ þ‘ Þ ÷ ù¢Â È ê ¤ DBAD ‚ µ ª¤ Ý ƒ û ¡ ü õ ñ‘ · õ ö € ä ‚ “

×þ ñ¢‘ãõ ‚î Àþ üõ ´¨¢ ‚“ 11010011 Àî ø ù¢¢¤Âì ¤ ö ñ¢‘ãõ µ‘îÂû

݃÷À“ ݃û¡ üõ Ýþ ¤¢ ¤ 01001001101Àî ÅØä ‚“ ‘þø ´¨ X f ·õ µ‘î


…

ý €þ‘“ Àî ´¨¤ ‚“ ² ´Þ¨ ¥ ¤‘î ßþ ýÂ“ ,Àª‘“ üõ ý‚µª¤ ‚ ñ¢‘ãõ Àî ßþ

Ý ƒ ¨Â “ Â Ñ ÷ ¢¤  õ  µ ‘ î ‚ “ ‘ — ù¢Â î ñ‘ ± ÷¢ ¤ ö ´ ¡¤¢ ‚ È þ ¤ ¥ ö‘ õÃ Þ û ø ùÀ ÷ ¡ ¤

.Àþ üõ ´¨¢‚“ BAME ý‚µª¤ °ƒ—— ßþÀ“.

À€þ üõ ´¨¢ ‚“ ¢Âê ‚“ Âʽ€õ –¤ ¬ ‚“ ‘û Àî ßþ ¥ ôÀî Âû ‚î ´ª¢ ‚›— Àþ‘“

.Àª‘“ üÞ÷ µð¤ Ó Àî ¥ ýÀ÷Èƒ• µ؝î Àî Âû ø

À“ ¤¢ ´ó‘Ÿ ßþ ¤¢ ݃€î Àî ßÞê‘û ©ø ¤ ‚“ ݃û¡ üõ ¤ µ‘î n ‚î Àƒ€î ­Âê

Àû¢ üõ  ¤ ü÷‘õ¥ ´ó‘Ÿ ßþ µð¤ Ó . ¢“ Àû¡ n-1 ‚Æþ÷ ·îÀŸ ñÏ ö‘õ¥ ßþ —

. ´¨ ô i-1‘— 1 ÂÊ€ä ¥ ü÷øÂê áÞ¹õ ¥ µð¤ Ó ö ü÷øÂê ô i Âʀä ýÂ“ ‚î

...
.. .
..
15 ...
....
....
....
....
7 ....
....
....
....
....
....
3 ....
....
....
1 2 4 8
101 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

MST ñ‘Þƒ€ƒõ ý‘ª• ´¡¤¢ ý‘û ݵþ ¤Úó 2.1.4

§Ž¤ ‚äÞ¹õ ‚îÀª‘“ ù¢‘¨ ¤¢ ´ú› ƒèÀ€±Þû éÂð ×þ G=<N,A> Àƒ€î ­Âê
‚î ´¨ üµ¡¤¢ éÂð ßþ¥‘ª•´¡¤¢ ×þ ¥¤ Ñ€õ.Àª‘“Aö ý‘úó‘þ ‚äÞ¹õø N ö

´¨ éÂð ßþ ý‘úó‘þ ¥ ü¡Â“ Ûõ‘ª ñ‘Ÿ ßƒä ¤¢ üóø ù¢“ éÂ𠧁Ž¤ ‚Þû Ûõ‘ª

.( ¤ ø¢ Àì‘êøÀ€±Þû)Àª‘“ ´¡¤¢ ¢¡ ùÀõ ´¨À“ ¤‘µ¡‘¨ ‚Øþ ¤ Î“

: ‚ƒÌì
:Àª‘“ ¤Âì“ üþ‘ú€— ‚“ Âþ ¥ ý‘û ù¤Ã𠥐 ×þ Âû Âð‘ú€—ø Â𐠴¨ ´¡¤¢ T=<V,E>
.(‘û ñ‘þ ¢À㗠q , §Ž¤ ¢À㗠p) p=q+1 ,Àª‘“ ¤ ø¢ Àì‘ê T .1

. p=q+1ø À€±Þû T .2

.Àª‘“ ‚µª¢ ¢›ø ƒÆõ ×þ‘Öƒì¢ Ãþ‘Þµõ §¤ ø¢Âû ߃“ .3

.Àª‘“ ¤ ø¢ Àì‘ê ø À€±Þû .4

 “ ö ñ‘ þ  û ‚ î ü µ ¡¤¢)¤¢ ‚ ÷‘ È ÷ ´ ¡¤¢ pp−2 ‚ ½ Ô ¬ ¤¢ ‚ Î Ö ÷ p ‘ “

.¢Âî ݨ¤ ö— üõ( ¢¤¢ °Æ

: ñ‘·õ

p−2
´ ¡¤¢ p ý¤¢ Kp Û õ‘ î ´ ¡¤¢ :ü Ž‘ Þ € û¤)?¢¤¢ ‘ ª • ´ ¡¤¢ À €   þ ¥ éÂ ð

(.´¨ ‘ª•

| {z } | {z }
K3 K4

:۟

33−2 × 44−2 = 48
‘“ ö ý‘úó‘þ ¥ ×þ Âû ‚îÀª‘“ ¤¢ ö¥ ø éÂð ¤ îÁõ ÍþÂª ‘“ G éÂð Âð ñ‘Ÿ

ß þ ¥ Ý Þ ƒ ÷ ü õ ý‘ ª • ´ ¡¤¢ × þ ö — ü õ À ª‘ “ ù¢¤  ¡ ° Æ   “ ü Ô € õ‘ ÷ ý¢À ä

(minimum spaning tree) Ýރ€ƒõ ý‘ª• ´¡¤¢ ×þ ¥ ¤ Ñ€õ¢Âî šÂ¿µ¨ éÂð


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 102

áÞ¹õ ‚Øþ ¤ Î“ ´¨ ö ý‘úó‘þ ¥ ü¡Â“ø éÂ𠧁Ž¤ ‚Þû Ûõ‘ª‚î ´¨ üµ¡¤¢

. ´¨ ‚€ƒÞî ‘ª• ý‘û ´¡¤¢ Âþ‘¨ ߃“ ¤¢ ö ý‘úó‘þ ö¥ ø

× þ ý¤¢ ‘f Þ µ Ÿ ù¢‘ ¨ ¤¢ ´ ú ›  ƒ è ¤¢ ö¥ ø À € ± Þ û éÂ ð  û: ‚ › —

À÷µƒõ ‚Ø€þ ü€ãþ Àª‘±÷ ¢Âê ‚“ Âʽ€õ À÷— üõ ‘õø Ãó ‚î ´¨ Ýރ€ƒõ ý‘ª• ´¡¤¢

.Àþ ´¨À“ ‘ª• ´¡¤¢ ßþ ¥ ǃ“

: ¢¤¢ ¢›ø Ýރ€ƒõ ý‘ª• ´¡¤¢ ßµê‘þ ýÂ“ ݵþ ¤ Úó À€ üÜî ¤ Ï ‚“

Prim , Kruskal, Boruvka ,Sollin

:kruskal ݵþ ¤Úó

:¢¥‘¨ üõ ¤ ñ‘Þƒ€ƒõ ý‘ª• ´¡¤¢ ×þ Âþ ¥ ùƒª ‚“ ݵþ ¤ Úó ßþ

¢À㗠‚“ ‚äÞ¹õ n ų¨ À€Øƒõ °—Âõ ý¢ã¬ °ƒ—— ‚“ ¤ éÂð ý‘úó‘þ Àµ“

¥ ‚ ä Þ ¹ õ  ¬‘ € ä ‚ î ´ ¨ ß þ  “ ­Â ê)¢¥‘ ¨ ü õ éÂ 𠧁 Ž¤ ‚ ä Þ ¹ õ  ¬‘ € ä

’‘¿µ÷ ñ‘þ n-1 ‘— ¢Èƒõ ¤Âؗ¤Àì ö greedy ‚Öܟ ñ‘Ÿ( À÷ù¢¤ ¡ °ÆÂ“ n ‘—1
find ⓑ— ų¨ Àª‘“ ‚µª¢ ¤ ßØÞõ ö¥ ø ßþ µÞî ‚î ¢ª üõ ’‘¿µ÷ üó‘þ. À÷ª

‚±¨‘½õ find(v) ,find (u) Àª‘“ e=uv Âð ü€ãþ¢Èƒõ ü÷¡Âê ñ‘þ ßþÂ¨ ø¢ ýÂ“

ù¢ø Ãê éÂð ý‘úó‘þ ‚äÞ¹õ ‚“ ¤ îÁõ ñ‘þ Àª‘±÷ “Â“ ø¢ ßþ Û¬‘Ÿ Âð.À÷ª üõ

°ƒî— Ýû ‘“ À÷ ùÀõ ´¨¢ ‚“ find(v),find(u) ¥ ‚î ý ‚Ôóõ ø¢ ߃€ Ýû ¢Èƒõ

.¢ª ñ‘þ n-1 Ûõ‘ª T ‘— À“‘þ üõ ‚õ¢ ¤ÀÖ÷ À÷ø ¤ ßþÀ÷ª üõ

function kruskal(G=< N,A > : graph,length :A → R+ ): set of edge


sort A by increasing length
initialize n sets , each set containing an element of N
T← ∅
{greedy loop}
repeat
e←{u,v}
A← A \ {e}
ucomp←find (u)
vcomp←find (v)
103 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

if(vcomp 6= ucomp) then


T←T ∪{e}
merge(ucomp,vcomp)
until T is containing n-1 elements
return T

:Àþ ƒړ ÂÑ÷ ¤¢ ¤ Âþ ¥ éÂð ñ‘·õ ö€ä ‚“

1 1 2
2
1 2 3
4 6 5 6 4
4
3
4 3 8
5 6

7 4 3
4 3

step T connected components


initialization - {1}{2}{3}{4}{5}{6}{7}
1 {1,2} {1,2}{3}{4}{5}{6}{7}
2 {2,3} {1,2,3}{4}{5}{6}{7}
3 {4,5} {1,2,3}{4,5}{6}{7}
4 {6,7} {1,2,3}{4,5}{6,7}
5 {1,4} {1,2,3,4,5}{6,7}
6 {2,5} reject
7 {4,7} {1,2,3,4,5,6,7}

.n log n Ýþ ¤¢ ü÷¡Âê ¤‘“ n‘“ ŕ , ´¨ log n ù¥À÷‚“ merge,find ö‘õ¥ ‘› ßþ ¤¢
: Ýþ ¤¢ üêÂÏ ¥. §Ž¤ ¢À㗠n ø ´¨‘úó‘þ¢À㗠|A| = a
n(n−1)
(n − 1) ≤ a ≤ 2
⇒ log (n − 1) ≤ log a ≤ log n + log (n − 1) − log 2
⇒ log n ≤ log a ≤ 2 log n
.¢“ Àû¡ O(a log a)¥ sort ö‘õ¥ ŕ ´¨log n ‚›¤¢ Ýû log a ŕ
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 104

: Prim ݵþ ¤Úó

:¢¥‘¨ üõ ¤ ‘ª• ´¡¤¢ Âþ ¥ ùƒª ‚“ ݵþ ¤ Úó ßþ

¢ÂƒðüõÂÑ÷ ¤¢ ´¨ G éÂ𠧁Ž¤ ¥ üØþ Ûõ‘ª ‚î ¤ B ô‘÷ ‚“ ý ‚äÞ¹õ Àµ“


´Æƒ÷ B ¤¢ ö ÂÚþ¢ ¨ ø ´¨ B ¤¢ ‘ú÷ ¨ ×þ ‚î éÂð ý‘û ñ‘þ ô‘Þ— ߃“ ų¨

‚ê‘® B ‚“ ¤ ÂÚþ¢ §b¤ ų¨,Àª‘“ ‚µª¢ ¤ °Æ “ ßþ µÞî ‚î À“‘þ üõ ¤ üó‘þ ,

.¢ª éÂ𠧁Ž¤ ü€ãþ N ‚äÞ¹õ “Â“ B ‘— ÀûÀƒõ ‚õ¢ ¤ÀÖ÷ ¤ À÷ø ¤ ßþ.À€î üõ

ý¥‘¨ ù¢‘ƒ• (üÆþ —‘õ ) ýÀ㓠ø¢ ý‘û ‚þ¤ ù¢¢ ö‘Þµ¡‘¨ ‘“ ¤ ݵþ ¤ Úó ßþ Âð

݃€î ý¥‘¨ ù¢‘ƒ• binary heap ×þ ‘“ Â𐠑õ . ´¨ O(n2 ) ÀŸ ¤¢ ô¥„ ö‘õ¥ ݃þ‘Þ÷

ö‘ õ¥ Ý ƒ þ‘ Þ ÷ ý¥‘ ¨ ù¢‘ ƒ • fibonacci heap ‘ “  ð ø ´ ¨ O((a + n)logn)Â › ö‘ õ¥

ƒƒç— ¤ Â› ö‘õ¥ ý ù¢¢ ö‘Þµ¡‘¨ ø ý¥‘¨ ù¢‘ƒ• ƒƒç— ŕ. ´¨ O(a + nlogn) Â›
.Àû¢ üõ

function Prim(G=< N, A >:graph,length:A −→ R+ ):set of edges


B ←− {an arbitary element of N}
T ←− ∅
while B 6= N do
e ←− { u,v} such that e is minimum and u∈ B,v∈ N\B
T ←− {e} ∪ T
B ←− B ∪ {v}
: –¤ ø‘¹õ Åþ —‘õ Õþ ÂÏ ¥ ý¥‘¨ ù¢‘ƒ•

function Prim(L[1..n,1..n]):set of edges


1. {initialization : only node 1 is in B}
2.T ←− φ {will contain the edges of the minimum spanning tree}
3. for i=2 to n do
4. nearest[i] ←− 1
5. mindist[i] ←− L[i,1]
6. {greedy loop}
7. repeat n-1 times
8. min ← ∞
9. for j ←2 to n do
10. if 0≤ mindist[j] < min Then
105 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

11. min ← mindist[j]


12. k←j
13. T=T ∪ {nearest[k],k}
14. mindist[k] ← -1{add k to B}
15. for j ← 2 to n do
16. if L[j,k] < mindist[j] Then
17. mindist[j] ←L[j,k]
18. nearest[j] ←k Return T
: heap ‘“ PRIM ݵþ ¤ Úó ý¥‘¨ ù¢‘ƒ•

HEAP-PRIM(G)
1. A ←− φ
2. for each x ∈ V do key[x] ←− ∞
3. H ←− Build-heap(key)
4. select arbitrary vertex v
5. S ←− {v}
6. Decrease-key(H,v,0)
7. Extract-min(H)
8. for each x adjacent to v do
decrease-key(H,x,w(v,x)), π [x] ←− v
9. for i=1 to |V| - 1 do
10. begin
11. Extract-min(H),let v be the vertex
12. A ←− A∪ {(v,π[v])}
13. S ←− S ∪ {v}
14. for each x ∈ V-S adjacent to v do
15. if w(x,v) < key[x] Then
16. Decrease-key(H,x,w(x,v)),π[x] ←−v
17. end
18. return A
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 106

: ݵþ ¤ Úó Ãó‘÷

ö‘õ¥ ø ö ´ó‘Ÿ ø¢ ‚î.¢ª ý¥‘¨ ù¢‘ƒ• À÷— üõ –¤ ¬ ø¢ ‚“ heap ݵþ ¤ Úó ßþ ¤¢
: ´¨ ùÀª ù¢¢ žÂª Âþ ¥ ¤¢ ‘û ö ýÂ“ ô¥„

:binary-heap ¥ ù¢‘Ôµ¨ •
üõ key[x] ‚ ƒ óø  þ¢‘ Ö õ ‘ “ heap × þ‘ õ ýÂ “ ‚ î Build-heap ⠓‘ — ´ ó‘ Ÿ ß þ ¤¢

heap ¤À Ö õ ß þ  µ Ø  î Extract-Min ⠓‘ — .¢Â ƒ ð ü õ O(|V |)  “Â “ ü ÷‘ õ¥ , ¢¥‘ ¨

ö¢¤ ø ´ ¨¢ ‚ “ ´ ó‘ Ÿ ß þ ¤¢ ‚ î,¢¥‘ ¨ ü õ š¤‘ ¡ heap ¥ ¤ ö øÀ ÷¢Â ð ü õ  “ ¤

Àþ‘“ ų¨,¢¤¢ ¤Âì ‚Èþ ¤ ¤¢ ¤ÀÖõ ßþ Âþ ¥, ´¨ Θ(1)¥ heap ¤ÀÖõ ßþ µ؝î

×þ ‚“ ù¤‘“ø¢ ø ݃þ‘Þ÷ ö ßþ ÃÚþ‘› ¤ heap Âʀä ßþ ¡, ‚µª¢Â“ ¤ ¤ÀÖõ ßþ

.Àª‘“ üõ O(log|V |) ¥ –‘ƒÜÞä ßþ ‚î, ݃€î Àƒ• ´¨¢ heap


üõ ßþ ÃÚþ‘› ý µ؝î ¤ÀÖõ ‘“ ¤ heap ¥Âʀä ×þ ¤ÀÖõ Decrease-Key ⓑ—
ß þ ´ ó‘ Ÿ ß þ ¤¢ ‚ î , À þ‘ Þ ÷ ü õ ý¥‘ ¨¥‘ “ ¤ heap ¤‘ µ ¡‘ ¨ ù¤‘ “ø¢ Å ³ ¨ ø À þ‘ Þ ÷

.Àª‘“ üõ O(log|V |) ¥ Ã÷ –‘ƒÜÞä

O(|E|log|V | + |V |log|V |) = ¥ Ý µ þ ¤  Ú ó Û î ö‘ õ¥ ° ƒ — — ß þ ‚ “

.Àª Àû¡ O((|E| + |V |)log|V |)

:fibonacci-heap(amortized) ¥ ù¢‘Ôµ¨ •
Extract-Min ⓑ— ,¢Âƒð üõ ¤ O(|V |) ö‘õ¥ ö‘Þû Build-heap ⓑ— ´ó‘Ÿ ßþ ¤¢

üõ ¤ O(1) ö‘õ¥ Decrease-Key ⓑ— ‘õ, ¢Âƒð üõ ¤ O(log|V |) ö‘õ¥ ö‘Þû Ã÷

.¢Âƒð

.Àª Àû¡ O(|E| + |V |log|V |) ¥ ݵþ ¤ Úó Ûî ö‘õ¥ –¤ ¬ ßþ ‚“

–¤ ¬ ö ¤¢ , |E| = Θ(|V |) ü€ãþ , Àª‘“ §¤‘³¨ éÂð ×þ ÂÑ÷ ¢¤ õ éÂð Âð

ü÷‘õ¥ ²ƒû ø¢ Âû Âþ ¥ ¢Âî Àû¿÷ ‘õ ‚“ ü÷À€ ×Þî fibonacci-heap¥ ù¢‘Ôµ¨


. ´êÂð À€û¡ ¤ O(|V |log|V |) “Â“

|E| = Θ(|V |2 ) ü€ãþ Àª‘“ (dense graph) ÝîÂµõ éÂð ×þ ÂÑ÷ ¢¤ õ éÂð Âð

fibonacci-¥ ù¢‘ Ô µ ¨ ö‘ õ¥ ø O(|V |2 log|V |) “Â “ binary-heap ¥ ù¢‘ Ô µ ¨ ö‘ õ¥ ,

.¢“ Àû¡ O(|V |2 )“Â“ heap


.Àª‘“ |V | ¥ µð¤ Ó |E| ‚î ´¨ µú“ ü÷‘õ¥ fibonacci-heap ¥ ù¢‘Ôµ¨ ßþÂ“‘€“
ü÷‘õ¥ –¤ ø‘¹õ Åþ —‘õ ‚܃¨ø ‚“ ݵþ ¤ Úó ßþ ý¥‘¨ ù¢‘ƒ• ‚î ´¨ üó‘Ÿ ¤¢ ßþ ø

.¢Âƒð üõ O(|V |2 ) ¥
107 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

: Sollin ݵþ ¤Úó

üõ ¤Âì ñ‘þ öøÀ“ ¤ ‘û §b¤ ô‘Þ— Àµ“ , ݃û¢ üõ žÂª Âþ ¥ ñ‘·õ ‘“ ¤ ݵþ ¤ Úó ßþ

µ؝î °Æ “ ÂÑ÷ ¥ ‚î ö ݃þ‘Þ÷ üõ ‚Æþ‘Öõ ‚“ áø ª ÂÔ¬ §¤ ¥ ų¨ ݃€ƒ

4| , {z
3, 2} ø1|{z}
, 6 ,5, 0 ´ ¡¤¢ ‚ ¨ –¤  ¬ ß þ ¤¢ À û¢ ü õ Û ƒ Ø È — ¤ ñ‘ þ × þ ´ ¨
|{z}
C B A
ý‘û ©¥ ¤ ý¤¢ ¢ª üõ Ûʵõ C ‚“ B ´¡¤¢ ¥ ‚î üþ‘û ƒÆõ . ¢ª üõ ۃØȗ

©¥ ¤ ý¤¢ ¢ª üõ Ûʵõ C ø B ‚“ A ´¡¤¢ ‚î üþ‘û ƒÆõ ø ´¨ 24ø18ø16

ø À€î üõ Ûʵõ C ‚“ ¤ B ´¡¤¢ ‚î ´¨ 16 ƒÆõ ßþ µû‘—î ŕ ´¨ 25ø28

.´¨ 25 ƒÆõ ßþ µû‘—î Ã÷ 28ø25 ߃“

28
0 1 0 1
10 14 16 10 14 16

5 6 2 - 5 6 2
24 18
25 12 25 12
4 3 4 3
22 22

algorithm Sollin(G)
begin
Fo =(v,k);
i=0;
while there is more than one tree Fj do
for each tree Tj in forest Fj do
choose the minimum weighted edge (u,v) joining some
vertix u in Tj to a vertix v in some other tree Tk in forest Fj
from the otherforest Fj+i by joining all Tj and Tk of Fj with tree
converesponding select edge
i++;
end;
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 108

: Boruvka ݵþ ¤Úó

Boruvka( G = (V, E) , w ) {
initialize each vertex to be its own component ;
A = {} ; //A holds edges of the MST
do {
for ( each component C ) {
find the lightest edge (u,v) with u in C and v not in C ;
add { u, v } to A ( unless it is already there ) ;}
apply DFS to graph H = (V, A) , to compute the new components
} while (there are 2 or more components) ;
return A ;} // return final MST edges
BORUVKA(V,E):
F=(V,∅)
while F has more than one component
choose leader using DFS
FIND-SAFE-EDGES(V , E)
for each leader v̄ add safe ( v̄ ) to F
FIND-SAFE-EDGES(V, E):
for each leader v̄
safe(v̄)← ∞
for each edge (u,v)∈ E
ū ← leader(u)
v̄ ← leader(v)
if ū 6= v̄
if (w(u,v) < w( safe( ū ))
safe ( ū) ← (u, v)
if (w (u,v) < w(safe(v̄ ))
safe ( v̄) ← (u, v)
109 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

: Knapsack üµÈ• ‚óî ݵþ ¤Úó 3.1.4

ßþ ݃û¡ üõ.À€î ÛÞ½— À÷— üõ ¤ W ö¥ ø ‚î Ýþ ¤¢ üµÈ• ‚óî ‚î Àƒ€î ­Âê

, i DZüª À€÷‘õ ‘û DZüª ¥ ×þ Âû Àƒ€î ­Âê .݃€î• 1, 2, ..., ný‘û DZüª ‘“ ¤ ‚óî
ßþ µȃ“ ‚“ üµÈ• ‚óî ö¢Âî • ýÂ“ ‚î ´Æ÷ éÀû .Àª‘“ vi ©¥ ¤ ø wi ö¥ ø ý¤¢

ø ÀŸø ÂÔ¬ ‘þ ÀŸø ×þ üª Âû ¥ ö— üõ ¤ Ñ€õ ßþ ýÂ“ ݃“‘þ ´¨¢ ßØÞõ ©¥ ¤

:‚¹ƒµ÷ ¤¢ .¢Âî ’‘¿µ÷ ÀŸø ¥ ý ÂÆî ‘þ


P
xi wi ≤ W , xi ∈ [0, 1]
P
. ݃¨Â“ max [ xi vi ] ‚“ ݃û¡ üõ ‘õ øÀ÷¤¢ ֵ݃Æõ ´±Æ÷ ©¥ ¤ ‘“‘û ’‘¿µ÷

vi
üóø Ã÷ ´ƒ“ — ‚“ ¤ ‘ú÷ ų¨. Ýþ ¤ ø üõ ´¨À“ DZ‘ƒª ô‘Þ— ýÂ“ ¤
wi ´±Æ÷ Àµ“
¤Âì ‚óî ¤¢ ø ݃€î üõ ’‘¿µ÷ ¤ DZ‘ƒª üØþ üØþ ´Æƒó ¨ ¥ ßþÂ“‘€“ ݃€î üõ °—Âõ

‘“ ü€ãþ Ýþ ¥À€ƒ“ ‚óî Û¡¢ Ûõ‘î ¤ Ï ‚“ ¤ ý üª ݃÷µ÷ ‚î üŽ‘› ‘—. ݃û¢ üõ

¥ µȃ“ weight ü€ãþ Ýþ ‚µ¿þ ¤ üµÈ• ‚óî Û¡¢ ¤¢ ‚î üŽ‘ƒª ö¥ ø üª ö ’‘¿µ÷

.¢ª W
W −weight
üõ ’‘¿µ÷ x[k] = W [k] ù¥À÷ ‚“ ô k üª ü€ãþ üª ßþ ýÂ“ –¤ ¬ ßþ ¤¢

‘ ú ÷ ©¥ ¤ ø .À ƒ ¨¤ À û ¡ W ‚ “‚ ó î Û ¡¢ –‘ ã Î ì ö¥ ø ü ª ß þ ’‘ ¿ µ ÷ ‘ “ .¢ ª

ý¥‘¨ °—ÂõÎÖê Âþ ¥ ´¨ O(n log n)¥ Ã÷ ݵþ ¤ Úó ßþ ö‘õ¥ .Àª Àû¡ max Ã÷

. Ýþ ¤¢

function Knapsack(w[1..n],v[1..n],W):array[1..n]
for i ←1 to n do
x[i] ←0
weight← 0
{gready loop}
while (weight<W) do
i ← the best remaining object
if(weight+w[i]≤ W )then
x[i]←1
weight←weight+w[i]
else
(W −weight)
x[i]← w[i]
weight ← W
return x
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 110

: DIJKSTRA ݵþ ¤Úó 4.1.4

Ýþ ¤¢ ¤‘ƒµ¡ ¤¢ ¤¢ ´ú› üêÂð Àƒ€î ­Âê .´¨ ü“‘þ ƒÆõ ݵþ ¤ Úó ݵþ ¤ Úó ßþ

ß þ  µ û‘ — î ñ Ï Ý ƒ û ¡ ü õ .´ ¨ ù¢¤  ¡ ° Æ Â “ ü Ô € õ‘ ÷ ý¢À ä ‘ “ ö ñ‘ þ  û ‚ î

¤  Ñ € õ ß þ ýÂ “ . Ý þ ¤ ø ´ ¨¢ ‚ “ § Ž¤  þ‘ ¨ ‘ — ¤ â ± € õ §b¤ ô‘ ÷ ‚ “ ü ¨b¤ ¥  ƒ Æ õ

.Ýþ ƒð üõ ÂÑ÷ ¤¢ ݵþ ¤ Úó ý¢ø ¤ ø ö€ä ‚“¤ éÂð ö¥ ø Åþ —‘õ

function Dijkstra(L[1..n,1..n]):array D[2..n]


C←−{2,..,n} {s=N\C exists only implicity}
for i ←− 2 to n
D[i]←− L[1,i]
repeat n-2 times
v←− some element of C minimizing D[v]
C←− C\{v}
for each w∈ C do
D[w]←− min(D[w], D[v]+L[v,w])
return D

 weight of edge (i,j)∈ E
Lij = 0 i=j

∞ else
: ñ‘·õ

1
10 50
=
5 30 R
100 32

10 20 5
U W
4 -3
50

step v C D
initilization - 2,3,4,5 [50,30,100,10]
1 5 2,3,4 [50,30,20,10]
2 4 2,3 [40,30,20,10]
3 3 2 [35,30,20,10]
111 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

‚µª¢ ¤ ƒÆõ ‚Ø€þ ýÂ“ ŕ .Àû¢ üõ ¤ ñ‘þ Âû ¥ ¤ ±ä ‚€þ Ãû ‘ú€— ݵþ ¤ Úó ßþ

݃û¡ ŕ Ýþ Ãþ “ ö ¤¢ ¤ ý ¤ ±ä ý‘û ùÂ𠑗 ݃ª‘“ ‚µª¢ ÂÚþ¢ ý ‚þ¤ Àþ‘“ ݃ª‘“

: ´ª¢

function Dijkstra(L[1..n,1..n]):array D[2..n] , array P[2..n]


C←−{2,..,n}{s=N\C exists only implicity}
for i ←− 2 to n
D[i]←− L[1,i]
P[i]←0
repeat n-2 times
v←− some element of C minimizing D[v]
C←− C\{v}
for each w∈ C do
if ( D[w]>D[v]+L[v,w] ) then
D[w]←− D[v]+L[v,w]
P[w]←− v
return D , P
: ü÷‘õ¥ ۃܽ—
:‘“ ´¨ “Â“ ‚î ´¨Â› ۟Âõ ¢À㗠¥ ü±þ ® ö‘õ¥

(n − 1)(n − 2)
(n − 2) + (n − 3) + . . . + 2 + 1 = ∈ θ(n2 )
2
:ƒÆõ ö¢Âî Àƒ•

: Ýþ ¤¢ Û±ì ñ‘·õ ýÂ“ .Ýþ ¤ ø üõ ´¨¢ ‚“ P ‚þ¤ ýø ¤ ¥ ¤ ƒÆõ

2 3 4 5
P 3 0 5 0

üõ ֵ݃Æõ §b¤ ö ‚“ 1 §b¤ ¥ ‚î À€µÆû ü€ãõ ßþÀ“ À€µÆû ÂÔ¬ ‚î üþ‘û ‚þ¤¢

4 §b¤ …
f · õ,Â Ô ¬  ƒ è ý‘ û ‚ þ¤¢ ‘ õ. ´ ¨  ƒ Æ õ ß þ  — ù‘ — î ¢ ¡  ƒ Æ õ ß þø Ý þø ¤

ý‚þ¤¢ ‘õ ø Ýþø ¤ 5 ‚“ Àµ“ Àþ‘“ 4 ‚“ ßµê¤ ýÂ“ ŕ ´¨ 5 ýø‘Ÿ 4 ý‚þ¤¢ ö,

. Ýþø ¤ üõ 4 ‚“ ų¨ ø 5 ‚“ 1 ¥ Àµ“4 ‚“ ßµê¤ ýÂ“ ŕ ´¨ ÂÔ¬ ýø‘Ÿ ¢¡ 5

‘ “ø O(a+nlogn) ¥ Fibonacci-heap ý¥‘ ¨ ù¢‘ ƒ •‘ “ Ý µ þ ¤  Ú ó ß þ ýÂ › ö‘ õ¥

.´¨ O((a+n)logn) ¥Binary-heap


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 112

(timetable or scheduling) ýÀ€“ ö‘õ¥ ý‘û ݵþ ¤Úó 5.1.4

–¤‘±ä ö‘õ¥ ý¥‘¨ ‚€ƒú“ ‘þ ø Ûõ‘ä ݵƃ¨ ö ü·Ÿ‘±õ ¤¢ Ýúõ ¤‘ƒÆ“ ¶Ÿ‘±õ ¥ üØþ

´¨ ¤‘î À€ ߃“ server ×þ ö‘õ¥ ݃Æ֗ ôúÔõ ‚“ ýÀ€“ ö‘õ¥. ýÀ€“ ö‘õ¥ ¥ ´¨

ÂÑ÷ ¢¤ õ éÀû ‚“ ݃÷µ“ À÷ƒð üõ Åþø ¨ ö‘þ µÈõ ô‘Þ— ‚Ø÷ ßƒä ¤¢ ‚î ý ¤ Ï ‚“

ùƒª ×þ ‚“ ´¨ Ó¬ ¤¢ ö‘þ µÈõ ö‘õ¥ ö¢Âî minimum éÀû Âð. ݃“‘þ ´¨¢ Ã÷

. ݃€î ÛÞä ÂÚþ¢ ùƒª ‚“ ´¨ ¥‘ƒµõ ‘þ ¢¨ ßþ µȃ“ ö¢¤ ø ´¨À“ éÀû Âð ø

À€ª‘“ üõ ´¿¨ ێ‘Æõ ¥ ‚µ¨¢ ×þ ýÀ€“ ö‘õ¥ ێ‘Æõ üÞµþ ¤ Úó ¶½“ Àþ¢ ¥

.݃“‘ƒ“ ‚ÜÿÆõ á÷ ‘“ °¨‘€õ ü“› ö ÍþÂª ‚“ ‚›— ‘“ ‚î

üõ ݃Æ֗ (with deadline) ¤¢ ´Üúõ ø (simple) ù¢‘¨ ‚µ¨¢ ø¢ ‚“ ýÀ€“ ö‘õ¥

.¢ª

ù¢‘¨ ýÀ€“ ö‘õ¥

n ñ‘·õ ýÂ“ ý µÈõ üÊ¿Èõ ¢À㗠‚“ Àû¡ üõ ‚î Ýþ ¤¢ server ×þ Àƒþ‘Þ÷ ­Âê

“ ­Âê. Àª‘“ üõ É¿Èõ Û±ì ¥ ý µÈõ Âû Åþø ¨ ö‘õ¥ .Àû¢ Åþø ¨ ý µÈõ

Âû ¤¢ ø ¢¤¢ ¥‘ƒ÷ ¤ ti (1 ≤ i ≤ n) ù¥À÷ ‚“ üÆþø ¨ ö‘õ¥ ôi ý µÈõ ‚î ´¨ ßþ

ü±ƒ—— ‚ ‚“ ݃€ƒ±“ ݃û¡ üõ. Àû¢ Åþø ¨ ý µÈõ ×þ ‚“ À÷— üõ server ö‘õ¥
Â Ñ ÷ ‚ Î Ö ÷ ¥ Ý µ Æ ƒ ¨ ¤¢ ùÀ ª ý  ³ ¨ ö‘ õ¥ ‘ — Ý ƒ û¢ Å þø  ¨ ö‘ þ  µ È õ ß þ ‚ “ ö — ü õ

ö‘õ¥ ͨµõ ö‘þ µÈõ ù‘Ú÷ ¥ ‚Ø€þ ü€ãþ . ¢¢Â𠂀ƒÞî Åþø ¨ ´ê‘þ ¤¢ ýÂ“ ö‘þ µÈõ

.Àª‘“ Ýރ÷ üõ ‘û ö ý ùÀª Óܗ

Ti = t1 + t2 + ... + ti−1 + ti : ôi ý µÈõ ¤‘ѵ÷ ö‘õ¥


P Ti
T̄ = n : Åþø ¨ ´ê‘þ ¤¢ ýÂ“ ¤‘ѵ÷ ö‘õ¥ ͨµõ
P
. ¢ª ‚€ƒÞî T ´¨ üê‘î ø ô¥„ T̄ öÀª ‚€ƒÞî ýÂ“
: Ýþ ¥¢Â• üõ Âþ ¥ ñ‘·õ ‚“ Ýþ ¤ ø ´¨À“ ݃û¡ üõ ‚î üÞµþ ¤ Úó µú“ í¤¢ ýÂ“

, 5 ñø ý µÈõ ýÂ“ ö‘õ¥. Àû¢ üõ Åþø ¨ ý µÈõ 3 ‚“ server ßþ Àƒþ‘Þ÷ ­Âê
.À÷ƒړ Åþø ¨ À€÷— üõ ´ó‘Ÿ 6 ‚“ ý µÈõ 3 ßþ ‚î ´¨ 3 ô¨ ø 10 ôø¢

1 , 2, 3 =⇒ T = 5 + (5 + 10) + (5 + 10 + 3)
1, 3, 2 =⇒ T = 5 + (5 + 3) + (5 + 3 + 10)
2, 1, 3 =⇒ T = 10 + (10 + 5) + (10 + 5 + 3)
2, 3, 1 =⇒ T = 10 + (10 + 3) + (10 + 3 + 5)

3, 1, 2 =⇒ T = 3 + (3 + 5) + (3 + 5 + 10)
3, 2, 1 =⇒ T = 3 + (3 + 10) + (3 + 10 + 5)
113 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

ý¢ã¬ °ƒ—— ‚“ Åþø ¨ ö‘õ¥ °ÆŸ “ ¤ ö‘þ µÈõ Åþø ¨ ýÂ“ ݵƃ¨ ßþ ¤¢

¤ À÷ø ¤ ßþ ø ݃û¢ üõ Åþø ¨ ‘ú÷ ‚“ ´Æƒó ýÀµ“ ¥ °ƒ—— ‚“ ø ݃þ‘Þ÷ üõ °—Âõ

ý¥‘¨ °—Âõ ö‘õ¥ ‘ú€— ö .À÷ƒړ ¤ ö‘ÈÆþø ¨ ‘û¤‘î ô‘Þ— ‘— ݃û¢ üõ ‚õ¢ ¤ÀÖ÷

. ´¨ O(n log n) ¥ Ýþ ¤¢ ¤

(Scheduling with deedline)¤¢ ´Üúõ ýÀ€“ ö‘õ¥

ø À÷¤¢ ¥‘ƒ÷ Â› ýÂ“ ö‘õ¥ ÀŸø ×þ ‚“ ×þ Âû ‚î Ýþ ¤¢ üþÂ› ¤‘î n Àƒþ‘Þ÷ ­Âê

ù¥À÷ ‚“ ý¢¨ ô i ¤‘î . ¢ª ô‘¹÷ À÷— üõ ¤‘î ×þ ÍÖê (T=1,2,...)ö‘õ¥ Âû ¤¢


Àª‘“ ö ´Üúõ ‚î di ýø‘Æõ µÞî ö‘õ¥ ¤¢ ¤ ¤‘î ßþ Â𐠑ú€—ø Âð À÷‘¨¤ üõ gi ≥ 0
Åþø ¨ ö‘þ µÈõ ßþ ‚“ Àþ‘“ ý½÷ ‚ ‚“ ݃÷À“ ݃û¡ üõ .Àª‘“ ùÀƒ¨¤ ô‘Þ— ‚“

. ݃“‘þ ü¨Âµ¨¢ ¢¨ ßþ µȃ“ ‚“ ‘— ¢¢

: ñ‘·õ

žÂª ‚“ ‘û ö deedlineø ¢¨ öÃƒõ °ƒ—— ‚“ ‚î Ýþ ¤¢ ý µÈõ ¤‘ú ‚î Àƒ€î ­Âê

: ´¨ Âþ ¥

i 1 2 3 4
gi 50 10 15 30
di 2 1 2 1
sequence profit sequence profit
1 50 2,1 60
2 10 2,3 25
3 15 3,1 65
4 30 4,1 80−→ optimum
1,3 65 4,3 45

fisible ‘þ ü÷Àª ‚ó‘±÷¢

‚äÞ¹õ Âû ßþÂ“‘€“ . ¢Âî Â› ¤ ‘ú÷ °ƒ—— ‚“ ݃÷µ“ Âð ݃Žð fisible ¤ ý ‚ó‘±÷¢

‚ó‘±÷¢ ‘õ ´¨ fisible , 2,1‚äÞ¹õ ñ‘·õ ýÂ“ .´¨ fisible ‚ó‘±÷¢ ×þ ý¤¢ fisible
. Àª‘“ üÞ÷ fisible , 1,2
° Ü Î õ ´ ƒ Ü î ¥ ‚ î ö öøÀ “ ø À ª‘ “ Û ç ª k ¥ ý ‚ ä Þ ¹ õ j À ƒ þ‘ Þ ÷ ­Â ê

–¤  ¬ ‚ “ ø À ÷ ù¢¤  ¡ ° Æ Â “ 1, 2, ..., k ‘ “ ‘ û¤‘ î ‚ î À ƒ € î ­Â ê ¢ ª Ý î


, 1, ..., k  𐠑 ú € —ø  𐠴 ¨ fisible , j –¤  ¬ ß þ ¤¢. À € ª‘ “ d1 ≤ d2 ≤ ... ≤ dk
. Àª‘“ fisible
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 114

°ƒ—— ‚“ À€÷‘¨¤ üõ ‚î ý¢¨ §‘¨Â“ ¤ ‘û Ûçª Àµ“ ¢¨ ßþ µȃ“ ‚“ öÀƒ¨¤ ýÂ“

¢¨ ßþ µȃ“ ‚“ ‘û ö deedline ßµ¡À÷ Õþã— ‚“ ‘“ ų¨ ø ݃€î üõ °—Âõ üóø Ã÷

. ݃“‘þ üõ ´¨¢

Function sequence(d[0..n]):k , array[1..k]


array j[0..n]
{The schedule is constructed step by step in the array j ,the variable k
say how many jobs are already in the schedule}
d[0]←j[0]←0 {sentinels}
k← j[1]←1{job 1 is always choosen}
{gready loop}
for i ← 2 to n do {decreasing order of g}
r←k
while d[j[r]]>MAX(d[i],r) do r ← r-1
if d[i]> r then
for m ← k step -1 to r+1 do j[m+1]←j[m]
j[r+1]←i
k ← k+1
return k,j[1..k]
: Ýþ ¤¢ ñ‘·õ ýÂ“

i 1 2 3 4 5 6
gi 20 15 10 7 5 3
di 3 1 1 3 1 3

3
Initialization: 1

1 3
Try 2: 2 1

Try 3: unchanged
1 3 3
115 (GREEDY ALGORITHMS) ‚÷‘Êþ Ÿ ý‘û ݵþ ¤Úó .1.4

Try 4: 2 1 4

Try 5: unchanged
Try 6: unchanged
=⇒ optimal sequence: 2 , 1 , 4 value=42
. ´¨ O(n2 )¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥

: Disjoin Set ¥ ù¢‘Ôµ¨ ‘“ ýÀ€“ ö‘õ¥ ôø¢ ݵþ ¤ Úó

üõ ô‘¹÷ ¤ ýÀ€“ ö‘õ¥ ‚ó‘b Æõ Ã¹õ ‚äÞ¹õ ù¢¢ ö‘Þµ¡‘¨ §‘¨ “ ݵþ ¤ Úó ßþ

f ±ì ‚î ¤ Ï ö‘Þû, Àû¢
merge ⓑ— ø À“‘þ üõ ¤ ´¡¤¢ ý‚Èþ ¤ find ⓑ— Àª ùÀû‘Èõ …
´¨ µÞî ©ÀƒÜî ‚î ö ø À€î üõ °ƒî— Ýû ‘“¤ É¿Èõ ý‘û ‚Èþ ¤ ‘“ ´¡¤¢ ø¢

.Àª Àû¡ ÂÚþ¢ ý‚Èþ ¤ À÷¥ Âê

Function sequence2(d[1..n]):k,array[1..k]
array j ,F[0..n]
{initialization}
for i ← 0 to n do
j[i] ← 0
F[i]← i
initialize set [i]
{gready loop}
for i ← 1 to n do {decreasing order of g}
k ← find(min(n,d[i]))
m ← F[k]
if m 6= 0 then
j[m] ←i , l← find(m-1)
F[k]←F[l]
merge(k,l)
k←0
for i ←1 to n do
if j[i]>0 then k←k+1 , j[k]←j[i]
return k,j[1..k]
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 116

. ´¨ ‚Èþ ¤ ö ‚î ¢ª üõ É¿Èõ Àª ©¢¡ ý ÂÊ€ä ¤À• Âð

: Ýþ ¤¢ Û±ì ñ‘·õ ýÂ“

Initilize : L=min(6, max(d,1))= 3

F= 0 1 2 3 4 5 6
0 1 2 3 4 5 6

Try 1: d1 =3 , assign task 1 to position 3

F= 0 1 2
0 1 2
M

Try 2: d2 =1 assign task 2 to position 1

0 2
F=
0 2
K K
1 3

Try 3: d3 =1 no free position available since the F value is 0


Try 4: d4 =3 assign task 4 to position 2

F= 0
0
 o
1 2
]
3

Try 5: d5 =1 no free position available


Try 6: d6 =3 no free position available
=⇒ optimal sequence: 2 , 1 , 4 value=42
. ´¨ O(nlogn)¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥
117 (DEVIDE AND CONQUER) ۟ø ݃Æ֗ .2.4

(devide and conquer) ۟ø ݃Æ֗ 2.4


¤‘µ¡‘¨ ×þ ¥ ù¢‘Ôµ¨ ‘“ üܬ ‚ó‘b Æõ ö ¤¢ ‚î ´¨ üÆþ÷ ‚õ‘÷“ ׃€Ø— D&C
ßþ ‘“ À€µÆû üܬ ‚ó‘b Æõ ‚ƒ±ª ‘f Öƒì¢ ‚ª üõ ݃Æ֗ üþ‘û‚ó‘b Æõ Âþ ¥ ‚“ üµÈ𥑓

‚ó‘b Æõ Âþ ¥ ۟ ‘“ .À€µÆû üܬ ‚ó‘b Æõ ¥ — ù¢‘¨ ‘þ µ؝î ù¥À÷ ÂÑ÷¥ ‚î –ø‘Ô—

¤¢ ’› ×þ ‚“ ö— üõ Ýû ‘“ ‘ú÷ °ƒî—ø ùÀª ù¢‘¨ ø םî üê‘î ù¥À÷ ‚“ ý‘û

.´ê‘þ ´¨¢ üܬ ‚ó‘bÆõ ýÂ“ ö‘Øõ –¤ ¬

:´¨ Âþ ¥ Փ‘Îõ D& Cݵþ ¤ Úó ×þ üÜî ¤‘µ¡‘¨

function DC(x)

if x is sufficiently small or simple then return adhoc(x);

decompose x into smaller instances x1 , x2 , ..., xl ;

for i←1 to l do yi ←DC(xi );

recombine the yi ’s to obtain a solution y for x;

return y;

ï¤ Ã“ ¢Àä ’® 1.2.4

¤  Ï ‚ “ Ý ƒ þ‘ Þ ÷ ’ ® Ý û ¤¢ ´ ¨ ü Þ ì¤ n  µ ð¤ à “ ¢À ä ‚ î ¤ ¢À ä ø¢ Ý ƒ û ¿ “  ð

?Àµê üõ üì‘Ô— ‚, ݃€î ÛÞä Âþ ¥ –¤ ¬ ‚“ Âð ñ‘Ÿ .Àª‘“ üõ O(n2 ) ¥ ñÞãõ

981 × 1234 = (|{z}


9 ×102 + |{z}
81 ) × (|{z}
12 ×102 + |{z}
34 ) =
w x y z
(w × 102 + x)(y × 102 + z) = wy × 104 + (wz + xy) × 102 + xz
n
: Ýþ ¤¢ ŕ. À÷ª âޛ Ýû ‘“ Àþ‘“ ¢Àä ¤‘ú Ýû ¥‘“
2
T (n) ≤ 4T ( 2 ) + θ(n) ⇒ T (n) ∈ O(n2 )
n

2
µú“ ¤ ö‘õ¥ ‚î ´¨ O(n ) ¥Ãƒ÷ ݵþ ¤ Úó ßþ Àƒþ‘õÂê üõ ‚ÑŸ…õ ‚î ¤ Î÷‘Þû

.Àª‘“ üÞ÷ µú“ ‚ȃÞû D&C ‚¹ƒµ÷ ¤¢ À€î üÞ÷

(wz + xy) = (w + x)(y + z) − wy − |{z}


xz = r − p − q
| {z } |{z}
r p q

⇒ wy × 104 + (wz + xy) × 102 + xz = p × 104 + (r − p − q) × 102 + q


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 118

3
⇒ T (n) ≤ 3T ( n2 ) + θ(n) =⇒ T (n) ∈ O(nlog2 )

n n n n
¤¢ ´ ¨
2 ¥ ü “ Â Ì õ Å • Ý þ ¤¢ â Þ ›
2 +1‘ þ 2 ü Þ ì¤
2 ¢À ä ø¢ â Þ › ýÂ “

.´¨ θ(n) ¥ ‚¹ƒµ÷

: ݵþ ¤ Úó

large-integer prod(large-integer u , large-integer v)


{
large-integer x,y,w,z,r,p,q
int m , n
n=Maximum(number of digits in u , number of digits in v )
if(u==0 || v==0)
return 0
else if(n≤ threshold )
return u×v obtained in usual way
else
{
m=b n2 c
x=u divide 10m ;
y= u rem 10m
w=v divide 10m ;
z= v rem 10m
r=prod(x+y,w+z)
p=prod(x,w)
q=prod(y,z)
return p×102m + (r-p-q)×10m+q
}
}
119 (DEVIDE AND CONQUER) ۟ø ݃Æ֗ .2.4

merge sort ݵþ ¤Úó 2.2.4

procedure merge-sort (T[1..n])


if n is sufficiently small then insertion sort(T[1..n])
else{
//arrayu[1 . . . b n2 c + 1], array v[1 . . . d n2 e + 1]
u[1 . . . b n2 c] ← T [1 . . . b n2 c]
v[1 . . . d n2 e] ← T [1 + b n2 c . . . n]
merge − sort(u[1 . . . b n2 c])
merge − sort(v[1 . . . d n2 e])
merge(u,v,T)}
insertion sort¥ Å • ´ ¨ ¢‘ þ ¥ ö¢ “ ° — õ ñ‘ Þ µ Ÿ À ª‘ “ ×  î ‚ î ü µ ó‘ Ÿ ¤¢

.¢Èƒõ ù¢‘Ôµ¨

procedure merge(u[1..m+1],v[1..n+1],T[1..m+n]){
i,j← 1
u[m+1]=v[n+1] ← ∞
for k← 1 to m+n Do
if u[i]<v[j]

T[k]←u[i]

i++

else

T[k]←v[j]

j++}
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 120

: ñ‘·õ

: ݃þ‘Þ÷ üõ °—Âõ ©ø ¤ ßþ ‘“ ¤ Âþ ¥ ý‚þ¤

1 2 3 4 5 6 7 8
9 10 11
31 3 10 54 7 17 12 43 48 8 2

31 3 10 54 7 17 12 43 48 8 2

31 3 10 54 7 17 12 43 48 8 2

3 31 10 7 54 17 12 43 48 2 8

3 31 10 ∞ 7 54 ∞ 17 ∞ 1243 ∞ 48 ∞ 2 8 ∞

3 31 ∞ 7 10 54 ∞ 12 17 43 ∞ 2 8 48 ∞

3 7 10 31 54 ∞ 2 8 12 17 43 48 ∞

2 3 7 8 10 12 17 31 43 48 54

: ݵþ ¤ Úó Ãó‘÷

T (n) = T (d n2 e) + T (b n2 c) + θ(n)

T (n) = 2T ( n2 ) + θ(n)

=⇒ T (n) ∈ O(nlogn)

–¤‘ ± ä‚ “ ¤ if u[i] < v[j] –¤‘ ± ä  𐠑 õ À ª‘ “ ü Þ ÷ stable Ý µ þ ¤  Ú ó ß þ

üÞ÷ inplace ݵþ ¤ Úó ßþ ߃€»Þû .¢“ Àû¡ stable ݃€î ÛþÀ±— if u[i] ≤ v[j]
.Àþ‘Þ÷ üõ ù¢‘Ôµ¨ üØÞî ý‚Ñꑟ ¥ Âþ ¥ Àª‘“
121 (DEVIDE AND CONQUER) ۟ø ݃Æ֗ .2.4

Quick Sort ݵþ ¤Úó 3.2.4

Procedure pivot(T[1. . .n] , var L)


{permutes the elements in array T[i . . .j] and return a value L such that
at the end,i ≤L ≤ j, T [k] < p for all i ≤k < L, T[L]=p and
T[k] > p for all L<k≤j , where p is the initial value of T[i] }
p ←T[i]
k←i , L←j+1
repeat k←k+1 until T[k] > p or k ≥ j
repeat L←L-1 until T[L] ≤ p
while(k < L) do
swap(T[L],T[k])
repeat k ←k+1 until T[k] > p
repeat L ←L-1 until T[L] ≤ p
swap(T[i],T[L])

procedure quicksort (T[i. . .j])


{sorts subarray T[i. . .j] into nondecreasing order}
if (j - i) is sufficiently small then sort with insertion-sort(T)
else
pivot(T[i . . . j],L)
quicksort(T[i . . . L -1])
quicksort(T[L+1 . . . j])

: ñ‘·õ

i j
5 1 17 4 48 7 3 9 8 5 25
1 2 3 4 5 6 7 8 9 10 11
p=T[i]=5
k←i
L ← j+1
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 122

5 1 17 4 48 7 3 9 8 5 25
↑ ↑
k L
5 1 5 4 48 7 3 9 8 17 25
↑ ↑
k L
5 1 5 4 3 7 48 9 8 17 25
↑ ↑
L k
.¢ª üõ ­ä p ‘“ ô LÂʀä ý‘› ŕ¢‘µêÂ—Ü› L¥ k

3 1 5 4 5 7 48 9 8 17 25

| {z } ⇑ | {z }

:ݵþ ¤ Úó ýÂ› ö‘õ¥ ‚±¨‘½õ

: ´ó ‘Ÿ ßþ —À“ ¤¢Ãƒó ‘÷ •


ŕ ¢Âî ´îŸ ‚þ¤ ¡ ý ÂÚþ¢ ø ñø üØþ éÂÏ ø¢ ¥ Àþ‘“ pivot ¤¢ ‚÷¥

. ´¨ θ(n) ¥ ‚ȃÞû


 T(n)=T(n-1)+θ(n)
=⇒ T(n)=θ(n2 )

T(n)=c n≤ n0
‚÷‘¡ ¤¢ ‘þ ø ¢¡ ý‘›¤¢ ‘þ ¤‘“ Âû ¤¢ pivot ‚î Àµê üõ ë‘Ô— ü÷‘õ¥ ´ó‘Ÿ ßþ —À“

‘ û ù¢¢ ‘ þ ‚ Ø ÷ ü € ã þ ¢Â ƒ 𠤐 ì , ´ Æ ÷ ü Ü ã ê Û ½ õ i ‚î (n-i+1) ü € ã þ ö ¤‘ Ö µ õ

.À€ª ‘“ ùÀª °—Âõ üóø Ã÷ ‘þ ø À÷ ý¢ã¬

: ´ó ‘Ÿ ßþ µú“ ¤¢ ݵþ ¤ Úó Ãó ‘÷ •


Àª‘“ ü÷‘ƒõ ¯‘Ö÷ ¤¢ pivot ‚î Àµê üõ ë‘Ô— üõ‘Ú€û ´ó‘Ÿ ßþ µú“ ݵþ ¤ Úó ßþ¤¢

:‘“ ´¨ “Â“ ݵþ ¤ Úó ýÂ› ö‘õ¥ –¤ ¬ ßþ ¤¢ ,

T(n)' 2T ( n2 ) + θ(n) ⇒ T (n) = θ(n log n)


123 (DEVIDE AND CONQUER) ۟ø ݃Æ֗ .2.4

: ñ‘·õ

×þ ‘“ ¤ ‚þ¤ ñø Âʀä 2 n+1 , quick sort ݵþ ¤ Úó ¥ ýÀþÀ› ý ‚÷ð ¤¢

‚“ ¤ ‚÷‘ƒõ Âʀä ų¨ , ݃þ‘Þ÷ üõ °—Âõ insertion sort À€÷‘õ ü—‘õÀÖõ ݵþ ¤ Úó
‚ ¥ ݵþ ¤ Úó ßþ ýÂ› ö‘õ¥ ´ó ‘Ÿ ßþ — À“ ¤¢ , Ýþ ƒð üõ ÂÑ÷ ¤¢ pivot ö€ä
?Àþ  üõ ´¨¢ ‚“ üµÈ𥑓 ‚Ф

:۟

=
2 n+1+1
=

n+1
‚÷‘ƒõ Âʀä
2

‚ µ ¨¢ ø¢ Å • À ÷‘ Þ “ ü ì‘ “ ¢ ¡ ý‘ › ¤¢ Â Ê € ä ß þ ‚ î ´ Æ ÷ ö ‘ õ¥ ß þ  —À “ Å •
√ √
.À㓠‚“ n+2 ¥ ôø¢ ‚µ¨¢ ø ¢Àä n ‚µ¨¢ ×þ , Ýþ ¤¢ Âʀä

√ 2
´ ¨ O((2 n + 1) ) ∈ O(n) Ý û insertion ý¥‘ ¨ ° — õ ýÂ “ ô¥„ ö‘ õ¥


T(n-( n+2)) θ(n)
√ z }| √ { z }| {
T(n)≤ T ( n) + T (n − n) + θ(n) + O(n)

: ͨµõ ´ó‘Ÿ ¤¢ ݵþ ¤ Úó Ãó ‘÷ •


k−1 n−k
z }| { z }| {
1 . . . k (k + 1) . . . n

T (n) = T (k − 1) + T (n − (k + 1) + 1) + θ(n) =

1 P (T (k − P P
n n n
n 1) + T (n − k)) + θ(n) = 1n T (k − 1) + n1 T (n −
k=1 k=1 k=1

1 P1
n−
1 P1
n−
0 2 P1
n−
k) + θ(n) = n T (k) + n T (k ) + θ(n) = n T (k) + θ(n) =
k=0 k=0 k=0
n−1 n−1
2 2 P T (k) + θ(n) = 2 × a + 2 P T (k) + θ(n)
n (T (0) + T (1)) + n n n
k=2 k=2
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 124

θ(n lg n) Í ¨ µ õ ´ ó‘ Ÿ ¤¢ ö‘ õ¥ ‚ î Ý ƒ û¢ ü õ ö‘ È ÷ ü ®‘ þ ¤ ýÂ Ö µ ¨ ‘ “ ñ‘ Ÿ

ü êÂ Ï ¥ .´ ¨ 1 Ý µ þ ¤  Ú ó ö‘ õ¥ À ª‘ “ n=2 𐠂 î ´ ¨ ¼ ®ø .´ ¨

üÂ“ Àƒ€î ­Âê ñ‘Ÿ. ´¨ ¤Âì“ ñø ô‘ð ŕ, ´¨ θ(n lg n) = θ(2 lg 2) = 2
ù‘Ú÷, ´¨ θ(k lg k) ô¥„ ö‘õ¥ k < nÂû

X1
2 × a + 2 n− X1
2 a + 2 n−
T (n) = T (k) + θ(n) = θ(klogk) + θ(n)
n n n n
k=2 k=2

X1
2 × a + θ(( 2 ) n−
→ T (n) = (klogk)) + θ(n)
n n
k=2
Z n−1
=
2 × a + θ( 2 xlogxdx) + θ(n)
n n 2
→ T (n) = θ(nlogn)

:ßþ Âޗ
´¨¢ ‚“ ¤ Âʀä ßþ µ؝î ߃õ k ‚î Àƒû¢ ƒƒç— ý ¤ Ï ¤ quick sort ݵþ ¤ Úó
. Ýþ ¤ ø

:ßþ Âޗ
.Àþ ¤ ø ´¨¢ ‚“ quick sort ¤¢ ƒƒç— ×þ ¥ ù¢‘Ôµ¨ ‘“ ¤ ‘û ‚÷‘ƒõ ö¢Âî Àƒ•

:Ýþ ¤ ø ´¨À“ Âþ ¥ ùƒª ‚“ ¤ ¢Àä ßþ µ؝î ߃õ k ݃û¡ üõ ñ‘Ÿ

‚µ¨¢ ×þ ·îÀŸ . ݃€î üõ ݃Æ֗ üþ‘— 5 ý‘û‚µ¨¢ (bn/5c) ‚“ ¤ Âʀä n Àµ“


‚ î Ý þ ¤¢ ùø  ð (dn/5e) ß þÂ “‘ € “ À ª‘ “ ‚ µ ª¢ Â Ê € ä 5 ¥  µ Þ î ý¢À 㠗 ´ ¨ ß Ø Þ õ

.´¨ Âʀä n mod 5 ý¤¢ ‘û ‚µ¨¢ ¥ üØþ ·îÀŸ

Ý µ þ ¤  Ú ó ¥ ù¢‘ Ô µ ¨ ‘ “ ¤ ý Ì ä 5 ý‘ û ‚ µ ¨¢ ¥ × þ  û ý‚ ÷‘ ƒ õ Â Ê € ä Å ³ ¨

šø ¥ ‚ µ ¨¢ × þ  ¬‘ € ä ¢À 㠗  𐠴 ¨ ¼ ®ø . Ý ƒ þ‘ Þ ÷ ü õ ‚ ± ¨‘ ½ õ insertion sort


üµÈ𥑓 ⓑ— ×þ ¥ ù¢‘Ôµ¨ ‘“ . ݃€î üõ ­Âê µ؝î ÂÊ€ä ¤ ‚÷‘ƒõ Âʀä Àª‘“

‚㨁— ‚¿Æ÷ ×þ ¥ ù¢‘Ôµ¨ ‘“ ,Ýþ ¤ ø üõ ´¨À“ ¤ ¬‘€ä ý‘û ‚÷‘ƒõ ‚÷‘ƒõ select ô‘€“
i ‚î Àƒ€î ­Âê ßþÂ“‘€“ . ݃€î üõ ݃Æ֗ x ü€ãþ ‘û ‚÷‘ƒõ ‚÷‘ƒõ ñŸ ¤ pivot‚µê‘þ
ý„‘“ Ç¿“ ¤¢ ¬‘€ä ¢À㗠n−i ŕ Àª‘“ ݃Æ֗ ‚ÎÖ÷ ߃þ‘• Ç¿“ ¬‘€ä ¢Àã—

ýÂ“ üµÈ𥑓 –¤ ¬ ‚“ select ¥ Àª‘“ i ýø‘Æõ µ؝î k Âð . Àª‘“ ݃Æ֗ ‚ÎÖ÷
125 (DEVIDE AND CONQUER) ۟ø ݃Æ֗ .2.4

k − i –¤ ¬ ßþ Âƒè ¤¢ø ݃€î üõ ù¢‘Ôµ¨ ü€ƒþ‘• Ç¿“ ¤¢ ‚€ƒÞî Âʀä ߃õ k ßµê‘þ

.݃þ‘Þ÷ üõ Àƒ• µð¤ Ó Ç¿“ ¤¢ ¤ ‚€ƒÞî Âʀä ߃õ

• • • • •
• • • • • •
• • x• ◦ ◦ ◦
• • ◦ ◦ ◦ ◦
• • ◦ ◦ ◦

x 3( 12 d n5 e − 2) ≥ 310n − 6
¥ µð¤ Ó ¬‘€ä ¢À㗠ÛìÀŸ:

x ¥ µ؝î ¬‘€ä ¢À㗠·îÀŸ: n − ( 310n − 6) = 710n + 6

üþ‘—5 ý‘û‚µ¨¢ ý¥‘¨ °—Âõ ýÂ“ ‚î ¢ª Âî£ Àþ‘“ ݵþ ¤ Úó ö‘õ¥ ü¨¤ “ ýÂ“

ŕ Ýþ ¤¢ ‚µ¨¢ dn/5e ö ø ´¨ O(1) ¥ ‚î ¢ª üõ ù¢‘Ôµ¨ insertion sort ¥
¤¢ .´¨ T (dn/5e) ŕ Àþ ´¨À“‘û‚÷‘ƒõ ‚÷‘ƒõ Àþ‘“ ÂÚþ¢ üêÂÏ ¥ø ´¨ O(n) ¥

3n − 6 ‘þ
7n + 6 ý‘û‚µ¨¢ ¥ üØþ ¤¢ Âʀä ߃õ k ‚î ´¨ ßØÞõ ´ó‘Ÿ ßþ —À“
10 10
: Ýþ ¤¢ Ûî ¤¢ ݃€î üõ ‚±¨‘½õ ¤ µð¤ Ó ‚µ¨¢ ö‘õ¥ Àû¢  ¤

T (n) = T (dn/5e) + T (7n/10 + 6) + O(n)


T (n) ∈ O(n)

ý ¬‘€ä ¢À㗠·îÀŸ 2(n/5) − 1 Ýþ ¤¢ ÂÚþ¢ ö‘“ ¥ ‚“ ùÀª ‚Ž¤ üÞµþ ¤ Úó ¤¢
.À€ª‘“ ‘û ‚÷‘ƒõ ‚÷‘ƒõ éÂÏ ø¢ ¤¢ À€÷— üõ ‚î ´¨

:À€ª‘“ ‘û ‚÷‘ƒõ ‚÷‘ƒõ éÂÏ ×þ ¤¢ À€÷— üõ ‚î ý ¬‘€ä ¢À㗠·îÀŸ

1 ((n − 1) − 2( n − 1)) + 2( n − 1) = 7n − 3
2 5 5 10 2

=⇒ T (n) ≤ T (d n5 e) + T ( 710n − 32 ) + O(n)


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 126

(‘û Åþ —‘õ ’® ݵþ ¤Úó) ߨÂµ¨ ݵþ ¤Úó 4.2.4

ü ªø ¤ ‘ “ Ý ƒ û ¡ ü õ , 2 ¥ ü ÷ — ‚
b ± — õ ¥ Ý þ ¤¢ ¤‘ ƒ µ ¡ ¤¢ Å þ  —‘ õ ø¢ À ƒ € î ­Â ê

ö‘È÷ ߨÂµ¨ .݃€î ‚±¨‘½õ ¤ ‘û Åþ —‘õ ’® Û¬‘ŸüóÞãõ ’® ¥ — âþ ¨

¥‘ƒ÷ θ(n3 ) bù¥À÷ ‚“ ü÷‘õ¥ ‚“ ý¢‘ä ´ó‘Ÿ ¤¢ ‚î ¤ ‘û Åþ —‘õ ’® ö— üõ ‚î ¢¢
7
.¢¢ Çû‘î θ(nlog2 ) ‘“ ñ¢‘ãõ ü÷‘õ¥ ‚“ , ¢¤¢

Åþ —‘õ ø¢ ’® ´Æ÷— ‘û Åþ —‘õ ö¢Âî ‚îÜ“ ‘“ ߨÂµ¨ ¤ Ñ€õ ßþ ýÂ“

.Àû¢ Çû‘î ’Â® 7 ‚“ ¢¤¢ ¥‘ƒ÷ ’® 8 ‚“ ý¢‘ä ´ó‘Ÿ ¤¢ ‚î ¤ 2×2


:Âþ ¥ ñ‘·õ À€÷‘õ
 
c11 c12 c13 c14  
 c21 c22 c23 c24  c011 c012
C=
 c31
=
c32 c33 c34  c021 c022
c41 c42 c43 c44

: ݃þ‘Þ÷ ’® Ýû ¤¢ ¤ Bø A, n × n Åþ —‘õ ø¢ ݃û¡ üõ ñ‘Ÿ

 n×n
2 2
  
A= a%
11 a12 B=
b11 b12
,C = A ×B
a21 a22 b21 b22 n×n
n×n

m1 = (a21 + a22 − a11 )(b22 − b12 + b11 )


m2 = a11 b11
m3 = a12 b21
m4 = (a11 − a21 )(b22 − b12 )
m5 = (a21 + a22 )(b12 − b11 )
m6 = (a12 − a21 + a11 − a22 )b22
m7 = a22 (b11 + b22 − b12 − b21 )
:Àþ üõ ´¨¢ ‚“ Âþ ¥ –¤ ¬ ‚“ C Åþ —‘õ
 
m2 + m3 m1 + m2 + m5 + m6
m1 + m2 + m4 − m7 m1 + m2 + m4 + m5

n n
. Ý þ ¤¢ ¥‘ ƒ ÷
2×2 ý‘ û Å þ  —‘ õ ýÂ “ â Þ › Û Þ ä 18ø ’ ® Û Þ ä 7 ß þÂ “‘ € “

: ´ª¢ ݃û¡ ŕ

n 2
T (n) = 7T ( n2 ) + 18 2

18 n2 2 ∈ O(nlog27 − ) =⇒ T (n) ∈ O(nlog27 )
. Ýþ¢Þ÷ ù¢‘Ôµ¨ O ¥ θ ý‘› ‚“ À÷ª ÂÔ¬ ‘û ‚þ¤¢ ¥ üÌ㓠´¨ ßØÞõ ö
127 (DEVIDE AND CONQUER) ۟ø ݃Æ֗ .2.4

:ßþ Âޗ
ÛìÀŸ.݃û¢ ô‘¹÷ ¤ 24 ‚±—Âõ ¥ Åþ —‘õ ø¢ ’® ߨÂµ¨ ©ø ¤ ‚“ ݃û¡ üõ

?´¨ ¤À֝ ’® ¢Àã—

T (24) = 7T (12) = 72 T (6) = 73 T (3) = 73 × 33


: ñ‘·õ
‚“¤ an ‚
b ±¨‘½õ ýÂ“ ô¥„ ý‘û ’® ¢À㗠ßþ µÞî ‚î ¤ üÞµþ ¤ Úó ݃û¡ üõ

. Ýþ ¤ ø ´¨¢ ‚“ Àû¢ üõ ‘õ

n 2

 (a 2 ) šø ¥ n
an = a × an−1 ¢Âê n


a n=1

: Ýþ ¤¢ ßþÂ“‘€“ . Ýþ ƒð üõ ‘û ’® ¢À㗠“Â“ ¤ T(n) : ۟


 n
T ( 2 ) + 1 n
šø ¥

T (n) = T (n − 1) + 1 n
¢Âê

0 n=1


 T (b 2 c) + 1
n
šø ¥ n
⇒ T (n) = T (n − 1) + 1 = T ( n−1 ) + 2 = T (b n c) + 2 ¢Âê n
 2 2
0 n=1

⇒ T (n) = T (b n2 c) + θ(1) , θ(1) = θ(nlog2 1 (logn)0 )

⇒ T (n) = θ(logn)
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 128

Dynamic Programming ‘þ• üÆþ÷ ‚õ‘÷“ 3.4

‘ “ ¤ ü Ü ¬ ‚ ó‘b Æ õ ö ¤¢ ‚ î ´ ¨‘ û Ý µ þ ¤  Ú ó ß µ ª ÷ ©ø ¤ × þ ‘ þ • ü Æ þ ÷ ‚ õ‘ ÷ “

‚Γ‘® ×þ ‚ó‘b Æõ ýÂ“ Àµ“ ¤¢ ü€ãþ ݃þ‘Þ÷ üõ ۟ üµÈ𥑓 ñõÂê ×þ ¥ ù¢‘Ôµ¨

‚ó‘bÆõ Àµ“ ¤¢ ‚î Ýþ ¤¢ ö “ ü㨠‚Ñꑟ ×þ ¥ ù¢‘Ôµ¨ ‘“ ų¨ .݃“‘þ üõ üµÈ𥑓

¤¢ ¤ ‚ó‘b Æõ ۟ — ü—‘õÀÖõ ý‘û ۟ °ƒî— ‘“ ų¨ ݃€î ۟ ‚ƒóø Âþ¢‘Öõ ýÂ“ ¤

ýÂ“ ü¿¨‘• ‚“ ‚î ݃û¢ üõ ‚õ¢ ‘¹÷ ‘— ¤ À÷ø ¤ ßþ . ݃þ‘Þ÷ üõ ñ‘±÷¢ —„‘“ ´ó‘Ÿ

Û Ÿ ù¤ À ÷ µ “ ‚ î ´ ¨ ùÀ þ¢Â ð áÀ “ ‘ ¹ ÷ ¥ DP ©ø ¤ . Ý ƒ “‘ þ ´ ¨¢ ü Ü ¬ ‚ ó‘b Æ õ

.Àþ‘Þ÷ éÁŸ ¤ Àþ ¢›ø ‚“ D&C ©ø ¤ ¤¢ ´¨ ßØÞõ ‚î ¤ ý ¤Âؗ ý‘û

. ´¨ Bottom - Up üܟ ù¤ ۟ ù¤ ‘› ßþ ¤¢

 
n
: k
‚±¨‘½õ 1.3.4

n
 1  
k = 0 or k=n
k = n−1 n−1
k + k−1 else
5
2
4 4
1 2
3 3 3 3
0 .1.. 1.. 2
.. . .. ..
? ... ... .... ...
2 2
1 1 2
1 1 ?
1 0 1
? ?
1 1

n

ݵþ ¤ Úó ßþ ýÂ“ ô¥„ ö‘õ¥ ÛìÀŸ ü€ãþ ´¨ µÞî ¥ üØþ‘û âޛ ¢Àã—
k
n

.´¨ Ω( k )


0 k = 0 or k=n
T (n − 1, k) + T (n − 1, k − 1) + 1
T(n,k)=
else
129 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

g(n, k) = T (n, k) + 1:­Âê


1 k = 0 or k = n
g(n − 1, k) + g(n − 1, k − 1)
g(n,k)=
else

g(n, k) = T (n − 1, k) + T (n − 1, k − 1) + 1 + 1 = (T (n − 1, k) + 1) +

(T (n − 1, k − 1) + 1) = g(n − 1, k) + g(n − 1, k − 1) ⇒ g(n, k) = nk

⇒ T (n, k) = nk − 1
: Ýþ ¤¢ üÜî¤ Ï ‚“


1−l k = 0 or k = n
T (n − 1, k) + T (n − 1, k − 1) + l
T(n,k)=
else


. ´¨ T (n, k) = n
k −l üµÈ𥑓 ‚ó¢‘ãõ ’› 1 − l ≥ 0 ýÂ“
ÛÞä Âþ ¥ Õþ ÂÏ ‚“ ׃õ‘€þ¢ üÆþ÷ ‚õ‘÷“ ¥ ù¢‘Ôµ¨ ‘“ °ƒî— ßþ ‚±¨‘½õ ýÂ“

:݃€î üõ

:݃÷¢ üõ ø Ýþ ƒð üõ ÂÑ÷ ¤¢ c ô‘÷ ‚“ ý ¤¢Â“

c[n,k]=c[n-1,k-1]+c[n-1,k]
. ݃û¢ ۃØȗ ¤ Âþ ¥ ñøÀ› ´¨ üê‘î ŕ

c 0 1 2 . . . . k-1 k
0 1

1 1 → 1

2 1 → 2 → 1
↓ ↓
3 1 → 3 → 3
. .
. .
. .
. .
. .
n-1 1 c[n-1,k-1] → c[n-1,k]

n 1 c[n,k]

. Ýþ ¤¢ öµ¨ kø ÂΨ n Âþ ¥ , ¢“ Àû¡ θ(nk) ¥ ‚±¨‘½õ ö‘õ¥ ŕ


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 130

‘û ñ• ö¢Âî ¢Â¡ ‚ó‘bÆõ 2.3.4

­Âê .݃€î ¢Â¡ ýÀŸø dn ,. . . d2 ,d1 ý‘û ‚ب ͨ— ¤ ñ• ÀŸø N ݃û¡ üõ

‚  ‚ “ Ý ƒ ÷À “ Ý ƒ û ¡ ü õ . ´ ¨ ¢ › õ ü ê‘ î ù¥À ÷ ‚ “ ‚ Ø ¨  û ¥ ‚ î ´ ¨ ß þ  “

‘û ‚ب ¢À㗠ßþ µÞî ¥ ‚î ý ¤ Ï ‚“ ݃€î ¢Â¡ ¤ ñ• ÀŸø N ßþ ݃÷— üõ ü±ƒ——

.݃€î ù¢‘Ôµ¨

¢Â¡ ýÂ“ ô¥„ ‚ب ¢À㗠ÛìÀŸ ¤ c[i,j] ݃€î üõ Óþ Â㗠‚ó‘b Æõ ßþ ۟ ýÂ“

. ýÀŸø di , . . . , d1 ý‘û ‚ب ͨ— ñ• ÀŸø j ö¢Âî



 0 j=0


∞ i=1 , j < d1
c[i,j]= 1+c[1 , j-d1 ] i=1 , j ≥ d1



 c[i-1 , j] i > 1 , j < di

min{ c[i-1 , j],1+c[i , j - di ]} i >1 , j ≥ di

:¢¢Âð üõ ¢Â¡ Âþ ¥ –¤ ¬ ‚“ ñ• ÀŸø 8 ñ‘·õ ö€ä ‚“

amount 0 1 2 3 4 5 6 7 8
d1 =1 0 1 2 3 4 5 6 7 8
d2 =4 0 1 2 3 1 2 3 4 2
d3 =6 0 1 2 3 1 2 1 2 2
:ݵþ ¤ Úó

Function coins(N,n)
{array d[1..n] specifies the coin,in example there are 1,4,6 units}
array d[1..n]
array C[0..n,0..N]
for i ←1 to n do
C[i,0]←0
for i ←1 to n do
for j ←1 to N do
if(i=1 and j<d[1]) then C[i,j]← ∞
else if(i=1 and j≥ d[1]) then C[i,j]← 1+C[1,j-d[1]]
else if(i>1 and j<d[i])then C[i,j]← C[i-1,j]
else C[i,j]← min{C[i-1,j],1+C[i,j-d[i]]}
. Ýþ ¤¢ öµ¨ N+1ø ÂΨ n Âþ ¥ ,´¨ θ((N + 1)n) ¥ ö‘õ¥
131 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

{0, 1} üµÈ• ‚óî ‚ó‘bÆõ 3.3.4

‘“ ¤ ö ݃û¡ üõ . À€î ÛÞ½— À÷— üõ ¤ W ö¥ ø ‚î Ýþ ¤¢ üµÈ• ‚óî Àƒ€î ­Âê

ö ö¢Âî • ýÂ“.Àª‘“ üõ vi ö ©¥ ¤ø wi ô i üª ö¥ ø .݃€î • 1,2,. . . n ý‘ƒª

Àþ‘“ ßþÂ“‘€“ .(Ýþ ¤À÷ ý ÂÆî ´ó‘Ÿ)¢ÂØ÷ ‘þ ¢Âî ’‘¿µ÷ ¤ üª ö ö— üõ ¤‘“ Âû
P P
.x i ∈ {0, 1} ö ¤¢ ‚î ݃ª‘“ ‚µª¢ ¤ max( xi vi ) ø xi wi ≤ W
ۓ‘ì ö¥ ø ‘“ üµÈ• ‚óî ×þ ‚î üª¥ ¤ ·îÀŸ v[i,j] ݃€î üõ Óþ Âã—ÛŸ ýÂ“

: Ýþ ¤¢ ßþÂ“‘€“ .Àª‘“ ‚µª¢ 1,2,. . . i ý‘ƒª ‘“ À÷— üõ j ÛÞ½—



0 j=0,∀ i


 −∞ or 0 i=1 , 0<j < w1
V[i,j]= V1 i=1 , j ≥ w1



 V[i-1,j] i > 1 ,0< j < wi

max{ V[i-1,j],Vi +V[i-1,j-wi ]} i >1 , j ≥ wi

:Àƒþ‘Þ÷ ´ì¢ Âþ ¥ ñ‘·õ ‚“

weight unit 0 1 2 3 4 5 6 7 8 9 10 11
w1 =1 v1 =1 0 1 1 1 1 1 1 1 1 1 1 1
w2 =2 v2 =6 0 1 6 7 7 7 7 7 7 7 7 7
w3 =5 v3 =6 0 1 6 7 7 18 19 24 25 25 25 25
w4 =6 v4 =22 0 1 6 7 7 18 22 24 28 29 29 40
w5 =7 v5 =28 0 1 6 7 7 18 22 28 29 34 35 40

. ´¨ θ((W + 1)n) ¥ ö‘õ¥ ¢ª üõ ùÀû‘Èõ ‚î ¤ Ï ö‘Þû

: Floyd ݵþ ¤Úó 4.3.4

¤¢ ´ú› éÂð ×þ ¥ Ãþ‘Þµõ §¤ ø¢ Âû ߃“ ƒÆõ ßþ — ù‘—î ‚€þ Ãû ݵþ ¤ Úó ßþ

.À€î üõ ‚±¨‘½õ ´¨ üԀõ‘÷ ö ñ‘þ Âû ö¥ ø ‚î ¤ ¤¢ ö¥ ø

Function Floyd (L[1..n,1..n]):array D[1..n,1..n]


D←L
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
D[i,j] ← min(D[i, j], D[i, k] + D[k, j])
return D
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 132

P üØÞî ‚þ¤ ¥ ö— üõ ƒÆõ ßµêÂð ýÂ“. ´¨ θ(n3 ) ¥ ݵþ ¤ Úó ßþ ö‘õ¥

:¢Þ÷ ù¢‘Ôµ¨

Function Floyd (L[1..n,1..n]):array D[1..n,1..n],array P[1..n,1..n]


D←L
P← ∅
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
if D[i,j] > D[i,k]+D[k,j]
D[i,j] ← D[i,k]+D[k,j]
P[i,j] ← k
return D , P
:Àƒþ‘Þ÷ ´ì¢ Âþ ¥ ñ‘·õ ‚“

 15
1 I 4
30 5
5 50 15 5

2 - 3
15


0 5 ∞ ∞ 0

5 ∞ ∞
 50 0 15 5   50 0 15 5 
D0 = L = 
30 ∞ 0 15  D1 = 
30 35 0 15 
15 ∞ 5 0 15 20 5 0

0 5 20 10  
0 5 20 10 
 50 0 15 5   45 0 15 5 
D2 = 
30 35 0 15  D3 = 
30 35 0 15 
15 20 5 0 15 20 5 0

0 5 15 10  
0 0 4 2
 20 0 10 5  4 0 4 0
D4 = 
30 35 0 15  P =
0 1 0 0
15 20 5 0 0 1 0 0
?À€î ¢‘¹þ À÷— üõ üÜØÈõ ‚ Àª‘“ üԀõ éÂð ý‘û ñ‘þ ö¥ ø Âð : ßþ Âޗ

? Àƒþ‘Þ÷ ü¨¤ “ Dijkstra ݵþ ¤ Úó ¢¤ õ ¤¢ ¤ „‘“ ßþ Âޗ : ßþ Âޗ


133 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

(chained matrix multiplication) ‘û Åþ —‘õ ýùƒ¹÷¥ ’® 5.3.4

üõ di−1 × di ‚ ± — õ ¥Ai Å þ  —‘ õ  û.À € µ Æ û ­ø Â Ô õ A1 , A2 , ..., An Å þ  —‘ õ n


’® ßþ ‚Øþ ¤ Ï ‚“ ݃þ‘Þ÷ ‚±¨‘½õ¤ A=A1 A2 .....An Û¬‘Ÿ ݃û¡ üõ .Àª‘“

‚÷ð ‚“ Àþ‘“ ‘û Åþ —‘õ ¤‘î ßþ ýÂ“ ´¨ üúþÀ“ .Àµê ë‘Ô— ßØÞõ ö‘õ¥ ßþ µÞî ¤¢

.¢ª ‚€ƒÞî ‘ú÷ ý‘ú“ ® ¢À㗠‚î À÷ª ý ¤Áðõ÷Â• ý

’® ¢À㗠ÛìÀŸ mij ‚îÝþ ¥‘¨ üõ ö‘€ ¤ M=(mij )n∗n Åþ —‘õ ¤ Ñ€õ ßþ ýÂ“
.Àª‘“ Ai Ai+1 .....Aj ’® ‚±¨‘½õ ýÂ“ ô¥„

mii = 0 Ai ‚±¨‘½õ ¤¢ô¥„ ’® ¢Àã—

mii+1 = di−1 di di+1 Ai Ai+1 ‚±¨‘½õ ¤¢ô¥„ ’® ¢Àã—

: Ýþ ¤¢ ¤ Âþ ¥ ͓ø ¤ ‘û Åþ —‘õ ’® ¤¢ ݃÷¢ üõ

A = (aij )p×q B = (bij )q×r C = (cij )p×r = AB


P
q
⇒ cij = aik bkj
k=1
for i=1 to p do
for j=1 to r do ∈ θ(pqr)
for k=1 to q do
cij = cij + aik bkj

: Ýþ ¤¢ ßþÂ“‘€“

( Ai Ai+1 ...Ak )(Ak+1 ...Aj )


0 i=j
mij =
mini≤k≤j−1 {mik + mk+1,j + di−1 dk dj } j>i , i=1,2,.....n-1

: Ýþ ¤¢ Âþ ¥ ƒçµõ ƒƒç— ‘“

j=i+s⇒

 0 s=0 , i=1,...,n

mi,i+s =

 mini≤k≤i+s−1 {mik + mk+1,i+s + di−1 dk di+s } i=1,2,.....n-s ,

1≤ s ≤n-1
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 134

: ݵþ ¤ Úó

int minmult(int n,const int d[ ],index p[ ][ ]){


index i,j,k,diagonal;
int M[1..n][1..n]
for (i=1;i ≤ n;i++)
M[i][[i]=0;
for (diagonal=1;diagonal ≤ n-1;diagonal++)
for (i=1;i ≤ n-diagonal;i++){
j ← i+diagonal
m[i][j] = mini≤k≤j−1 {m[i][k] + m[k + 1][j] + d[i − 1]d[k]d[j]}
p[i][j]=a value of k that gave the minimum
}
return M[1][n];}

: ´¨ Âþ ¥ –¤ ¬‚“ ݵþ ¤ Úó ýÂ› ö‘õ¥

X1
n−
s(n − s) = Θ(n3 )
s=1

.Àþ ¤ ø ´¨À“ ¤ ‘û ’® ¢À㗠ÛìÀŸ Âþ ¥ ý‘û Åþ —‘õ ¤¢: ñ‘·õ

A1 = A13×5 A2 = B5×89 A3 = C89×3 A4 = D3×34



 m =0
 11

m22 =0
=0
s=0 =⇒

 33
 m
m =0
 44
 m12 = d0 d1 d2 = 13 × 5 × 89 = 5785
s=1 =⇒ m23 = d1 d2 d3 = 5 × 89 × 3 = 1335

m34 = d2 d3 d4 = 89 × 3 × 34 = 9078


 m13 = {m11 + m23 + 13 × 5 × 3, m12 + m33 + 13 × 89 × 3}
s=2 =⇒ = min{1530, 9256} = 1530

m24 = 1845
135 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

 m14 = min{m11 + m24 + d0 d1 d4 , m12 + m34 + d0 d2 d4 , m13 +
s=3 =⇒

m44 + d0 d3 d4 }=2856

‚€ƒú“ üþø¢ø¢ ý¹µÆ› ´¡¤¢ 6.3.4

´¡¤¢ ×þ ýø ¤ ¤ ÀƒÜî n ßþ ݃û¡ üõ ‚î Ýþ ¤¢ ¤‘ƒµ¡ ¤¢ ÀƒÜî n ‚î Àƒ€î ­Âê

ýÂ “ ü € ƒ ã õ ñ‘ Þ µ Ÿ ý¤¢ ‘ û À ƒ Ü î ß þ ¥ × þ  û .Ý ƒ û¢ ¤Â ì ü þø¢ø¢ ý ¹ µ Æ ›

¢Âê ‚“ Âʽ€õ ÀƒÜî) .À÷ ùÀª °—Âõ ý¢ã¬ °ƒ—— ‚“ ‘û ÀƒÜî .À€ª‘“ üõ ü¨Âµ¨¢

´¨ ¼®ø .Àª‘“ üõ pi ô i ÀƒÜî ‚“ ü¨Âµ¨¢ ñ‘ÞµŸ ‚î ´¨ ßþ “ ­Âê( ´¨

P
n
× þ ‚ “ ¤ À ƒ Ü î n ß þ ü ± ƒ — — ‚  ‚ “ ‚ î Ý ƒ þ‘ Þ ÷ ü ¨¤  “ Ý ƒ û ¡ ü õ . pi = 1 ‚î
i=1
Ýރ÷ üõ ‘û ùÂ𠂓 ü¨Âµ¨¢ ‚€þ Ãû ͨµõ ‘— ݃þ‘Þ÷ ÛþÀ±— üþø¢ø¢ ý¹µÆ› ´¡¤¢

.Àª‘“

Փ‘Îõ ‘ú÷ ‚“ ü¨Âµ¨¢ ñ‘ÞµŸ ø ‘û ÀƒÜî ù¤‘Þª ñøÀ› ‚î Àƒ€î ­Âê ñ‘·õ ýÂ“

:Àª‘“ Âþ ¥

node 6 12 18 20 27 34 35
probability 0.2 0.25 0.05 0.1 0.05 0.3 0.05
üõ. ´¨ Âþ ¥ Ûت ´ª¢ üþø¢ø¢ ý¹µÆ› ´¡¤¢ ×þ ö— üõ ‚î ü—„‘Ÿ¥ üØþ

¢À㗠üꢑʗ ƒçµõ ŕ. ݃“‘ƒ“ ¤ ‘û ùÂð áÞ¹õ ‚“ ü¨Âµ¨¢ ‚€þ Ãû ͨµõ ݃û¡

.Àª‘“ üõ ü¨Âµ¨¢ ýÂ“ ‘û ‚Æþ‘Öõ

34

12 35

6 20

18 27

1×0.3+2×0.25+2×0.05+3×0.2+3×0.1+4×0.05+4×0.05 = 2.2
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 136

P
n
ñ‘Ÿ , Àª‘“ üõ pi (depth(ci ) + 1) “Â“‘û ùÂð ô‘Þ— ‚“ ü¨Âµ¨¢ ‚€þ Ãû ͨµõ
i=1
.Àõ Àû¡ ´¨¢ ‚“ ý ÂÚþ¢ ¤ÀÖõ Àª‘“ Âþ ¥ –¤ ¬ ‚“ ´¡¤¢ Âð

12

6 20

18 34

27 35

1×0.25+2×0.2+2×0.1+3×0.05+3×0.3+4×0.05+4×0.05 = 2.3

: ‚ÜÿÆõ ۟ ýÂ“ ׃õ‘€þ¢ ù¤

.À € ª‘ “ ci , ci+1 , . . . , cj ý‘ ûùÂ ð ‚ “ ü ¨Â µ ¨¢ ýÂ “ ‚ € þ à û Û ìÀ Ÿ cij À ƒ € î ­Â ê


.´¨ ‚Èþ ¤ ¤¢ ‚î Ýþ ¤¢ ùÂð ×þ . ´¨ ‚ó‘bÆõ ’› c1n ßþÂ“‘€“

ck

ci < . . . < ck−1 ck+1 < . . . < cj

1)i = j ⇒ cii = pi
2)j > i ⇒ ci < . . . < ck−1 < ck < ck+1 < . . . < cj
´¨¤ ´Þ¨ ýÂ“ ‚€þ Ãû ø ´¨ ci,k−1 “Â“ ck ² ´Þ¨ ý‘û‚Èþ ¤ ýÂ“ ‚€þ Ãû

‚“ ck öÀ ÷¥  ê ô‘ Þ — ¥ ñ‘ Þ µ Ÿ × þ Ý þ ¤¢ ¤ ck ‚ È þ ¤ ‚ î ü µ ìø Å • .´ ¨ ck+1,j


. ´¨ pk Ã÷ ‚Èþ ¤ ¢¡ ýÂ“ .¢ª üõ ‚ê‘® áÞ¹õ

j
X
ci,k−1 + ck+1,j + pk + pi + ... + pk−1 + pk+1 + . . .+ pj = cik−1 + ck+1j + pt
t=i
137 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

j
X
⇒ cij = mini≤k≤j {(cik−1 + ck+1j ) + pt }
t=i
: Ýþ ¤¢ ŕ j = s + i ݃þ‘Þ÷ üõ Óþ Âã—



 pi s = 0, i = 1, ..., n



ci,i+s = P
i+s



mini≤k≤i+s {ci,k−1 + ck+1,i+s } + pt 1≤s≤n−1

 t=i
,1 ≤ i ≤ n−s
.´¨ ‘û Åþ —‘õ ý ùƒ¹÷¥ ’® ‚ƒ±ª ‚õ‘÷“ ßþ Àî

P1
n−
. (n − s)(s + 1) = θ(n3 ): Ýþ ¤¢ Ã÷ Â› ö‘õ¥ ýÂ“ ø
s=1
: ñ‘·õ

:Ýþ ¤¢ ¤‘ƒµ¡ ¤¢ ¤ Âþ ¥ ÀƒÜî 4 Àƒ€î ­Âê

Don Isabelle Rulph Wally


key[1] key[2] key[3] key[4]
p1 = 38 p2 = 38 p3 = 18 p4 = 18

C 1 2 3 4 P 1 2 3 4
1 38 98 11 7
8 4
1 1 1 2 2
2 3 5 2 2 2 2
8 8 1
1 3 3 3 3
3 8 8
4 1
8 4 4

Isabelle

Don Rulph

Wally

. ´¨ ƒÆõ Åþ —‘õ P


üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 138

íµÈõ ‚µª¤ Âþ¥ ßþ µð¤ Ó 7.3.4

Ý ƒ û ¡ ü õ .À € ª‘ “ ‚ µ ª¤ ø¢ Y = y1 y2 . . . yn , X = x1 x2 . . . xn À ƒ € î ­Â ê

,‚µª¤ ×þ ¥ ‚µª¤ Âþ ¥ ×þ ¥ ¤ Ñ€õ.݃“‘ƒ“ ¤ ø¢ ßþ ߃“ íµÈõ ‚µª¤ Âþ ¥ ßþ µð¤ Ó

.Àª‘“ ùÀõ ´¨À“ ‚µª¤ ö ¥ éŸ À€ ‘þ ×þ ‘þ ºƒû éÁŸ ¥ ‚î ´¨ ý ‚µª¤

:׃õ‘€þ¢ ۟

.Yj = y1 y2 . . . yj ,Xi = x1 x2 . . . xi ݃þ‘Þ÷ üõ Óþ Âã—

LCS=Longest Common Subsequence






0 i = 0 or j = 0
LCS[Xi , Yj ] =
1 + LCS[Xi−1 , Yj−1 ] xi = yj , i 6= 0, j 6= 0



M ax{LCS[Xi−1 , Yj ], LCS[Xi , Yj−1 ]} xi 6= yj , i 6= 0, j 6= 0
LCS-length(X,Y)
1 m← length(X)
2 n← length(Y)
3 for i← 1 to m do
4 c[i,0]← 0
5 for j←0 to n do
6 c[0,j]←0
7 for i←1 to m do
8 for j←1 to n do
9 if (xi = yj ) then
10 c[i,j] ←− c[i-1,j-1]+ 1
11 b[i,j] ←− ” - ”
12 else if (c[i-1,j] ≥ c[i,j-1]) then
13 c[i,j] ←− c[i-1,j]
14 b[i,j] ←− ” ↑ ”
15 else
16 c[i,j] ←− c[i,j-1]
17 b[i,j] ←− ” ← ”
18 return c and b //θ(nm)
139 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

Print-LCS(b,X,i,j)
1 if i=0 or j=0 then
2 return
3 if b[i,j]=”-” then
4 Print-LCS(b,X,i-1,j-1)
5 Print xi
6 if b[i,j]=”↑ ” then
7 Print-LCS(b,X,i-1,j)
8 else Print-LCS(b,X,i,j-1) //O(n + m)
Âþ ¥ ö¢¤ ø ´¨¢ ‚“ ýÂ“.Àª‘“ üÞ÷ ¢Âê ‚“ Âʽ€õ ’› ßþ ‚î ¢Þ÷ ‚›— Àþ‘“

.݃þ‘Þ÷ ñ‘±÷¢ ¤ ‘û ÇÜê ´ú› Àþ‘“ íµÈõ ‚µª¤

: ñ‘·õ

X=ABCBDAB Y=BDCABA LCS=BCBA

j 0 1 2 3 4 5 6

i yj B D C A B A

0 xi 0 0 0 0 0 0 0

↑ ↑ ↑ - ← -
1 A 0 0 0 0 1 1 1

- ← ← ↑ - ←
2 B 0 1 1 1 1 2 2

↑ ↑ - ← ↑ ↑
3 C 0 1 1 2 2 2 2

- ↑ ↑ ↑ - ←
4 B 0 1 1 2 2 3 3

↑ - ↑ ↑ ↑ ↑
5 D 0 1 2 2 2 3 3

↑ ↑ ↑ - ↑ -
6 A 0 1 2 2 3 3 4

- ↑ ↑ ↑ - ↑
7 B 0 1 2 2 3 4 4
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 140

ü÷‘ú› –‘Ö“‘Æõ ý ‚ó‘bÆõ 8.3.4

ö ¤¢ À“‘þ ´¨¢ ý¥ ø ƒ• n ‚“ ݃— ø¢ ¥ üØþ ‘— À€û¢ üõ ‚Ö“‘Æõ ¤ÀÖ÷ B ø A ݃— ø¢

ø¢ ßþ ‚î ´¨ üúþÀ“ .À“‘þ ù¤ –‘Ö“‘Æõ À㓠ý‚ÜŸÂõ ‚“ À÷— üõ ݃— ßþ –¤ ¬

.(¢¤À÷ ¢›ø ýø‘Æ— ´ó‘Ÿ ) À€ª‘“ ‚µª¢ ý¥‘“ 2n − 1 ·îÀŸ ø n ÛìÀŸ Àþ‘“ ݃—
Àª‘“ q “Â“ B ¢Â“ ‚¹ƒµ÷ ¤¢ A ´Æت ñ‘ÞµŸ ø p ý¥‘“ Âû ¤¢ A ݃— ¢Â“ ñ‘ÞµŸ Âð

‚ » ÷‘ €  ¢Â ƒ ð ü õ –¤  ¬ Â Ú þ¢ ý¥‘ “ ¥ Û Ö µ Æ õ Û Ø ª ‚ “ ý¥‘ “  û ‚ î Ý ƒ € î ­Â ê ø

ý¥ ø ƒ• j ‚“ B ݃— ø ý¥ ø ƒ• i ‚“ A ݃— ‚Ø€þ “ ¯ø ÂÈõ Àª‘“ A ¢Â“ ñ‘ÞµŸ p(i, j)
.Àª‘“ ‚µª¢ ¥‘ƒ÷
: ´¨ Âþ ¥ üµÈ𥑓 Ûت ‚“ p(i, j) üó‘ÞµŸ ý ‚Ф •

 p(i, j) = p ∗ p(i − 1, j) + q ∗ p(i, j − 1) , i ≥ 1, j ≥ 1
p(0, j) = 1 ∀j ≥ 1

p(i, 0) = 0 ∀i ≥ 1

: Devide and conquer ©ø ¤ ‚“ ۟ •

function p(i,j){

if i=0 then return 1

else if j=0 then return 0

else return p*p(i-1,j)+q*p(i,j-1)

};

ý ‚Ф ø ݃“‘ƒ“ ¤ p(n, n) ý ‚±¨‘½õ ýÂ“ ô¥„ ý‘û âޛ ¢À㗠݃û¡ üõ ñ‘Ÿ

: Ýþ ¤¢ ‘û âޛ ¢À㗠ýÂ“ ¤ Âþ ¥ üµÈ𥑓



g(i, j) = g(i − 1, j) + g(i, j − 1) + 1
g(i, 0) = g(0, j) = 0

:Àþ üõ ¤¢ Âþ ¥ –¤ ¬ ‚“ „‘“ ý ‚Ф h(i + j, j) = g(i, j) ƒçµõ ƒƒç— ‘“


a


h(i + j, j) = h(i + j − 1, j) + h(i + j − 1, j − 1) + 1
h(i + 0, 0) = h(0 + j, j) = 0


.¢“ Àû¡
i+j
j −1 ‚µê¤ ¤‘î ‚“ ý‘û âޛ ¢À㗠ñ‘ب‘• ñõÂê ¥ ù¢‘Ôµ¨ ‘“

¢À㗠“Â“ ø¢ ü¨¤ “ ¢¤ õ üµÈ𥑓 ý ‚Ф ¤¢ ‘û ’® ¢À㗠‚î ݃÷¢ üõ ‘õ


141 DYNAMIC PROGRAMMING ‘þ• üÆþ÷ ‚õ‘÷“ .3.4

üõ ô‘¹÷ ’® 2 2n − 2 ¢À㗠‚“ p(n, n) ý ‚±¨‘½õ ýÂ“ ŕ . ´¨‘û âޛ


n
üõ
4 n
Ω 2n
2n+1 ¥ µð¤ Ó ‚î, ´¨
n ÀŸ ¤¢ ü÷‘õ¥ ý¤¢ „‘“ Àî ‚¹ƒµ÷ ¤¢ ø .¢ª

. ´Æƒ÷ Àõ¤‘î ©ø ¤ ßþ ¥ ù¢‘Ôµ¨ ŕ. Àª‘“

: Dynamic programming ©ø ¤ ‚“ ۟ •

function series(n,p){

array p[0..n,0..n]

q=1-p

{ fill from topleft to diagonal }

for s = 1 to n do

p[0,s] = 1 , p[s,0] = 0

for k = 1 to s - 1 do

p[k,s - k] = p * p[k - 1,s - k] + q * p[k,s - k - 1]

{ fill from below diagonal to bottomright }

for s = 1 to n do

for k = 0 to n - s do

p[s + k,n - k] = p * p[s + k - 1,n - k] + q * p[s + k,n - k - 1]

return p[ n , n ]};

. ´¨ Û±ì ©ø ¤ ¥ — ‚€ƒú“ ¤‘ƒÆ“ ‚î Àª θ(n2 )¥ ü÷‘õ¥ üðÀƒ»ƒ• °ƒ—— ßþ ‚“

2 3 4 n
 0 1 
 
0 .... ..1.. ..1.. 




0
.........1 ........ ........ 


.... . . 
1

.....0 ........p .... .p+pq
.. ..
.... 



 ..... . .... 

 ..2.. 3 2 

2....0 ........p p + pq + pq 
.....
3 ....0.
 


 


 


 


 


 

 

n 0 
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 142

¢Âð ù¤ø¢ ùÀ€ªø Âê ‚ó‘bÆõ 9.3.4

‚î ý ¤ Ï ‚“ ¢¢Âð“ ø ¢ø “ Âúª n ‚“ Àû¡ üõ ¢Âð ù¤ ø¢ ùÀ€ªø Âê ×þ Àƒ€î ­Âê

É¿Èõ ‚€þ Ãû ‘“ ƒÆõ ×þ ÂÚþ¢ Âúª ‚“ Âû ¥ ü€ãþ Àª‘“ ‚µª¢ ¤ ƒÆõ ‚€þ Ãû ßþ µÞî

.¢¤¢ ¢›ø

ü õ ö‘ È ÷ W ýÀ 㠓 ø¢ ‚ þ¤ Í ¨ — ¤ §¤ n ‘ “ ¤¢ ´ ú › éÂ ð ‘ ¹ € þ ¤¢ :Û Ÿ

minlenght. ´ ¨ j ø i §¤ ß ƒ “ ñ‘ þ ñ Ï ùÀ € û¢ ö‘ È ÷ W[i][j] –¤  Ê ÷ ¤¢ Ý ƒ û¢

ø ´¨ n ‘— 1 ¥ ö ý‘ûÂΨ ‚î ´¨ ýÀ㓠ø¢ ‚þ¤ P, ´¨ ‚€ƒú“ ¤ — ùÀ€û¢ ö‘È÷

ÅþÀ÷ P[i][A] . ´¨ ùÀª ý ¤Áð ÅþÀ÷ V − v1 ý‘û ‚äÞ¹õ Âþ ¥ ͨ— ö öµ¨

A §Ž¤ ‚Þû ¥ ‚î ´¨ v1 ‚“ vi ¥ ƒÆõ ßþ — ù‘—î ýø ¤ vi ¥ ŕ §¤ ߃µÆ¿÷

. ¢¤Áð üõ ¤‘±Øþ ‘fփì¢

void travel(int n,const number W,index P,number & minlenght)

index i,j,k;

number D[1..n][subset of V-{v1 }];

for (i=2 ;i ≤ n;i++)

D[i][∅]=W[i][1];

for(k=1;k≤n-2;k++)

for(all subset A ⊆ V -{v1 }contaning k vertices)

for (i such that i6=1 and vi is not in A){

D[i][A]=minj:vj ∈A (W[i][j]+D[j][A-{vj });

P[i][A]=value of j that gave the minimum ;

D[1][V-{v1 }]=min2≤j≤n (W[1][j]+D[j][V-{v1 , vj }])

P[1][V-{v1 }]=value of j that gave the minimum;

minleght=D[1][V-{v1 }];

}
143 ‘û ý¥‘“ ´€Þ÷¤— .4.4

‘û ý¥‘“ ´€Þ÷¤— 4.4


Âʀä n ߃“ ¤ Âʀä ßþ µð¤ Ó ߃õø¢ üãÎì ݵþ ¤ Úó ×þ ¥ ù¢‘Ôµ¨ ‘“ ݃û¡ üõ

©ø ¤ ßþ ¤¢. ´¨ ùÀª ý ¤¢Â“ Úó üêÁŸ ý‘û ô‘› ©ø ¤ ¥ ´€Þ÷¤ — ©ø ¤ ݃“‘ƒ“

:¢ª üõ ÛÞä Âþ ¥ ùƒª ‚“‘û ݃— ö‘õÂúì ö¢¤ ø ´¨À“ ýÂ“

‚Ö“‘Æõ Ýû ‘“ ݃— ø¢ Âû ų¨ ùÀª ݃Æ֗ üþ‘— 2 ý‘û ‚µ¨¢ ‚“ ¢›õ ý‘û ݃—

ý ¤Ãð “ ‘“ À÷ø ¤ ßþ ų¨ À“‘þ ù¤ À㓠¤ ø¢ ‚“ À÷— üõ ‘û ݃— ßþ ö‘õÂúì À€û¢ üõ

.¢ª É¿Èõ ‘û ö‘õÂúì ö‘õÂúì ‚Ø÷ ‘— À“‘þ üõ ‚õ¢ ýÀ㓠´€Þ÷¤ —

:ñ‘·õ

20

20 13

20 14 12 13

19 20 14 9 12 5 13 8

¢À 㠗.´ ¨ dlg ne á‘ Ô —¤ À ª‘ ± ÷ 2 ö — ¥ ü “ Â Ì õ  ¬‘ € ä ¢À 㠗  𐠑 ¹ € þ ¤¢

.´¨ 2h − n“Â“ −∞ ¬‘€ä

(1 − ( 12 )lg n ) 1
lg n
X n
n
n=2 : k
= 2 = n(1 − ) = n − 1
i=1
2i 1− 2 1 n

Àª‘±÷ 2 ¥ ü÷— ö‘ª¢À㗠‚î ý ¬‘€ä ÝÞþ Ãî‘õ ö¢¤ ø ´¨À“ ýÂ“ ©ø ¤ ßþ

´ ¨À “ ýÂ “ ‘ õ ¢¤À ÷ ü ó Þ ã õ ©ø ¤ ‘ “ ü ìÂ ê ‚ î,¢¤¢ ¥‘ ƒ ÷ ‚ Æ þ‘ Ö õ n-1 ö‘ Þ û ‚ “ à ƒ ÷

. ´¨ °¨‘€õ Âʀä ßþ µð¤ Ó ߃õø¢ ö¢¤ ø

‚Èþ ¤ ý‘û ùÀ÷¥‘“ ߃“ ¤¢ ¤ ÝރÆî‘õ Àþ‘“ Âʀä ßþ µð¤ Ó ߃õø¢ ö¢¤ ø ´¨À“ ýÂ“

.Ýþ ¤¢ ‚Èþ ¤ ‚“ ùÀ÷¥‘“ Âʀä, ´¡¤¢ á‘Ô—¤ ù¥À÷ ‚“ ŕ . ݃þ‘Þ÷ Àƒ•( 20 ü€ãþ)

n-1+dlg ne-1=n+dlg ne-2 Û î ¤¢ Å • . ´ ¨ dlg ne -1 ‘ û ‚ Æ þ‘ Ö õ ¢À 㠗 Å •

. Ýþ ¤¢ ‚Æþ‘Öõ
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 144

ö¢¤ ø ´¨À“ ýÂ“ ‚Æþ‘Öõ ¢À㗠ÛìÀŸ ‚Æþ‘Öõ ¢À㗠ßþ Àƒû¢ ö‘È÷ : ßþ Âޗ

. ´¨ Âʀä ßþ µð¤ Ó ߃õø¢

B &T ( Back Tracking ) 5.4


¥ ‚ÜÿÆõ ×þ ۟ ýÂ“ ö ¤¢ ‚î ´¨ üÆþ÷ ‚õ‘÷“ ׃€Ø— ¢Âð °Öä ‚“ ´È𥑓

ý‘Ìê ¥ ‚ÎÖ÷ Âû ü€ãþ. ¢¢Âð üõ ù¢‘Ôµ¨(¤¢ ´ú› ´¡¤¢) ¤¢ ´ú› éÂð ×þ

üõ ÂÑ÷ ¤¢ ¤¢ ´ú› ´¡¤¢ ‘þ éÂð ×þ ¥ ñ‘þ ×þ ‘þ ø §¤ ×þ ‘“ Âґ€µõ ¤ ‚ÜÿÆõ

Âþ ¥ ‚µØ÷ ‚“ üµÆþ‘“ ‚î ´Æ÷ ´¨ Ýúõ B&T ێ‘Æõ ۟ ¢¤ õ ¤¢ ‚î ‚»÷ . Ýþ ƒð

Promissing ⓑ— : ’ ‚¡‘ª ¤ µî‘ê : Óó . ¢ª ‚›—

ö € ä ‚ “ §b¤ × þ ¥ ö — ü õ ‚ î ü þ‘ û ù 𠢐À 㠗  · îÀ Ÿ ü € ã þ ‚ ¡‘ ª ¤  µ î‘ ê

‘ þ ‚ î À € î ü õ ü ¨¤  “ ‚ î ´ ¨ ü 㠓‘ — promissing ⠓‘ — ø ´ ª¢ ü ¨Â µ ¨¢ À ÷¥  ê

ü€ãþ ƒ¡ ùÂ𠒑¿µ÷ ö‘Øõ ¥ ¤ Ñ€õ . ƒ¡ ‘þ ´¨ (fisible) ü÷Àª ƒ¡ ùÂ𠒑¿µ÷

ßþ ´þ Ãõ . ƒ¡ ‘þ Àõ Àû¡ ¢›ø ‚“üÜ¡À— ‘þ Ë쑀— ùÂð ßþ ’‘¿µ÷ ‘“ ‚Ø€þ

. ´¨‘û ´ó‘Ÿ ô‘Þ— ö¢¢ ©ø ¤

Âþ¥ø n‚ó‘Æõ 1.5.4

ý ¤ Ï ‚“ ݃û¢ ¤Âì ¤ Âþ ¥ ø n ,n×n ¸÷ÂΪ ‚½Ô¬ ×þ ¤¢ ݃û¡ üõ ‚î Àƒ€î ­Âê

Û ¡À — ‚ î ¢ ª ×  À þ‘ “ ‘ ¹ € þ ¤¢ .À € þ‘ Þ € ÷ À þÀ ú — ¤ Â Ú þÀ Ø þ ý  þ ¥ ø ø¢ º ƒ û ‚ î

[k,col[k]] ‚÷‘¡ ¤¢ ôk Âþ ¥ ø . ´¨ [i,col[i]] ‚÷‘¡ ¤¢i Âþ ¥ ø . ÀµÔƒ÷ ë‘Ô— ô kø ôiÂþ ¥ ø
:Àû¢  ¤ Âþ ¥ ´ó‘Ÿ ø¢ k < i ýÂ“ Àþ‘“ ŕ .´¨

col[i]6= col[k], |col[i] − col[k]| 6= i-k


bool promssing (index i) {
index k;
bool switch;
k=1;
switch=true;
while(k < i && switch){
if (col[i]==col[k] k abs(col[i]-col[k])==i-k)
switch=false;
k++;}//{end of while}
return switch;}
.´¨n ‚¡‘ª ¤ µî‘ê ŕ Ýþ ¤¢ öµ¨ n‚¡‘ª Âû¤¢ :‚¡‘ª ¤ µî‘ê
145 B &T ( BACK TRACKING ) .5.4

void queens (index i){


index j;
if (promissing (i))
if (i==n)
cout<< col[1] through col[n];
else
for(j=1;j <= n;j++){
col[i+1]=j;
queens(i+1);}
}
.Àþ‘Þ÷ ÛÞ½—¤ W ö¥ ø À÷— üõ ‚î Ýþ ¤¢ üµÈ• ‚óî ×þ ‚î Àƒ€î ­Âê : ñ‘·õ

¤¢.´ ¨ wi ö¥ ø ý¤¢ ü ª  û.Ý ƒ € î  • n, ..., 2, 1 ‘ ƒ ª ‘ “ ¤ ‚ ó î ß þ Ý ƒ û ¡ ü õ

‚î Àª‘“ ý ‚÷ð ‚“ Àþ‘“ ö¢Âî • ù½÷.݃þ‘Þ÷ ’‘¿µ÷ ݃÷µƒõ ¤ üª ºƒû ‘þ ×þ ‘¹€þ

ßØÞõ ý‘úµó‘Ÿ ô‘Þ— ݃û¡ üõ .Àª‘“ w “Â“ À÷ª üõ ’‘¿µ÷ ‚î üþ‘ƒª ö¥ ø ‘fփì¢

.݃“‘ƒ“ ¤

w1 = 3, w2 = 4, w3 = 5 ‘ƒª ö¥ ø ø 13 üµÈ• ‚óî ö¥ ø ‚î Àƒ€î ­Âê

Â𐠢ª üõ ‚ê‘® üª ö Àª‘“ ×þ ñ‘þ °ÆÂ“ Â𐠴¡¤¢ ßþ ¤¢ .Àª‘“ w4 = 6 ø

¤¢ Âþ ¥ ´¨ 2 ‚¡‘ª ¤ µî‘ê .´¨ ÕÞä ñø ´¡¤¢ ßþ. ¢ÈƒÞ÷ ‚ê‘® Àª‘“ ÂÔ¬

.Ýþ ¤¢ À÷¥ Âêø¢ ùÂðÂû

w1 0
1 0

w2 3 0
1 0 1 0

7 3 4 0
w3 1 1 0 1 0
0 ×
12 7 8 3 9 4
1 0 × × × ×
×
13 7

×
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 146

P1
k− P
n
weightk = xi wi , totalk = wi
i=1 i=k
void sum-of-subset(index i,int weight,int total){
if (promissing(i)){
if(weight==W)
cout<<x[1] through x[n];
else{
x[i+1]←− 1
sum-of-subset(i+1,weight+w[i+1],total-w[i+1]);
x[i+1]←− 0
sum-of-subset(i+1,weight,total-w[i+1]);}
}
}
bool promissing(index i){
return
((weight + total ≥ W )&&((weight == W )||(weight + w[i + 1] ≤ W ))
}

ü÷µÜƒÞû ¤ø¢ ßµê‘þ ‚ó‘bÆõ 2.5.4

void hamiltonian(index i)
index j;
if(promissing(i))
if(i==n-1)
cout  vindex[0] through vindex[n-1];
else
for(j = 2 ;j ≤n ; j++){
vindex[i+1] = j ;
hamiltonian(i+1);}
}
147 B &T ( BACK TRACKING ) .5.4

bool promissing(index i){


index j;
bool switch;
if (i==n-1 && !w[vindex[n-1]][vindex[0]])
switch=false;
else if (i > 0 && !w[vindex[i-1]][vindex[i]])
switch=false;
else {
switch=true;
j=1;
while(j <i && switch){
if(vindex[i]==vindex[j])
switch=false;
j=j+1;}
}
return switch;}
m-coloring ‚ó‘bÆõ 3.5.4

ýÃõ Ù÷¤ ö‘€ Ù÷¤ m ‘“ ¤ Àª‘“üõ §b¤ n Ûõ‘ª ‚î éÂð ×þ §Ž¤ ݃û¡üõ

.À€ª‘±÷ Ù÷ÂÞû ý ¤ ø‘¹õ §b¤ ø¢ ºƒû ‚î ݃€î

. ´¨ m ‚¡‘ª ¤ µî‘ê ‚ó‘bÆõ ßþ ¤¢

void m-coloring (index i){


int color;
if (promissing(i))
if (i == n)
cout << vcolor[1] through vcolor[n]
else
for (color = 1; color <= m; color++) {
volor[i+1] = color;
m-coloring(i+1);}
}
üÆþ÷ ݵþ ¤ Úó Óܵ¿õ ý‘û ©ø ¤ üêÂãõ .4 ÛÊê 148

bool promissing (index i) {


index j;
bool switch = true;
j = 1;
while (j < i && switch) {
if (W[i][j] && vcolor[i] == vcolor[j])
switch = false;
j++; }
return switch;
}

Branch and Bound (B&B) ׃€Ø— 6.4

Back Tracking ©ø ¤ ‚“ ü÷øÂê ´û‘±ª ‚î ´¨‘û ݵþ ¤ Úó ý¥‘¨ù¢‘ƒ• ׃€Ø— ×þ

¤¢ ù ð  û ýÂ “ (‚ ƒ óø ¤À Ö õ) Bound ×þ B&B f  Þ ã õ .¢¤¢


©ø ¤ ý¥‘ ¨ù¢‘ ƒ • ¤¢ „

݃€î ‚›— ùÂð Âû ’‘¿µ÷ ø ƒÆõ Âû ’‘¿µ÷ ýÂ“ üµÆþ‘“ ‚¹ƒµ÷ ¤¢ ,¢ªüõ ‚µêÂð ÂÑ÷

ÂÑ÷ ¤¢ „‘Ÿ ‘— ‚î Àª‘“ ýÀ÷‘“ ¥ µȃ“ ÀÊÖõ ùÂ𠂓 ùÂð ßþ ¥ öÀƒ¨¤ ‚
b €þ Ãû Â𐠂î

ýÂ “ ‘f ± ó‘ è .¢ ªü õ §Â û  ƒ Æ õ ö â ìø ¤¢ .Ý þ  ƒ Ú ÷ Â Ñ ÷ ¤¢ ¤  ƒ Æ õ ö ,Ý þ‚ µ ê ð

.¢¢Âðüõ ù¢‘Ôµ¨ BFS ¤‘µ¡‘¨ ¥ ©ø ¤ ßþ ý¥‘¨ù¢‘ƒ•


5 ÛÊê

‘û éÂð Çþ•

Exploring graphs ‘û éÂð Çþ• .5

(Depth First Search) DFS ݵþ ¤Úó 1.5


procedure DF-Search(G=< N,A > )
for each v ∈ N do mark[v] ← not-visited

for each v ∈ N do

if mark[v] 6= visited then

DFS(v)

procedure DFS(v)

{Node v has not previously been visited}

mark[v] ← visited

for each node w adjacent to v do

if mark[w] 6= visited then

DFS(w)
‘û éÂð Çþ• .5 ÛÊê 150

: Ýþ ¤¢ ñ‘·õ ö€ä ‚“

2 3 4

5 6 7 8

2 4

3 7

6 8

1. DFS(1) =⇒ mark[1]=visited , mark[2] 6= visited


2. DFS(2)
3. DFS(3)
4. DFS(6)
5. DFS(5)→ .À÷ùÀª visit ¤ ø‘¹õ ý‘û¢÷ ‚Þû ‘¹€þ

6. DFS(4)
7. DFS(7)
8. DFS(8)
: ñ‘·õ

ö‘ õ¥ ¤¢ ‚ î ¢¤¢ ¢ ›ø DF-Search Ý µ þ ¤  Ú ó ¥ ý¥‘ ¨ ù¢‘ ƒ • À ƒ û¢ ö‘ È ÷

. ´¨‘úó‘þ ¢À㗠A ø §Ž¤ ¢À㗠N ‚î .¢ª üõ ý¥‘¨ ù¢‘ƒ• θ(|N | + |A|)


151 (DEPTH FIRST SEARCH) DFS ݵþ ¤Úó .1.5

:۟

ö‘õ¥ ö‘Þû ‚“ ‘Öƒì¢ „‘“ ݵþ ¤ Úó ý¥‘¨ ù¢‘ƒ• ‚î ¢Þ÷ ö‘È÷ Âϑ¡ Àþ‘“ ¾¨‘• ¤¢

.¢Þ÷ ù¢‘Ôµ¨ ´Æƒó ‘“ ý¥‘¨ ù¢‘ƒ• ¥ ö— üõ ø ¢¤¢ ¥‘ƒ÷ ùÀª Âî£

‘“ ‚î ´¨ ßØÞõ ¤¢ ´ú› éÂð ×þ ¤¢ ´¨ ¢úÈõ Âþ ¥ ñ‘·õ ¤¢ ‚î ¤ Î÷‘Þû

´¨ ¼®ø Àª‘“ À€±Þû ƒè éÂ𠂻÷‘€.¢Âî Çþ‘Þƒ• ¤ §Ž¤ ô‘Þ— öµ÷ §b¤ ×þ

.¢ª ü÷¡Âê DF-Search ñø ¤ üµÆþ‘“ éÂð ý‘û ‚Ôó


b õ ¢À㗠ù¥À÷ ‚“ ‚î

1
6
- R
2 3 4
6I 6 6
R ?
5 w 6 7  8
I

-
1
~
2 4
 o
5
? ?
3 q 8
K ?
? 6
7

1. DFS(1)
2. DFS(2)
3. DFS(3)
4. DFS(4)
5. DFS(8)
6. DFS(7)
7. DFS(5)
8. DFS(6)
‘û éÂð Çþ• .5 ÛÊê 152

‚µÈ• ‘“ ý¥‘¨ ù¢‘ƒ• 1.1.5

Procedure DFS2(v)
p ←empty-stack
Mark[v]←visited
push v on to p
while p is not empty do
while there exites a node w adjacent to top(p)
such that mark[w]6=visited do
mark[w]← visited
push w on to p{w is the new top(p)}
pop(p)
(Breath First Search) BFS ݵþ ¤Úó 2.5
procedure BF-Search(G)
for each v ∈ N do Mark[v]← not-visited
for each v ∈ N do
if Mark[v] 6= visited then
BFS(v)

procedure BFS(v)
Q← empty-queue
mark[v] ← visited
enqueue v into Q
While Q is not empty do
u ← first(Q)
dequeue u from Q
for each node w adjacent to u do
if Mark[w] 6= visited then
Mark[w] ←− visited
enqueue w into Q
ö¢Â î visit‚ “ à ƒ ÷ ‘ û ñ‘ þ ô‘ Þ — Â þ ¥ ´ ¨ O(|N | + |A|) ¥  µ Þ î Ý µ þ ¤  Ú ó ö‘ õ¥

.À÷¤À÷
153 TOPOLOGICAL SORT ý¦ó•— ý¥‘¨ °—Âõ .3.5

: Ýþ ¤¢ ñø ñ‘·õ ýÂ“ ‚÷Þ÷ ö€ä ‚“

node visited Q

1. 1 16 , 2 , 3, 4
| {z }
2. 2 26 , 3, 4, 5 ,6
|{z}
3. 3 36 , 4, 5, 6
4. 4 46 , 5, 6, 7 ,8
|{z}
5. 5 56 , 6, 7, 8
6. 6 66 , 7, 8
7. 7 76 , 8
8. 8 86

Topological Sort ý¦ó•— ý¥‘¨ °—Âõ 3.5


üó‘þ — °Öä ‚“ —Ü› §b¤ ¥) ´¨ ¤ ø¢ Àì‘ê ø ¤¢ ´ú› ‚î Ýþ ¤¢ üêÂð Àƒ€î ­Âê

‚“ ‚î ´¨ ö éÂð ßþ §Ž¤ ý¥‘¨ °—Âõ ýÂ“ ©ø ¤ ×þ –¤ ¬ ßþ ¤¢ ( Ýþ ¤À÷

: ݃€î ÛÞä Âþ ¥ ùƒª

´¨ ÕÞä ñø Çþ‘Þƒ•) .´¨ ÂÔ¬ ö ý¢ø ¤ ø ‚›¤¢ ‚î ݃“‘þ üõ ¤ ü¨b¤ Àµ“

Ûʵõ ý‘û ñ‘þ ô‘Þ— ø §b¤ ö ų¨ ù¢Þ÷ ’‘¿µ÷ ¤ §b¤ ö ( ‚ê‘® ¯Âª ‘“ ‘õ

¤À Ö ÷ ¤ À ÷ø ¤ ß þ . Ý ƒ ûÀ ƒ õ ‚ õ¢ ¤ Õ “‘ ¨ ‚ þø ¤ f¢À ¹ õ . Ý ƒ € î ü õ éÁ Ÿ ¤ ö ‚ “

¥ ‚µêÂð“ ©ø ¤ ßþ ‚î ´¨ üúþÀ“ . À÷ª ù¢Þƒ• éÂ𠧁Ž¤ ô‘Þ— ‘— ݃ûÀƒõ ‚õ¢

ö‘õ¥ ‚¹ƒµ÷ ¤¢. µȃ“ ùÀ€€î ¢øÀ½õ ÍþÂª ‘“ ‘õ , ´¨ ö ‚“‘Èõ ‘f Öƒì¢ ø DFS ©ø ¤
.´¨ θ(1) ¥ ùÀ€€î ¢øÀ½õ ÍþÂª ö‘õ¥ Âþ ¥ ´¨ θ(|N | + |A|) ÀŸ ¤¢ ö ýÂ›

¥ Óܵ¿õ ü›ø ¡ À€ ø Àª‘“ üÞ÷ ¢Âê ‚“ Âʽ€õ ’› ‚î ´ª¢ ‚›— Àþ‘“

: Ýþ ¤¢ ñ‘·õ ö€ä ‚“ . ݃ª‘“ ‚µª¢ ݃÷— üõ ݵþ ¤ Úó ßþ

-6
*3
j
1 5
3
s 2? -4

1,3,2,4,6,5 :Àª‘“ –¤ ¬ ßþ‚“ À÷— üõ ‘û ü›ø ¡ ¥ üØþ ñ‘·õ ßþ ýÂ“


‘û éÂð Çþ• .5 ÛÊê 154

Bellman Ford ݵþ ¤Úó 4.5

Bellmanݵþ ¤ Úó ¢ª ù¢‘Ôµ¨ ü“‘þ ƒÆõ ýÂ“ À÷— üõ ‚î üþ‘û ݵþ ¤ Úó ¥ üØþ

À÷µƒõ ö ý‘úó‘þ °ÆÂ“ ‚î ¤¢ ´ú› éÂð ×þ ýÂ“ ݵþ ¤ Úó ßþ . ´¨ Ford
¤‘Ø“ §Ž¤ Âþ‘¨ ‘— ⱀõ ùÂð ô‘÷ ‚“ üûÂ𠥐 ƒÆõ ßþ µû‘—î ßµê‘þ ´ú› Àª‘“ üԀõ

üԀõ ö¥ ø ‘“ ý ¤ ø¢ ‚î À€î ¤‘î ´¨¤¢ À÷— üõ ü÷‘õ¥ ݵþ ¤ Úó ßþ ‚µ±ó . ¢ø ¤ üõ

.Àª‘“ ‚µªÀ÷

. À€ª‘“ üõ ⱀõ §¤ s ø G=< V, E > ø éÂ𠧁Ž¤ ‚äÞ¹õ V[G]

IN IT ILIZE − SIN GLE − SOU RCE(G, s)


1. for each vertex v ∈ V [G] do
2. d[v] ← ∞
3. π[v] ← N IL
4. d[s] ← 0

RELAX(u, v, w)
1. if d[v] >d[u]+w[u,v] then
2. d[v] ← d[u] + w[u, v]
3. π[v] ← u

BELLM AN − F ORD(G,w,s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i← 1 to (|V [G]|-1) do
3. for each edge (u, v) ∈ E(G) do
4. RELAX(u,v,w)
5. for each edge (u, v) ∈ E(G)
6. if d[v] > d[u]+w(u,v) then
7. return FALSE
8. return TRUE //θ(|V ||E|)

: Ýþ ¤¢ ñ‘·õ ö€ä ‚“


155 DAG ݵþ ¤Úó .5.5

5 5
r ^x r R x
∞ ∞ 6 ∞
6> Y
-2
6 3 I-2  6  6
8 -3 8 -3
s 0y2 7 s 0 y2 7
-4 -4
j ? R 7s ? R
7 ∞ -∞ 7 -∞
9 y 9
y z z
(a) (b)

5 5
r ^ x r ^ x
6 -2 4 2 4
6 I 7 6 6 3 I -2  6
8 -3 8 -3
s 0 i 2 7 s 0 y2 7
? -4 w 7 s ? -4
-R
7s 7 -2 7 2
9 y 9 z
y z
(c) (d)

5
r x
w
2 4
63 I-2  6
8 -3 7
s 0 y2 ? -4R
q - -2
7 7
y 9 z
(e)

DAG ݵþ ¤Úó 5.5


éÂð ×þ ¤¢ §Ž¤ ח ח ‘— É¿Èõ ùÂð ×þ ¥ ƒÆõ ßþ — ù‘—î ݵþ ¤ Úó ßþ

.Àû¢ üõ ‘õ ‚“ ¤ ¤ ø¢ Àì‘ê ¤¢ ´ú›

DAG-Shortest-Path(G,w,s)
1. Topologically sort the vertics of G
2. initialize-single-source(G,s)
3. for each vertex u,taken in Topologically sorted order do
for each vertex v∈Adj[u] do
RELAX(u,v,w)
‘û éÂð Çþ• .5 ÛÊê 156

ý¥‘ ¨ ° — õ ´ Ü ä ‚ “ ö‘ õ¥ ø ´ ¨ –¤ ø‘ ¹ õ ´ Æ ƒ ó ‘ “ Ý µ þ ¤  Ú ó ß þ ý¥‘ ¨ ù¢‘ ƒ •

θ(|V | + |E|)“Â“
:Àƒ€î ´ì¢ Âþ ¥ ñ‘·õ ‚“ .´¨ ñ‘Øþ¦ ó•—

6 1
r s t ~
x y sz
∞ 5- 0 2- ∞ 7- ∞ -1- ∞ -2 -∞
* 4 > >
3
2
6 1
r s x y
7-s -1- ∞ -2-~
t z
∞ 5- 2- ∞
0 ∞ ∞
3 4 * 
3
6 2
1
s
r
2-
t sx y ~z
∞ 5- 0 2 7 - 6 -1- ∞ -2- ∞
> 4  
3
6 2 1
r s x y z
7 -s
5- t s
∞ 2- -1- -2-
0 2 6 6 4
 4 * >
3 2
6 1
r s y z
t s x -1 -2~
∞ 5-
0
2-
2
7- 6 - 5 - 4
3 4 3 3
3 2
6 1
r s t j x y jz
∞ 5 - 0 2- 2 7 - 6 -1- 5 -2- 3
3 > 
4
3
2
6 1
r x z
-2 s
s t s y
-1-
∞ 5- 0 5 -3
2- 7-
2 6
* * 3
3 4
2
6 ÛÊê

¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷

LOOP INVARIANT 1.6


? ´Æƒ LOOP INVARIANT

. ´¨ üÜØÈõ ¤‘ƒÆ“ ¤‘î Àµê üõ üì‘Ô— ‚ ‚Öܟ Âû¤¢ ݃÷À“ ‘õ ‚î ßþ

,À÷ÂþÁ• üõ ö‘þ‘• ÂÑ÷ ¢¤ õ éÀû ‚“ öÀƒ¨¤ öøÀ“ ‚î üþ‘û ‚Öܟ ‘þ ö‘þ‘• ü“ ý‘û ‚Öܟ

Loop Invariant ßþ ‚€ƒõ¥ ßþ ¤¢.À€µÆû —ƒ³õ‘î üÆþ÷ ‚õ‘÷“ ¤¢ ¸þ¤ ÛØÈõ ×þ

.À€î üõ ×Þî ‘õ ‚“ ‚î ´¨

´¨¤¢ ݵþ ¤ Úó ×þ ‘þ ݃÷À“ ‘— ݃þ‘Þ÷ üõ ù¢‘Ôµ¨ ‘û Loop Invariant ¥ ‘õ âìø ¤¢

.ƒ¡ ‘þ À€î üõ ¤‘î

‘û ƒçµõ ߃“ ‚Ф ‚î ´¨ ‚µƒó ‘õÂê –¤‘±ä À€ üû‘ð ‘þ ×þ Loop Invariant
߃€»Þû ø ‚Öܟ Û¡¢ ö‘õ¥ Âû ,‚Öܟ ýÂ› ¥ Û±ì ‚î À€î üõ ö‘ƒ“ ¤‘õ ý‚õ‘÷“ ¤¢

.À÷‘õ üõ ü쑓 ´¨¤¢ ‚Öܟ ý‘úµ÷ ¤¢

ø LOOP INVARINT ‚ ƒ óø Ý ƒ û‘ Ô õ ‚ î ´ ¨ ö  “ ü ã ¨ ´ Þ Æ ì ß þ ¤¢

ñ‘·õ À€ Ã÷ ‘úµ÷ ¤¢ ø ¢ª ù¢¢ ¼ƒ®— Âʵ¿õ ü܃¡ –¤ ¬ ‚“ ö ¥ ù¢‘Ôµ¨ ¥ ÂÏ

. ´¨ ùÀª ù¢¤ ø ö ¥ ù¢‘Ôµ¨ ¥ Óܵ¿õ

ùÀ û‘ È õ ¤ Loop Invariant ¥ ù¢‘ Ô µ ¨ ü Ü î ý Ú ó × þ ´ Þ Æ ì ß þ ¤¢


¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 158

:Àƒþ‘Þ÷ üõ

...
// the Loop Invariant must be true here
while ( TEST CONDITION ) {
//top of the loop
...
//bottom of the loop
//the Loop Invariant must be true here
}
// Termination + Loop Invariant ⇒ Goal
...

üÞ÷ ý ƒƒç— Loop Invariant üµ¨¤¢ À€€î ƒƒç— ‚Öܟ ¤¢ ‘û ƒçµõ Âþ¢‘Öõ ‚î üµìø

–¤‘±ä ‚î ¢Þ÷ ‚›— ‚µØ÷ ßþ ‚“ Àþ‘“ Loop Invariant ö¢Þ÷ É¿Èõ ýÂ“ .À€î

‘õ À€÷‘õ üõ ü쑓 ´¨¤¢ ø ´“‘™ ‚Öܟ ¤Âؗ Âû ¥ À㓠ø Û±ì ‚î À÷¤¢ ¢›ø ý ¤‘ƒÆ“

‘“ ‚î ßþ — Ýúõ ö ¥ ø ´¨ ͱ— Âõ ‚Öܟ ¤‘î ‘“ ‚î ´¨ Loop Invarint ü—¤‘±ä

ö ¥ ¤‘ѵ÷ ¢¤ õ ø ù¿ó¢ ‚¹ƒµ÷ ‚“ ¤ ‘õ Loop Invariant üµ¨¤¢ ‚Öܟ ßµê‘þ ö‘þ‘•


: ݃þ‘Þ÷ üõ É¿Èõ ¤ ž…ܐ À€ ´ÞÆì ßþ ¤¢ .À÷‘¨¤ üõ ‚Öܟ

.Àª‘“ ´¨¤¢ ‚Öܟ ýÂ› ¥ Û±ì Àþ‘“ ‚î ´¨ ýÝ ö:Pre-condition

À÷‘õ üõ ü쑓 ´¨¤¢ ‚Öܟ Ûõ‘î ýÂ› ¥ À㓠‚î ´¨ ýÝ ö :Post-condition

‚¹ƒµ÷ ö‘Þû ‚î Àþ üõ ´¨¢ ‚“ Loop Invariant ¤¢ ‚Öܟ ¥ šø ¡ ¯Âª ý¥ ‚“ ø

.´¨ ‚Öܟ ¥ ¤‘ѵ÷ ¢¤ õ

¥ šø  ¡ â ìø ¤¢ ø À € î ü õ ñ  µ € î ¤ ‚ Ö Ü Ÿ ýÂ › ‚ î ´ ¨ ü Ï ª :Loop Variant


.Àþ‘Þ÷ üõ ñ µ€î ¤ ‚Öܟ ý‚õ¢ ‘þ‚Öܟ

: ݃û¢ ¤Âì ‚›— ¢¤ õ ¤ Âþ ¥ –‘Ø÷ Àþ‘“ ‚Öܟ ×þ ¤‘î ö¢Âî ם ýÂ“

. ´¨ ´¨¤¢ ¢ª ¥‘è ‚Öܟ ‚Ø€þ ¥ Û±ì Pre-condition Ýþª ßÿÞÎõ ‹1

.´¨ ´¨¤¢ Pre-condition ýÂ“ Loop Invariant ݃û¢ ö‘È÷ ‹2

.¢¤À÷ Loop Invariant üµ¨¤¢ “ ü±÷‘› ™ Loop Variant ýÂ› ݃û¢ ö‘È÷ ‹3

.´¨ ´¨¤¢ ‚Öܟ ýÂ› Âû¥ À㓠Loop Invariant ݃û¢ ö‘È÷ ‹4
159 LOOP INVARIANT .1.6

ü µ ¨¤¢  “ ´ ó„¢ Loop Invariant ‚ Ö Ü Ÿ ö‘ þ‘ • ¢Â ¹ õ ‚ “Ý ƒ û¢ ö‘ È ÷ ‹5


.¢¤¢ Post-condition

.Àû¢ üõ Çû‘î ‘þ ÇþÃꐐ¤ Loop Variant ‚Öܟ ¤Âؗ Âû ݃û¢ ö‘È÷ ‹6

×þ ¥ Û±ì loop Invariant ÂðÝƒû¢ ö‘È÷ Àþ‘“ ô¤‘ú ¢¤ õ üµ¨¤¢ ö¢¢ ö‘È÷ ýÂ“

.À÷‘õ üõ ´¨¤¢ Ã÷ ýÀ㓠¤Âؗ ¥ Û±ì ´¨ ´¨¤¢ ‚Öܟ ¤Âؗ

‚ î ´ ¨ ß þ ö ø Ý ƒ þ‘ Þ ÷ ‚ › — ü ± ó‘ › ‚ µ Ø ÷ ‚ “ À þ‘ “ ´ Þ Æ ì ß þ ¤¢

‚þ‘• 2 ¢¤ õ ö¢¢ ö‘È÷ ‘“ âìø ¤¢. ´¨ ü®‘þ ¤ ýÂÖµ¨ ‚“‘Èõ 1‹4 ۟Âõ ýÂ›

–ø‘Ô— ßþ ‘“ Ýþ ù¢Þƒ• ¤ ÂÖµ¨ ý‘û ô‘ð 4 ¢¤ õ ö¢¢ ö‘È÷ ‘“ø Ýþ ù¢‘ú÷ ‘€“ ¤ ÂÖµ¨

‘ “ â ìø ¤¢. Ý ƒ ª‘ “ ‚ µ ª¢ ¢øÀ ½ õ ýÂ Ö µ ¨ ၠ÷ × þ ‘ õ ‚ î ¢ ª ü õ ¶ ä‘ “ 5 ¢¤  õ ‚ î

.¢¢Âð üõ Ó쁵õ ÂÖµ¨ ‚Öܟ ßµê‘þ ö‘þ‘•

: ݃û¢ üõ ö‘È÷ ¤ ë ê ۟Âõ ô‘¹÷ ñ‘·õ À€ Âî£ ‘“ ñ‘Ÿ

:Àþ‘Þ÷ üõ ‚±¨‘½õ ¤ n‘— 1 ¥ ¼ƒ½¬ ¢Àä áÞ¹õ ‚î üÞµþ ¤ Úó(1

1. int sum=0;
2. int k=0;
3. while(k < n){

4. k++;

5. sum+=k;
6. }
........................................................................................................
precondition : sum = 0, k = 0

P
n
postcondition : sum = i
i=1

P
k
loop invariant : sumk = i
i=1

INITIALIZATION:
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 160

´¨ ´¨¤¢ precondition ýÂ“ loop invariant ¢¢Âð üõ ùÀû‘Èõ ‚î ¤ Ï ö‘Þû

:
0
P
k = 0 ⇒ sum = i = 0 = sum
i=1

MAINTENANCE:

Àª‘“ ´¨¤¢ ‚Öܟ ¤Âؗ ¥ ô j ‚ÜŸÂõ ýÂ“ loop invariant ÂðÝƒû¢ üõ ö‘È÷ ñ‘Ÿ

: ´¨ ´¨¤¢ Ã÷ ‚Öܟ ¤Âؗ ¥ ô j+1 ‚ÜŸÂõ ýÂ“


kj+1
P
sumj = j , kj+1 = kj + 1
j=1
kj kj kj+1
P P P
sumj+1 = sumj + kj+1 = ( j) + kj+1 = ( j) + kj + 1 = j
j=1 j=1 j=1

TERMINATION:

–¤‘±ä loop invariant ö ý¥ ‚“ ‚î ݃þ‘Þ÷ üõ ם ¤ ‚Öܟ ¥ šø ¡ ¯Âª ñ‘Ÿ

ý¥ ‚“ ‚îÀª‘“ üõ k=n ‚Öܟ ¥ šø ¡ ¯Âª.Àû¢ üõ ‘õ ‚“ ¤ postcondition


: Ýþ ¤¢ ö

P
n
sum = i ≡ postcondition
i=1

:Àƒþ‘Þ÷ üõ ‚±¨‘½õ ¤ ÂÔ¬ ¥ µð¤ Óø ¼ƒ½¬ ý¢Àä Ûþ ¤ µî‘ê ‚î üÞµþ ¤ Úó(2

1. int factorial(n){

2. i =1;

3. fact =1;

4. while(i ! = n){

5. i++;

6. fact=fact×i; }

7. return fact;

8. }

.............................................................................................................
precondition : n ≥ 1
161 LOOP INVARIANT .1.6

loop invariant : f act = i !


postcondition : f act = n !
........................................................................................
INITIALIZATION:
i = 1 ⇒ f act = 1! = 1 ⇒ f act = i !
MAINTENANCE:
f act0 = j 0 ! , j = j 0 + 1 , f act = f act0 × j
⇒ f act = j 0 ! × j = j 0 × (j 0 + 1) = (j 0 + 1)! = j! ⇒ f act = j!
TERMINATION:
i = n , f act = i! ⇒ f act = n! ≡ postcondition

‚“ ¤ precondition ¯Âª ´ƒÞû ö ¤¢ ‚î ´¨ üÞµþ ¤ Úó, ݵþ ¤ Úó ßþ : ‚µØ÷

Ýþ ƒړ ÂÔ¬ “Â“ ¤ nø ‚µêÂÚ÷ ÂÑ÷ ¤¢ ¤ n ≥ 1 Ϫ Â𐠐Âþ ¥,Àû¢ üõ ö‘È÷ ü“¡

.À÷‘¨¤ Àû¿÷ ù¿ó¢ ‚¹ƒµ÷ ‚“ ¤ ‘õ øÀª Àû¡ ¤Âؗ ¤‘“ ´þ‘ú÷ ü“ ‚Öܟ

¤ ÂÔ¬ ¥ µð¤ Ó ¼ƒ½¬ ¢Àä ø¢ ߃“ íµÈõ ‚ƒÜä ôÆÖõ ßþ µð¤ Ó ‚î üÞµþ ¤ Úó(3

:À÷¢Âð üõ “

1. int gcd(int m ,int n){

2. int mprime = m;

3. int nprime = n;

4. while(mprime ! =nprime){

5. if(mprime > nprime)

6. mprime - = nprime;

7. else

8. nprime - = mprime;}

9. return mprime;

10. }
.....................................................................................................
precondition : m, n ∈ Z +
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 162

loop invariant : gcd[m, n] = gcd[mprime, nprime]

postcondition : gcd[m, n] = mprime


...........................................................................
INITIALIZATION:

mprime = m nprime = n ⇒ gcd[m, n] = gcd[mprime, nprime]

MAINTENANCE:

gcd[m, n] = gcd[mprimei , nprimei ]

• if (mprimei > nprimei ) :


mprimei+1 = mprimei − nprimei , nprimei+1 = nprimei
⇒ gcd[mprimei+1 , nprimei+1 ] = gcd[mprimei −nprimei , nprimei ] =
gcd[mprimei , nprimei ] = gcd[m, n]

• else :
nprimei+1 = nprimei − mprimei , mprimei+1 = mprimei
⇒ gcd[mprimei+1 , nprimei+1 ] = gcd[mprimei , nprimei −mprimei ] =
gcd[mprimei , nprimei ] = gcd[m, n]

TERMINATION:

mprime = nprime ⇒ gcd[m, n] = gcd[mprime, nprime] =


gcd[mprime, mprime] = mprime ≡ postcondition

:Àþ‘Þ÷ üõ ‚±¨‘½õ ¤ en ¤ Üƒ— ÍƓ ñø ý‚Üޛ k ‚î üÞµþ ¤ Úó(4

1. double TaylorExp(double n ,int k){


163 LOOP INVARIANT .1.6

2. double result =1;

3. int count =0;

4. int denom=1;

5. while (count<k){

6. count ++;

7. denom×=count;

8. result+=pow(n,count)/denom;

9. }

10. return result;


11. }
......................................................................................................
precondition : n ∈ Z k ∈ N, k > 0

2
loop invariant : result = 1 + n + n2! + · · · + ncount
count! , denom = count!

2
postcondition : 1 + n + n2! + · · · + nK! = result
K

...............................................................
INITIALIZATION:
0
count = 0, denom = 1, result = 1 ⇒ n0! = 1 = result

MAINTENANCE:
2
resulti = 1 + n + n2! + · · · + ncounti
counti ! , denomi = counti !

⇒ denomi+1 = denomi ×counti+1 = (counti !)×counti+1 = (counti+1 )!,

2
= 1 + n + n2! + · · · + ncounti ! + (count
counti+1 counti counti+1
n n
resulti+1 = resulti + (count i+1 )! i+1 )!

TERMINATION:
2
count = k ⇒ result = 1 + n + n2! + · · · + nK
K! ≡ postcondition
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 164

ô‘¹÷ ¤ üãƒ±Ï ¢Àä ø¢ âޛ Âþ ¥ ݵþ ¤ ÚóÀƒû¢ ö‘È÷ loop invariant ¥ ù¢‘Ôµ¨ ‘“(5

:Àû¢ üõ

function add(y,z)

comment return y + z, where y,z ∈ N

1. x := 0 ; c := 0 ; d := 1 ;
2. while(y > 0 ) ∨ (z > 0 ) ∨ (c > 0 )do
3. a := y mod 2;
b :=z mod 2;
4. if a ⊕ b ⊕ c then x : = x+d ;
5. c := (a ∧ b) ∨ (b ∧ c) ∨ (a ∧ c);
6. d :=2 d; y := b y /2 c;
z:=b z /2 c;
7. return(x)

.......................................................................................................
loop invariant : (yj + zj + cj )dj + xj = y0 + z0

: Ýþ ¤¢ ‚Öܟ áø ª ¥ Û±ì Ýþ ƒړ ÂÑ÷ ¤¢ z0 ø y0 ¤ ‚ƒóø z ø yÂð

x0 = 0, c0 = 0, d0 = 1 ⇒
(yj + zj + cj )dj + xj = (y0 + z0 + 0) × 1 + 0 = y0 + z0

: ü€ãþ ´¨ ¤Âì“ ô j ý‚ÜŸÂõ ýÂ“ loop invariant ݃þ‘Þ÷ üõ ­Âê ñ‘Ÿ

(yj + zj + cj )dj + xj = y0 + z0

: Ýþ ¤¢ ‚Öܟ ñø ¤ Õ±Ï ßƒ€»Þû

aj+1 = yj mod 2 , bj+1 = zj mod 2


yj+1 = byj / 2 c , zj+1 = bzj /2c , dj+1 =2×dj

¥‚ Ü ŸÂ õ ß þ ¤¢ ‚ î cj+1 ¢ ª ü õ ùÀ û‘ È õ Ý ¹ € • Í ¡ ¤¢ ‚ î ¤  Ï ö‘ Þ û ß ƒ € » Þ û

ø bj+1 ø aj+1 ¥ ‘— ø¢ ‚î ´¨ ×þ üµìø ‘ú€— Àþ üõ ´¨¢ ‚“ cjbj+1 ø aj+1


ø

: ´ª÷ Âþ ¥ –¤ ¬ ‚“ ¤ cj+1 ö— üõ ŕ,À€ª‘“ ×þ cj


165 LOOP INVARIANT .1.6

cj+1 = b(aj+1 + bj+1 + cj ) / 2 c

¢›ø ‚“ ¤ xj+1 ø ùÀª âޛ dj ‘“ xj üµìø ô¤‘ú Í¡ Փ‘Îõ Ýû –¤ ¬ ߃Þû ‚“

–¤ ¬ ‚“ ¤ xj+1 ö— üõ ŕ Àª‘“ ×þ aj+1 ⊕ bj+1 ⊕ cj –¤‘±ä ‚î À÷¤ ø üõ

:¢Þ÷ Óþ Â㗠Âþ ¥

xj+1 = xj + dj ((aj+1 + bj+1 + cj ) mod 2)

‚ “ø Ý ƒ õ‘ ÷ ü õ (*) ‚ Î “¤ ¤ ö ‚ î Â Ú þ¢ Ý ú õ ‚ Î “¤ × þ ‚ “ ¥‘ ƒ ÷ –‘ ± ™ ‚ õ¢ýÂ “

: Ýþ ¤¢ Ã÷ ´¨ Âþ ¥ –¤ ¬

2b n /2c + ( n mod 2)= n ∀n ∈ N (*)

: Ýþ ¤¢ø ‚µª÷ ¤ loop invariant ýø‘Æ— ñø éÂÏ ñ‘Ÿ

(yj+1 + zj+1 + cj+1 )dj+1 + xj+1 =

(byj /2c + bzj /2c + b(yj mod 2 + zj mod 2 + cj )/2c) × 2dj + xj +

dj ((yj mod 2 + zj mod 2 + cj ) mod 2) = (byj /2c + bzj /2c) × 2dj

+xj + (b(yj mod 2 + zj mod 2 + cj )/2c) × 2dj +

dj ((yj mod 2 + zj mod 2 + cj ) mod 2) (∗) (byj /2c + bzj /2c) × 2dj

+xj + dj (yj mod 2 + zj mod 2 + cj ) = byj /2c × 2dj +

dj (yj mod 2) + bzj /2c × 2dj + dj (zj mod 2) + cj × dj + xj (∗)

(yj + zj + cj ) dj + xj = y0 + z0

¤Â Ø — k ¥ À 㠓  ð,À € î ü õ À ƒ • ‚ Þ —‘ ¡ ‚ Ö Ü Ÿ ‚ î Ý þ  ƒ ð ü õ Â Ñ ÷ ¤¢ ¤ ü ÷‘ õ¥ ñ‘ Ÿ

: Ýþ ¤¢ loop invarint Õ±Ï À“‘þ ‚Þ—‘¡ ‚Öܟ

(yk + zk + ck )dk + xk = y0 + z0

À€“‘þ üõ Çû‘î bz/2cø by/2c Âþ¢‘Öõ ‚“ ¤Âؗ Âû ¤¢ zø y ¤‘“ Âû ö ߃€»Þû

: Ýþ ¤¢ ¤Âؗ ߃õ k ¥ À㓠ü€ãþ ‚Öܟ ¥ šø ¡ ö‘õ¥

yk = zk = ck = 0 ⇒ xk = y0 + z0
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 166

ùÀ÷¢Âð “ ‚î ý x ¤ÀÖõ À¨¤ üõ ö‘þ‘• ‚“ ݵþ ¤ Úó üµìø ‚î ¢ª üõ ùÀû‘Èõ ŕ

. ´¨‘õ ý‚ƒóø zø y áÞ¹õ “Â“ ¢ª üõ

ô‘¹÷ ¤ üãƒ±Ï ¢Àä ø¢ ’® Âþ ¥ ݵþ ¤ ÚóÀƒû¢ ö‘È÷ loop invariant ¥ ù¢‘Ôµ¨ ‘“(6
:Àû¢ üõ

function multiply(y,z)

comment return y z , where y , z ∈ N

1. x :=0
2. while (z >0)do
3. x :=x + y × (z mod 2);
4. y := 2 y; z :=b z/2 c;
5. return(x)

........................................................................................................
loop invariant : xj + yj × zj = y0 × z0

x0 = 0 Àµ“ ¤¢ , Ýþ ƒð üõÂÑ÷ ¤¢ z0 ø y0 ¤ zø y ‚ƒóø Âþ¢‘Öõ Û±ì ñ‘·õ Փ‘Îõ


: Ýþ ¤¢ ø ù¢“

x0 = 0 ⇒ xj + yj × zj = x0 + y0 × z0 = y0 × z0

: ü€ãþ ´¨ ¤Âì“ ô j ý‚ÜŸÂõ ýÂ“ loop invariant ݃þ‘Þ÷ üõ ­Âê ñ‘Ÿ

xj + yj × zj = y0 × z0

: Ýþ ¤¢ ‚Öܟ ñø ¤ Õ±Ï ßƒ€»Þû

xj+1 = xj + yj (zj mod 2) yj+1 = 2 yj zj+1 = bzj /2c

: Ýþ ¤¢ ŕ

xj+1 + yj+1 × zj+1 = xj + yj (zj mod 2)+2yj (bzj /2c) =


c))(∗)
xj + yj ((zj mod 2)+2bzj /2 =
xj + yj × zj = y0 × z0
167 LOOP INVARIANT .1.6

k ¤¢ ë‘Ô— ßþ Â𐠂î Àª Àû¡ ÂÔ¬ “Â“ z ¤ÀÖõ À“‘þ üõ ö‘þ‘• ‚Öܟ ‚î ‘úµ÷ ¤¢ø
: Ýþ ¤¢ Àû¢  ¤ ‚Öܟ ¤Âؗ ߃õ

zk = 0, xk + yk × zk = y0 z0 ⇒ xk + yk × zk = xk + yk × 0 = xk = y0 × z0

ùÀ ÷¢Â 𠓠‚ î ý x ¤À Ö õ À ¨¤ ü õ ö‘ þ‘ • ‚ “ Ý µ þ ¤  Ú ó ü µ ìø ‚ î À ª ùÀ û‘ È õ Å •

ø ’ÜÎõ ‚¹ƒµ÷ ö‘Þû ßþ ø ¢“ Àû¡ z ø y ý‚ƒóø Âþ¢‘Öõ ’® “Â“¢ª üõ

. ´¨‘õ ¤‘ѵ÷ ¢¤ õ

ùÀ÷‘õ ü쑓 ø ´ÞÆì š¤‘¡ Âþ ¥ ݵþ ¤ ÚóÀƒû¢ ö‘È÷ loop invariant ¥ ù¢‘Ôµ¨ ‘“(7

:À÷¢Âð üõ “ ¤ üãƒ±Ï ¢Àä ø¢ ݃Æ֗

function divide(y,z)

comment return q, r ∈ Nsuch that y = qz+r

and r < z , where y , z ∈ N

1. r :=y; q :=0; w :=z;

2. while w ≤y do w :=2 w;

3. while w > z do

4. q :=2 q; w := b w /2 c ;

5. if w ≤ r then

6. r :=r - w; q :=q+1;

7. return (q , r)

.......................................................................................................
loop invariant : qj wj + rj = y0 , rj < wj

r = y0 ø q=0 ‚Öܟ ‚“ ¢ø ¤ ø ¥ Û±ì, Ýþ ƒð üõ ÂÑ÷ ¤¢ ¤¢ y0 ¤ y ý‚ƒóø ¤ÀÖõ


: Ýþ ¤¢ ŕ ´¨

r = y0 , q = 0 ⇒ qj wj + rj = 0 + y0 = y0

: ü€ãþ,Àª‘“ ¤Âì“ ô j ý‚ÜŸÂõ ýÂ“ loop invariant ݃€î üõ ­Âê ñ‘Ÿ


¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 168

qj wj + rj = y0 , rj < wj

‚Öܟ ø¢ ýø ¤ ý ¤Àì ´¨ ô¥„ ýÀ㓠¤Âؗ ¤¢ ‘û ƒçµõ ´ƒã®ø ߃ƒã— ýÂ“ ñ‘Ÿ

‚Öܟ ¤¢, ݃µêÂð ÂÑ÷ ¤¢ z “Â“ ¤ w ñø ‚Öܟ áø ª ¥ Û±ì,݃þ‘Þ÷ Ûõ‘— ݵþ ¤ Úó

ý‘úµ÷ ¤¢ ŕ, Àþ‘Þ÷ üõ “Â“ ø¢ ¤ w ´¨ ¤Âì“ w≤y ¯Âª ‚î üãìõ ‘— ñø

ü õ šø ¥ ý¢À ä ß ƒ € » Þ ûø ´ ¨  µ ð¤ à “ y ¥‚ î À þ ü õ ´ ¨¢ ‚ “ ýw ‚ Ö Ü Ÿ ß þ

w ´ ¨ w>z ‚ î ü ã ì õ ‘ — ‚ Ö Ü Ÿ ß þ ¤¢, Ý þ ª ü õ ôø¢ ‚ Ö Ü Ÿ ¢¤ø ñ‘ Ÿ,À ª‘ “

šø ¥ ý¢Àä bw/2c ù¤Þû ‚î ´¨ ¼®ø,¢¢Âð üõ ‚±¨‘½õ ö Óî ø ùÀª ÓÊ÷

šø ¥ z Âð, ݃µêÂð ÂÑ÷ ¤¢ w = z ‘õ Âõ ýÀµ“ ¤¢ ‚î ´¨ Âϑ¡ ßþ ‚“ ßþ ø ´¨

¤‘ “  û ‘ “ø À ÷‘ õ ü õ ü ì‘ “ šø ¥ à ƒ ÷ öÀ ª  “Â “ ø¢ ¤‘ “  û ‘ “ ø ù¢ “ šø ¥ à ƒ ÷ w À ª‘ “

Àª‘“ ¢Âê z ¤ÀÖõ Âð ñ‘Ÿ, Ýþ ƒړ ùÀþ¢‘÷ ¤ Óî ´õ…ä ݃÷— üõ Ã÷ öÀª ÓÊ÷

‚“ w ßþ Ã÷ ôø¢ ‚Öܟ ¤¢ ‘õ , Ýþ ¤¢ ¤‘ƒµ¡ ¤¢ ü›ø ¥ ý w ñø ‚Öܟ ý‘úµ÷ ¥ Àã“

ý¢Âê ¤ÀÖõ Àû¿“ Âð ø ¢¢Âð üõ ‚±¨‘½õ ö Óîø ùÀª ÓÊ÷ ¤‘“ Âû ùÀõ ´¨¢

‚Öܟ ý ¤Âì“ ¯Âª ‚î ´¨ üó ‘Ÿ ¤¢ ßþ ø ¢“ Àû¡ z ¢¡ ‘“ “Â“ Àª‘“ ‚µª¢

.Àª‘“ üõ w/2‘“ “Â“ bw/2c ¤ÀÖõ ‚Öܟ ßþ ¤¢ ¤Âؗ ¤‘“ Âû ¤¢ ŕ, ´¨ w>z
qj+1 = 2qj , wj+1 = bwj /2c : Ýþ ¤¢ ô j + 1 ¤Âؗ ¤¢ ñ‘Ÿ

: ݃þ‘Þ÷ ü¨¤ “ ¤ ´ó‘Ÿ ø¢ Àþ‘“ ñ‘Ÿ

: Ýþ ¤¢ Àª‘±÷ ¤Âì“ ݹ€• Í¡ ¯Âª Âð (a)





1: rj+1 = rj , qj+1 wj+1 + rj+1 =





 2qj bwj /2c + rj = 2qj (wj /2) + rj = qj wj + rj = y0
wj+1 > rj ⇒





2: wj+1 > rj ⇒ rj < wj+1 , rj+1 = rj




⇒ rj+1 < wj+1

: Ýþ ¤¢ Àª‘“ ¤Âì“ ݹ€• Í¡ ¯Âª Âð (b )


169 LOOP INVARIANT .1.6




1: rj+1 = rj − wj+1 = rj − bwj /2c, qj+1 =






 qj+1 + 1 = 2qj + 1 ⇒ qj+1 wj+1 + rj+1 =





(2qj + 1)bwj /2c + rj − bwj /2c =
wj+1 < rj ⇒





 qj wj + bwj /2c + rj − bwj /2c = y0









2: wj+1 < rj , rj+1 = rj − wj+1 (1)

ö‘ û “ ¥ ¤‘ î ß þ ýÂ “, rj+1 < wj+1 Ý ƒ þ‘ Þ ÷ –‘ ± ™ À þ‘ “ (1) ß µ ª¢‘ “ ñ‘ Ÿ


: Ýþ ¤¢ ŕ rj+1 ≥ wj+1 ݃þ‘Þ÷ üõ ­Âê ø ù¢Þ÷ ù¢‘Ôµ¨ ÓÜ¡
rj − wj+1 ≥ wj+1 ⇒ rj ≥ 2wj+1 ⇒ rj ≥ 2bwj /2c ⇒ rj ≥ wj
ý‚ÜŸÂõ ýÂ“ loop invariant ö¢“ ¤Âì“ ö‘Þû ‚î ‘õ ­Âê ‘“ ‚¹ƒµ÷ ßþ ‚î

ôø¢ ´ Þ Æ ì ° ƒ — — ß þ ‚ “ , rj+1 < wj+1 : Ý þ ¤¢ ø ‚ µ ª¢ Ë ì‘ € — ¢ “ ô j


. ´¨ ¤Âì“ Ã÷ ô j+1 ý‚ÜŸÂõ ýÂ“ Ã÷ loop invariant

ö‘þ‘• ‚“ ¤Âؗ k ¥ À㓠ݵþ ¤ Úóø ùÀª š¤‘¡ ôø¢ ý‚Öܟ ¥ üµìø Ã÷ ‘úµ÷ ¤¢

.wk = z0 Ýþ ¤¢ À¨¤ üõ

ݵþ ¤ Úó ŕ qk z0 + rk = y0 , rk < z0 : Ýþ ¤¢ loop invariant Õ±Ï Å•


.À÷¢Âð“ ‘õ ýÂ“ ¤ ¼ƒ½¬ ¢Àä ø¢ ݃Æ֗ ýùÀ÷‘õ ü쑓ø ´ÞÆì š¤‘¡

ö — ‚ “ ¤ ü Ö ƒ Ö Ÿ ý¢À ä  þ ¥ Ý µ þ ¤  Ú óÀ ƒ û¢ ö‘ È ÷ loop invariant ¥ ù¢‘ Ô µ ¨ ‘ “(8

:À÷‘¨¤ üõ üãƒ±Ï ý¢Àä

function power(y,z)

comment return yz , where y ∈ R,z ∈ N

1. x :=1;

2. while z > 0 do

3. if z is odd then x := x.y;

4. z :=bz /2 c;

5. y :=y2 ;

6. return (x)
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 170

loop invariant : xj yj zj = y0 z0

loop Õ ± Ïø ù¢ “ x0 = 1 À µ “ ¤¢ , Ý þ  ƒ ð ü õÂ Ñ ÷ ¤¢ z0 ø y0 ¤ zø y ‚ ƒ óø  þ¢‘ Ö õ


: Ýþ ¤¢ invariant

xj yj zj = y0 z0

j+1 ‚ÜŸÂõ ýÂ“ ‚Öܟ ñø ¤ Õ±Ï ø ô j ‚ÜŸÂõ ýÂ“ loop invariant §‘¨ “ ñ‘Ÿ

: Ýþ ¤¢ ô

xj yj zj = y0 z0 ,
 2
 Àª‘“ ¢Âê z Âð ⇒ xj+1 = xj yj , zj+1 = bzj /2c, yj+1 = yj

Àª‘“ šø ¥ z Âð ⇒ xj+1 = xj , zj+1 = bzj /2c, yj+1 = yj 2

: Ýþ ¤¢ ŕ

 2×bzj /2c

 Àª‘“ ¢Âê z Âð : xj+1 yj+1 zj+1 = xj yj × (yj ) =





 2×(zj −1)/2

 xj yj × (yj ) = xj yj zj = y0 z0





 2×bzj /2c

 Àª‘“ šø ¥ z Âð : xj+1 yj+1 zj+1 = xj × (yj ) =





 2×(zj )/2
xj × (yj ) = xj yj zj = y0 z0
ô k ‚ÜŸÂõ À€÷‘õ ‚ÜŸÂõ ×þ ¤¢ ‚î À¨¤ üõ ö‘þ‘• ‚“ üãìõ ‚Öܟ Ã÷ ¤‘î ý‘úµ÷ ¤¢

: Ýþ ¤¢ ŕ z=0݃ª‘“ ‚µª¢

xk × yk zk = y0 z0 , zk = 0 ⇒ xk = y0 z0

ö— ‚“ ‚ƒóø y ¢ª üõ ù¢¢ Ûþ½— ‘õ ‚“ ݵþ ¤ Úó ý‘úµ÷ ¤¢ ‚î ý x °ƒ—— ßþÀ“

.¢¢ ‘õ ‚“ ¤ ’ÜÎõ ý‚¹ƒµ÷ Ã÷ ݵþ ¤ Úó ßþ ŕ, ´¨ ‚ƒóø z
171 LOOP INVARIANT .1.6

‚ þ¤ ¤¢ ¢ › õ  þ¢‘ Ö õ  þ ¥ Ý µ þ ¤  Ú óÀ ƒ û¢ ö‘ È ÷ loop invariant ¥ ù¢‘ Ô µ ¨ ‘ “(9

:Àþ‘Þ÷ üõ âޛ ÂÚþÀØþ ‘“¤ A[1 · · · n]

function sum(A)
Pn
comment return i=1 A[i]

1. s :=0;

2. for i := 1 ton do

3. s :=s+A[i]

4. return (s)

.....................................................................................................

j
X
loop invariant : sj = A[i]
i=1

loop Õ ± Ï Ã ƒ ÷ ‘ õ ø ´ ¨ Â Ô ¬  “Â “ ‚ þ¤  ¬‘ € ä á Þ ¹ õ ‚ Ö Ü Ÿ ‚ “ ¢ø ¤ ø ¥ Û ± ì

: Ýþ ¤¢ invariant

0
X
j = 0 ⇒ s = s0 = A[i] = 0
i=1

ý‚ÜŸÂõ ýÂ“, Àª‘“ ¤Âì“ ô j ý‚ÜŸÂõ ýÂ“ loop invarint ݃€î üõ ­Âê ñ‘Ÿ
: Ýþ ¤¢ ô j+1

j
X j+1
X
sj+1 = sj + A[j + 1] = ( A[i]) + A[j + 1] = A[i]
i=1 i=1

: Ýþ ¤¢ Ã÷ ‚Öܟ ¥ šø ¡ ô‘Ú€û ¤¢

n
X
j = n ⇒ s = sn = A[i]
i=1

. ´¨ ݵþ ¤ Úó ¥ ‘õ ¤‘ѵ÷ ¢¤ õ ý‚¹ƒµ÷ ö‘Þû ‚î


¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 172

ý‚ Ü Þ › À € ¤À Ö õ þ ¥ Ý µ þ ¤  Ú óÀ ƒ û¢ ö‘ È ÷ loop invariant ¥ ù¢‘ Ô µ ¨ (10


‘“

À€î üõ ‚±¨‘½õ Horner ©ø ¤ ¥ ù¢‘Ôµ¨ ‘“¤ an xn + an−1 xn−1 + · · · a1 x + a0


:À÷ùÀª ùƒ¡£ A[0..n] ‚þ¤ ¤¢ °þÂ® ö ¤¢ ‚î

A[i] = ai f or all 0≤i≤n


function Horner(A , n)
Pn
comment return i=0 A[i].xi

1. v := 0

2. for i := n downto 0 do

3. v := A[i]+v.x

4. return (v)

..................................................................................................
n
X
loop invariant : vj = A[i]x i−j
i=j

Á ó, À þ‘ Þ ÷ ü õ ù¢‘ Ô µ ¨ ü È û‘ î for ý‚ Ö Ü Ÿ ¥ Ý µ þ ¤  Ú ó ß þ ‚ î ¢ Þ ÷ ‚ › — À þ‘ “

Ý þ ¤¢ ‚ Ö Ü Ÿ ‚ “ ¢ø ¤ ø ¥ Û ± ì ø ‚ µ ê ð Â Ñ ÷ ¤¢ „‘ “ –¤  ¬ ‚ “¤ loop invariant ‘õ

: Ýþ ¤¢ loop invariant Õ±Ï Å• j=n+1


n
X
j =n+1 ⇒v = A[i]xi−(n+1) = 0
i=n+1

: Ýþ ¤¢ ýÀ㓠ý‚ÜŸÂõ ¤¢, Àª‘“ ¤Âì“ ô j ý‚ÜŸÂõ ýÂ“loop invariantÂð ñ‘Ÿ
P
n P
n
A[i]xi−j −A[j] A[i]xi−j
vj −A[j] i=j i=j+1
vj = A[j] + vj+1 .x ⇒ vj+1 = x = x = x =

P
n P
n
A[i]xi−j−1 = A[i]xi−(j+1)
i=j+1 i=j+1

P
n
ö‘Þû ‚î j=0⇒v= A[i]xi : Ýþ ¤¢ ø ùÀª j=0 ‚Öܟ ¥ šø ¡ ô‘Ú€û ¤¢
i=0
. ´¨‘õ¤‘ѵ÷ ¢¤ õ ý‚¹ƒµ÷
173 (AMORTIZED ANALYSIS) üî…úµ¨ Ãó‘÷ .2.6

(Amortized Analysis) üî…úµ¨ Ãó‘÷ 2.6


Ãó‘÷ ¤¢ . ´¨ üî…úµ¨ Ãó‘÷ , –‘ƒÜÞä ¥ ‚äÞ¹õ ×þ Ãó‘÷ ý‘úªø ¤ ¥ üØþ

–‘ƒÜÞä ô‘Þ— ýø ¤ ý ù¢¢ ö‘Þµ¡‘¨ –‘ƒÜÞä ¥ ‚äÞ¹õ ×þ ýÂ› ö‘õ¥ üî…úµ¨

݃€î üõ ù¢‘Ôµ¨ ‚µØ÷ ßþ ö¢¢ ö‘È÷ ýÂ“ ©ø ¤ ßþ ¥ . ¢ª üõ ßت¨ ùÀª Â›

‚€þ Ãû ‚äÞ¹õ ×þ öø ¤¢ ÛÞä ×þ Âð üµŸ ´¨ םî ÛÞä Âû ͨµõ ‚€þ Ãû ‚î

Ãó‘÷ ‘õ ݃€î üõ ´±½¬ ͨµõ ø ߃Ú÷‘ƒõ ¢¤ õ ¤¢ ‘õ ‚ Âð . Àª‘“ ‚µª¢ ý¢‘þ ¥

üÞ÷ Ûõ‘ª ¤ ý ¤‘õ ý‘û ©ø ¤ ø ¢¤¢ –ø‘Ô— ߃Ú÷‘ƒõ ´ó‘Ÿ ¤¢ Ãó‘÷ ‘“ üî…úµ¨

´÷‘Þ® ¤ ´ó‘Ÿ ßþ —À“ ¤¢ ÛÞä Âû ͨµõ ýÂ› ö‘õ¥ üî…úµ¨ Ãó‘÷ ×þ . ¢ª

. À€î üõ

üî…úµ¨ Ãó‘÷ á÷


À € —¤‘ ± ä ü î… ú µ ¨ à ƒ ó‘ ÷ ý‘ û ©ø ¤ ß þ  —¢Â “ ¤‘ î  • ¥ ©ø ¤ ‚ ¨

:¥

(Aggregate Analysis) üãÞ¹— Ãó‘÷ •

(Acconting Method) ü“‘ÆŸ ©ø ¤ •

(Potential Method) ۃÆ÷‘µ• ©ø ¤ •

(Aggregate Analysis) üãÞ¹— Ãó‘÷ 1.2.6

¤¢ –‘ƒÜÞä n ¥ ‚äÞ¹õ ×þ,‘û n ô‘Þ— ý¥ ‚“ ݃û¢ üõ ö‘È÷ üãÞ¹— Ãó‘÷ ¤¢

ͨµõ ‚€þ Ãû , ´ó‘Ÿ ßþ —À“ ¤¢ ßþ “‘€“ . ¢Âƒð üõ ¤ T (n) ‘fäÞ¹õ, ´ó‘Ÿ ßþ —À“
T (n)
ýÂ“ ͨµõ ‚€þ Ãû ßþ ‚î Àƒ€î ‚›— . ´¨
n –‘ƒÜÞä Âû üî…úµ¨ ‚€þ Ãû ‘þ
¢›õ ‚äÞ¹õ ¤¢ –‘ƒÜÞä ¥ á÷ ßþÀ€ ‚î üµìø üµŸ , ´¨ 뢑¬ –‘ƒÜÞä Âû

–‘ƒÜÞä ‚“ ¤ ü—ø‘Ôµõ ͨµõ ý‘û ‚€þ Ãû ´¨ ßØÞõ ýÀ㓠©ø ¤ ø¢ ¤¢ ‘õ . À€µÆû

¤ ýÀ㓠©ø ¤ ø¢ ´ì¢ ‘õ ´¨ ù¢‘¨ ‚ Â𐠩ø ¤ ßþ . ݃û¢ ´±Æ÷ Óܵ¿õ ý‘û

Âû ‚“ «Ê¿õ üî…úµ¨ ‚€þ Ãû ×þ ۃÆ÷‘µ• ø ü“‘ÆŸ ý‘û ©ø ¤ ÛÞä ¤¢ . ¢¤À÷

. À€û¢ üõ «‘ʵ¡ ÛÞä


¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 174

. ݃€î üõ Ãó‘÷ ¤
1 ‚µÈ• ù¢¢ ö‘Þµ¡‘¨ , üãÞ¹— Ãó‘÷ ¥ ñ‘·õ ߃óø ¤¢ : 1 ñ‘·õ
¢¤ø ¤ x Âʀä : push(S, x) :¥ ´Æ—¤‘±ä À€µÆû O(1) ¥ ‚î ‚µÈ• üܬ ÛÞä ø¢

.À€Øƒõ S ‚µÈ•

.À÷¢Âð üõ“ ¤ ö , ‚µª¢Â“ ¤ S ‚µÈ• Âʀä ßþ —„‘“ : pop(S)


üõ ­Âê À÷ª üõ Â› O(1) ö‘õ¥ ¤¢ ‘û –‘ƒÜÞä ßþ ¥ ôÀî Âû ‚î üþ‘¹÷ ¥

push –‘ƒÜÞä n ¥ ‚äÞ¹õ ×þ üþ‘ú÷ ‚€þ Ãû ßþÂ“‘€“ . Àª‘“ 1 ôÀî Âû ‚€þ Ãû ݃€î

. ´¨ n , pop ø
ý„‘“ ¥ ¤ Âʀä k ‚î ݃€î üõ ‚ê‘® ‚µÈ• ‚“ ¤ multipop(S, k) ÛÞä ×þ ‘õ ñ‘Ÿ

ü õ ü ó‘ ¡ ¤ ö , À ª‘ “ ‚ µ ª¢ Â Ê € ä k ¥  µ Þ î , S ‚ µ È •  𐑠þ ø . ¢¤À ƒ õ “ S ‚µÈ•

. À€î

‚µÈ• ¤¢ ý ÂÊ€ä ºƒû Â𐠢ƒð üõ TRUE ¤ÀÖõ Stack-Empty ⓑ— Âþ ¥ Àî ‚±ª ¤¢

. À÷¢Âð üõ“ ¤ FALSE ¤ÀÖõ –¤ ¬ ßþ Âƒè ¤¢ , Àª‘±÷ ¢›õ

Multipop(S, k)
1. while not Stack-Empty(S) and k 6= 0
2. do pop(S)
3. k ←k−1

‚µÈ• ×þ ýø ¤ ¤ multipop ø pop ø push –‘ƒÜÞä n ¥ ‚äÞ¹õ ×þ ‘¹€þ ¤¢

‚µÈ• Ãþ‘¨ Âð ´ó‘Ÿ ßþ —À“ ¤¢ ‚äÞ¹õ ßþ¤¢ , ݃€î üõÃó‘÷ ´Æƒó‘¡ Àµ“ ¤¢ ‚î

ýÂ› ö‘õ¥ ´ó‘Ÿ ßþ —À“ ¤¢ . ´¨ O(n) ¥ multipop ÛÞä ‚€þ Ãû Àª‘“ n ·îÀŸ

. ¢ª üõ O(n2 ) Ûî ‚€þ Ãû ßþÂ“‘€“ ´¨ O(n) ¥ ‚µÈ• ¤¢ –‘ƒÜÞä Âû

ßþ —À“ ¤¢ ‚€þ Ãû ‚“ ‚›— ‘“ ùÀõ ´¨¢ ‚“ ‚¹ƒµ÷ ‘õ ´¨ ´¨¤¢ ۃܽ— ßþ ‚ Âð

‚äÞ¹õ ‚“ ‚›— ‘“ ݃÷µƒõ üãÞ¹— Ãó‘÷ ©ø ¤ ¥ ù¢‘Ôµ¨‘“ . ´Æƒ÷ Ýؽõ ´ó‘Ÿ

ÛÞä ‚ Âð , ´ÖƒÖŸ ¤¢ . Ýþ ¤ ø‘ƒ“ ´¨À“ µú“ üþ„‘“ öÂî ×þ –‘ƒÜÞä n Ûõ‘ª

ø push –‘ ƒ Ü Þ ä n ¥ ‚ ä Þ ¹ õ  û ‘ õ ¢Â “ ü õ ý¢‘ þ ¥ ‚ € þ à û ü þ‘ ú € — ‚ “ multipop


. ¢¤¢ ¤ O(n) ‚ € þ à û ,  · îÀ Ÿ ü ó‘ ¡ ýÀ µ “ ¤¢ ‚ µ È • × þ ýø ¤ multipop ø pop
‚ µ ª¢Â “ ‚ µ È • ¥ ¤‘ ± Ø þ À ÷ — ü õ Í Ö ê ‚ µ È • ‚ “ ¢ø ¤ ø ¤‘ “  û ý¥ ‚ “ Â Ê € ä  û ö 

×þ ýø ¤ , (multipop ßµêÂð ÂÑ÷ ¤¢ ‘“) pop ⓑ— ü÷¡Âê ¢À㗠, ßþÂ“‘€“ . ¢ª

‚ “ . ´ ¨ n  “Â “  · îÀ Ÿ ‚ î ´ ¨ push ⠓‘ — ü ÷ ¡Â ê ¢À 㠗 ‚ “ ü ú —  ƒ è ‚ µ È •

stack 1
175 (AMORTIZED ANALYSIS) üî…úµ¨ Ãó‘÷ .2.6

. ¢Âƒð üõ ¤ O(n) Ûî ö‘õ¥ multipop ø pop , push n ¥ ‚äÞ¹õ Âû , n Âû ý¥

‚€þ Ãû , üãÞ¹— Ãó‘÷ ¤¢ . ´¨


O(n)
n = O(1) ÛÞä Âû ßت¨ ‘þ ߃Ú÷‘ƒõ ‚€þ Ãû

ßþ ¤¢ ßþÂ“‘€“ . ´¨ ÛÞä ö ͨµõ ‚€þ Ãû ö‘Þû âìø ¤¢ ÛÞä Âû ýÂ“ üî…úµ¨

. ´¨ O(1) ‚µÈ• ÛÞä 3 Âû üî…úµ¨ ‚€þ Ãû ñ‘·õ

ý‘û ©ø ¤ ¥ ‘õ Ýþ ù¢Âî ‚±¨‘½õ ¤ ͨµõ ‚€þ Ãû ‘õ ‚ Âð ‚î ¢ª üõ Àƒî‘b— ù¤‘“ø¢

. Ýþ ù¢ÂØ÷ ù¢‘Ôµ¨ ý ¤‘õ

. ´¨
2 üÈþÃê k-bit üþø¢ø¢ ùÀ÷¤‘Þª ‚ó‘b Æõ ©ø ¤ ßþ ¥ ÂÚþ¢ ñ‘·õ : 2 ñ‘·õ
üþø¢ø¢ ¢Àä . ´¨ ùÀ÷¤‘Þª ö€ä ‚“ length[A]=k ñÏ ‚“ A[0..k − 1] ‚þ¤ ×þ

¤¢ ©¥ ¤ ßþ µȃ“ ø A[0] ¤¢ ©¥ ¤ ßþ µÞî ý¤¢ ¢ª üõ ùƒ¡£ ùÀ÷¤‘Þª ¤¢ ‚î x


: ¢Âƒð üõ –¤ ¬ Âþ ¥ ⓑ— ͨ— ÇþÃê ÛÞä .´¨ A[k − 1]

INCREMENT(A)
1: i ← 0
2: while i < length[A] and A[i] = 1
3: do A[i] ← 0
4: i←i+1
5: if i < length[A]
6: then A[i] ← 1

1 ‘û ´ƒ“ ‚Þû ‚î ü÷‘õ¥ ) ´ó‘Ÿ ßþ —À“ ¤¢ üþ‘ú€— ‚“ INCREMENT ýÂ›

ßþ —À“ ¤¢ INCREMENT ÛÞä n ¥ ‚µª¤ ×þ ßþ “‘€“ ¢Âƒð üõ ¤ Θ(k) ö‘õ¥(À€ª‘“


. ¢Âƒð üõ ¤ O(nk) ö‘õ¥ , ´¨ ÂÔ¬ Àµ“ ¤¢ ‚î ùÀ÷¤‘Þª ×þ ýø ¤ ´ó‘Ÿ

öÂ î Ý ƒ ÷ — ü õ À € € î ü Þ ÷  ƒ ƒ ç — ‘ û ´ ƒ “ ‚ Þ û ü ÷ ¡Â ê  û ¤¢ ‚ î ß þ ‚ “ ‚ › — ‘ “

–‘ ƒ Ü Þ ä n ¥ ‚ µ ª¤ × þ ýÂ › ´ ó‘ Ÿ ß þ  —À “ ¤¢ ‚ î ý ¤  Ï ‚ “ Ý ƒ û¢ ‚ Ž¤ ý  µ Ö ƒ ì¢

A[1] , À€î üõ ƒƒç— ⓑ— ü÷¡Âê ¤‘“ Âû ¤¢ A[0] . ¢ª O(n) ¥ INCREMENT
. À€î üõ ƒƒç— ¤‘“
n
b4c Âû A[2] ø À€î üõ ƒƒç— ¤‘“
n
b2c Âû ⓑ— ýÂ› ¤‘“ n ¤¢

. À€î üõ ƒƒç— ¤‘“ b 2ni c Âû , A[i] ´ƒ“ , i = 0, 1, 2, · · · , blog2 c ýÂ“


n
, üÜî ¤ Ï ‚“

. À€î üÞ÷ ƒƒç— ÃðÂû A[i] ´ƒ“ , i > blogn2 c ýÂ“

Incrementing a Binary Counter 2


¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 176

:‘“ ´¨ “Â“ ‚äÞ¹õ ¤¢ ‘û ƒƒç— Ûî ¢Àã—

blg nc
X n

X 1
b c<n = 2n
i=0
2i
i=0
2i

O(2n)
. ¢ª üõ
n = O(1) –‘ƒÜÞä Âû ͨµõ ‚€þ Ãû ßþÂ“‘€“

(Accounting Method) ü“‘ÆŸ ©ø ¤ 2.2.6

ü—ø‘Ôµõ ý‘û ‚€þ Ãû Óܵ¿õ ý‘û –‘ƒÜÞä ‚“ , üî…úµ¨ Ãó‘÷ ¥ ü“‘ÆŸ ©ø ¤ ¤¢

‘þ µÞî ÛÞä üãìø ‚€þ Ãû ¥ ´¨ ßØÞõ üû‘ð ¦ ¤‘ª ßþ ‚î ¢ª üõ ù¢¢ «‘ʵ¡

‚ € þ à û ¤ ¢ ª ü õ ù ƒ ¡£ Û Þ ä × þ ýø ¤ ¦ ¤‘ ª ö € ä ‚ “ ‚ î ý ‚ € þ à û .À ª‘ “  µ È ƒ “

, Àª‘“ ÛÞä üãìø ‚€þ Ãû ¥ µȃ“ üî…úµ¨ ‚€þ Ãû ‚î üõ‘Ú€û . ݃þð í…úµ¨

ü õ ‚ µ ê ð Â Ñ ÷ ¤¢ ù¢¢ ö‘ Þ µ ¡‘ ¨ ¤¢ « Ê ¡ ‚ “ Û Þ ä ö ¤‘ ± µ ä ö € ä ‚ “ é… µ ¡

‚ € þ à û ‚ î ü þ‘ û –‘ ƒ Ü Þ ä ýÂ “ ýÀ 㠓 ý‘ û ´ ¡¢Â • ¤¢ fÀ 㠓 À ÷ — ü õ ¤‘ ± µ ä . ¢ ª

¤‘ƒÆ“ üãÞ¹— ©ø ¤ ‘“ ©ø ¤ ßþ . ¢ª ù¢‘Ôµ¨ ´¨ üãìø ‚€þ Ãû ¥ µÞî ö‘Èî…úµ¨

. ´¨ –ø‘Ôµõ

¥ ݃û¡ üõ ‘õ Âð . ¢ª É¿Èõ ´ìÀ“ –‘ƒÜÞä Âû üî…úµ¨ ‚€þ Ãû Àþ‘“ Àµ“

םî –‘ƒÜÞä Âû ´ó‘Ÿ ßþ —À“ ¤¢ ͨµõ ‚€þ Ãû ‚Ø€þ –‘±™ ýÂ“ üî…úµ¨ ©ø ¤

Ûî üãìø ‚€þ Ãû ýÂ“ üþ„‘“ öÂî ×þ üÜî üî…úµ¨ ‚€þ Ãû Àþ‘“ ݃€î ù¢‘Ôµ¨ , ´¨

. Àª‘“

É¿Èõ Ĉi ‘ “ ¤ ô i Û Þ ä ü î… ú µ ¨ ‚ € þ à û ø Ci ‘ “ ¤ ô i Û Þ ä ü ã ìø ‚ € þ à û  ð

: Ýþ ¤¢ , ݃€î

n
X n
X
Ĉi ≥ Ci
i=1 i=1

ü î… ú µ ¨ ø ü ã ìø ‚ € þ à û ß ƒ “ é… µ ¡ ù¢¢ ö‘ Þ µ ¡‘ ¨ ¤¢ ùÀ ª ù ƒ ¡£ ü þ‘ ú ÷ ¤‘ ± µ ä

. ´¨
n
X n
X
Ĉi − Ci
i=1 i=1

‚ € þ à û ù‘ Ú ÷ À ª ü Ô € õ ü þ‘ ú ÷ ¤‘ ± µ ä Â ð . À ª‘ “ ü Ô € õ  ƒ è ‚ È ƒ Þ û À þ‘ “ ¤À Ö õ ß þ

Ûî üî…úµ¨ ‚€þ Ãû ‚¹ƒµ÷ ¤¢ ø ¢Âƒð üõ ¤Âì Ûî üãìø ‚€þ Ãû Âþ ¥ üþ‘ú÷ üî…úµ¨

üþ‘ú÷ ¤‘±µä ݃ª‘“ °ìÂõ Àþ‘“ ßþÂ“‘€“ . ¢“ Àû¿÷ üãìø ‚€þ Ãû ýÂ“ „‘“ öÂî ×þ
177 (AMORTIZED ANALYSIS) üî…úµ¨ Ãó‘÷ .2.6

. ¢È÷ üԀõ ù¢¢ ö‘Þµ¡‘¨ ¤¢

–‘ƒÜÞä üãìø ‚€þ Ãû ‚î ݃€î üõ ý ¤ ø¢¢‘þ Àþ ƒړ ÂÑ÷ ¤¢ ¤ ‚µÈ• ñ‘·õ :1 ñ‘·õ
: ´¨ Âþ ¥ –¤ ¬ ‚“

Push : 1
Pop : 2
Multipop : 3

. Àª‘“ Âþ ¥ –¤ Ê“ ÛÞä Âû ýÂ“ üî…úµ¨ Âþ¢‘Öõ Àƒ€î ­Âê

Push : 2
Pop : 0
Multipop : 0

ⓑ— üî…úµ¨ ‚€þ Ãû ‘õ ´¨ ƒçµõ multipop üãìø ‚€þ Ãû ‚ Âð Àƒ€î ‚›—

¥ ( üãìø ‚€þ Ãû ù¥À÷ ‚“) ÀŸø 1 ݃€î üõ ‚µÈ• ¢¤ø ¤ Âʀä ×þ üµìø ´¨ ÂÔ¬

ùƒ¡£ üª ¤¢ ùÀ÷‘õ ü쑓 ÀŸø ×þ ø Ýþ ¥¢Â• üõ ¤‘î ßþ ô‘¹÷ ýÂ“ üî…úµ¨ ‚€þ Ãû
b
. ¢ª üõ

ⓑ— üµìø . ´¨ ‚µÈ• ¥ üª ßµª¢Â“ ‚€þ Ãû âìø ¤¢ üª ýø ¤ ùÀª ùƒ¡£ ¤‘±µä ßþ
b b
¢ª üõ ߃õ‘— ùÀª ùƒ¡£ ¤‘±µä ßþ ¥ Âʀä ßµª¢Â“ ‚€þ Ãû ¢ª üõ ü÷¡Âê pop
¤ ü ª ýø ¤
b
multipop ‘þ pop Û Þ ä ô‘ ¹ ÷ ýÂ “ ü ê‘ î ¤‘ ± µ ä ‚ È ƒ Þ û ‘ õ ß þ  “‘ € “ .

. ¢ª üÞ÷ üԀõ ù‘𠺃û ¤‘±µä ´¨ ‚µÈ• ¤¢ ý ÂÊ€ä ‚î ü÷‘õ¥ ‘— . Ýþ ¤¢

‚î ´¨ O(n) í…úµ¨ ‚€þ Ãû multipop ø pop ø push ÛÞä n ¥ ‚äÞ¹õ Âû ýÂ“

. ´Æû Ã÷ üãìø ‚€þ Ãû ýÂ“ „‘“ öÂî

f ±ì . ݃€î üõ ü¨¤ “ ¤ üÈþÃê üþø¢ø¢ ùÀ÷¤‘Þª ñ‘·õ


ö‘õ¥ ‚î ÝþÀþ¢ … :2 ñ‘·õ
üî…úµ¨ ‚€þ Ãû . À€€î üõ ƒƒç— ‚î ´¨ üþ‘û ´ƒ“ ¢À㗠‘“ °¨‘€µõ ⓑ— ßþ ýÂ›

¢ª üõ 1 ´ƒ“ ×þ ‚î üõ‘Ú€û . Ýþ ƒð üõ ÂÑ÷ ¤¢ 2 ¤ ×þ ‚“ ÂÔ¬ ¥ ×þ ƒƒç—

ƒƒç— ÂÔ¬ ‚“ ¤ ´ƒ“ ‚î ü÷‘õ¥ ýÂ“ ¤‘±µä ÂÚþ¢ ÀŸø ø ¢ª üõ ´¡¢Â• ÀŸø ×þ

ùƒ¡£ ¤‘±µä ÀŸø ×þ ùÀ÷¤‘Þª ¤¢ 1 Âû , Â› ¥ ü÷‘õ¥ Âû ¤¢ . ¢ª üõ ùƒ¡£ ݃û¢

ÀþÀ› ¤‘±µä ‚“ ý¥‘ƒ÷ ø ´¨ ¢›õ ô¥„ ¤‘±µä , ö ö¢Âî ÂÔ¬ ýÂ“ ßþÂ“‘€“ ¢¤¢

Ã÷ üþ‘ú÷ ¤‘±µä ŕ ´Æƒ÷ üԀõ ù‘𠺃û ùÀ÷¤‘Þª ¤¢ ‘û ×þ ¢À㗠ö ø ´Æƒ÷

‚€þ Ãû ýÂ“ „‘“ öÂî ‚î ´¨ O(n) ßت¨ ‚€þ Ãû ßþÂ“‘€“ . ¢ª üÞ÷ üԀõ ù‘𠺃û

. ´¨ üãìø
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 178

(Potential Method) ۃÆ÷‘µ• ©ø ¤ 3.2.6

‚î ÂÚþ¢ ý‘û ©ø ¤ 酡 “ ‚î ´¨ ۃÆ÷‘µ• ©ø ¤ , ü€ØªÂ¨ Ãó‘÷ ©ø ¤ ߃õ¨

ýø ¤ Û ƒ Æ ÷‘ µ • ©ø ¤ , À ÷¢Â î ü õ ¤‘ î ù¢¢ ö‘ Þ µ ¡‘ ¨ × þ ¤¢ É ¿ È õ ü ª × þ ýø ¤
b
üꑮ ¤ÀÖõ ‚î ´Æ€þ ü“‘ÆŸ ©ø ¤ ‘“ ö ë Âê . À€î üõ ¤‘î Ûõ‘î ‘û ù¢¢ ö‘Þµ¡‘¨

. ¢ª üõ Óþ Â㗠ۃÆ÷‘µ• ý¦ Â÷ ö€ä ‚“ ‘¹€þ ¤¢ ¢¨ ö‘Þû ‘þ ùÀª ùƒ¡£

ýÂ“ Àª‘“ ‚ƒóø ù¢¢ ö‘Þµ¡‘¨ D0 ø Àª‘“ n ‘— 1 ¥ ù¢¢ ö‘Þµ¡‘¨ ý‘ûÂ› ¢À㗠Âð

: ݃€î üõ Óþ Â㗠i = 1, 2, · · · , n Âû
Ci =ô i–‘ƒÜÞä ô‘¹÷ üãìø ö‘õ¥
Di = ô i –‘ƒÜÞä ô‘¹÷ ¥ À㓠ù¢¢ ö‘Þµ¡‘¨

Φ =(Potential Function)¢Â“ üõ üփ֟ ¢Àä ‚“ ¤ Di ù¢¢ ö‘Þµ¡‘¨ Âû ‚î ۃÆ÷‘µ• ⓑ—


Φ : Di → RealN umber
Ĉi0 = Ci + Φ(Di ) − Φ(Di−1 ) ô i ‚ÜŸÂõ ¤¢ ü€ØªÂ¨ ‚€þ Ãû

:–‘ƒÜÞä n ýÂ“ Ûî üî…úµ¨ ‚€þ Ãû

P
n P
n P
n P
n
Ĉi = [Ci + Φ(Di ) − Φ(Di−1 )] = Ci + Φ(Di ) − Φ(Di−1 )
i=1 i=1 i=1 i=1

P
n
= Ci + Φ(Dn ) − Φ(D0 )
i=1

ýÂ“ üþ„‘“ öÂî Ûî üî…úµ¨ ‚€þ Ãû ßþÂ“‘€“ Φ(Dn ) > Φ(D0 ) ݃€î ­ÂêÂð

. ¢ ª ô‘ ¹ ÷ ´ ¨ ß Ø Þ õ –‘ ƒ Ü Þ ä À €  Ý ƒ ÷¢ ü Þ ÷ ‘ õ ö  . ´ ¨ Û î ü ã ìø ‚ € þ à û

: i Âû ýÂ“ ݃€î ­Âê Âð ßþÂ“‘€“



Φ(Di ) ≥ Φ(D0 )
⇒ Φ(Di ) ≥ 0
Φ(D0 ) = 0

´¨ “Â“ ۃÆ÷‘µ• –Âƒƒç— ßþÂ“‘€“ . ´¨ üԀõ ƒè ⓑ— ×þ ۃÆ÷‘µ• ⓑ— ü€ãþ

:‘“

Φ(Di ) − Φ(0) > 0 for all i

‚“ . ´¨ ‚µÆ“ø ۃÆ÷‘µ• ⓑ— ’‘¿µ÷ ‚“ ‘¹€þ ¤¢ ùÀõ ´¨À“ üî…úµ¨ ‚€þ Ãû

ßþÂ“‘€“ . ´ª¢ ݃û¡ Óܵ¿õ üî…úµ¨ ý‘û ‚€þ Ãû Óܵ¿õ ý‘ûۃÆ÷‘µ• ⓑ— ý¥

. ´¨ ۃÆ÷‘µ• ⓑ— ßþ µú“ ’‘¿µ÷ ۃÆ÷‘µ• ©ø ¤ ¤¢ ‘õ ¤‘î ßþ µÞúõ


179 (AMORTIZED ANALYSIS) üî…úµ¨ Ãó‘÷ .2.6

: ݃€î üõ Óþ Â㗠. ݃€î üõ ü¨¤ “ ¤ ‚µÈ• ñ‘·õ : 1 ñ‘·õ


Φ(Di ) = ‚µÈ• Û¡¢ ¬‘€ä ¢Àã—

D0 = üó‘¡ ‚µÈ•

Φ(D0 ) = 0
Å • ¢ ª ü Þ ÷ ü Ô € õ ù‘ ð º ƒ û ‚ þ¤ × þ Û ¡¢  ¬‘ € ä ö 

Φ(Di ) ≥ 0 = Φ(D0 )
Àª‘“ ‚µª¢ Âʀä s ‚µÈ• , i − 1 ‚ÜŸÂõ ¤¢ ݃€î üõ ­Âê

Φ(Di−1 ) = s

:¢ª Â› ô i ‚ÜŸÂõ¤¢ push Âð

Φ(Di ) − Φ(Di−1 ) = (s + 1) − s = 1
Ĉi = Ci + Φ(Di ) − φ(Di−1 ) = 1 + 1 = 2 → O(1)

:¢ª ô‘¹÷ ô i ‚ÜŸÂõ ¤¢ pop ⓑ— Âð

Φ(Di ) − Φ(Di−1 ) = (s − 1) − s = −1
Ĉi = Ci + Φ(Di ) − φ(Di−1 ) = 1 − 1 = 0 → O(1)

:¢ª Â› multipop ⓑ— Âð

0 0 0
k = min(s, k) Φ(Di ) − Φ(Di−1 ) = (s − k ) − s = −k
Ĉi = Ci + Φ(Di ) − φ(Di−1 ) = k 0 − k 0 = 0 → O(1)

. ´¨ O(n) ¥ Ûî üî…úµ¨ ‚€þ Ãû ßþÂ“‘€“

: Ýþ ¤¢ üþø¢ø¢ ùÀ÷¤‘Þª ¤¢ :2 ñ‘·õ


Φ(Di ) = bi ô i ‚ÜŸÂõ ¤¢ ‘û ×þ ¢Àã—

‚î ´¨ ¼®ø

Φ(Di ) ≥ 0
ti = ô i ‚ÜŸÂõ ¤¢ À÷ª üõ ÛþÀ±— ÂÔ¬ ‚“ ‚î üþ‘û ×þ ¢Àã—

Ci = ti + 1
¤‘€ƒÞ¨ ø¢ ‚Ž¤ “ Âʵ¿õ üû‘Ú÷ .6 ÛÊê 180


if bi = 0 ⇒ bi−1 = k = ti 
⇒ bi ≤ bi−1 − ti + 1

if bi > 0 ⇒ bi = bi−1 − ti + 1

Φ(Di ) − Φ(Di−1 ) ≤ bi−1 − ti + 1 − bi−1 = −ti + 1

Ci0 = Ci + Φ(Di ) − Φ(Di−1 ) ≤ ti + 1 − ti + 1 = 2 ⇒ Ci0 ≤ 2

P
n
Ci0 ≤ 2n → O(n)
i=1

Вам также может понравиться