Solution suggestions for examination of Logic, Algorithms and Data Structures,

Similar documents
Advanced Implementations of Tables: Balanced Search Trees and Hashing

Amortized analysis. Amortized analysis

Data Structures and Algorithms Winter Semester

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Heaps and Priority Queues

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

CS361 Homework #3 Solutions

Heaps Induction. Heaps. Heaps. Tirgul 6

Lists, Stacks, and Queues (plus Priority Queues)

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

Introduction to Computing II (ITI1121) FINAL EXAMINATION

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

An Introduction to Z3

Binary Search Trees. Motivation

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Fundamental Algorithms

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Assignment 5: Solutions

Notes on induction proofs and recursive definitions

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Lecture 14: Nov. 11 & 13

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

A Simple Implementation Technique for Priority Search Queues

INF2220: algorithms and data structures Series 1

Introduction to Computing II (ITI 1121) MIDTERM EXAMINATION

CSC236H Lecture 2. Ilir Dema. September 19, 2018

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Part IA Algorithms Notes

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Functional Data Structures

Module 1: Analyzing the Efficiency of Algorithms

Discrete Mathematics Review

ENS Lyon Camp. Day 5. Basic group. C October

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

Central Algorithmic Techniques. Iterative Algorithms

Algorithms Theory. 08 Fibonacci Heaps

Stacks. Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d

(c) Give a proof of or a counterexample to the following statement: (3n 2)= n(3n 1) 2

Elementary Sorts 1 / 18

We introduce one more operation on sets, perhaps the most important

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Data Structures and and Algorithm Xiaoqing Zheng

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Computing Static Single Assignment (SSA) Form

Searching. Constant time access. Hash function. Use an array? Better hash function? Hash function 4/18/2013. Chapter 9

REVIEW QUESTIONS. Chapter 1: Foundations: Sets, Logic, and Algorithms

Advanced Data Structures

1. Introduction Bottom-Up-Heapsort is a variant of the classical Heapsort algorithm due to Williams ([Wi64]) and Floyd ([F64]) and was rst presented i

Reading and Writing. Mathematical Proofs. Slides by Arthur van Goetham

Queues. Principles of Computer Science II. Basic features of Queues

Binary Decision Diagrams. Graphs. Boolean Functions

CS Data Structures and Algorithm Analysis

Advanced Data Structures

INTRODUCTION TO HASHING Dr. Thomas Hicks Trinity University. Data Set - SSN's from UTSA Class

CS/IT OPERATING SYSTEMS

CTL satisfability via tableau A C++ implementation

Directed Graphs (Digraphs) and Graphs

Data Structures and Algorithms " Search Trees!!

CS 250/251 Discrete Structures I and II Section 005 Fall/Winter Professor York

Part I: Definitions and Properties

Hash Tables. Direct-Address Tables Hash Functions Universal Hashing Chaining Open Addressing. CS 5633 Analysis of Algorithms Chapter 11: Slide 1

data structures and algorithms lecture 2

Introduction to Hash Tables

Searching, mainly via Hash tables

Array-based Hashtables

A Lecture on Hashing. Aram-Alexandre Pooladian, Alexander Iannantuono March 22, Hashing. Direct Addressing. Operations - Simple

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

Introduction to Hashtables

Priority queues implemented via heaps

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

Equalities and Uninterpreted Functions. Chapter 3. Decision Procedures. An Algorithmic Point of View. Revision 1.0

Lecture 2 September 4, 2014

Verifying Java-KE Programs

CSE 311: Foundations of Computing. Lecture 10: Set Operations & Representation, Modular Arithmetic

Lecture 4: Stacks and Queues

Linear-Time Algorithms for Finding Tucker Submatrices and Lekkerkerker-Boland Subgraphs

Computation and Inference

Collision. Kuan-Yu Chen ( 陳冠宇 ) TR-212, NTUST

Interpolation. Seminar Slides. Betim Musa. 27 th June Albert-Ludwigs-Universität Freiburg

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

CPSC 320 Sample Final Examination December 2013

Review Of Topics. Review: Induction

Lecture 5: Hashing. David Woodruff Carnegie Mellon University

CSCE 750, Spring 2001 Notes 2 Page 1 4 Chapter 4 Sorting ffl Reasons for studying sorting is a big deal pedagogically useful Λ the application itself

Divide-and-Conquer Algorithms Part Two

Data Structures. Outline. Introduction. Andres Mendez-Vazquez. December 3, Data Manipulation Examples

Transcription:

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