Introduction to Algorithms I

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

FINAL EXAM SOLUTIONS

Total Points: 60. Duration: 1hr

Data Structures Brett Bernstein

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

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

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

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

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

CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

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

II (Sorting and) Order Statistics

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Question 7.11 Show how heapsort processes the input:

CS521 \ Notes for the Final Exam

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

Solving problems on graph algorithms

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

INDIAN STATISTICAL INSTITUTE

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

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

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

CS 350 Final Algorithms and Complexity. It is recommended that you read through the exam before you begin. Answer all questions in the space provided.

COMP3121/3821/9101/ s1 Assignment 1

CS61BL. Lecture 5: Graphs Sorting

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

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

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

Exam 3 Practice Problems

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

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

Number Theory and Graph Theory

CSci 231 Final Review

Bulldozers/Sites A B C D

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

CMPSCI 311: Introduction to Algorithms Practice Final Exam

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

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

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

Principles of Algorithm Design

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

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

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

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

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Programming II (CS300)

1 Minimum Cut Problem

S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 165

Greedy algorithms is another useful way for solving optimization problems.

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

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Design and Analysis of Algorithms - - Assessment

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

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

( ) 1 B. 1. Suppose f x

Multiple-choice (35 pt.)

Graph Algorithms Using Depth First Search

COMP Data Structures

Design and Analysis of Algorithms

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Exam Sample Questions CS3212: Algorithms and Data Structures

Solution for Homework set 3

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

Practice Sheet 2 Solutions

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Divide-and-Conquer. Dr. Yingwu Zhu

CS 157: Assignment 5

Exercise 1 : B-Trees [ =17pts]

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

Module 2: Classical Algorithm Design Techniques

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

Lecture 10: Strongly Connected Components, Biconnected Graphs

CSC Design and Analysis of Algorithms

Final Exam Solutions

Algorithms IV. Dynamic Programming. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Practice Problems for the Final

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Homework 4 Solutions

Solutions to Problem Set 1

Course Review for Finals. Cpt S 223 Fall 2008

Midterm solutions. n f 3 (n) = 3

6.854J / J Advanced Algorithms Fall 2008

Sorting and Selection

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?

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

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

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

2. CONNECTIVITY Connectivity

Subset sum problem and dynamic programming

Solutions to Exam Data structures (X and NV)

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

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

CSCI2100B Data Structures Heaps

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Transcription:

Summer School on Algorithms and Optimization Organized by: ACM Unit, ISI and IEEE CEDA. Tutorial II Date: 05.07.017 Introduction to Algorithms I (Q1) A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree the number of nodes with two children is exactly one less than the number of leaves. In the inductive step delete the root and then argue on the component/s formed. (Q) Consider the claim: Let G be a graph on n nodes, where n is an even number. If every node of G has degree at least n, then G is connected. Decide whether you think the claim is true or false. Proof by contradiction. Let G is disconnected and let S be the smallest component. S n. S can not have vertices with degree at least n. (Q3) Draw a connected graph which becomes disconnected when any edge is removed from it. Prove that a graph with n vertices which satisfies this condition has exactly n 1 edges. The graph is a tree. Prove by induction. Delete an edge from the tree of size n and then argue on the smaller components. (Q4) Given a list of n natural numbers d 1, d,..., d n show how to decide in polynomial time whether there exists an undirected graph G = (V, E) whose node degrees are precisely the numbers d 1, d,..., d n. (i.e., if V = {v 1, v,..., v n }, then the degree of v i should be exactly d i.) G should not contain multiple edges between the same pair of nodes, or loop edges with both points equal to the same node. Sort the degrees in descing order. Delete d 1 and substract 1 from each of d, d 3,..., d d1 +1. Now you have a degree sequence of length n 1. Repeat the same procedure. (Q5) The diameter of a tree T = (V, E) is defined as max u,v V δ(u, v), that is, the largest of all shortest-path distances δ(u, v) in the tree. Give an efficient algorithm to compute the diameter of a tree, and analyse the running time of your algorithm.

