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

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

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

Problem Set 4 Solutions

CS 21 Decidability and Tractability Winter Solution Set 3

I Credit I Max I Problem 1 20 Problem 2 20 Problem 3 20 Problem 4 20

Data Structures and Algorithms Winter Semester

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Algorithms and Data Structures 2014 Exercises week 5

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

Lecture 4: Stacks and Queues

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 ;

CS361 Homework #3 Solutions

CS177 Fall Midterm 1. Wed 10/07 6:30p - 7:30p. There are 25 multiple choice questions. Each one is worth 4 points.

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

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

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

Amortized analysis. Amortized analysis

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

CSCI-141 Exam 1 Review September 19, 2015 Presented by the RIT Computer Science Community

Since CFL is closed under intersection with regular languages, we can do

CS325: Analysis of Algorithms, Fall Midterm

Algorithms Exam TIN093 /DIT602

Discrete Mathematics CS October 17, 2006

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

Friday Four Square! Today at 4:15PM, Outside Gates

Tutorial 4. Dynamic Set: Amortized Analysis

Recursion and Induction

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Abstract Data Types. EECS 214, Fall 2017

Examination paper for TDT4120 Algorithms and Data Structures

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)

Theory Bridge Exam Example Questions

Part I: Definitions and Properties

Outline. policies for the second part. with answers. MCS 260 Lecture 10.5 Introduction to Computer Science Jan Verschelde, 9 July 2014

Exam EDAF May 2011, , Vic1. Thore Husfeldt

8.3. SPECIAL METHODS 217

Recursion: Introduction and Correctness

CS 1110: Introduction to Computing Using Python Sequence Algorithms

MATH 521, WEEK 2: Rational and Real Numbers, Ordered Sets, Countable Sets

Lecture 8: Decision-making under total uncertainty: the multiplicative weight algorithm. Lecturer: Sanjeev Arora

Please give details of your answer. A direct answer without explanation is not counted.

CS 170 Algorithms Fall 2014 David Wagner MT2

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

Algorithms and Their Complexity

CSEE W3827 Fundamentals of Computer Systems Homework Assignment 3

Lecture Notes for Chapter 17: Amortized Analysis

Application: Bucket Sort

Divide-and-Conquer Algorithms Part Two

COP-5555 PROGRAMMING LANGUAGEPRINCIPLES NOTES ON THE MECHANICAL EVALUATION OF APPLICATIVE EXPRESSSIONS

Computer Science. Questions for discussion Part II. Computer Science COMPUTER SCIENCE. Section 4.2.

Putnam Greedy Algorithms Cody Johnson. Greedy Algorithms. May 30, 2016 Cody Johnson.

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

1 Definition of a Turing machine

6.045J/18.400J: Automata, Computability and Complexity. Quiz 2. March 30, Please write your name in the upper corner of each page.

Pushdown Automata. Chapter 12

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

5.3 Conditional Probability and Independence

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

Algorithms. NP -Complete Problems. Dong Kyue Kim Hanyang University

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

Decentralized Control of Discrete Event Systems with Bounded or Unbounded Delay Communication

Math 391: Midterm 1.0 Spring 2016

Heaps Induction. Heaps. Heaps. Tirgul 6

Graduate Analysis of Algorithms Dr. Haim Levkowitz

CSE 105 THEORY OF COMPUTATION

CSE613: Parallel Programming, Spring 2012 Date: May 11. Final Exam. ( 11:15 AM 1:45 PM : 150 Minutes )

CS 350 Algorithms and Complexity

CSEE W3827 Fundamentals of Computer Systems Homework Assignment 3 Solutions

CS 350 Algorithms and Complexity

CpSc 421 Final Exam December 15, 2006

Solutions to Old Final Exams (For Fall 2007)

Dynamics Final Report

Algorithms and Networking for Computer Games

1 More finite deterministic automata

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

Introduction to Languages and Computation

E23: Hotel Management System Wen Yunlu Hu Xing Chen Ke Tang Haoyuan Module: EEE 101

8. More on Iteration with For

9A. Iteration with range. Topics: Using for with range Summation Computing Min s Functions and for-loops A Graphics Applications

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

Complexity Theory Part I

MCS 260 Exam 2 13 November In order to get full credit, you need to show your work.

0.1 Motivating example: weighted majority algorithm

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

PRAM lower bounds. 1 Overview. 2 Definitions. 3 Monotone Circuit Value Problem

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

A Note on Turing Machine Design

Welcome to CSE21! Lecture B Miles Jones MWF 9-9:50pm PCYN 109. Lecture D Russell (Impagliazzo) MWF 4-4:50am Center 101

