CS 146 Exam 1 Grade:

Similar documents
DATA STRUCTURES USING C

Algorithms and Data Structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Data Structures and Algorithms Written Examination

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

PES Institute of Technology-BSC QUESTION BANK

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Data Structures. Level 6 C Module Descriptor

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

10CS35: Data Structures Using C

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Dynamic Programming. Lecture Overview Introduction

Data Structure [Question Bank]

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Data Structures and Data Manipulation

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Sample Questions Csci 1112 A. Bellaachia

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Symbol Tables. Introduction

CompSci-61B, Data Structures Final Exam

Data Structures and Algorithms

Ordered Lists and Binary Trees

CS473 - Algorithms I

Any two nodes which are connected by an edge in a graph are called adjacent node.

Binary Search Trees CMPSC 122

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Converting a Number from Decimal to Binary

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

COMPUTER SCIENCE. Paper 1 (THEORY)

Data Structures Fibonacci Heaps, Amortized Analysis

Factorizations: Searching for Factor Strings

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

From Last Time: Remove (Delete) Operation

Analysis of a Search Algorithm

PROBLEMS (Cap. 4 - Istruzioni macchina)

Positional Numbering System

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Regular Expressions and Automata using Haskell

Java Software Structures

International Journal of Software and Web Sciences (IJSWS)

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Big Data and Scripting. Part 4: Memory Hierarchies

Algorithms Chapter 12 Binary Search Trees

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

6.3 Conditional Probability and Independence

Analysis of Algorithms I: Binary Search Trees

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Automata and Computability. Solutions to Exercises

Lecture 2 February 12, 2003

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

Analysis of Algorithms I: Optimal Binary Search Trees

CmpSci 187: Programming with Data Structures Spring 2015

Binary Trees and Huffman Encoding Binary Search Trees

Why Use Binary Trees?

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

VISUAL ALGEBRA FOR COLLEGE STUDENTS. Laurie J. Burton Western Oregon University

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

2) Write in detail the issues in the design of code generator.

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Scale Factors and Volume. Discovering the effect on the volume of a prism when its dimensions are multiplied by a scale factor

5. Binary objects labeling

Data Structure with C

Matrix Multiplication

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Solutions to Math 51 First Exam January 29, 2015

AP Computer Science Java Mr. Clausen Program 9A, 9B

Solutions to Homework 6

Java the UML Way: Integrating Object-Oriented Design and Programming

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Session 6 Number Theory

CS473 - Algorithms I

Unit 6. Loop statements

Binary Search Trees (BST)

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Binary Heap Algorithms

Why? A central concept in Computer Science. Algorithms are ubiquitous.

International Journal of Advanced Research in Computer Science and Software Engineering

How to Make the Most of Excel Spreadsheets

Algorithms and Abstract Data Types

Questions and Answers in Computer Science. By Yonas Tesfazghi Weldeselassie weldesel

C H A P T E R Regular Expressions regular expression

SECTION 2-1: OVERVIEW SECTION 2-2: FREQUENCY DISTRIBUTIONS

Big data algorithm optimization. Case study of a sales simulation system KASPER KARLSSON TOBIAS LANS

Chapter 3: Restricted Structures Page 1

Introduction to Data Structures and Algorithms

Transcription:

CS 146 Exam 1 Name Grade: The test will be open book, open notes, 75 minute time limit. Please write your answers on the exam sheet. Ten problems, 10 points per problem. 1. Analysis of non-recursive algorithms. Consider the problem of finding the longest palindrome occurring as a substring of a given input string s. For example, if we are given abaracecardog we should find racecar. (There might, of course, be two palindromes of equal longest length. In that case, we want the one starting nearest the beginning of the input string.) You will not be asked to write code to solve this problem, but rather, to analyze two possible approaches to the problem. Let N be the length of the input string. Plan A: First write a method Palindrome(String z) that checks if its input is or is not a palindrome. Then, to solve the main problem, examine, for each i and j with i+j < N, the substring of j characters beginning at index i. Call Palindrome to check if that substring is a palindrome and keep track of the longest one found so far. Plan B: examine, for each i < N, the longest palindrome centered either at the i-th input character or (in the case of even-length palindromes) between the i-th input character and the next input character, by looking for the first mismatch between characters at i-k and i+k (for odd-length palindromes) or between characters at i-k and i+k+1 (for evenlength palindromes). (a) Which plan leads to the fastest code, plan A or plan B? Plan B (b) Give the running time of plan A in terms of N using Θ-notation. Θ(N 3 ) Explanation: it takes three nested loops, one over i, one over j, and one in Palindrome. (c) Give the running time of plan B in terms of N using Θ-notation. Θ(N 2 ) Explanation: it takes two nested loops, one over i and one over k. (d) What is the largest value of N for which you would expect to be able to run your chosen program in one minute on a 1 gigahertz machine? 10 N 2 = 6 times N 10 so N is about 80000. The 10 on the left is for the loop body; if it s 20 instead we get more like 50000, so anyway, somewhere between 10000 and 100000.

