Faculty of Science FINAL EXAMINATION

Similar documents
Faculty of Science FINAL EXAMINATION

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2010

Solutions to relevant spring 2000 exam problems

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

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

UNIVERSITY REGULATIONS

Second Examination Solution

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

CS171 Final Practice Exam

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

CS171 Final Practice Exam

INSTITUTE OF AERONAUTICAL ENGINEERING

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Figure 4.1: The evolution of a rooted tree.

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

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

CS301 - Data Structures Glossary By

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CS 146 Midterm Exam 2 Grade: Professors Howard, Fine, and Howard have proposed the following sorting algorithm:

Homework Assignment #3 Graph

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Homework No. 4 Answers

COMP 103. Data Structures and Algorithms

Draw a diagram of an empty circular queue and describe it to the reader.

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Recursively Defined Functions

Chapter 4: Trees. 4.2 For node B :

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

CS 373: Combinatorial Algorithms, Spring 1999

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

COMP 250 Fall recurrences 2 Oct. 13, 2017

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

COMP 250 Midterm #2 March 11 th 2013

CS302 Data Structures using C++

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Trees Algorhyme by Radia Perlman

CSL 201 Data Structures Mid-Semester Exam minutes

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Computer Science Foundation Exam

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Midterm solutions. n f 3 (n) = 3

CS24 Week 8 Lecture 1

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

CS171 Midterm Exam. October 29, Name:

W4231: Analysis of Algorithms

Midterm 2. Read all of the following information before starting the exam:

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Data Structures Question Bank Multiple Choice

Computer Science Foundation Exam

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

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

Midterm CSE 21 Fall 2012

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

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

DATA STRUCTURES AND ALGORITHMS

Technical University of Denmark

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

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Info 2950, Lecture 16

Fundamental Graph Algorithms Part Four

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Final Exam in Algorithms and Data Structures 1 (1DL210)

CSCE f(n) = Θ(g(n)), if f(n) = O(g(n)) and f(n) = Ω(g(n)).

CSE 332, Spring 2010, Midterm Examination 30 April 2010

COMP 250 Fall heaps 2 Nov. 3, 2017

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

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

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Analysis of Algorithms

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Final Exam in Algorithms and Data Structures 1 (1DL210)

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Solutions to Exam Data structures (X and NV)

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

Sorting and Selection

Programming II (CS300)

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Umesh Vazirani February 13, Midterm 1

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Lecture 3. Recurrences / Heapsort

Transcription:

Faculty of Science FINAL EXAMINATION COMPUTER SCIENCE COMP 250 INTRODUCTION TO COMPUTER SCIENCE Examiner: Prof. Michael Langer April 20, 2009 Associate Examiner: Mr. Joseph Vybihal 9 A.M. 12 P.M. Instructions: Read all questions before you start. The number of points per question is unrelated to the difficulty of the questions, so use your time wisely. The exam is five pages (doublesided), including this cover page. There are seven questions, worth a total of 30 points. Answer all questions in the exam book. You may keep the exam question sheet. No calculators, notes, or books are allowed. 1

COMP 250 1. (3 points) Consider the following binary numbers, which are each between 0 and 1: p 1 =.0011, p 2 =.0101, p 3 =.0001, p 4 =.0101, p 5 =.001 Define F i = i p k, i = 1, 2, 3, 4, 5. k=1 (a) Write each F i as a binary number. (b) Draw a complete binary tree of height h = 4 and label the leaves with labels from {1,...,5} increasing left to right, such that p i is the fraction of leaves with label i. 2. (2 points) (a) Describe what the following recursive method does. The method has one parameter which is a reference to a node in a list. myprintmethod(listnode node){ if (node.prev!= null){ myprintmethod( node.prev ); } System.out.println(" " + node.tostring()); } (b) Give and solve a recurrence relation for the runtime of this method in terms of the number n of nodes in the list. 2

COMP 250 3. (5 points) State the formal definitions of f(n) is O(g(n)), and f(n) is not O(g(n)). Use these formal definitions to prove the following: (a) 3n + 8 is O(n). (b) 3n + 8 is not O(1). (c) 6n is O(2 n ). (You must prove this one by induction.) You will lose marks if your definitions or proofs take the limit as n. 4. (5 points) What is the O( ) for the runtime of the following operations, in terms of the number of elements n in the data structure? If you are unsure, then state any assumptions you make. Note: get returns an element but does not remove it. (a) Get the i th element from an array. (b) Insert a new element into a sorted array. (c) Add an element to the front of a linked list. (d) Get the i th element from a linked list. (e) Remove the i th element from a linked list. (f) Add an element to a minheap. (g) Get the minimum element from a minheap. (h) Get the maximum element from a minheap. For (f)-(h), assume the heap is represented using an array. 3

