Sie sind auf Seite 1von 13

SOME EXERCISES TO PRACTICE FOR THE EXAM.

Question 1 [1 point] What is the running time complexity of the following Algorithm (in big-Oh notation) ? Algorithm Hello(A). Let A be an array of size n. for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { A[i] = A[i] + j/2; } } a) O(nlog n) b) O(n2) c) O(n3) d)O(n4) e) None of the above. Question 2 [1 point] The running time of enqueue and dequeue methods can be O(1) if the Queue ADT is implemented using a doubly-linked list with access to the first and to the last element where: a) enqueue uses the insertFirst method and dequeue uses removeFirst. b) enqueue uses the insertLast method and dequeue uses removeLast. c) enqueue uses the insertLast method and dequeue uses removeFirst. d) None of the above. Question 3 [1 point] The number of internal nodes in a FULL binary tree is: a) O(log n) b) O(n) c) O(n log n) d) O(n2) e) None of the above

[Question 1] (marks) Let S be a sequence containing distinct integers. Indicate the worst-case complexity of the following two sorting algorithms. 1) Let T be a binary search tree that is initially empty. Read the integers from S and insert them one at a time into T. Perform in-order traversal on T to return a sorted sequence. Worst-case complexity in big-Oh: O( )

2) Let A be an AVL tree that is initially empty. Read the integers from S and insert them one at a time into A. Perform in-order traversal on A to return a sorted sequence. Worst-case complexity in big-Oh: O( ) [Question 2] (marks) What is the worst case big-Oh complexity (as a function of the number of nodes n) of removing the minimum key (and rearrange the structure if necessary) in: A min-heap: A max-heap: An AVL tree: A binary search tree: An unsorted sequence: a) O(1) a) O(1) a) O(1) a) O(log n) a) O(log n) b) O(log n) b) O(log n) b) O(log n) b) O(n) b) O(n) c) O(n) c) O(n) c) O(n log n) c) O(n log n) c) O(n log n) d) O(n log n) d) O(n2) d) O(n2) d) O(n2) d) O(n2)

[Question 3] (marks) The big-Oh cost of performing in-order traversal of an AVL tree with n nodes and m edges is: a) O(log m) b) O(log n) c) O(m) d) O(n * m) e) none of the above

[Question 4] (marks) Consider a hash table constructed with double hashing: hj(ki) = [h(ki) + jd(ki)]mod N The primary hash function is h(ki) = ki mod 11 and the secondary hash function is d (ki) = ki div 5 where div is division.( e.g. 10 mod 3 = 1 and 10 div 3 =3). Mark in the table given below cells which are probed when there is a call of findElement(15). i= 0 1 1 2 3 15 4 5
AVAILA BLE

6 39

9 42

10

[Question 5] (marks) Insert the key 15 in the hash table given below to use the quadratic probing with the following hash function h(ki) = (2* ki +5) mod 13 (in a table of size N = 13) i= 0 1 2 3 4 10 5 6 7 1 8 9 2 10 22 11 12

[Question 6] (marks) Draw a tree where the pre-order traversal gives AEASYEXAM and the post-order gives YESXAAEMA

[Question 7] (marks) Remove the key 14 from the (2, 4) tree given below and create a new (2, 4) tree.

[Question 8] (marks) Perform Insert(21) in the AVL tree given below.

[Question 9] (marks) Illustrate ALL the steps in In-place quick sorting. Use the last element as pivot for each input sequence. Input sequence: 3 2 10 6 8 9 1 4 7 5

Output sorted sequence:

10

[Question 10] (marks) Let an array H = [2; 5; 8; 10; 6; 12; 9; 20; 14; 11] which represents a heap. a) Draw a new array representing the heap H1 after RemoveMin() in H.

b) Draw a new array representing the heap H2 after Insert(3) in H1

[Question 11] (marks) Suppose you are given a set of n distinct integers and you want to determine the 5-th smallest value. What would be the worst case big-Oh complexity for each? a) Insertion Sort: O( ) )

b) Bubble Sort (moving the smallest at each iteration): O( [Question 12] (marks) Which tree traversal corresponds to a depth-first search? a) pre-order b) in-order c) post-order d) none of the above

