





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























February 17
Using top-down dynamic programming,evaluate: z Must initialize matrix or results could bewrong
⎞ ⎟ ⎟ ⎠
⎛ ⎜⎜ ⎝
− −
⎞ ⎟⎟ ⎠
⎛^ ⎜⎜ ⎝
−
= ⎞ ⎟⎟ ⎠ ⎛ ⎜⎜ ⎝
(^11)
1
N^ k
k N
N k
matrix(nrow = 10, ncol = 10)^ •
Creates a matrix with 10 rows and columns
matrix(data = 0, nrow = 10, ncol = 10)^ •
The optional parameter allows the matrix tobe pre-initialized with an element of choice,which could even be a vector!
Using bottom-up dynamic programming,evaluate: z Calculation is still slow, due to nestedsum… but this can be simplified
⎧ ⎪⎪⎨ ⎪ ⎪⎩
≥ ≤
−
−
=
1
0
2
)
( ) 1 (
1
) (^
1
N^ N k N C k C N N N C
N k
comparisons
[Nmax
];
double
inner_sum
=
0.0;
comparisons
[^1
]^
=^
comparisons
[^0
]^
=^
0;
for
(
i^
=^
2 ;
i
<
Nmax
;^
i++)
{ inner_sum
+=
2
comparisons
[i
1
];
comparisons
[i
]^
=^
i^
+^
inner_sum
/
i;
}
Further improvements to Quick Sort
-^
Maintaining explicit stacks
z Merge Sort
-^
Another
N log N
sort
-^
Fastest stable sort
Divide array into smaller sub-arrays
-^
Sort right and left sub-arrays independently
z Find a partitioning element such that:
-^
All elements to the right are greater
-^
All elements to the left are smaller
partition
(Item
a
int
start
int
stop
{ int
up
start, down
stop
part
a
[stop
while
{ while
isLess
(a
[up
part
up
while
isLess
(part
a[
down
up
down
down
if
up
down
break;
Exchange
(a
[up
a
[down
up
down
Exchange
(a
[up
a
[stop
return
up;
}
Delaying sort for small sub-arrays z Using median-of-three partitioning
-^
Random partitioning element can also help!
z Avoiding deep recursion
The computer stack has a limited size z Quick Sort can call itself up to N-1 times
-^
Unlikely, but very deep recursion is possible!
z Can we provide a guarantee on depth ofrecursion?
After partitioning, handle smaller half first
-^
At most,
log
N
smaller halves!
z Keep track of sections to be solved in“explicit” stack
A common feature in computer programs z A simple way to avoid recursion
-^
More effort for the programmer
z Another application is in graph traversal
Divide and Conquer Algorithm
-^
Recursive calls can be “hidden”
z Optimizations
-^
Choice of median
-^
Threshold for brute-force methods
-^
Limiting depth of recursion