School of Computer Science

Similar documents
School of Computer Science

Data Structures and Algorithms 2018

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

FUNCTIONAL PROGRAMMING

Topic 5: Examples and higher-order functions

F# - LISTS. In this method, you just specify a semicolon-delimited sequence of values in square brackets. For example

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

CS 321 Programming Languages

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Shell CSCE 314 TAMU. Functions continued

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Computer Science Spring 2005 Final Examination, May 12, 2005

Programovací jazyky F# a OCaml. Chapter 5. Hiding recursion using function-as-values

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Programming Languages and Compilers (CS 421)

Code No: R Set No. 1

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

INF121: Functional Algorithmic and Programming

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS3110 Spring 2016 Lecture 6: Modules

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Quiz 2 CSCI 1103 Computer Science I Honors KEY. Tuesday November 8, 2016 Instructor Muller Boston College. Fall 2016

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming in Haskell Aug-Nov 2015

CS 445: Data Structures Final Examination: Study Guide

Abstraction and Specification

List Processing in Ocaml

COMP 352 FALL Tutorial 10

COS 226 Midterm Exam, Spring 2009

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Course Review for Finals. Cpt S 223 Fall 2008

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

CSCI 262 Data Structures. Recursive Function Analysis. Analyzing Power. Analyzing Power. Analyzing Power 3/31/2018

INSTITUTE OF AERONAUTICAL ENGINEERING

Total Score /1 /20 /41 /15 /23 Grader

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

( ) n 3. n 2 ( ) D. Ο

Chapter 3 Linear Structures: Lists

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Total No. of Questions :09] [Total No. of Pages : 02. II/IV B.Tech. DEGREE EXAMINATIONS, NOV/DEC First Semester CSE/IT DATA STRUCTURES USING C

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING ADVANCED DATA STRUCTURES AND ALGORITHMS EXAM EXAMINATION JUNE 2014

List Functions, and Higher-Order Functions

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Sample Exam; Solutions

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

CSCE 2014 Final Exam Spring Version A

Chapter 3 Linear Structures: Lists

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

MLR Institute of Technology

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Solutions to Exam Data structures (X and NV)

SNU Programming Language Theory

CPSC 331 Term Test #2 March 26, 2007

CS-141 Exam 2 Review April 4, 2015 Presented by the RIT Computer Science Community

15 210: Parallel and Sequential Data Structures and Algorithms

COMP Data Structures

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Programming Languages

Computer Science E-22 Practice Final Exam

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

COP 3502 Spring 2018 Study Union Review

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

CSE341, Fall 2011, Midterm Examination October 31, 2011

CS421 Spring 2014 Midterm 1

CSE341 Spring 2016, Midterm Examination April 29, 2016

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Closed Book Examination. One and a half hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Wednesday 20 th January 2010

Gain familiarity and practice with folding and mapping. The following supplementary materials may be helpful in completing this assignment:

Modules and Representation Invariants

Computer Algorithms. Introduction to Algorithm

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

The University of Nottingham

CSE373 Spring 2015: Final

Design and Analysis of Algorithms

COS 226 Midterm Exam, Spring 2011

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

CSCI 2041: First Class Functions

Foundations of Computation

CSE 341 Sample Midterm #2

CSE341 Spring 2016, Midterm Examination April 29, 2016

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

02157 Functional Programming Tagged values and Higher-order list functions

CSCI-GA Scripting Languages

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

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

ECE 250 Algorithms and Data Structures

Transcription:

A27217 No calculator permitted in this examination School of Computer Science First Year - BSc Artificial Intelligence and Computer Science First Year - BSc Natural Sciences First Year - BSc Computer Science First Year - MEng Computer Science/Software Engineering 06 22754 Foundations of Computer Science Summer Examinations 2012 Time allowed: 3 hr [Answer ALL Questions] [Use the Separate Answer Book for EACH Part] -1- Turn over

Part A [Use the Separate Answer Book for THIS Section] IN THIS SECTION YOU ARE ALLOWED TO USE ALL THE FUNCTIONS IN THE APPENDIX 1. Write an OCaml function called cd which counts the number of elements which have consecutive duplicates. What is the asymptotic complexity of your function? For example: cd [1; 2; 3; 3; 4; 5; 5] = 2 For example: cd [1; 2; 2; 2; 3] = 2 2. Using symbolic evaluation and showing all steps of computation, calculate map (fun n -> n+1) [1; 2] where map is defined as let rec map f = function [ ] -> [ ] x::xs -> (f x)::(map f xs) 3. Write an OCaml function sf which orders the elements of a list according to their increasing frequency. What is the asymptotic complexity of your function? For example: sf [1; 2; 3; 4; 2; 3; 4; 4] = [1; 2; 3; 4] A27217-2- Turn over

4. Consider this function, which "flips" a binary tree: let rec flip = function Empty -> Empty Node (a, l, r) -> Node (a, flip r, flip l) Using structural induction on trees show that flip is its own inverse, which means flip (flip t) = t for any tree t. 5. (a) Explain the term amortised complexity. Give the 2-list implementation of queues ("Banker's queues"). Explain why your implementation in is superior to the naive implementation below: Open List type a queue = a list let enq (x, xs) = xs @ [x] let deq xs = (hd xs, tl xs) A27217-3- Turn over

Part B [Use the Separate Answer Book for THIS Section] 6. (a) Give an inductive definition of a Binary Tree and of a Quad Tree. [2%] (d) Explain how a Quad Tree can be used to represent a square gray-scale image. Define appropriate primitive operators for Quad Trees and use them to create an efficient pseudocode algorithm that turns a gray-scale image upside down. [6%] What additional conditions must be satisfied for a Binary Tree to be a valid Binary Search Tree? [2%] Draw the Binary Search Tree that results from inserting the items [5 9 7 3 1 8 4 2] in that order into an initially empty tree. Then draw the tree that results from removing item 5 from that tree using the standard Binary Search Tree delete algorithm. [4%] 7. (a) Describe in words the general idea underlying the Quicksort algorithm for sorting an array of items. [3%] (d) What are the average and worst case time complexities of the Quicksort algorithm? Give a simple example of how the worst case can easily arise, and how it can be avoided. [4%] What does it mean to say that a sorting algorithm is stable? Discuss the stability of the Quicksort algorithm and the relation of stability to the computational costs of the algorithm. [3%] Suppose you had data about seven million customers to sort according to how much they had spent, but only needed to know the fifty who had spent the most. Explain how those details would affect your choice of sorting algorithm. [3%] A27217-4- Turn over

8. (a) Explain what is meant by the terms hash table, hash collision, linear probing and secondary hashing. [3%] (d) Comment on the advantages and disadvantages of using hash tables for data storage. [3%] Suppose four digit keys are to be stored in a hash table represented as an array of size 13. The hash function is simply the first digit. Why is that not a sensible choice of hash function? Draw the initially empty hash table and insert the following keys into it using linear probing: 3487, 1346, 4143, 3571, 2583 and 0937. [3%] Suggest an improved hash function for the above type of data and explain why it is better. [2%] 9. (a) What does it mean to say a graph is planar? Suggest a practical application where that property is important. Draw the graphs K 5 and K 3,3. Why are these graphs important in the context of planarity? [4%] Outline the general idea of Dijkstra s algorithm for finding the shortest path between two nodes in a weighted graph. Apply the algorithm to find the shortest path from A to F in the following weighted graph, showing the computations involved at each stage. [8%] A27217-5- Turn over

APPENDIX : THE LIST MODULE (SELECTED FUNCTIONS) val length : 'a list -> int Return the length (number of elements) of the given list. val hd : 'a list -> 'a Return the first element of the given list. Raise Failure "hd" if the list is empty. val tl : 'a list -> 'a list Return the given list without its first element. Raise Failure "tl" if the list is empty. val nth : 'a list -> int -> 'a Return the n-th element of the given list. The first element (head of the list) is at position 0. Raise Failure "nth" if the list is too short. Raise Invalid_argument"List.nth" if n is negative. val rev : 'a list -> 'a list List reversal. val append : 'a list -> 'a list -> 'a list Catenate two lists. Same function as the infix operator @. Not tail-recursive (length of the first argument). The @ operator is not tail-recursive either. val rev_append : 'a list -> 'a list -> 'a list List.rev_append l1 l2 reverses l1 and concatenates it to l2. This is equivalent to List.rev l1 @ l2, but rev_append is tail-recursive and more efficient. val map : ('a -> 'b) -> 'a list -> 'b list List.map f [a1;...; an] applies function f to a1,..., an, and builds the list [f a1;...; f an] with the results returned by f. Not tail-recursive. val rev_map : ('a -> 'b) -> 'a list -> 'b list List.rev_map f l gives the same result as List.rev (List.map f l), but is tail-recursive and more efficient. val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a List.fold_left f a [b1;...; bn] is f (... (f (f a b1) b2)...) bn. val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b List.fold_right f [a1;...; an] b is f a1 (f a2 (... (f an b)...)). Not tail-recursive. val for_all : ('a -> bool) -> 'a list -> bool for_all p [a1;...; an] checks if all elements of the list satisfy the predicate p. That is, it returns (p a1) && (p a2) &&... && (p an). val exists : ('a -> bool) -> 'a list -> bool exists p [a1;...; an] checks if at least one element of the list satisfies the predicate p. That is, it returns (p a1) (p a2)... (p an). A27217-6- Turn over