2. (a) Indicate, for each pair of expressions (A,B) in the table below, whether A is O of B. Write Yes or No in each blank box of the table. A B is A = O(B)? n 27 2 n yes lg n n yes lg n + n n yes n n + 5 yes n + 5 n yes n lg n n 2 yes lg n n yes 2 (b). Use Θ-notation to express the worst-case running times of the following algorithms that have been discussed in class: (a) Insertion sort, on an array of length n. Θ( n 2 ) (b) A faster sorting algorithm, on an array of length n. Θ( n lg n ) (c) Binary search, on a sorted array of length n. Θ( lg n ) (d) Testing an n-digit integer to see if it is a prime, by dividing it in turn by each number up to its square root. Θ( 2 n/2 ) Note: the number itself is about 2 n, where n is the number of digits; so its square root is about 2 n/2. (e) Multiplying two n by n matrices (whose entries are of fixed size, say doubles). Θ( n 3 ) Explanation: this takes three nested loops, one over rows of the first matrix, one over columns of the second matrix, and one more to compute the entry at a given row and column. 3. Suppose given an initially empty stack, and suppose the following operations are executed. Assume the add method works as in your Calculator program. push(5); push(1); push(3); add(); multiply(); x = peek(); pop(); (a) What is the final value of x? 20 (b) What are the final contents of the stack? empty (no contents) Solution. The stack is successively 5 1,5 3,1,5 4,5 20 20 empty

4. Queues. Suppose a queue is implemented in a fixed array capable of holding 100 entries, using the method discussed in the textbook and in class. Suppose the queue is initially empty, and then objects are put into the queue at the rate of 10 per minute while meantime they are processed and removed from the queue at the rate of 5 per minute. After 120 elements have been added to the queue, which of the following is true? (a) You can t add 120 elements to an array holding 100 entries. (b) There will be 60 elements in the queue, 20 of them at the end of the array where the queue started, and 40 at the other end. (c) There will be 60 elements in the queue, all in one contiguous segment of the array. (d) There will be 60 elements in the queue, 40 of them at the end of the array where the queue started, and 20 at the other end. Answer: (b). The tail of the queue, which starts at 0, has moved 120 units, so it has wrapped around and is at index 20. The head of the queue, which also starts at 0, has moved 60 units, so at that time the queue occupies the last 40 elements of the array and the first 20. 5. Linked lists. The class StringList has these members: { String key; StringList next; } Write a static method boolean find(string needle, StringList haystack) that returns true if needle is in the list haystack, and false if not. The input null for haystack is legal, meaning haystack is an empty list. public static boolean find(string needle, StringList haystack){ for(stringlist marker = haystack; marker!= null; marker = marker.next) if(marker.key.equals(needle)) return true; return false; } 6. The operation Append on linked lists makes one list out of two. For example, Append applied to (cat, dog, giraffe) and (ant, salamander, deer) produces (cat, dog, giraffe, ant, salamander, deer). Write a member function Append(StringList x) that appends x to this. (After it runs, this will be the new, longer, list.) public StringList Append(StringList x){ for(stringlist marker = this; marker.next!= null; marker = marker.next) ; // empty loop body // now marker points to the last element of this.

marker.next = x; } 7. Binary Search Trees. The picture represents a binary search tree. The numbers shown are arbitrary node labels, not numbers representing the contents of the nodes. The contents are not shown. If node 1 is deleted, using binary search tree deletion, what will be the new root node? Answer 6 [It is not required to draw the tree after the deletion.] 8. Red-black trees. For each of the following two trees, either indicate how to color the nodes red and black (use R and B to label the nodes) to make the tree a red-black tree, or explain why that is not possible. In these pictures, as in the applet we used, the NIL leaf nodes are not shown. (a) (b) Answers: (a) The root has to be black, so 3 is black. Then 1 has to be black too, or else the black-height will be only 1, which is too small for the branch of length 4. So the black height has to be 2. Then there must be 2 red nodes on the path of length 3; since they can t be consecutive they must be nodes 7 and 11, and 9 must be black. Then 5

must be black to achieve black height 2 on the path 3-7-5. That coloring (7 and 11 red and everything else black) meets all the necessary conditions for a red-black tree. (b) We proved that in a red-black tree, the shortest path is at least half as long as the longest path; but here the shortest path has length 2 and the longest has length 5, so this can t be made into a red-black tree. Alternately you could argue as follows directly from the definition of red-black tree: The shortest branch has length 2, so the black-height can t be more than 2. On the branch of length 5, you would have to have 3 red nodes, each of which must have a black parent, which is impossible with only two black nodes available. 9. Red-black insertion. Node 7 is to be inserted into the following red-black tree. In these pictures, as in the applet we used, the NIL leaf nodes are not shown. After the red-black insertion algorithm completes, (a) What color will 7 be? red (b) What will be the path from the root of the tree to 7? 8,5,6,7 (c) What will be the black height of the tree after the insertion? 2 (d) What node will be the root of the tree? 8 (e) What will be the color of the root node? black

10. We studied the Google PageRank algorithm. Consider an example web containing three pages A, C, and G, in which pages G and C both link to page A and to each other, and A links only to C. Assume that the pages A, C, and G are indexed as pages 0,1, and 2 respectively. (a) What is the B-matrix for this example? nlinks[0] = 1 nlinks[1] = 2 nlinks[2] = 2 B = 0 0.5 0.5 1 0 0.5 0 0.5 0 (b) What is the rank order of these pages as produced by the Google algorithm (in descending order of rank)? Circle one of the six possible answers: (C,A,G) is correct. Starting with (1,1,1) = R0 for the ranks, we get R1 = BR0 = (1,1.5, 0.5), corresponding to rank order C,A,G, and then R2 = BR1 = (1, 1.25, 0.75), also corresponding to rank order C,A,G. Since the rank order didn t change, the algorithm terminates. Is this intuitively correct? Both A and C have two pages pointing at them, so they should both be highly ranked. Both are pointed at by G and by the other one; but the power of C s links is diluted since there are two links on page C, but only one on page A, so, A s link counts more and makes C higher-ranked than A. At first it seemed to me that A should be higher ranked; but intuition is only an approximation here; the algorithm tells the truth.