IE 172 Final Examination Practice Problems

Similar documents
DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Final Examination CSE 100 UCSD (Practice)

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

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

Exam 3 Practice Problems

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

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

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

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

( D. Θ n. ( ) f n ( ) D. Ο%

Practice Problems for the Final

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

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

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Computer Science E-22 Practice Final Exam

(a) Write code to do this without using any floating-point arithmetic. Efficiency is not a concern here.

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

INSTITUTE OF AERONAUTICAL ENGINEERING

) $ f ( n) " %( g( n)

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

COMP3121/3821/9101/ s1 Assignment 1

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

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

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

CSE 373 Final Exam 3/14/06 Sample Solution

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Lecture Notes, CSE 232, Fall 2014 Semester

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Midterm Exam 2B Answer key

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Chapter 9 Graph Algorithms

CS521 \ Notes for the Final Exam

CSci 231 Final Review

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

Student number: Datenstrukturen & Algorithmen page 1

Solutions to relevant spring 2000 exam problems

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

( ) 1 B. 1. Suppose f x

Design and Analysis of Algorithms - - Assessment

FINAL EXAM SOLUTIONS

END-TERM EXAMINATION

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

COMP 251 Winter 2017 Online quizzes with answers

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

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

Chapter 9 Graph Algorithms

Introduction to Algorithms Third Edition

CISC 320 Midterm Exam

Multiple-choice (35 pt.)

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

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Review for Midterm Exam

Course Review for Finals. Cpt S 223 Fall 2008

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

CSE 100: GRAPH ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Chapter 9 Graph Algorithms

CMSC Theory of Algorithms Second Midterm

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Subset sum problem and dynamic programming

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CS1800 Discrete Structures Final Version A

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Thursday 1st June 2017 Time: 14:00-16:00

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

L.J. Institute of Engineering & Technology Semester: VIII (2016)

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

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

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

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

Quiz 1 Practice Problems

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

n 2 ( ) ( ) + n is in Θ n logn

University of Manitoba Open Programming Contest September 22, General Instructions:

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

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Horn Formulae. CS124 Course Notes 8 Spring 2018

Binary Arithmetic. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T.

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

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

Solutions to Exam Data structures (X and NV)

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

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

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Algorithms Dr. Haim Levkowitz

Transcription:

IE 172 Final Examination Practice Problems Dr. T.K. Ralphs 1. This question concerns the graph below. (a) Execute Dijkstra s Algorithm on this graph by hand. Assuming the priority queue is stored as a heap in an array-based binary tree data structure, write down what the array containing the heap would look like at the beginning of each iteration. Please show as much work as possible if you would like partial credit in the case you make a mistake. Clearly indicate each arrays in your answer and what step it is associated with. (b) Show that Prim s Algorithm produces the same tree and give a general condition under which this would be true for a given graph. (c) Suppose you have an undirected graph with weighted edges, and perform a depth-first search, such that the edges going out of each vertex are always explored in order by weight, smallest first. Is the search tree resulting from this process guaranteed to be a minimum spanning tree? Explain why, if it is, or, if it isn t, provide a counterexample (specifying the start vertex for the DFS and showing both trees). (Hint: for the graph above, this algorithm does produce a minimum spanning tree. Think about the effect certain modifications to the graph would have on the outcome.) 2. The following is a search method that could be used with the Graph class we used in several of the laboratories. 1