[Question 13] (marks) Let G be a connected graph where all the edges have different weights. Suppose that the number of edges m is the same as the number of vertices n. You want to find a minimal spanning tree (MST) of G. The graph is represented by the adjacency list structure.

a) What is the complexity to use Prim-Jarniks MST Algorithm with a heap?

b) Is it possible to have an algorithm with complexity of O(n) to find a MST of G? If so, explain the algorithm.

[Question 14] (marks) Let G be a graph with n nodes and m edges. Write the big-Oh complexities of the Dijkstras shortest path algorithm and of Prim-Jarniks MST Algorithm using an unsorted sequence, depending on the representation of the graph. Adjacency matrix Dijkstras shortest path Prim-Jarniks MST Adjacency List

[Question 15] (marks) Consider the Dijkstra algorithm for finding the shortest path spanning tree of a graph. Execute the algorithm for the undirected graph represented by the following adjacency list, starting from node A. (The numbers in parenthesis are the weights of the corresponding edges). A B(2), D(3) B A(2), C(8), D(4), E(7) C B(8), D(1), E(3) D A(13), B(4), C(1) E B(7), C(3) Draw the graph. Fill the chart below to keep track of the changes of the distance labels after including each new node to the cloud. The first line of the chart is already filled with the initial distance labels. Next Vertex A B 2 C D 3 E Connection edge AB

[Question 16] (marks) The Boyer-Moore algorithm is used to find the pattern P in the test T. Indicate the next 6 comparisons performed by the algorithm in the table given below. The first comparison is already filled out. 0 C B 1 A A 2 D T 3 E E 4 A A 5 U U i 5 j 5 T(i) P(j) U U 6 7 D 8 A 9 N 10 S 11 12 B 13 A 14 T 15 E 16 A 17 U

T= P=

Comparison # 1 2 3 4 5 6 7

[Question 17] (marks) Suppose you have a connected graph G represented by an adjacency matrix and you want to find out if G is a tree. Which of the following strategy would work? a) Perform DFS. If there are n - 1 discovery edges, it is a tree b) Perform BFS. If all the nodes are visited it is a tree c) Perform DFS. If I do not encounter any back edge, it is a tree d) Perform BFS. If I do not have more than n discovery edges , it is a tree e) None of the above. [Question 18] (marks) True or false; a) ( T F ) An AVL tree is a special binary search tree. b) ( T F ) A heap is a special binary search tree. c) ( T F ) A binary search tree is a special 2-4 tree. d) ( T F ) If every edge has weight 1 in a weighted graph G, the depth first search starting at u constructs a shortest path spanning tree. e) ( T F ) The depth first search cannot return a shortest path spanning tree in a graph G with more than 4 vertices where each vertex has degree less than 3. f) ( T F ) Dijkstras shortest path algorithm does not work if weights in a graph G are negative. [Question 19] (marks)

Suppose that we have the following text: DEGREE TREE GOT G Character Frequency D 1 E 5 G 3 O 1 R 2 T 2 3

Draw the tree that represents a Huffman code (always place the smallest frequency to the left, 0 corresponds to the left branch, 1 to the right one) and encode the text as a collection of bits.

[Question 20] (marks) Given the initial sequence: {85 24 63 47 17 31 96 50}, at the last step in the merge sort, the sequences to be merged are: a) {24 47 63 85} and {17 31 50 96} b) {17 24 31 47} and {50 63 85 96} c) {24 85} {47 63} {17 31} and {50 96} d) {17 24 31 47 63 85 96} and {50} e) depends on choice of pivot [Question 21] (marks) Assume that there are 400 second year students in a school. We are creating a hash table to store the information for these students. Which of the following would be the best choice for use by a hash function? a) Year of birth (4 digits) b) Date of birth (2 digits (date) + 2 digits (month)) c) Postal code of home address : first 3 characters d) Height in cm e) First 3 digits of telephone number [Question 22] (marks) If the sequence: (2,2) (0,4) (1,4) (0,1) (1,2) (0,6) (2,1) (1,1) were sorted using a stable sort by ascending order on the first key and then using the same method were sorted again on the second key, the result would be: a) (0,4) (0,1) (0,6) (1,4) (1,2) (1,1) (2,2) (2,1) b) (0,1) (2,1) (1,1) (2,2) (1,2) (0,4) (1,4) (0,6) c) (2,1) (1,1) (0,1) (2,2) (1,2) (1,4) (0,4) (0,6) d) (0,1) (1,1) (2,1) (1,2) (2,2) (0,4) (1,4) (0,6) e) (0,1) (0,4) (0,6) (1,1) (1,2) (1,4) (2,1) (2,2)