You have 3 hours to complete the exam. Some questions are harder than others, so don t spend too long on any one question.

Theory of Computation

CS 170 Algorithms Spring 2009 David Wagner Final

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Informed Search. Chap. 4. Breadth First. O(Min(N,B L )) BFS. Search. have same cost BIBFS. Bi- Direction. O(Min(N,2B L/2 )) BFS. have same cost UCS

Exam EDAF August Thore Husfeldt

Module 9: Tries and String Matching

Lecture: Analysis of Algorithms (CS )

Informed Search. Day 3 of Search. Chap. 4, Russel & Norvig. Material in part from

CS 580: Algorithm Design and Analysis

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Transcription:

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Linked Lists 1. You are given the linked list: 1 2 3. You may assume that each node has one field called value and one called next. (a) 1 points to 2 and 2 points to 3. What does 3 point to? The implementer may cause the 3 node to point to either None or a sentinel node. (b) Draw out the linked list structure and add a 5 to the end. +-------------------+ head: size: 4 +--- ---------------+ V +----------+ +----------+ +----------+ +------------+ value: 1 value: 2 value: 3 value: 5 next: -------> next: -------> next: -------> next: None +----------+ +----------+ +----------+ +------------+ OR +-------------------+ head: size: 4 +--- ---------------+ V +----------+ +----------+ +----------+ +----------+ +-----------------+ value: 1 value: 2 value: 3 value: 5 value: SENTINEL next: -------> next: -------> next: -------> next: -------> +----------+ +----------+ +----------+ +----------+ +-----------------+ (c) Write pseudocode to add an element onto the end of a linked list. def append ( l i n k e d l i s t, value ) : newend = new node newend. value = value newend. next = None # ( or SENTINEL) cur = l i n k e d l i s t. head while cur i s not end o f l i n k e d l i s t : cur = cur. next cur. next = newend 1

(d) Let s say we want to add a 4 to the list from 1b. What is a procedure for inserting this value at a certain position in the linked list? (There are many possibilities.) Use some variation of this strategy: Find the preceding element s node, link its next node to the new node containing 4, then link that new node to what was the following node. Binary Search using Python Lists 2. Given the sorted list [1, 4, 9, 16, 25, 69, 420, 1337], write out the steps that a binary search would take to find the number 69. [ 1, 4, 9, 1 6, 2 5, 6 9, 4 2 0, 1 3 3 7 ] [ 1, 4, 9, 1 6, 2 5, 6 9, 4 2 0, 1 3 3 7 ] [ 1, 4, 9, 1 6, 2 5, 6 9, 4 2 0, 1 3 3 7 ] Naive Sorting 3. Below is Python code for a function that performs an insertion sort and prints data after each iteration of the for loop. 1 def i n s e r t i o n s o r t ( data ) : 2 for marker in range ( 1, len ( data ) ) : 3 v a l = data [ marker ] 4 i = marker 5 while i > 0 and data [ i 1] > v a l : 6 data [ i ] = data [ i 1] 7 i = 1 8 data [ i ] = v a l 9 print ( data ) (a) Write out what the function will print for the input list: [3,2,7,1]. [ 2, 3, 7, 1 ] [ 2, 3, 7, 1 ] [ 1, 2, 3, 7 ] (b) What is the sort algorithm s time complexity? O(N 2 ) 2

Stacks and Queues 4. Suppose we wanted to implement a stack by using a linked list. Define linked-list operations that would correspond to push() and pop(). push( stack, element ) insertfront( linked-list, element ) pop( stack ) removefront( linked-list ) 5. It is possible to implement both stacks and queues using only simple Python lists. (a) Write the following functions that implement stack functionality atop a Python list. Your stack must provide the following functionality: push(lst, elm) Push elm onto the top of the stack pop(lst) Return the top element of the stack and remove it from the stack isempty(lst) Return whether the stack is empty peek(lst) Return the top element of the stack without modifying the stack 1 def push ( l s t, val ) : 2 l s t. append ( val ) 3 def pop ( l s t ) : 4 return l s t. pop ( ) 5 def peek ( l s t ) : 6 return l s t [ 1] 7 def isempty ( l s t ) : 8 return ( len ( l s t ) == 0) (b) Write the following methods to create a queue in similar fashion to previous question (with a Python list as the data structure managing elements under the hood ): enqueue(lst, val) - put a value into the queue dequeue(lst) - take a value out of the queue 1 def enqueue ( l s t, val ) : 2 l s t. append ( val ) 3 def dequeue ( l s t ) : 4 l s t. pop ( 0 ) (c) Which of the data structures you implemented is more efficient and why? Give a better way to implement the slower structure, and discuss how this would change the time complexity of its operations. Because the queue must be able to modify both ends of the list, it pays an O(n) cost to remove the beginning element during each dequeue operation. This could be reduced to O(1) by using a linked list instead of a Python list. 3

