Sei sulla pagina 1di 3

Name-Surname:.

BAKENT UNIVERSITY, DEPARTMENT OF COMPUTER ENGINEERING BIL367 ALGORITHMS MIDTERM EXAM, 30.11.2010 Answer all 6 questions in 100 minutes. Question 1 (15p). Analyze the time complexity for following three code segments and give your results in Big-.
i = n; while(i > 10) { i = i/2; for(j=0; j<n ;j+) printf("loop"); } i = n; while(i > 10) { i = i/2; for(j=0; j<i; j+) printf("loop"); } i = n; while(i > 10) { i = i/2; for(j=0; j<lg(i); j+) printf("loop"); }

Question 2 (15p). Given f(n) = 3 and g(n) = n2 . Which of the following is/are true; f = O(g), g = O(f), f = (g)? Justify your answer.

Question 3 (10p). Given Insertion Sort, Selection Sort, Merge Sort, Quicksort and Heapsort, which one is best in terms of space complexity? Which one is worst? Why? (There may be multiple answers!)

Question 4 (15p). Let A be an array of integers. We say that A is bitonic if the elements of A are first strictly increasing and after reaching a maximum value, they are strictly decreasing (Ex: 2,4,5,6,8,9,7,6,3). Describe an O(lgn)-time algorithm that takes a bitonic integer array of size n and returns the maximum element.

Question 5 (15p). MAX_HEAPIFY(A,i,n) procedure takes a binary tree A, rotted at i where left and right subtrees are max-heaps, and modifies the tree so that it becomes a max-heap. Write an algorithm using MAX_HEAPIFY, to extract (read and delete) the largest element of a max-heap. Analyze the running time of your algorithm?

Question 6 (40p). You have n types of items, where the i item type has an integer size si and a real value vi . You are trying to fill a knapsack of total capacity C with a selection of items of maximum value. You can add multiple items of the same type to the Tur knapsack (Knapsack = Srt antas). (a) (b) (c) (d) Describe a recursive relation for M(j), which denotes the maximum value you can pack into a size j knapsack. Write a dynamic programming algorithm (in pseudocode) based on relation (a) to find M(C). Write a greedy algorithm (in pseudocode) to fill the knapsack. Compare two algorithms in terms of their time complexities.

th

Potrebbero piacerti anche