Department of VT12 Software Engineering and Managment DIT725 (TIG023) Göteborg University, Chalmers 24/5-12 Solution suggestions for examination of Logic, Algorithms and Data Structures, Date : April 26, 2012 1
2
Task 1: Given A = true, B = false and C = false, calculate: (A B) C B. (1 p) A B C B C A B (A B) C B T F F T T T T Task 2: Given A = true, B = true and C = false, calculate: C (A A ) B. (1 p) A B C A A A B C (A A ) B T T F F F F F Task 3: Construct a truth table for: (A B ) (B C ) (1 p) A B C B C A B B C (A B ) (B C ) F F F T T F T T F F T T F F F F F T F F T F T T F T T F F F T T T F F T T T T T T F T T F T F T T T F F T F T T T T T F F F T T Task 4: Construct a truth table for: C ( A B) (1 p) A B C A B C (A B) F F F T T F F T T T F T F T T F T T T T T F F F T T F T F F T T F T T T T T T T 3
Task 5: Let an edge be denoted by < from, to, weight >. Show the adjacency matrix that corresponds to the following neighbour lists table: 0 (< 0, 2, 3 >, < 0, 3, 1 >) 1 (< 1, 0, 2 >, < 1, 2, 4 >, < 1, 3, 2 >) 2 (< 2, 3, 3 >) 3 (< 3, 1, 1 >, < 3, 2, 2 >) 0 1 2 3 0 3 1 1 2 4 2 2 3 3 1 2 (1 p) Task 6: Find the powerset of S, where S = {x, y, z} (1 p) P(S) {{}, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z}} Task 7: Let A = {1, 2, 3}, B = {2, 3, 4}, and C = {3, 4, 5}, find the value of 2 A (B C) (1 p) We have that: (B C) = {2}, why also A (B C) = {2} and therefore: 2 A (B C) is true. Task 8: Define predicativly, i.e. give a set generator, the set of all even natural numbers. (1 p) {x N x modulo 2 = 0} or {x x N x modulo 2 = 0} In stead of modulo, it is OK with mod, rem, or % Task 9: Given A = << 1, 7 >, < 9, 5 >, < 13, 6 >, < 17, 19 >>, calculate π 2 (π 3 A) (1 p) π 2 (π 3 A) = π 2 < 13, 6 > = 6 4
Task 10: Give the definition of a height balanced tree. (1 p) A height balanced tree is: the empty tree,, or a tree: root / \ A B where A and B are height balanced trees and height( A ) height( B ) < 2 Task 11: The dog do not like herring, and the cat hate vegetables. Negate the above statement! (1 p) Since (A B) = A B we get: The dog like herring, or the cat do note hate vegetables. Task 12: 5 2 6 1 4 7 3 Give all the nodes in preorder. (1 p) In preorder you take the root first, then left subtree and last right subtree. For the given tree we get the following order: 5, 2, 1, 4, 3, 6, 7 5
Task 13: Given that the universe is N (the natural numbers), which of two following predicates is true. A: ( x)( y) x < y or B: ( x)( y) y < x (1 p) Since all natural numbers have a successor, predicate A is true. Since natural number 0 has no predecessor, predicate B is false. Task 14: In order to demonstrate how a hash table works, your task is to store the following integers: 12, 44, 13, 88, 23, 94, 11, 39, 20, 16, and 5 The complete hash code function you shall use is: h(i) = (2 i + 5) modulo 11 ( modulo is % in Java ). In order to solve the collision problem, you shall use two forms: a) separate chaining: Write down the hash table you get by adding the elements in given order. (2 p) We get the following array of lists: under index is the list 0 [] 1 [20] 2 [] 3 [] 4 [16, 5] 5 [44, 88 11] 6 [94, 39] 7 [12, 23] 8 [] 9 [13] 10 [] 6
b) linear probing: Write down the table (array) you get by adding the elements in given order and motivate why the element 11 is placed where you put it. (2 p) The final array is: 11 39 20 5 16 44 88 12 23 13 94 When 11 is added, we have the following array: -1-1 -1-1 -1 44 88 12 23 13 94 where -1 denotes a free position. The complete hash code for 11 is 5, and since all positions with index 5 or bigger are occupied, the first free position is 0. 7
Task 15: Heaps are usually stored in arrays. Given the two arrays; a: 1 3 7 4 5 8 6 9 index 1 2 3 4 5 6 7 8 and b: 1 3 6 4 5 8 7 9 index 1 2 3 4 5 6 7 8 a) Decide which array that is a heap, and write it down as a binary tree. (1 p) Array b is a heap. 1 / \ / \ 3 6 / \ / \ 4 5 8 7 / 9 b) Motivate why the other tree is not a heap. (1 p) The value 6 under index 7 must not be less than the value of the father which is found under index 7/2 = 3, i.e. the value 7. c) Then, put in 2 into the heap and write down the array after the operation. (2 p) b: 1 2 6 3 5 8 7 9 4 index 1 2 3 4 5 6 7 8 9 d) After that, remove the smallest element from the heap and write down the array after the removal. (2 p) b: 2 3 6 4 5 8 7 9 index 1 2 3 4 5 6 7 8 8
Task 16: a) Put in the values 10, 4, 3, 15, 6, 18, 5, 8, and 11 into an empty binary search tree (No balancing). You shall put them in exactly in the given order. Write down the resulting tree. (2 p) You shall place a new node at first free place without changing the tree. 10 / \ / \ 4 15 / \ / \ 3 6 11 18 / \ 5 8 b) Remove 10, i.e. the root, from the tree and write down the resulting tree. (1 p) The rule is to replace the node to be deleted with the rightmost element in the left subtree. (Or the leftmost element in the right subtree.) 8 11 / \ OR / \ / \ / \ 4 15 4 15 / \ / \ / \ \ 3 6 11 18 3 6 18 / / \ 5 5 8 9
c) Put in the values 1, 4, 5, 2, 3 into an empty AVL tree, in exactly the given order. Show the balancing operations performed, by writing down the tree before and after the balancing and with involved nodes marked (or mentioned). (3 p) 1 4 4 \ RL / \ / \ 4 --> 1 5 RL 2 5 \ 1,4 \ --> / \ 5 2 1,2 1 3 \ 3 Task 17: Demonstrate how merge sort work on the following array: 70 20 60 40 10 80 50 30 You demonstrate it by showing the current array after each iteration. (2 p) 70 20 60 40 10 80 50 30 }{{}}{{}}{{}}{{}}{{}}{{}}{{}}{{} }{{}}{{}}{{}}{{} 20 70 40 60 10 80 30 50 }{{}}{{}}{{}}{{} }{{}}{{} 20 40 60 70 10 30 50 80 }{{}}{{} }{{} 10 20 30 40 50 60 70 80 10
Task 18: public void WhatEverIdo( int[] a ) { int sum = 0; for ( int i = 0; i < a.length; i++ ) for ( int j = i; j < a.length; j++ ) for ( int k = 2; k < 5; k++ ) sum = sum + (a[i] + a[j]) % k; System.out.println( sum ); } Give the time complexity for the method above. (2 p) First, the innermost for-loop is constant, so it is of O( 1). Second, reducing all constant work to 1, we get the summations: n 1 i=0 n 1 j=i 1 = n 1 i=0 n i = n+(n 1)+...+1 = n (n + 1) 2 O( n 2 ) 11
Task 19: Given the interfaces: public interface Stack<E> public interface Queue<E> { boolean isempty(); { boolean isempty(); void push(e elem); void enqueue(e elem); void pop(); void dequeue(); E top(); E front } // Stack } \\ Queue Write a method void reversestack( Stack<E> s ), that reverse the given stack, i.e. the undermost element will be the uppermost element, the next undermost element will be the second element, and so on. (It is OK to give an algorithm instead, if it is detailed enough.) (3 p) Since we need a constructor for Queue, I assume one is called ArrayQueue(), but the name is not important here, only that the interface never has constructors. public static <E> void reversestack( Stack<E> s ) { Queue<E> q = new ArrayQueue<E>(); while(! s.isempty() ) { q.enqueue( s.top() ); s.pop(); } while(! q.isempty() ) { s.push( q.front() ); q.dequeue(); } } // reversstack 12