Hashing and Hash Tables 6. Chris made a mistake in his hash table implementation! 1 from r i t l i b import 2 3 class WonkyHashTable ( s t r u c t ) : 4 s l o t s = ( ( int, s i z e ), ( l i s t, t a b l e ) ) 5 6 def createwonkyhashtable ( s i z e =5): 7 t a b l e = [ None for i in range ( s i z e ) ] 8 return WonkyHashTable ( s i z e, t a b l e ) 9 10 def add element ( wonkyhashtable, element ) : 11 hash = bad hash ( wonkyhashtable, element ) 12 wonkyhashtable. t a b l e [ hash ] = element 13 14 def c o n t a i n s ( wonkyhashtable, element ) : 15 return element in wonkyhashtable. t a b l e 16 17 def bad hash ( wonkyhashtable, element ) : 18 return len ( element ) % len ( wonkyhashtable. t a b l e ) 19 20 # main program 21 22 h t a b l e = createwonkyhashtable ( ) 23 for elm in I w r e s t l e d a bear once. s p l i t ( ) : 24 add element ( htable, elm ) (a) Show what the hash table looks like after the for loop on line 23 completes. [ None, a, None, w r e s t l e d, once ] (b) What is wrong with the code? What can we do to make the function behave as Chris expects it to behave? The issue is on line 12. Whenever bad hash() dictates that an element should be placed in an occupied bucket, that bucket s contents get overwritten! Change wonkyhashtable. table [ hash ] = to element while wonkyhashtable. table [ hash ] is not None : hash = ( hash + 1) % wonkyhashtable. size wonkyhashtable. table [ hash ] = element Note that this code assumes that the specified size of the table is large enough to hold all elements. This is usually not the case. (c) Draw the table of the properly behaving hash function. [ once, I, a, w r e s t l e d, bear ] 4

(d) Assuming that this hash table will only be used on strings, is the hashing function being used a good one? Why or why not? No: It ignores the fact that most English words are the roughly the same length. The number of collisions is expected to be massive. We should take advantage of the characters in the input strings, not the number of characters. Structures 7. For each of the following structures with the given attributes, write the structure code and a corresponding maker function: A Hotel name (a string) rooms (a list of Room structures) location (a string) A Room number (an integer) capacity (an integer) price per night (a float) 1 from r i t l i b import 2 3 class Hotel ( s t r u c t ) : 4 s l o t s = ( ( str, name ), ( l i s t, rooms ), ( str, l o c a t i o n ) ) 5 6 def c r e a t e H o t e l (name, rooms, l o c a t i o n ) : 7 return Hotel (name, rooms, l o c a t i o n ) 8 9 class Room( s t r u c t ) : 10 s l o t s = ( ( int, number ), ( int, c a p a c i t y ), ( float, p r i c e ) ) 11 12 def createroom ( number, capacity, p r i c e ) : 13 return Room( number, capacity, p r i c e ) 5

Greedy Algorithms 8. Given that an algorithm is greedy, is it guaranteed to return an optimal solution? NO. Greedy algorithms always choose the current best solution, which is not necessarily the overall best solution! 9. In the game Black and White 1, the player is faced with a row of identical double-sided chips. You can probably guess what colors the two sides of each chip are. The objective is to flip as many chips as necessary so their exposed colors match that of a target pattern. The catch? Reordering the chips is said to be Impossible by those who seem to know what they re talking about. The real catch? Flipping a group of consecutive tiles can be accomplished in a single action. If every flip takes one action, write a function bwmoves that, given a starting pattern and target pattern as equal-length strings, returns the minimum number of actions required to get them to match. For instance, bwmoves( BBWBBWBBBB, WWWWWBBWWB ) should return 3. 1 def bwmoves( s t a r t, t a r g e t ) : 2 a c t i o n s=0 3 f i r s t =0 4 for index in range ( len ( s t a r t ) ) : 5 i f s t a r t [ index]== t a r g e t [ index ] : 6 i f f i r s t!= index : # Each f l i p works up to ( but not i n c l u d i n g ) 7 a c t i o n s+=1 # the index p o i n t e r. I f f i r s t==index, t h a t s 8 f i r s t=index+1 # 0 elements, so t h e r e i s nothing to f l i p. 9 # ( i. e. There were two no f l i p s in a row. ) 10 i f s t a r t [ 1]!= t a r g e t [ 1]: 11 a c t i o n s+=1 12 return a c t i o n s 1 Special thanks to Professor Butler for unwittingly allowing us to rip off his problem. 6