1 def search(self, source, destination = None): 2 3 q = Stack() 4 5 nl = self.get_node_list() 6 for i in nl: 7 self.get_node(i).set_attr( color, black ) 8 9 pred = {source:source} 10 if str(source)!= str(destination): 11 found = False 12 while not q.isempty() and not found: 13 current = q.pop() 14 self.get_node(current).set_attr( color, blue ) 15 if current == str(destination): 16 found = True 17 break 18 for n in self.get_neighbors(current): 19 if (self.get_node(n).get_attr( color )!= green and 20 self.get_node(n).get_attr( color )!= yellow : 21 self.get_node(current).set_attr( color, yellow ) 22 pred[n] = current 23 q.push(str(neighbor)) 24 self.get_node(current).set_attr( color, green ) 25 26 return pred (a) What specific method does this function currently implement? (b) What method would it implement if line 3 were changed to 3 q = Queue() (c) What information about a vertex does the color of a node indicate? List all possible colors and what each one indicates. (d) Write a Python function find path(pred, destination) to print out the path from the source to any other node, taking the output of the search() method as input. 3. A d-dimensional box with dimensions (x 1, x 2,..., x d ) nests inside another d-dimensional box with dimensions (y 1, y 2,..., y d ) if there exists a permutation π of 1, 2,..., n such that x π(1) < y 1, x π(2) < y 2,..., x π(n) < y n. (a) Give an efficient algorithm for determining whether a given box nests inside another one. Specify the running time of your algorithm. (b) Suppose that you are given a sequence of n boxes B 1, B 2,..., B n. Give an efficient algorithm for determining the longest sequence of boxes B i1, B i2,..., B ik such that B ij nests within B ij+1 for all j = 1,..., k 1. (Hint: use the fact that the nestedness property is transitive and construct an associated graph). 2

4. The longest common subsequence (LCS) problem is to find the longest subsequence that is common to each of two input sequences. Recall from your mathematics courses that a subsequence of a sequence is a sequence comprised of a subset of a given sequence in which the elements of the subsequence come in the same order as they did in the original sequence. For example, the sequence [1, 3, 8, 2] is a subsequence of the sequence [1, 6, 3, 8, 9, 2]. The following is a recursive function that calculates the length of the longest common subsequence of sequences X and Y, given as two Python lists. def LCSLength(X, Y): if len(x) == 0 or len(y) == 0: return 0 if X[-1] == Y[-1]: return LCSLength(X[:-1], Y[:-1]) + 1 else: return max(lcslength(x, Y[:-1]), LCSLength(X[:-1], Y)) (a) Explain the basic logic of this function. (b) What is the worst case running time of this function? You can use big-o notation to give a rough answer, but be sure to explain how you got your answer. (c) The following is a non-recursive version of the function. def NRLCSLength(X, Y): m, n = len(x), len(y) C = [[0 for i in range(n)] for j in range(m)] for i in range(m): for j in range(n): if X[i] == Y[j]: C[i][j] = C[i-1][j-1] + 1 else: C[i][j] = max(c[i][j-1], C[i-1][j]) return C[m-1][n-1] How can we interpret C[i][j] when this function terminates? (d) What is the running time of the non-recursive version? 5. Suppose you are implementing a spreadsheet program in which you must maintain a grid of cells. Some cells contain values, while other cells contain formulas that depend on other values. After the user enters a new formula into a given cell or updates an already-existing formula, the spreadsheet program must first determine whether there are any circular dependencies, meaning a sequence of calculations that all depend on each other in a circular fashion. For example, if the expression in cell E1 depends on the value in cell C5 and the expression in cell C5 depends on the value in cell D3, then the expression in cell D3 is not allowed to depend 3

on the value in cell E1. If a circular dependency exists, then the program must notify the user of the error and prompt for a correction. After it has been determined that there are no more circular dependencies, the program must then recalculate the values in the spreadsheet. In order to do this, the program must determine (1) which values may have changed and (2) in what order the changed values need to be recalculated. Implementing the spreadsheet requires a data structure for storing the spreadsheet in memory and one for storing the dependencies. In addition, we might have a separate file format for storing the spreadsheet on disk when not in use. The data structure for the spreadsheet itself is used to store the contents of each cell, including the formula for computing the value in the cell (if there is one) and the value itself. The data structure for the dependencies is used to look for circular dependencies and determine the order of calculation during an update. (Actually, there is a way to link these two data structures into one, but we will consider them separately). (a) Describe two alternative data structures for storing the spreadsheet in memory while it is being edited and discuss the advantages and disadvantages of each. Hint: Consider the time-space tradeoff. For each data structure, consider both the time required to look up the contents of a cell and the space required to store the spreadsheet. (b) Suppose the spreadsheet user is editing a particular cell. State an algorithm that either (1) determines all cells that must be recalculated after a change to the cell being edited or (2) detects a circular dependency. Determine the running time of the algorithm. Hint: You may want to construct a graph. If so, please state explicitly how the graph is constructed. (c) Assuming no circular dependencies exist, state an algorithm for determining the order in which the cell values should be recalculated and analyze its running time. 6. Because most computers represent numbers in binary format, certain operations can be accomplished very efficiently using bit operations. For example, to multiply a number by 2, we simply add a 0 to the end of its binary representation (this is the same as shifting all the bits one spot to the left). (a) Describe how to divide a number by 2 and truncate the answer (i.e., throw away the fractional part) using a bit shift operation. (b) Describe how to quickly determine whether a binary number is odd or even by examining one bit (this is called a parity check). (c) Consider the following properties of the greatest common divisor of a and b. If a and b are both even, then gcd(a, b) = 2 gcd(a/2, b/2). If a is odd and b is even, then gcd(a, b) = gcd(a, b/2). If a and b are both odd, then gcd(a, b) = gcd((a b)/2, b). Using the bit operations from (a) and (b), state an efficient algorithm for computing gcd(a, b). (d) Analyze the number of bit operations necessary to execute your algorithm (in the worstcase), in terms of a, assuming a b. Justify your answer. 4

7. This problem will lead you through the worst-case and average-case analysis for a simple algorithm. The following subroutine takes as input an array L that is a random permutation of the integers 1 through n and returns the first value of i for which L[i] < L[i 1]. def firstdecrease(l): for i in range(len(l)): if L[i] >= L[i-1]: break return i (a) What is the worst-case running time of this function? (b) If the numbers in the array L are ordered randomly, then the probability that firstdecrease(l) returns the value k is k/(k + 1)! except for the special case when k = n for which the probability 1/n!. Use this fact to write an expression for the average value returned by the algorithm. The answer can be expressed as a sum. You do not have to simplify this sum. (c) Use your answer from (b) to determine the running time of this algorithm in the average case. 8. Suppose you are working on a computer system that is only capable of multiplying k-bit integers (integers that can be represented in binary using k bits) and suppose you want to multiply two numbers u and v, at least one of which is bigger than k-bit. This can be done using a formula that reduces the multiplication of a pair of n-bit integers to several pairs of n/2 -bit numbers, as follows. First define m = n/2 and let so that u = 2 m w + x and v = 2 m y + z. Then we have w = u/2 m, (1) x = u mod 2 m, (2) y = v/2 m, and (3) z = v mod 2 m, (4) uv = (2 m w + x)(2 m y + z) (5) = 2 2m wy + 2 m wz + 2 m xy + xz. (6) Note that w, x, y, and z all have at most m digits. for this problem, you may assume that all integers are positive and that the addition of arbitrary integers can be done in constant time. (a) Using the above formula as a basis, write a Python function for a recursive algorithm to multiply two n-bit integers. (b) Write a recurrence for the running time of your algorithm and solve it. (c) Note that if we define then we have r = (w + x)(y + z) = wy + (wz + xy) + xz, (7) uv = 2 2m wy + 2 m wz + 2 m xy + xz (8) = 2 2m wy + 2 m (r wy xz) + xz. (9) 5

Explain how you might use this observation to improve your algorithm and analyze the running time of the improved algorithm. 9. Suppose you are given a collection of intervals [a i, b i ] for i = 1,..., n. An interval graph is a graph G = (V, E), where each vertex corresponds to an interval and the edges consists of pairs of vertices whose corresponding intervals overlap. (a) Describe a Θ( V log V + E ) algorithm for determining the edge set of an interval graph. Note that the naive algorithm of just comparing every interval to every other interval is Θ( V 2 ). Under what circumstances is your algorithm better than the naive algorithm? (b) Given an interval graph, describe an efficient algorithm for determining the union of all the intervals. 10. The following questions refer to a generic list data structure. Recall that a list can be stored either as an array (Python list) or a linked list. (a) Assuming the use of a Python list, write a Python function that moves the smallest item to the beginning of the list. What is the running time of this operation in the worst case? (b) Assuming a linked-list implementation, write a Python function that moves the smallest item to the beginning of the list. What is the running time of this operation in the worst case? (c) Describe how to use the subroutine from parts (a) and (b) to sort an array. What is the worst-case running time of this sorting algorithm. What standard sorting algorithm is this equivalent to? 11. A Josephus election is conducted as follows. The n candidates sit in a circle. Starting with a randomly chosen spot in the circle, the candidates count off and every m th candidate is eliminated until only one remains. (a) Describe how to implement an algorithm for determining the winner of a Josephus election using modular arithmetic. (b) What are the requirements on m and n in order for the algorithm to produce a winner? (c) What is the running time of your algorithm, in terms of m and n. (d) How much memory does the algorithm use? 12. One difficulty in working with functions involving n! is that it cannot actually be computed for large n without causing an overflow. Describe a recursive method for computing the quantity (n! mod M) such that overflow is not an issue. 13. Write a recursive function to find the maximum element of an Python list. 14. Draw the result of inserting the keys E A S Y Q U E S T I O N into a binary search tree in that order. 15. Consider the following recursive algorithm for topological sort of a directed graph. The algorithm either returns None, if there exists a directed cycle or list, containing a topologically sorted list of the vertices of the graph. 6

Algorithm 1 Topological sort method if The graph has no nodes then Return list end if Find node i with no incoming arcs if No such node exists then Return None else Append i to list Delete i from graph Call topological sort method end if (a) Describe details of an implementation of this algorithm for the case when the graph is stored in a standard adjacency list data structure (such as the one described in the textbook) and analyze its running time. Be sure to describe details of the algorithm, such as how to find a node with no incoming arcs. (b) Describe any modifications to the algorithm, the data structure, or the input graph itself that would improve the running time. 16. In this problem, we consider a data structure for storing a document in a simple text editor. The goal of our data structure is to allow basic editing operations, such moving the cursor and entering text, to be performed efficiently. We consider here a keyboard-based system, so the only way to move around the document is to use the arrow keys. The data structure that has been proposed is to store each line of the document as a linked list and to store the entire document as a (regular Python) list of these linked lists. (a) What is the running time of the following basic operations? Typing a character Deleting a character Hitting one of the arrow keys (up, down, right, left). You can consider the input to be a function of the maximum number of characters per line C and the number of lines N. Note that without some kind of line wrapping capability, the size of a single line can grow quite large. In your analysis, consider a carriage return (new line what you get when hit the Return key) to be a character (you might want to treat this as a special case). (b) Suppose we want to include a word wrap capability. In other words, each time the typing of character causes a line to exceed a certain maximum length, the last word on the offending line must be moved to the next line. Write a function to insert a character into a line, taking into account word wrap, keeping in mind that the wrapping of one line may necessitate the wrapping of additional lines. (c) Suggest an algorithm for efficiently spell-checking the document and analyze its runing time. There are multiple ways of doing this. Try to choose the most efficient one. 17. The well-known Morse code converts each character of a given string into a series of up to four dots and dashes. For example, the letter X becomes - -. The scheme can be 7

pictured in the form of a tree in which each node contans a character and the corresponding code is the sequence of dots and dashes associated with the edged encountered on the path from the root. For Morse code, the tree looks as follows. This yields a simple method of decoding in real time as each dot and dash is received. We simply trace the path in the trtee until we find the appropriate letter. (a) The goal of Morse code is to make the transmission time of encoded messages as short as possible. Assuming that a dot takes 1 time unit to send and a dash takes three time units, how should we allocate the letters to nodes of the tree in order to minimize the transmission time? (b) Suppose we try to use this scheme on a computer as a simple compression algorithm. We think of the dots as 0 and the dashes as 1, then we can re-interpret Morse code as a mapping of each letter to a 4-bigit binary number. By interpreting the entire sequence of binary digits as a single binary numbner, we hope to get a more compact representation of the original string. Will this scheme work? (Hint: try encoding the word HIS ). (c) Huffman codes were invented as an alternative to the Morse code scheme just suggested. They work as just described except for two enhancement. First, the letters are only allowed to be associated with the leaves of the tree. Second, we also allow the tree to be unbalanced. What are the advantages of this scheme over the Morse code scheme. Be specific. 18. Consider the problems of finding the first non-repeated character in a string. (a) The most straightforward algorithm is to move through the string character by character, searching the remaining part of the string linearly for a matching character. Any character found to be matching is marked and skipped when it is encountered later. What is the worst case running time of this algorithm? (b) Describe a more efficient method of solving this problem and analyze its running time (Hint: One approach is similar to an algorithm we used in one of the labs). 8

19. A phone number can be converted into a string using the mapping given by the set of letters corresponding to each given number on the phone dial (2: ABC, 3:DEF, 4:GHI, 5:JKL, 6: MNO, 7: PQRS, 8:TUV, 9:WXYZ). (a) Write a recursive Python function for printing out all the strings corresponding to a given phone number and analyze its running time. (b) Describe how you might check as you go along whether the curent string consists of words in English? 20. Suppose we are given an undirected graph and an ordering of the nodes. Describe an algorithm for orienting the edges of the graph such that the resulting directed graph is acyclic and the given ordering is one of the associated topological orderings of the nodes of the new directed graph. 21. Consider the problem of finding the slice of a Python list with the greatest sum. When the list can contain negative items, this is a non-trivial problem. (a) The following one line of Python will solve this problem print max([sum(a[i:j]) for i in range(n) for j in range(i+1, n+1)]) What is the running time of this algorithm? (b) To improve efficiency, we can avoid summing from scratch each time as follows. best = A[0] for size in range(1, n+1): cur = sum(a[:size]) for i in range(n - size): cur += A[i+size] -= A[i] best = max(best, cur) Explain briefly how this algorithm works and and analyze the worst-case running time. Compare this running time to that of the previous algorithm. (c) Describe how to further improve the algorithm to linear time. 9