Vous êtes sur la page 1sur 9

# DIT725, SE&M, GU EXAMINATION

2013-08-28

## Logic, Algorithms and Data Structures (DIT725), 7.5 hec

Software Engineering & Management Computer Science and Engineering University of Gothenburg

## Date Time Place Responsible Allowed aids Grade limits

Wednesday, August 28, 2013 8:30-12:30 V, Johanneberg Fredrik Lindblad, tel. 7721051, will visit around 9:30 and 11:00 None (English dictionary) Pass (G) 14 p, pass with honors (VG) 23 p (max is 36 p)

Please observe the following (failing to do so may result in point deduction): In order to keep down the number of solution sheets, feel free to put answers to multiple questions on the same sheet. Write in legible English or Swedish (unreadable means no points). Motivate your answers, and clearly state any assumptions made. Write only on one side of the paper. Before handing in your exam, number and sort the sheets in task order. Write your anonymous registration code on every page.

Good Luck!

1/9

2013-08-28

## (2p) Solution: A T T T T F F F F B T T F F T T F F C T F T F T F T F AB T T F F F F F F (A B ) F F T T T T T T (A B ) C T F T T T T T T C F T F T F T F T BC T T F T T T F T result T T F T T T F T

2. For each of the following formulas, specify if it is a tautology, contradiction or neither. (No calculations needed, just the answer.) (a) (A B ) A B (b) A B (B A) (c) (A B ) C (d) (A C ) (A B C ) Solution: (a) contradiction, (b) tautology, (c) neither, (d) tautology 3. Let A and B be arbitrary logical formulas. Construct a formula C which contains A and B but not the equivalence connective such that C is a tautology if and only if A and B are equivalent. (1p) Solution: e.g. C = (A B ) (B A) 4. Construct a deductive proof of the following arguments validity. (A B ) B (B C ) A C (1p) (1p) (1p) (1p)

2/9

DIT725, SE&M, GU

2013-08-28

You may only use to the following equivalences and inference rules when constructing the proof. equivalence (X Y ) (X Y ) X X (X Y ) (Y X ) (X Y ) X Y name impl dn symm de morgan premises X Y, X X Y, Y X, Y X conclusion Y X X Y X Y name mp mt conj add

The deductive proof should consist of numbered lines with the hypotheses of the argument in the initial lines and the conclusion as the last line. Each derived formula should be motivated by the name of the rule and the premises that are used. (2p)

Solution: 1. 2. 3. 4. 5. 6. 7. 8. 9.

AB B B C B A BA BC A C AC

## hyp hyp hyp symm 1 impl 4 impl 3 mp 5 2 mp 6 2 conj 7 8

5. Assume you have a class called Node which represents a node in a binary tree. The class has, at least, the following member methods. Node leftChild() Node rightChild() int key() which gives the left child or null if no such exists which gives the left child or null if no such exists which gives the key stored in the node

The class is used to store a heap representing a priority queue. Write a recursive method in java or pseudo code which uses the above methods and checks, given the root node, whether a tree is a heap or not. The method should have the signature boolean isHeap(Node n) (2p)

Solution:

3/9

DIT725, SE&M, GU

2013-08-28

boolean isHeap(Node n) { if (n.leftChild() != null && n.leftChild().key() < n.key()) return false; if (n.rightChild() != null && n.rightChild().key() < n.key()) return false; return (n.leftChild() == null || isHeap(n.leftChild())) && (n.rightChild() == null || isHeap(n.rightChild())); } 6. Let Gn be the set of undirected, unlabelled, simple graphs containing n verteces. (a) What is the cardinality of G3 ? (1p) (b) What is the cardinality of {x|x G4 x is complete}? (1p) (c) What is the cardinality of {x|x G4 x is connected}? (1p)

## Solution: (a) 4, (b) 1, (c) 7

7. Consider a method isElem for deciding if a given integer is contained in an array of integers. The signature is boolean isElem(int n, int[] a) (a) With no assumptions on the array a what is the best time complexity (in terms of the length of a) a implementation of isElem can have? (1p) (b) Assuming that the array a is sorted, what is the best time complexity for an implementation? (1p) (c) Write an recursive implementation of isElem in java or pseudo code which assumes a to be sorted. It should have optimal time complexity. (2p)

Solution: Let m be the length of a. (a) O(m), (b) O(log (m)), (c) see course literature

4/9

DIT725, SE&M, GU 8. You start with an empty heap representing a priority queue.

2013-08-28

(a) Insert the numbers 9,12,8,5,4,2 (in that order) into the heap. What does it look like after inserting the numbers (as a binary tree)? (1p) (b) Now remove the minimum element. What does the heap look like after this operation? (1p)

Solution: (a) 2

12 (b)

12

## 9. Consider the following undirected graph with weights on the edges:

5/9

DIT725, SE&M, GU A 3 F 10 7 B 12 6 9 1 1 D 2 C 7 G 2

2013-08-28

E (a) Show the adjecency matrix representation of the graph (on an abstract level). (1p) (b) Illustrate how Dijkstras algorithm works when nding the shortest distances from node A to all nodes. Do this by constructing a table with one column for each node and one line for each iteration of the algorithm. The values in the table should be the so far shortest known distance from A to the given node. Use to denote for so far unreached nodes. The table should start like this. A B C D E F G 0 (2p) Solution: (a) C D E F G A 12 3 B 1 10 C 1 2 1 9 2 D 12 2 6 7 E 1 7 F 3 10 9 6 7 G 2 7 (b) A 0 0 0 0 0 0 0 B 13 13 13 12 12 C 12 11 11 11 11 D 12 9 9 9 9 9 E 10 10 10 10 10 F 3 3 3 3 3 3 G 16 16 13 13 A B

6/9

DIT725, SE&M, GU

2013-08-28

10. Let A = {1, 3, 5, 8}, B = {2, 3, 4, 6}, C = {3, 5, 7, 8, 9}. Compute (enumerate the elements of) the set (A B ) (C A). (1p)

Solution: {1, 2, 4, 6}

11. Let

## A = {n|n N+ (m)(m N+ 3m = n)} B = {5n|n N+ } C = {n|n N+ n < 50}

Compute A B C . (2p)

## Solution: {15, 30, 45}

12. This question is about the merge sort algorithm. (a) Show the algorithm as a recursive method in java or pseudo code. The signature should be int[] mergeSort(int[] a) (3p) (b) For n being 4, 8 and 16 respectively, what is the exact number of times your method is called (initial plus recursive calls) when used to sort an array of length n? (1p) (c) What is the time complexity of merge sort? (1p)

Solution: (a) See course literature, (b) typically 7, 15, 31 times, (c) O(n log (n)), where n is the length of a 3 2 3 2 1 13. Let A = and B = 2 5. Compute the matrices A B and B A (where 4 2 0 4 1 means matrix multiplication). (1p)

7/9

DIT725, SE&M, GU

2013-08-28

Solution:

17 10 3 17 17 AB = , B A = 26 14 2 16 18 16 10 4

14. For each of the following matrices, calculate its inverse, or, in case an inverse does not exist, state that this is the case. (a) 2 0 3 1 (1p) (b) 1 2 4 0 (1p)

## 15. Consider the following binary tree.

2

12

19

15

13

17

8/9

DIT725, SE&M, GU

2013-08-28

(a) Write down the sequence of node labels resulting from a post-order traversal of the tree. (1p) (b) Is there a kind of tree traversal (pre-order, in-order or post-order ) which for binary search trees visits the nodes in increasing label order? In that case, which kind of traversal? (1p)

9/9