2019FINALEXAM
2019FINALEXAM
Final Exam
60 Total Marks
1
Question (2) show amortized analysis for dynamic hash table which start by one
size and extend 2 power i I in 0 1 ,2 4 8 ……. By aggregation methods (10 points)
2
Question (3) color the following red-blact tree
5 9
12
3
Question (4) write a recursion function that compute ab where a and b are
positive integers. and compute its time complexity using master method. (7.5 points)
4
Question (5) Write an algorithm that finds both the smallest and largest numbers in
a list of n numbers. Try to find a method that does at most 1.5 n comparisons of
array items. Show the full analysis. (7.5 points)
First divide the n elements to two sub arrays n/2 according to compare the
two adjacent values big go to first one and the small go to the second
This take n/2 comparisons
After that you can search for min in the second one take n/2
And search for max in the first one take n/2
Total =3n/2 = 1.5n
Question (6) use the greedy Algorithm and dynamic programming to solve the
following Knapsack problem (10 points) and determine which one is the best
solution. W=5
id weight value
1 1 10
2 2 15
3 3 14
4 1 12
Item/W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 10 10 10 10 10
2 0 10 15 25 25 25
3 0 10 15 25 25 29
4 0 12 22 27 37 37
Items 1 ,2,4
15 + 14 = 29
5
6
Question(7) Find the shotrest path for the following graph from node A to each
nodes using Dijkstra’s algorithm (10 points)
PARENT A A E G E B
# A B C D E F G
0 - - - - - -
5 3 - - - -
5 10 10 - -
10 8 - 6
10 7 -
9 8
9
ABG = 6
ABGEF = 8
ABGE =7
ABGED=9
AC = 3
AB = 5
7
Good Luck
Dr Basheer Youssef.