Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)
Analysis of Algorithms - 3
-
Upload
atakan-aral -
Category
Software
-
view
221 -
download
0
description
Transcript of Analysis of Algorithms - 3
![Page 1: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/1.jpg)
BLG 335E – Analysis of Algorithms IFall 2013, Recitation 3
06.11.2013
R.A. Atakan [email protected] – Research Lab 1
R.A. Doğan [email protected] – Research Lab 3
![Page 2: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/2.jpg)
Preliminary Information
• Although MAX-HEAPIFY costs O(lg n)time and there are O(n) calls to it, still wecan find a tighter bound than O(n lg n) for:
• Because, cost of MAX-HEAPIFY dependson the height of the node in the tree, andand the heights of most nodes are small.
![Page 3: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/3.jpg)
Exercise 1
• Using a similar reasoning, can we find atighter bound than O(n lg n) forHeapsort?
![Page 4: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/4.jpg)
O(n) O(n log n)
Exercise 1 – Solution
![Page 5: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/5.jpg)
Exercise 2
• A ‘d-ary’ heap is like a binary heap, butnon-leaf nodes have d children instead of2 children.
• How would you represent a d-ary heap inan array?
• Verify that:
d-ary-parent(d-ary-child(i, j)) = i
![Page 6: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/6.jpg)
Exercise 2 – Solution
binary-parent(i)
return ⌊i / 2⌋
d-ary-parent(i)
return ⌊((i−2)/d) + 1⌋
binary-child(i,j)
return 2i − 1 + j
d-ary-child(i,j)
return d(i−1)+1+j
• Root’s d children are kept in A[2]~A[d+1]
• Their children are kept in A[d+2]~A[d²+d+1] and so on.
![Page 7: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/7.jpg)
Exercise 3
• Banks often record transactions on anaccount in order of the times of thetransactions.
• But many people like to receive their bankstatements with checks listed in order bycheck number.
• Banks need to convert time-of-transactionordering to check-number ordering.
• Insertion Sort vs. Quick Sort
![Page 8: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/8.jpg)
Exercise 3 – Solution
• People usually write checks in order bycheck number, and merchants usuallycash them with reasonable dispatch.
• The problem is therefore the problem ofsorting almost-sorted input.
• For Quick Sort, best and average casetime complexity are the same (O(n log n)).
• For Insertion Sort, the best case is O(n)and the average case is O(n+d).
![Page 9: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/9.jpg)
Exercise 4
• Illustrate the operation of Counting-Sorton the array below.
A = [6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2]
1. Max{A[i]}=6
2.
3.
4.
0 0 0 0 0 0 0
2 2 2 2 1 0 2
2 4 6 8 9 9 11
![Page 10: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/10.jpg)
Exercise 45.
6 0 2 0 1 3 4 6 1 3 2
2 4 6 8 9 9 11
2 2 4 5 8 9 9 11
2 3 2 4 5 7 9 9 11
1 2 3 2 3 5 7 9 9 11
0 0 1 1 2 2 3 3 4 6 6
1 2 3 6 2 3 5 7 9 9 10
1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6
1 2 3 4 6 2 3 5 7 8 9 10
1 2 3 3 4 6 2 3 5 6 8 9 10
1 1 2 3 3 4 6 2 2 5 6 8 9 10
0 1 1 2 3 3 4 6 1 2 5 6 8 9 10
0 1 1 2 2 3 3 4 6 1 2 4 6 8 9 10
0 0 1 1 2 2 3 3 4 6 0 2 4 6 8 9 10
0 2 4 6 8 9 9
![Page 11: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/11.jpg)
Exercise 5
• Illustrate the operation of Radix-Sort onthe following list of English words:
COW, DOG, SEA, RUG, ROW, MOB,BOX, TAB, BAR, EAR, TAR, DIG, BIG,TEA, NOW, FOX.
![Page 12: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/12.jpg)
Exercise 5 – Solution
COW
DOG
SEA
RUG
ROW
MOB
BOX
TAB
BAR
EAR
TAR
DIG
BIG
TEA
NOW
FOX
![Page 13: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/13.jpg)
Exercise 5 – Solution
SEA
TEA
MOB
TAB
DOG
RUG
DIG
BIG
BAR
EAR
TAR
FOX
BOX
COW
ROW
NOW
![Page 14: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/14.jpg)
Exercise 5 – Solution
TAB
BAR
EAR
TAR
SEA
TEA
DIG
BIG
MOB
DOG
FOX
BOX
COW
ROW
NOW
RUG
![Page 15: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/15.jpg)
Exercise 5 – Solution
BAR
BIG
BOX
COW
DIG
DOG
EAR
FOX
MOB
NOW
ROW
RUG
SEA
TAB
TAR
TEA
![Page 16: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/16.jpg)
Exercise 6
• Illustrate the operation of Bucket-Sort onthe array below.
A = [.79, .13, .16, .64, .39, .20, .89, .53, .71, .42]
A = [.13, .16, .20, .39, .42, .53, .64, .71, .79, .89]
.13
.16
.20
.39
.42.64.53
.79
.89
.71
.53
.64
.71
.79
.89
.13
.16
.20
.39
.42
![Page 17: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/17.jpg)
Exercise 7
• Explain how to implement two stacks inone array A[1…n] in such a way that
– neither stack overflows unless the totalnumber of elements in both stacks is n.
– the PUSH and POP operations should run inO(1) time.
top1 top2
5 3 2 3 6 9 1
![Page 18: Analysis of Algorithms - 3](https://reader034.fdocuments.net/reader034/viewer/2022052411/5578f259d8b42a5c5c8b5268/html5/thumbnails/18.jpg)
Extra Exercises
• Implement a queue by a singly linked listL. The operations Enqueue and Dequeueshould still take O(1) time.
• Write an O(n)-time procedure that, givenan n-node binary tree, prints out the keyof each node in the tree.
a) Recursively
b) Non-recursively using a stack