Algorithms Tutorial 27th Sept, 2019.

Slides:



Advertisements
Similar presentations
Greedy Algorithms Amihood Amir Bar-Ilan University.
Advertisements

Complexity ©D Moshkovitz 1 Approximation Algorithms Is Close Enough Good Enough?
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Week 2: Greedy Algorithms
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
The Fundamentals: Algorithms, the Integers & Matrices.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Cardinality of Sets Section 2.5.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Number Systems Part 2 Numerical Overflow Right and Left Shifts Storage Methods Subtraction Ranges.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursive Algorithms &
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Example 2 You are traveling by a canoe down a river and there are n trading posts along the way. Before starting your journey, you are given for each 1
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
NAME THAT ALGORITHM #2 HERE ARE SOME PROBLEMS. SOLVE THEM. GL HF.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CSE15 Discrete Mathematics 03/06/17
Probabilistic Algorithms
CSC 427: Data Structures and Algorithm Analysis
Discrete Mathematics Algorithms.
CSCE 411 Design and Analysis of Algorithms
Analysis of Algorithms
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Data Structures and Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
COMPUTING FUNDAMENTALS
Randomized Algorithms
Matrix Chain Multiplication
Streaming & sampling.
Algorithm Analysis CSE 2011 Winter September 2018.
Computational Molecular Biology
Presented by Po-Chuan & Chen-Chen 2016/03/08
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithms Chapter 3 With Question/Answer Animations
CS330 Discussion 4 Spring 2017.
Objective of This Course
Matrix Chain Multiplication
Randomized Algorithms
Data Structures Review Session
Lectures on Graph Algorithms: searching, testing and sorting
Applied Discrete Mathematics Week 6: Computation
Sorting … and Insertion Sort.
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Divide and Conquer Algorithms Part I
Greedy Algorithms Alexandra Stefan.
CSE 2010: Algorithms and Data Structures Algorithms
On the Range Maximum-Sum Segment Query Problem
Analysis of Algorithms
CSC 427: Data Structures and Algorithm Analysis
Copyright © Cengage Learning. All rights reserved.
CENG 351 Data Management and File Structures
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSIT 301 (Blum)
The Selection Problem.
Discrete Mathematics CS 2610
Divide and Conquer Merge sort and quick sort Binary search
DIVIDE AND CONQUER.
Presentation transcript:

Algorithms Tutorial 27th Sept, 2019

Question 1 You are given a sequence of n songs where the ith song is Li minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD1, CD2, CD3,....,CDk) where each CD can hold m minutes. Furthermore The songs must be recorded in the given order, song 1, song 2, ..., song n. All songs must be included. No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to and an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity.

Question 1 You are given a sequence of n songs where the ith song is Li minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD1, CD2, CD3,....,CDk) where each CD can hold m minutes. Furthermore, the songs must be recorded in the given order, song 1, song 2, ..., song n. All songs must be included. No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to and an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity. Hint-1 Try to think greedily about putting songs on CDs.

Question 1 You are given a sequence of n songs where the ith song is Li minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD1, CD2, CD3,....,CDk) where each CD can hold m minutes. Furthermore, the songs must be recorded in the given order, song 1, song 2, ..., song n. All songs must be included. No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to and an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity. Hint-2 Try to fit as many songs as possible on one CD.

Question 1 Solution: Put as many songs (from song 1 to song g) on the 1st CD as possible without exceeding the m minute limit. Then iteratively repeat this procedure for the remaining CDs. Since this just requires keeping a running sum in which each of the n song lengths are included once, clearly the above is an O(n) algorithm.

Question 1 Sketch Proof: First let’s prove that first CD is optimal. Let S be an optimal solution in which the 1st CD holds songs 1 to f. The greedy algorithm puts songs 1 to g on the 1st CD. We now prove there is an optimal solution which puts the 1st g songs on the 1st CD. If f = g then S is such a solution. Now suppose that f ≠ g. Since the greedy algorithm puts as many songs on the 1st CD as possible, it follows that f < g. We construct a solution S’ by modifying S to move all songs from f + 1 to g onto the 1st CD. We now argue that S’ is a legal solution which is optimal. First note that the number of CDs used by S’ is at most the number of CDs used by S. All that remains is to argue that S’ is legal (i.e. no CD holds more than m minutes of songs). By definition of the greedy choice the 1st CD can hold songs 1 to g. Since S is a legal solution all of the CDs in it hold at most m minutes. For all but CD 1 the number of songs is only reduced and hence must still hold at most m minutes. Hence S’ is legal.

Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on.

Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Hint 1: You will never need to press the same switch twice. Why? Because it is equivalent to not pressing the switch and you will end up with the same state as before. So we can always solve the problem in at most n switch flips.

Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Hint 2: The order in which you press the switch does not affect the final state.

Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Hint 2: The order in which you press the switch does not affect the final state.