val mem : 'a -> 'a list -> bool mem a l is true if and only if a is equal to an element of l. val find : ('a -> bool) -> 'a list -> 'a find p l returns the first element of the list l that satisfies the predicate p. Raise Not_found if there is no value that satisfies p in the list l. val filter : ('a -> bool) -> 'a list -> 'a list filter p l returns all the elements of the list l that satisfy the predicate p. The order of the elements in the input list is preserved. val find_all : ('a -> bool) -> 'a list -> 'a list find_all is another name for List.filter. val split : ('a * 'b) list -> 'a list * 'b list Transform a list of pairs into a pair of lists: split [(a1,b1);...; (an,bn)] is ([a1;...; an], [b1;...; bn]). Not tail-recursive. val combine : 'a list -> 'b list -> ('a * 'b) list Transform a pair of lists into a list of pairs: combine [a1;...; an] [b1;...; bn] is [(a1,b1);...; (an,bn)]. Raise Invalid_argument if the two lists have different lengths. Not tailrecursive. val sort : ('a -> 'a -> int) -> 'a list -> 'a list Sort a list in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see Array.sort for a complete specification). For example, compare is a suitable comparison function. The resulting list is sorted in increasing order. List.sort is guaranteed to run in constant heap space (in addition to the size of the result list) and logarithmic stack space. The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space. val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list Same as List.sort, but the sorting algorithm is guaranteed to be stable (i.e. elements that compare equal are kept in their original order). The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space. val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list Same as List.sort or List.stable_sort, whichever is faster on typical input. val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list Merge two lists: Assuming that l1 and l2 are sorted according to the comparison function cmp, merge cmp l1 l2 will return a sorted list containting all the elements of l1 and l2. If several elements compare equal, the elements of l1 will be before the elements of l2. Not tail-recursive (sum of the lengths of the arguments). A27217-7- End of paper