COMP 250 5. (3 points) (a) Show the final contents a queue, after the following sequence of operations have been performed. Be sure to indicate the front and back. initialize empty queue enqueue(a) enqueue(b) enqueue(c) dequeue() enqueue(d) dequeue() enqueue(e) enqueue(f) dequeue() enqueue(g) enqueue(h) (b) Suppose we implement the queue as a circular queue, using an array that is initialized with size m = 4. Show the contents of the array after each of the above operations have been performed. Note that the number of enqueue operations is five more than the number of dequeue operations, so it will be necessary at some point to increase the size of the array. 6. (4 points) (a) What is the maximum number of nodes in a binary tree of height h? (b) What is the sum of the depths of all nodes in a binary tree of height h, assuming all levels of the tree are full? It is sufficient to write a summation expression here. You are not required to evaluate this summation. (c) Give a recursive algorithm NumNodes(node) for computing the number of nodes in a tree whose root node is the argument. The tree is not necessarily a binary tree. (d) Give a recursive algorithm SumDepths(... ) for computing the sum of the depths of all nodes in a rooted tree. You will need to choose the argument(s) appropriately. As in (c), the tree is not necessarily binary. 4

7. (8 points) COMP 250 The figure below shows a set of rectangles whose overlap relationships can be represented with a directed graph. Each rectangle is represented by one vertex, and there is a directed edge whenever one rectangle overlaps another rectangle. For example, there is an edge (A,B). F G GH A D C B E (a) Give the adjacency list for this graph. The vertices must be ordered alphabetically. (b) Draw the graph, labelling the vertices A,B,C,D,E,F,G,H. (c) Give the ordering of vertices visited in a breadth first search traversal of the graph, starting from vertex C. Show the BFS tree. NOTE: In this question and the next, there is only one answer since you must order the vertices alphabetically. (d) Give the ordering of vertices visited in a depth first search traversal, starting from vertex C. Show the DFS tree. (e) Give a valid topological sorting of the vertices in this graph. (f) Give a new example having four rectangles I,J,K,L, such that corresponding graph contains a cycle. Draw the rectangles and the graph. 5

1.a) F1 =.0011 F2 =.1000 F3 =.1001 F4 =.1110 F5 = 1.0000 b) The labels are 1112222234444455. MARKING SCHEME: (3 points in total) 2 points for the F values. 1 point for the labels ---------------------------------------------------------------- 2. a) It prints the nodes in the list, starting from the front of the list and up to (and including) the argument node. b) t(n) = c1 + t(n-1) = c1 (n-1) + c2 MARKING SCHEME: (2 points in total) 1 point for (a). 1 point for (b). 0.5 point for t(n) = 1 + t(n-1) 0.5 point for saying t(n) is O(n). (0 points for the base case, i.e. we ignored it..) ---------------------------------------------------------------- 3. a) 3n + 8 <= 3n + 8n for n>=1 = 11n for n>=1 so take n0 = 1, c = 11. b) I want to show that no matter what c or n0 I choose, there exists an n> n0 such that 3n + 8 > c. 3n + 8 > c <-> n > (c - 8)/3 so I just take n > (c-8)/3 c) I want to show there exists c, n0 such that 6n <= c 2^n for any n > n0. 6

Proving this by induction is subtle. I start by choosing c=6 and n0 = 1. Then, the statement I want to prove by induction is: "6n <= 6 2^n for all n >=1", which is equivalent "n <= 2^n for all n >=1". To prove the latter by induction is easy: the base case n=1 is trivial. Next assume for n=k (inducation hypothesis) and prove for n=k+1. n+1 <= 2^n + 1 (by induction hypothesis) <= 2^n * 2 for any n (obvious) = 2^{n+1}. MARKING SCHEME: Definitions: 1 point (0.5 for each) a) 1 point (assuming the definition is stated correctly, and is used to provide the result) b) 1 points (again, assuming the definition is stated correctly) c) 2 points (0.5 points if only the base case was given, but rest was confused) ----------------------------------------------------------- 4 a) 1 b) n (you need to shift) c) 1 d) n e) n f) lg n g) 1 h) n MARKING SCHEME: Because you could guess O(1), O(n), O(lg n) on each question, we marked this question by taking subtracting the number of 7

incorrect answers (including blanks) from the number of questions (8). Thus if you got only 3 out of 8 correct (5 incorrect), then you received a grade of 0/5. If you got 6 out of 8 correct (i.e. 2 incorrect), then you received a grade of 3/5). ---------------------------------------------------------------- 5. a) defgh (with d at front and h and back) b) a*** ab** abc* *bc* *bcd **cd e*cd efcd (c front, f back) ef*d efgd (d front, g back) defgh (expand and shift with d at front) MARKING SCHEME: 1 point for (a) and 2 points for (b), the latter being broken down into 1 point for correct add/delete and 1 point for correct expansion. ---------------------------------------------------------------- 6. a) 2^{h+1} - 1 b) sum_{i=0}^h i 2^i c) NumNodes(node){ if node == null return 0 else{ sum = 1 for each child c of node // does nothing if sum = sum + NumNodes(c) // there are no children return sum } } 8

d) SumDepths(node,depth){ if node == null return 0 else{ sum = depth for each child c of node sum = sum + SumDepths(c,depth+1) return sum } MARKING SCHEME: 1 point for each. For (c),(d), no points were deducted if the null case was ignored. -------------------------------------------------------------- 7.) a) A - B B - C - E,F,G D - E - D,H F - G - D H - B,F b) not shown c) C ordering is CEFGDHB / \ E F G / \ D H B 9

d) C ordering is CEDHBFG / \ E G / \ D H / \ B F e) There are many. Here is one: A C GE HD F B f) Use the overlap method for the four (folding) top pieces of a packing box. -- ------- ------- - 10