Question 2 Solution Approach: N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Solution Approach: Therefore we can choose a particular order. To make things easier let's go from left to right. At the current position if the bulb is on after pressing the switches to its left, we move to the right, else we switch it on. This works because changing any switch to the right of it will not affect it anymore.

Question 3 You are manager of Grand Budapest Hotel. You are given the arrival and departure time of each guest. But your cleaning staff is lazy. So you have to instruct them to clean minimum number of rooms. If there are two guests and their stay in the hotel does not coincide then you need only one cleaned room because after 1st guest leaves you can give the same room to 2nd guest. So for given arrival and departure time of each guest design optimal algorithm to return minimum number of rooms to be cleaned by the cleaning staff. Analyze the time complexity of the algorithm. You may assume departure time is strictly greater than arrival time and all time instances provided are distinct.

Question 3 You are manager of Grand Budapest Hotel. You are given the arrival and departure time of each guest. But your cleaning staff is lazy. So you have to instruct them to clean minimum number of rooms. If there are two guests and their stay in the hotel does not coincide then you need only one cleaned room because after 1st guest leaves you can give the same room to 2nd guest. So for given arrival and departure time of each guest design optimal algorithm to return minimum number of rooms to be cleaned by the cleaning staff. Analyze the time complexity of the algorithm. You may assume departure time is strictly greater than arrival time and all time instances provided are distinct. Hint-1 Try Using Sorting.

Question 3 You are manager of Grand Budapest Hotel. You are given the arrival and departure time of each guest. But your cleaning staff is lazy. So you have to instruct them to clean minimum number of rooms. If there are two guests and their stay in the hotel does not coincide then you need only one cleaned room because after 1st guest leaves you can give the same room to 2nd guest. So for given arrival and departure time of each guest design optimal algorithm to return minimum number of rooms to be cleaned by the cleaning staff. Analyze the time complexity of the algorithm. You may assume departure time is strictly greater than arrival time and all time instances provided are distinct. Hint-2 Try to go chronologically/greedily along each event.

Question 3 Input: (a1,d1) , (a2,d2) , ….. Convert input in individual event pair <time,arrival/departure> . (ie. (a1,arrival) , (d1,departure) ….) Sort all events pair based on time(first element of pair). max = 0 current_guests = 0 For all events pairs If event second element == arrival current_guests++ else current_guests-- If current_guests > max max = current_guests Return max Time Complexity: O(nlogn)

Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Approach: It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it better and possibly in a single pass? (PS: The solution shouldn’t depend on the size of integer ) Space complexity should be O(n).

Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Hint 1: Suppose you had the solution for all numbers smaller than n, then can you find it for n using them?

Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Solution Approach: Suppose the number is 11001. If you right shift it one place, you will get a smaller value whose number of bits have already been computed and you only need to check now for the units place in the original number.

Question 5 Given an array A, of N integers. Return the highest product possible by multiplying 3 numbers from the array.

Question 5 Given an array A, of N integers. Return the highest product possible by multiplying 3 numbers from the array. Hint 1: Do we need to consider all the elements from the array ? Is it enough to consider just the 3 maximum numbers from the array ?

Question 5 Given an array A, of N integers. Return the highest product possible by multiplying 3 numbers from the array. Hint 1: Do we need to consider all the elements from the array ? Is it enough to consider just the 3 maximum numbers from the array ?

Question 5 Given an array A, of N integers. Return the highest product possible by multiplying 3 numbers from the array. Hint 2: Obviously No. Product of 2 negative numbers is positive. So, Negative numbers with higher absolute value might also be of interest. How about maximum 3 elements, and 2 negative elements with the highest absolute value ?

Question 5 Choosing 3 maximum elements in the array and 2 negative elements with the highest absolute value should be enough. Once you have the 5 elements you desire, your answer would be one of the following : 1) Product of 3 maximum elements 2) Product of the 2 negative elements with max absolute value and maximum positive value.

Question 6 There are N Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x − 1. Any of these moves consumes 1 minute. There can be any number of moves. Assign mice to holes so that the time when the last mouse gets inside a hole is minimized.

Question 6 There are N Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x − 1. Any of these moves consumes 1 minute. There can be any number of moves. Assign mice to holes so that the time when the last mouse gets inside a hole is minimized. Hint: Sorting

Question 6 Solution: Sort mice positions Sort hole positions (in the same order) Get maximum value of |mice[i]-hole[i]|