Page of 7 Randomly pick any vertex u and compute the farthest vertex from it, say v. Compute the farthest vertex w from v. The path vw is a diameter. (Q6) Given an undirected graph G = (V, E), design and analyze an efficient algorithm to determine if G contains an odd cycle. A cycle with odd number of edges is called an odd cycle. Run a BFS and mark vertices in each level as even or odd. If you encounter a back edge between same labelled vertices then there is an odd cycle. (Q7) Suppose you are given an array A with n entries, with each entry holding a distinct number. You are told that the sequence of values A[1], A[],..., A[n] is unimodal: For some index p between 1 and n, the values in the array entries increase up to position p in A and then decrease the remainder of the way until position n. (So if you were to draw a plot with the array position j on the x-axis and the value of the entry A[j] on the y-axis, the plotted points would rise until x-value p, where they d achieve their maximum, and then fall from there on.) Show how to find the entry p by reading at most O(log n) entries of A. Similar to binary search. Only check 3 contiguous entries at a time. If A[i 1] < A[i] < A[i + 1] go right of i and vice versa. (Q8) Given an array A of real numbers find two indices i and j with i < j such that A[j] A[i] is the maximum. Clearly, there s a simple algorithm that takes time O(n ): try all possible pairs of i and j and see which pair has the highest difference. Show how to find the correct numbers i and j in time O(n log n). Can you improve it to O(n)? Divide the array into parts. Recursively solve the same problem for the smaller parts. The solution is the maximum of the solution of the parts and the value A A 1. Where A 1 is the minimum of the first part and A is the maximum of the second part. (Q9) Construct the binary tree from the given preorder and inorder traversal: Preorder: 10, 5,, 0, 3, 7, 9, 13, 3, 0. Inorder: 0,, 3, 5, 7, 9, 10, 13, 0, 3. (Q10) If the preorder and postorder traversal sequence of a binary tree are given, can you construct the tree uniquely? Justify the answer. No. There is a simple counter example with 3 vertices. (Q11) Design an algorithm to find the shortest path between two vertices u, v in an unweighted graph G. Start a BFS from u. Stop when your reach v. Report the path from u to v. (Q1) Describe an algorithm that, given n integers in the range 0 to k, k < n preprocesses its input and then answers any query about how many of the n integers fall into a range [a, b] in O(1) time. Your algorithm should use Θ(n + k) preprocessing time.

Page 3 of 7 Construct array A[0... k] to store the number of times integer i occurs in A[i]. Construct array B[0... k] as B[i] = i j=0 A[j]. For any query [a, b] output B[b] B[a 1]. (Q13) Show how to sort n integers in the range 0 to n 3 1 in O(n) time. Do you need an extra array to sort these numbers? Use radix sort with base n. (Q14) Prove the lower bound of 3n comparisons in the worst case to find both the maximum and minimum of n numbers. ( Consider how many numbers are potentially either the maximum or minimum, and investigate how a comparison affects these counts.) Check solution of Cormen, Thomas H. Introduction to algorithms. MIT press, 009, exercise 9.1-. (Q15) Show that the second smallest of n elements can be found with n + log n comparisons in the worst case. ( Also find the smallest element.) Check solution of Cormen, Thomas H. Introduction to algorithms. MIT press, 009, exercise 9.1-1. (Q16) In the algorithm SELECT (median finding algorithm), the input elements are divided into groups of 5. Will the algorithm work in linear time if they are divided into groups of 7? Argue that SELECT does not run in linear time if groups of 3 are used. (Q17) Show how quicksort can be made to run in O(n log n) time in the worst case, assuming that all elements are distinct. Use SELECT to chose the pivot element. (Q18) Suppose that you have a black-box for the worst-case linear-time median finding routine. Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic. For the k th order statistics use the routine to find the median m. If k < m, then discard half of the elements greater than m and continue to find the k th order statistics on the remaining elements. If k > m proceed similarly. (Q19) You are given two sorted arrays A and B of size n each. Can you suggest an O(log n) time algorithm to find the median of the merged array. Find median m A and m B of A and B. Let m A < m B. Discard elements in A left of m A and elments in B right of m B. Proceed recursively. (Q0) Let G π = (V ; E π ) be a tree formed by a DFS traversal on the tree G = (V, E). Let v be a nonroot vertex of G π. Prove that v is an articulation point of G if and only if v has a child s such that there is no back edge from s or any descant of s to a proper ancestor of v. BoyerMoore majority vote algorithm, Wikipedia.

Page 4 of 7 (Q1) Show how to compute all articulation points of G in O(E) time. (Q) Prove that an edge of G is a bridge if and only if it does not lie on any simple cycle of G. (Q3) Show how to compute all the bridges of G in O(E) time. (Q4) A majority element in an array of size n is an element whose frequency is strictly greater than n. Give a linear time recursive algorithm to find the majority element in an array, if it exists. (Q5) Given k sorted arrays A 1, A,..., A k each of size n, find an efficient way to merge them into a single array of size nk. Insert the first items of each array in a min heap of size k. Extract the minimum from the min heap and insert the next element into it from the array from which the minimum was deleted. (Q6) You are given an array A of size n, containing at most k distinct elements. Write an O(n log k) time algorithm to sort A. Create a Min-Heap of size k + 1 with first k + 1 elements of unsorted A. This takes O(k) time. Next, one by one remove minimum element from heap, put it in the output array, and add a new element to heap from remaining elements from A. (Q7) You are given an array A of n distinct integers. For a given k, design a sublinear time algorithm to report k elements of A in which none of the reported elements is either the maximum or the minimum element in A. Chose the first k + elements. Report elements which are neither maximum or minimum. Introduction to Algorithm II (Q8) Given n matrices A 1, A,... A n, where for 1 i n, A i is a p i 1 p i matrix, parenthesize the product A 1, A,..., A n so as to minimize the total cost of multiplying these matrices, assuming that the cost of multiplying a p i 1 p i matrix by a p i p i+1 matrix using the naive algorithm is p i 1 p i p i+1. Matrix chain multiplication. Cormen, Thomas H. Introduction to algorithms. MIT press, 009, section 15.. (Q9) Given a set of U = {u 1,..., u n } of n objects with the corresponding sizes s 1,..., s n, s i Z +, and a knapsack capacity C Z +, find a subset of U whose total size is bounded by C and the total profit is maximized. The profit for chosing u i U in the knapsack is v i, v i Z + Kleinberg, Jon, and Eva Tardos. Algorithm design. Pearson Education India, 006, section 6.4.

