CMSC351 - Fall 2014, Homework #2

Similar documents
Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Trees. CSE 373 Data Structures

SFU CMPT Lecture: Week 9

Search Trees. Undirected graph Directed graph Tree Binary search tree

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Trees. (Trees) Data Structures and Programming Spring / 28

Homework #3. CMSC351 - Spring 2013 PRINT Name :

Computer Science 302 Spring 2007 Practice Final Examination: Part I

12 Abstract Data Types

TREES. Trees - Introduction

March 20/2003 Jayakanth Srinivasan,

Data Structures Question Bank Multiple Choice

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

CS350: Data Structures Tree Traversal

ECE250: Algorithms and Data Structures Midterm Review

Why Do We Need Trees?

CS24 Week 8 Lecture 1

Analysis of Algorithms

Solutions for Algorithm Design Exercises and Tests

Multi-Way Search Tree

Binary Trees, Binary Search Trees

Midterm solutions. n f 3 (n) = 3

Assignment 8 CSCE 156/156H/RAIK 184H Spring 2017

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

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

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

Binary Search Trees, etc.

Section 05: Solutions

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

CE 221 Data Structures and Algorithms

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Binary Trees

INF2220: algorithms and data structures Series 1

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CSE 326: Data Structures Binary Search Trees

Data Structure Advanced

CS171 Midterm Exam. October 29, Name:

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Course Review. Cpt S 223 Fall 2009

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Name CPTR246 Spring '17 (100 total points) Exam 3

We have the pointers reference the next node in an inorder traversal; called threads

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Properties of red-black trees

Chapter 20: Binary Trees

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

DATA STRUCTURES AND ALGORITHMS

Computer Science E-22 Practice Final Exam

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

COSC 2011 Section N. Trees: Terminology and Basic Properties

Lecture 6: Analysis of Algorithms (CS )

Chapter 4: Trees. 4.2 For node B :

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Computer Science Foundation Exam

CS 216 Exam 1 Fall SOLUTION

(2,4) Trees. 2/22/2006 (2,4) Trees 1

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

Second Examination Solution

Binary search trees (BST) Binary search trees (BST)

Spring 2018 Mentoring 8: March 14, Binary Trees

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

COMP Analysis of Algorithms & Data Structures

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

COMP 250 Fall Homework #4

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

A set of nodes (or vertices) with a single starting point

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

COMP 250 Winter 2010 Exercises 6 - trees March 2010

Course Review for Finals. Cpt S 223 Fall 2008

13.4 Deletion in red-black trees

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

INF2220: algorithms and data structures Series 1

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;

COMP Analysis of Algorithms & Data Structures

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Recursively Defined Functions

Basic Data Structures (Version 7) Name:

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].

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CSE 143 Final Part 1, August 18, 2011 Sample Solution

Transcription:

CMSC351 - Fall 2014, Homework #2 Due: October 8th at the start of class Name: Section: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used, so that the grader will be able to understand it easily. You should ALWAYS prove the correctness of your algorithms either directly or by referring to a proof in the book. Write your answers in the spaces provided. If needed, attach other pages. The grades would be out of 100. Four problems would be selected and everyones grade would be based only on those problems. You will also get 25 bonus points for trying to solve all problems. Problem 1 Suppose you are given an expression with n paranthesis. Design an algorithm with running time O(n) to check whether it is valid. Examples of valid expressions are (()())()() and ()()(((()()))). Examples of invalid expressions are ())()( and ((()()))((). Solution: Use a stack. For each open paranthesis push it into the stack. For each close paranthesis pop stack. If at some point the stack should be popped but it is empty the expression is invalid. If at the end of expression the stack is nonempty, the expression is invalid. Otherwise it is valid. N ote: The whole process can also be done by incrementing and decrementing a counter as well. 1