Exercise (1.5 points) In-place Max heap construction. Fill the blank spaces. Input sequence: 3 3 2 10 6 8 9 9 3 1 1 2 10 4 4 7 6 6 5 5 8 9 1 4 7 5

3 3 10 10 Output Max-heap:

8 8 8

10 10 3

7 7

2 5

9 9 9 3 10

1 1 1 1 8

4 4 4 4 9

6 6 6 6 7

5 2 2 2 5 3 1 4 6 2

Exercise (1.5 points) In-place heap sort using Max heap. Fill the blank spaces. Input Max-heap 10 2 9 6 8 8 4 7 7 1 6 2 5 5 1 4 4 1 3 1 8 8 8 6 7 7 4 6 6 5 5 2 4 4 1 2 2 2 9 9 2 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 7 7 7 7 7 6 6 4 4 4 4 4 4 2 2 1 4 4 4 4 5 5 5 5 5 5 5 5 5 5 1 1 1 1 5 5 5 5 5 5 5 3 3 3 2 2 2 2 2 2 10 1 1 1 1 1 1 1 1 1 1 8 4 4 4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 2 3 9 6 6 6 6 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 4 7 5 2 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 5 6 7 8 9 10 3 1 4 6 2

6 6 6 6 6 6 6 6 6 6 1

7 7 7 7 7 7 7 7 7 7 7

Output sorted sequence:

Exercise (2 points) In-place quick sort. Use the first element as pivot for each input sequence. Fill the blank spaces. Input sequence: 3 2 10 6 8 9 3 1 2 10 4 7 6 5 8 9 1 4 7 5

1 1

2 2 6 4 4 4 8 5 5 5 10 9 7 8 9 6 10 10 4 9 7 7 5 8

7 7

9 Output sorted sequence: 1 2 3 4 5 6 7 8 9 10

Question 24 [3 points] Suppose that we have a weighted oriented graph G with six vertices represented by the following adjacency list: A: (B, 4) (F, 2) // i.e. A is connected to B with weight 4 and to F with weight 2 B: (A, 1) (C, 3) (D, 4) C: (A, 6) (B, 3) (D, 7) D: (A, 6) (E, 2) E: (D, 5) F: (D, 4) (E, 3) a. Write down the adjacency matrix that represents the graph G (include the weight): A A B C D E F B C D E F

b. Complete the chart below to find out the shortest paths from vertex A to all vertices using Dijkstras algorithm (i.e., perform only the next 2 steps needed to include the next 2 nodes in the spanning tree). Next vertex A F B 4 4 C 6 D 5 E 2 F Connecting edge (A, F)

Consider the following graph.


a c d h

Perform a breadth-first search traversal in G, starting from node a, assuming that: - The first edge to be traversed is (a,b) and - The edges of a vertex are processed counter clockwise. For example, referring to G above, the edges incident to a are processed in the order (a,b) (a,c) (a,d). The order in which the vertices are visited is: ____________________________ Question 14 [1 point] Suppose that you are using Mergesort to sort the sequence S = (5, 1, 25, 15, 20, 10, 30, 0). What are the two sub-sequences S1 and S2 that would be merged in the final step of Mergesort ? S1: S2: Question 11 [3 points] Suppose we have the following 2-4 tree (called X): a) Show the 2-4 tree that results from inserting 25 in X.
20 32 42

17 19

30

35

47 50

b) Show the 2-4 tree that results from deleting 30 from the original X (i.e. without 25) using a fusion operation:

c) Show the 2-4 tree that results from deleting 30 from the original X (i.e. without 25) using a transfer operation:

Question 10 [3 points] Draw the trees resulting from the insertion of keys 2, 1, 4, 3, and 5 into each of the following (initially empty) ADTs: a) Binary Search Tree c) AVL Tree d) 2-4 Tree

Das könnte Ihnen auch gefallen