Page 5 of 7 (Q30) The following problem is known as the edit distance problem. Given two strings S 1 of m characters and S of n characters, the problem is to modify S 1 and convert it into S through a sequence of character edits. The character edits can be insertion, deletion and overwrite. Insertion and deletion takes unit cost, overwrite takes a cost of two. Design and analyze an efficient algorithm that minimizes the number of edits (and hence the time) required. The characters belong to the same alphabet set. Algo- Dasgupta, Sanjoy, Christos H. Papadimitriou, and Umesh Vazirani. rithms. McGraw-Hill, Inc., 006, section 6.3. (Q31) Maximum size square sub-matrix with all 1s - Given a binary square matrix consisting of 0 s and 1 s, find the maximum size square sub-matrix consisting of only 1 s. Construct a sum matrix S[ ][ ] for the given M[ ][ ] Copy first row and first columns as it is from M[ ][ ] to S[ ][ ] For other entries, use following expressions to construct S[ ][ ] if M[i][j] is 1 then S[i][j] = min(s[i][j 1], S[i 1][j], S[i 1][j 1]) + 1 else S[i][j] = 0 Find the maximum entry in S[i][j] Using the value and coordinates of maximum entry in S[i][j], print sub-matrix of M[ ][ ] Time complexity: O(mn). (Q3) Largest Sum Contiguous Subarray: Given an array containing both positive and negative integers, find the contiguous subarray with the maximum sum. max so far = max ing here = 0 for each element of the array do max ing here = max ing here + a[i] if max ing here < 0 then max ing here = 0 if max so far < max ing here then max so far = max ing here return max so far Time Complexity: O(n). (Q33) Longest Common Subsequence: Find the longest common subsequence of two strings A and B where the elements are letters from the two strings and they should be in the same order.

Page 6 of 7 if m == 0 or n == 0 then return 0 if X[m 1] == Y [n 1] then return 1 + lcs(x, Y, m 1, n 1) else return max(lcs(x, Y, m, n 1), lcs(x, Y, m 1, n)) Time Complexity: O(mn). (Q34) Longest Increasing Subsequence: The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. for all 1 < i < n do L(i) = 1 + max(l(j)), 0 < j < i & a[j] < a[i] L(i) = 1 if no such j exists return max(l(i)) (Q35) Coin Change: Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = {S 1, S,..., S m } valued coins, how many ways can we make the change? count[m, N] = count[m 1, N] + count[m, N S m ]. Time Complexity: O(mn). (Q36) Partition problem: Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets is same. Let issum(n, sum ) be the function that returns true if there is a subset of arr[0... n 1] with sum equal to sum issum(n, sum sum sum ) = issum(n 1, ) OR issum(n 1, arr[n]). Time Complexity: O(sum n). (Q37) Count number of ways to cover a distance: Given a distance n, count total number of ways to cover the distance with steps of 1, and 3 units.. count[i] = count[i 1] + count[i ] + count[i 3]. Time Complexity: O(n). (Q38) Dice Throw: Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown.

Page 7 of 7 Let the function to find X from n dice be: sum(m, n, X). Then the recursion is sum(m, n, X) = sum(m, n 1, X 1)+sum(m, n 1, X )+...+Sum(m, n 1, X m) (Q39) A number is said to be made up of non-decreasing digits if all the digits to the left of any digit is less than or equal to that digit. For example, the four-digit number 134 is composed of digits that are non-decreasing. Some other fourdigit numbers that are composed of non-decreasing digits are 0011, 1111, 111, 11, 3. As it turns out, there are exactly 715 four-digit numbers composed of non-decreasing digits. Notice that in a k digit number leading zeroes are allowed, for example: 0000, 0001, 000 are all valid four-digit numbers with non-decreasing digits. Write a program to determine the number of such k digit numbers for an input k. Create a matrix a[ ][ ] where a[i][j] = count of all valid j digit numbers with i as the leading digit. a[i][j] = 9 k=i a[k][j 1]. The End.