Problem 2 Function T (n) is defined by the following recurrence relation. { 1 if n = 1 T (n) = 2T ( n ) + 1 if n > 1 Prove T (n) = O(log n). Solution: We prove this by Substitution Method. We want to show T (n) c log n b for constants c = 4 and b = 1. Note that T (2) = 3 4log(2) 1 and T (3) = 3 4log(3) 1, and thus we have T (n) c log n b for n = 2, 3. Suppose T (m) c log m b for any integer m < n. T (n) = 2T ( n ) + 1 (I.H) 2c log n 2b + 1 2c log n 2b + 1 = 2c( 1 log n) 2b + 1 = c log n 2b + 1 c log n b 2 2

Problem 3 Function T (n) is defined by the following recurrence relation. { 1 if n = 1 T (n) = 2T ( n 2 ) + n log n if n > 1 Prove T (n) = O(n(log n) 2 ) by the substitution method. Solution: We want to prove T (n) cn(log n) 2. This holds for n = 2, 3 if c 3. Suppose T (m) cm(log m) 2 for m < n and for some constant c 3. (c can be determined later). T (n) = 2T ( n ) + n log n 2 (I.H) 2c( n 2 )(log( n 2 ))2 + n log n 2c( n 2 )(log(n 2 ))2 + n log n 2c( n 2 )((log n)2 + (log 2) 2 2 log n log 2) + n log n = cn((log n) 2 + 1 2 log n) + n log n = cn(log n) 2 + n(c 2c log n + log n) cn(log n) 2 The last inequality comes from the fact that c 2c log n + log n 0 for c 3 and n 2. 3

Problem 4 Solve the following recurrence relations using the Master Theorem, or just state that the Master Theorem does not apply. (a) T (n) = 8T ( n 2 ) + 10n3 log 2 (n) Note that the growth of 10n 3 log 2 (n) is not the same as the growth of any polynomial function. So, Master Theorem does not apply here. H int: There is a general version of the Master Theorem which is beyond the scope of this class. By the general version of the Master Theorem if f(n) = Θ(n c log k (n)) for c = log b a then T (n) = Θ(n c log k+1 (n)). Here, log 2 8 = 3 and f(n) = Θ(n 3 log 2 (n). Therefore, T (n) = Θ(n 3 log 3 (n)). Both solutions are acceptable! (b) T (n) = 5T ( n 3 ) + Θ(n2.3 ) Here 2.3 > log 3 (5). Therefore, T (n) = Θ(f(n)) = Θ(n 2.3 ). (c) T (n) = 4T ( n) + O(n 2 ) Master Theorem does not apply here. (d) T (n) = 9T ( n 3 ) + 100n2 Here c = log b a = log 3 9 = 2 and f(n) = Θ(n c ) so T (n) = Θ(n c log(n)) = Θ(n 2 log n). 4

Problem 5 Implement a queue by using two stacks. Solution: Use two stacks S 1 and S 2. Whenever you need to push an element in the queue, push it in S 1. The first time that you need to pop out of queue you need to access the last element of stack. Pop elements of S 1 one by one and push them into S 2. Now the bottom element in S 1 is the top element in S 2. So pop out one element of S 2. From now on whenever you have to dequeue, you only need to pop one element out of S 2 and whenever you have to push into queue, you push the element in S 1. If at some point you have to dequeue and S 2 is empty, again pop elements of S 1 and push them into S 2 one by one. Also note that for each element, it is pushed once to S 1 and once out of S 1 and also once into S 2 and once out of S 2 (once an element is popped out of a stack it never returns to the stack). So the running time of inserting and removing n elements would be O(n). 5

Problem 6 Suppose you have a linked list of size n. Give an O(n) algorithm to find out whether the linked list has a loop. Note that you cannot store the elements of the list in another array or anything like that. In other words, you can use extra O(1) space. Note: We do not know the size of the linked list. The input is the pointer to the first element of the linked list. Hint: Define two pointers and move one with speed 1 and another with speed 2! Solution : First of all, if a pointer is moving though a link list with a loop, once it falls inside the loop it always moves through the elements of the loop and never comes out. Now assume both pointers are in the loop. At each point the distance between the two pointers is decremented by one. Therefore, in at most m steps the two pointers would be on the same node where m is the size of the loop. If no loop exists, the second pointer would hit the end of the linked list sooner than the first pointer. 6

Problem 7 Design an algorithm to find the lowest common ancestor of two given nodes in a balanced binary search tree. Your algorithm should runs in O(log n) where n is the number of nodes. Solution: Let N 1 and N 2 denote the values of the two given nodes. Start from the root of the tree. Whenever you are at a node, compare N 1 and N 2 against the value of this node and decide for each of them whether you should move to the right child or the left child. If for both of them you should move to the same child then move to that node. For example, if both N 1 and N 2 are bigger than the value of the current node, move to the right child. However, if at some node, N 1 is bigger than the value of the node and N 2 is smaller (or the other way), the current node must be the lowest common ancestor. At each step if N 1 (or N 2 ) is equal to the value of the current node, the current is the lowest common ancestor. The order of this algorithm is height of the binary search tree which is O(log(n)). 7

Problem 8 Design an algorithm to find the next (e.g. in-order successor) node of a given node in a binary search tree. Solution: You need to output the element in tree that is bigger than the value of the given node. The first observation is that the answer is either in the nodes right subtree or is one of the ancestors. You can check that no other node can be an answer to this problem. Do the following 1) If right subtree of node is not NULL, then successor lies in right subtree. Go to the right subtree and return the node with minimum key value in right subtree. To find the node with min key value, all you need to do is to move to the left child while possible. 2) If right subtree of node is NULL, then successor is one of the ancestors. In this case travel up using the parent pointer until you see a node which is left child of its parent. The parent of such a node is the successor. 8

Figure 1: In-order and Pre-order arrays Problem 9 Design an algorithm to construct a binary tree from given in-order and pre-order tree walks. (Note: This is not necessarily a binary search tree!) Solution: The inorder and preorder tree walks can be given in an array as shown in figure 1. You can find the root simply from the preorder array(first element). Root is l + 1-th element of the in-order array as well. Once you find root in the inorder array, you know its location (or the number of elements in the left subtree denoted by l). Also you can find the pr-order and in-order representations of both left and right subtrees. This means you can find the left and right subtree recursively. Also you have the root itself. All you need to do now is to set the left subtree s root as the left child of current root and the right subtree s root as the right child. The following function builds the tree for given arrays of preorder and inorder recursively. The output is the root of the tree. The location of root in the in-order array is denoted by k = l + 1. function BuildTree(preorder, inorder, n) if n 0 then return null else k location of root in inorder array root.key preorder(1) root.left BuildTree(preorder[2:k],inorder[1,k-1], k-1) root.right BuildTree(preorder[k+1:n],inorder[k+1:n], n-k) return root end if end function 9