CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

Size: px
Start display at page:

Download "CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples."

Transcription

1 CSE 101 Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

2 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. An interval [a,b] is a set of integers starting at a and ending at b. For example: [16,23] = {16,17,18,19,20,21,22,23} An overlap between two intervals [ a,b] and [c,d] is their intersection. Given two intervals [a,b] and [c,d], how would you compute the length of their overlap?

3 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given two intervals [a,b] and [c,d], how would you compute the length of their overlap? procedure overlap([a,b],[c,d]) [Assume that a c] if b < c: return????????? else: if b d: return?????????? if b > d: return??????????

4 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given two intervals [a,b] and [c,d], how would you compute the length of their overlap? procedure overlap([a,b],[c,d]) [Assume that a c] if b < c: return 0 else: if b d: return b c + 1 if b > d: return d c + 1

5 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. Example: What is the greatest overlap of the intervals: [45,57],[17,50],[10,29],[12,22],[23,51],[31,32],[10,44],[27,35]

6 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. Example: What is the greatest overlap of the intervals: [45,57],[17,50],[10,29],[12,22],[23,51],[31,32],[10,15],[23,35]

7 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. Brute Force algorithm:

8 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. Brute Force algorithm: olap:=0 for i from 1 to n-1 for j from i+1 to n if overlap([a_i,b_i],[a_j,b_j])>olap then olap:=overlap([a_i,b_i],[a_j,b_j]) return olap

9 DIVIDE AND CONQUER EXAMPLES (GREATEST OVERLAP.) Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. Brute Force algorithm olap:=0 for i from 1 to n-1 for j from i+1 to n if overlap([a_i,b_i],[a_j,b_j])>olap then olap:=overlap([a_i,b_i],[a_j,b_j]) return olap What is the runtime? Can we do better?

10 GREATEST OVERLAP. Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. Compose your base case Break the problem into smaller pieces Recursively call the algorithm on the smaller pieces Combine the results

11 COMPOSE YOUR BASE CASE What happens if there is only one interval?

12 COMPOSE YOUR BASE CASE What happens if there is only one interval? if n=1 then return 0

13 BREAK THE PROBLEM INTO SMALLER PIECES The question here is Would knowing the result on smaller problems help with knowing the solution on the original problem? (In this stage, let s keep the combine part in mind.) How would you break the problem into smaller pieces?

14

15 BREAK THE PROBLEM INTO SMALLER PIECES Would it be helpful to break the problem into two depending on the starting value?

16 BREAK THE PROBLEM INTO SMALLER PIECES Sort the list and break it into lists each of size n/2. [10,15],[10,29],[12,22],[17,50],[23,51],[27,35],[31,32],[45,57]

17 BREAK THE PROBLEM INTO SMALLER PIECES Sort the list and break it into lists each of size n/2. [10,15],[10,29],[12,22],[17,50],[23,51],[27,35],[31,32],[45,57] Let s assume we can get a DC algorithm to work. Then what information would it give us to recursively call each subproblem?

18 BREAK THE PROBLEM INTO SMALLER PIECES Let s assume we can get a DC algorithm to work. Then what information would it give us to recursively call each subproblem? overlapdc([10,15],[10,29],[12,22],[17,50])=13 overlapdc([23,51],[27,35],[31,32],[45,57])= = =

19 BREAK THE PROBLEM INTO SMALLER PIECES overlapdc([10,15],[10,29],[12,22],[17,50])=13 overlapdc([23,51],[27,35],[31,32],[45,57])=9 Is this enough information to solve the problem? What else must we consider? = =

20 BREAK THE PROBLEM INTO SMALLER PIECES overlapdc([10,15],[10,29],[12,22],[17,50])=13 overlapdc([23,51],[27,35],[31,32],[45,57])=9 The greatest overlap overall may be contained entirely in one sublist or it may be an overlap of one interval from either side = =

21 COMBINE THE RESULTS So far we have split up the set of intervals and recursively called the algorithm on both sides. The runtime of this algorithm satisfies a recurrence that looks something like this. What goes into the O(??? )? T n = 2T n 2 + O(??? )

22 COMBINE THE RESULTS So far we have split up the set of intervals and recursively called the algorithm on both sides. The runtime of this algorithm satisfies a recurrence that looks something like this. What goes into the O(??? )? T n = 2T n 2 + O(??? ) How long does it take to combine. In other words, how long does it take to check if there is not a bigger overlap between sublists?

23 COMBINE What is an efficient way to determine the greatest overlap of intervals where one is red and the other is blue?

24 GREATEST OVERLAP BETWEEN SETS. Let s formalize our algorithm that finds the greatest overlap of two intervals such that they come from different sets sorted by starting point. procedure overlapbetween ([[a 1, b 1 ], [a l, b l ]], [[c 1, d 1 ], [c k, d k ]]) (a 1 a 2 a l c 1 c 2 c k ) if k=0 or l = 0 then return 0.

25 GREATEST OVERLAP BETWEEN SETS. Let s formalize our algorithm that finds the greatest overlap of two intervals such that they come from different sets sorted by starting point. procedure overlapbetween ([[a 1, b 1 ], [a l, b l ]], [[c 1, d 1 ], [c k, d k ]]) (a 1 a 2 a l c 1 c 2 c k ) if k==0 or l == 0 then return 0. minc = c 1 maxb = 0 olap = 0 for i from 1 to l: if maxb < b i : maxb = b i for j from 1 to k: if olap < overlap([minc,maxb],[c k, d k ]): olap = overlap([minc,maxb],[c k, d k ]) return olap runtime?

26 GREATEST OVERLAP. Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. procedure overlapdc ([a 1, b 1 ], [a n, b n ]) if n==1 then return 0.

27 GREATEST OVERLAP. Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. procedure overlapdc ([a 1, b 1 ], [a n, b n ]) if n==1 then return 0. sort([a 1, b 1 ], [a n, b n ]) by a values. mid = n/2 LS = [[a 1, b 1 ], [a mid, b mid ]] RS = [[a mid+1, b mid+1 ], [a n, b n ]] olap1 = overlapdc(ls) olap2 = overlapdc(rs) olap3 = overlapbetween(ls,rs) return max(olap1,olap2,olap3)

28 GREATEST OVERLAP RUNTIME. T n = 2T n 2 + O n log n

29 GREATEST OVERLAP. Given a list of intervals [a_1,b_1], [a_n,b_n] write pseudocode for a D/C algorithm that outputs the length of the greatest overlap between two intervals. procedure overlapdc (sort([a 1, b 1 ], [a n, b n ])) if n==1 then return 0. mid = n/2 LS = [[a 1, b 1 ], [a mid, b mid ]] RS = [[a mid+1, b mid+1 ], [a n, b n ]] olap1 = overlapdc(ls) olap2 = overlapdc(rs) olap3 = overlapbetween(ls,rs) return max(olap1,olap2,olap3)

30 GREATEST OVERLAP RUNTIME. If you sort first then run the overlapdc algorithm you will have Sorting first: T n = 2T n 2 S n = O n log n + O n T n = O n log n Total runtime = O n log n + O n log n = O(n log n)

31 GREATEST OVERLAP (CORRECTNESS) It is clear that the greatest overlap will be from two intervals in the left half, two from the right half, or one from the left and one from the right. Our algorithm finds all three of these values and outputs the max.

32 DIVIDE AND CONQUER EXAMPLES (132-MATCHES) Given a list a 1,, a n of distinct positive integers, write pseudocode for a D/C algorithm that outputs true if there exists a 132-pattern and false if there does not exist a 132-pattern. Example: 4,12,22,28,26,8,5,15,11,17 Brute Force:

33 DIVIDE AND CONQUER EXAMPLES (132-MATCHES) Given a list a 1,, a n of distinct positive integers, write pseudocode for a D/C algorithm that outputs true if there exists a 132-pattern and false if there does not exist a 132-pattern. Example: 4,12,22,28,26,8,5,15,11,17 Brute Force: for i from 1 to n-2: for j from i+1 to n-1: for k from j+1 to n: if a i < a k < a j : return true return false

34 DIVIDE AND CONQUER EXAMPLES (132-MATCHES) Given a list a 1,, a n of distinct positive integers, write pseudocode for a D/C algorithm that outputs true if there exists a 132-pattern and false if there does not exist a 132-pattern. Can anyone think of an example of a sequence of numbers that avoids 132?

35 DIVIDE AND CONQUER EXAMPLES (132-MATCHES) Given a list a_1,,a_n of distinct positive integers, write pseudocode for a D/C algorithm that outputs true if there exists a 132-pattern and false if there does not exist a 132-pattern. Can we do better than O(n 3 )?

36 COMPOSE YOUR BASE CASE What happens if there is less than three integers?

37 COMPOSE YOUR BASE CASE What happens if there is less than 3 integers? if n<3 then return false

38 BREAK THE PROBLEM INTO SMALLER PIECES Usually the smaller pieces are each of size n/2. The question here is Would knowing the result on smaller problems help with knowing the solution on the originial problem? (In this stage, let s keep the combine part in mind.)

39 BREAK THE PROBLEM INTO SMALLER PIECES This problem is a little different than most D/C problems. There is not a fixed size or position where we break the original. Instead we will break the list at the maximum element. break the list at the maximum element. if a m is the max then set L 1 = a 1,, a m 1 L 2 = a m+1,, a n

40 RECURSIVELY CALL THE ALGORITHM ON THE SMALLER PIECES Must we call on both sublists? How many times?

41 COMBINE THE RESULTS Let s assume that we can get the algorithm to work. Then if 132dc(L 1 ) or 132dc(L 2 ) is true then we are done!!! What else must we consider?

42 COMBINE THE RESULTS Let s assume that we can get the algorithm to work. Then if 132dc(L 1 ) or 132dc(L 2 ) is true then we are done!!! What else must we consider? What if there were a 132 occurrence with numbers from L 1 and L 2?

43 COMBINE THE RESULTS Let s assume that we can get the algorithm to work. Then if 132dc(L 1 ) or 132dc(L 2 ) is true then we are done!!! What else must we consider? What if there were a 132 occurrence with numbers from L 1 and L 2? Example: [24, 36, 20, 46, 13, 48, 25, 38, 12]

44 COMBINE THE RESULTS what must happen if there is not a 132-pattern between an element from L_1, a_m, and L_2?

45 COMBINE THE RESULTS if min(l_1)<max(l_2) then there cannot be a 132-pattern between the two lists.

46 132-PATTERNS procedure 132dc (a_1, a_n) if n<3 then return false m:=findmaxindex(a_1, a_n) L_1:=a_1,,a_(m-1) L_2:=a_(m+1),,a_n B1:=132dc(L_1) B2:=132dc(L_2) if min(l_1)<max(l_2) then return true else return (B1 or B2)

47 132-PATTERNS (RUNTIME) procedure 132dc (a_1, a_n) if n<3 then return false m:=findmaxindex(a_1, a_n) L_1:=a_1,,a_(m-1) L_2:=a_(m+1),,a_n B1:=132dc(L_1) B2:=132dc(L_2) if min(l_1)<max(l_2) then return true else return (B1 or B2)

48 132-PATTERNS (RUNTIME) Worst case runtime: Best case runtime:

Randomized Algorithms: Selection

Randomized Algorithms: Selection Randomized Algorithms: Selection CSE21 Winter 2017, Day 25 (B00), Day 16 (A00) March 15, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Selection Problem: WHAT Given list of distinct integers a 1, a 2,,

More information

CSE Winter 2015 Quiz 2 Solutions

CSE Winter 2015 Quiz 2 Solutions CSE 101 - Winter 2015 Quiz 2 s January 27, 2015 1. True or False: For any DAG G = (V, E) with at least one vertex v V, there must exist at least one topological ordering. (Answer: True) Fact (from class)

More information

(Refer Slide Time: 01.26)

(Refer Slide Time: 01.26) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

More information

EDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN?

EDIT DISTANCE. Given two words (strings), how can we define a notion of closeness. For example: Is PELICAN closer to PENGUIN or POLITICIAN? CSE 101 Algorithm Design and Analysis Miles Jones mej016@eng.ucsd.edu Office 4208 CSE Building Lecture 22: Dynamic Programming Examples (Edit Distance/Knapsack) EDIT DISTANCE Given two words (strings),

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms Divide and Conquer Algorithms T. M. Murali February 19, 2009 Divide and Conquer Break up a problem into several parts. Solve each part recursively. Solve base cases by brute force. Efficiently combine

More information

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Module 07 Lecture - 38 Divide and Conquer: Closest Pair of Points We now look at another divide and conquer algorithm,

More information

12/30/2013 S. NALINI,AP/CSE

12/30/2013 S. NALINI,AP/CSE 12/30/2013 S. NALINI,AP/CSE 1 UNIT I ITERATIVE AND RECURSIVE ALGORITHMS Iterative Algorithms: Measures of Progress and Loop Invariants-Paradigm Shift: Sequence of Actions versus Sequence of Assertions-

More information

COMP 352 FALL Tutorial 10

COMP 352 FALL Tutorial 10 1 COMP 352 FALL 2016 Tutorial 10 SESSION OUTLINE Divide-and-Conquer Method Sort Algorithm Properties Quick Overview on Sorting Algorithms Merge Sort Quick Sort Bucket Sort Radix Sort Problem Solving 2

More information

Divide and Conquer Algorithms

Divide and Conquer Algorithms CSE341T 09/13/2017 Lecture 5 Divide and Conquer Algorithms We have already seen a couple of divide and conquer algorithms in this lecture. The reduce algorithm and the algorithm to copy elements of the

More information

Randomized Algorithms, Hash Functions

Randomized Algorithms, Hash Functions Randomized Algorithms, Hash Functions Lecture A Tiefenbruck MWF 9-9:50am Center 212 Lecture B Jones MWF 2-2:50pm Center 214 Lecture C Tiefenbruck MWF 11-11:50am Center 212 http://cseweb.ucsd.edu/classes/wi16/cse21-abc/

More information

Lecture 6: Divide-and-Conquer

Lecture 6: Divide-and-Conquer Lecture 6: Divide-and-Conquer COSC242: Algorithms and Data Structures Brendan McCane Department of Computer Science, University of Otago Types of Algorithms In COSC242, we will be looking at 3 general

More information

Divide-and-Conquer. Dr. Yingwu Zhu

Divide-and-Conquer. Dr. Yingwu Zhu Divide-and-Conquer Dr. Yingwu Zhu Divide-and-Conquer The most-well known algorithm design technique: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances independently

More information

Better sorting algorithms (Weiss chapter )

Better sorting algorithms (Weiss chapter ) Better sorting algorithms (Weiss chapter 8.5 8.6) Divide and conquer Very general name for a type of recursive algorithm You have a problem to solve. Split that problem into smaller subproblems Recursively

More information

Subset sum problem and dynamic programming

Subset sum problem and dynamic programming Lecture Notes: Dynamic programming We will discuss the subset sum problem (introduced last time), and introduce the main idea of dynamic programming. We illustrate it further using a variant of the so-called

More information

Divide-and-Conquer Algorithms

Divide-and-Conquer Algorithms Divide-and-Conquer Algorithms Divide and Conquer Three main steps Break input into several parts, Solve the problem in each part recursively, and Combine the solutions for the parts Contribution Applicable

More information

Mergesort again. 1. Split the list into two equal parts

Mergesort again. 1. Split the list into two equal parts Quicksort Mergesort again 1. Split the list into two equal parts 5 3 9 2 8 7 3 2 1 4 5 3 9 2 8 7 3 2 1 4 Mergesort again 2. Recursively mergesort the two parts 5 3 9 2 8 7 3 2 1 4 2 3 5 8 9 1 2 3 4 7 Mergesort

More information

Principles of Algorithm Design

Principles of Algorithm Design Principles of Algorithm Design When you are trying to design an algorithm or a data structure, it s often hard to see how to accomplish the task. The following techniques can often be useful: 1. Experiment

More information

CP222 Computer Science II. Searching and Sorting

CP222 Computer Science II. Searching and Sorting CP222 Computer Science II Searching and Sorting New Boston Dynamics wheeled robot Tech News! Tech News! New Boston Dynamics wheeled robot Man charged with arson based on pacemaker data Quiz! How do you

More information

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search

More information

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8 CSE341T/CSE549T 09/17/2014 Lecture 8 Scan and its Uses 1 Scan Today, we start by learning a very useful primitive. First, lets start by thinking about what other primitives we have learned so far? The

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS Fast sorting algorithms Shellsort, Mergesort, Quicksort Summary of the previous lecture Why sorting is needed? Examples from everyday life What are the basic operations in

More information

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

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved. Chapter 4 Divide-and-Conquer Copyright 2007 Pearson Addison-Wesley. All rights reserved. Divide-and-Conquer The most-well known algorithm design strategy: 2. Divide instance of problem into two or more

More information

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Solutions to Problem 1 of Homework 3 (10 (+6) Points) Solutions to Problem 1 of Homework 3 (10 (+6) Points) Sometimes, computing extra information can lead to more efficient divide-and-conquer algorithms. As an example, we will improve on the solution to

More information

ECE 122. Engineering Problem Solving Using Java

ECE 122. Engineering Problem Solving Using Java ECE 122 Engineering Problem Solving Using Java Lecture 27 Linear and Binary Search Overview Problem: How can I efficiently locate data within a data structure Searching for data is a fundamental function

More information

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search

More information

Binary Search and Worst-Case Analysis

Binary Search and Worst-Case Analysis Yufei Tao ITEE University of Queensland A significant part of computer science is devoted to understanding the power of the RAM model in solving specific problems. Every time we discuss a problem in this

More information

Divide and Conquer. Algorithm Fall Semester

Divide and Conquer. Algorithm Fall Semester Divide and Conquer Algorithm 2014 Fall Semester Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances

More information

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems MERGESORT 315 Sorting Recursively Do you remember any iterative sorting methods? How fast are they? Can we produce a good sorting method by thinking recursively? We try to divide and conquer: break into

More information

Unit-2 Divide and conquer 2016

Unit-2 Divide and conquer 2016 2 Divide and conquer Overview, Structure of divide-and-conquer algorithms, binary search, quick sort, Strassen multiplication. 13% 05 Divide-and- conquer The Divide and Conquer Paradigm, is a method of

More information

CS 380 ALGORITHM DESIGN AND ANALYSIS

CS 380 ALGORITHM DESIGN AND ANALYSIS CS 380 ALGORITHM DESIGN AND ANALYSIS Lecture 14: Dynamic Programming Text Reference: Chapter 15 Dynamic Programming We know that we can use the divide-and-conquer technique to obtain efficient algorithms

More information

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT. Chapter 3:- Divide and Conquer Compiled By:- Assistant Professor, SVBIT. Outline Introduction Multiplying large Integers Problem Problem Solving using divide and conquer algorithm - Binary Search Sorting

More information

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018 Data structures More sorting Dr. Alex Gerdes DIT961 - VT 2018 Divide and conquer Very general name for a type of recursive algorithm You have a problem to solve: - Split that problem into smaller subproblems

More information

Divide and Conquer 4-0

Divide and Conquer 4-0 Divide and Conquer 4-0 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain

More information

Sorting. Weiss chapter , 8.6

Sorting. Weiss chapter , 8.6 Sorting Weiss chapter 8.1 8.3, 8.6 Sorting 5 3 9 2 8 7 3 2 1 4 1 2 2 3 3 4 5 7 8 9 Very many different sorting algorithms (bubblesort, insertion sort, selection sort, quicksort, heapsort, mergesort, shell

More information

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics. Fundamental mathematical techniques reviewed: Mathematical induction Recursion Typically taught in courses such as Calculus and Discrete Mathematics. Techniques introduced: Divide-and-Conquer Algorithms

More information

Reasoning and writing about algorithms: some tips

Reasoning and writing about algorithms: some tips Reasoning and writing about algorithms: some tips Theory of Algorithms Winter 2016, U. Chicago Notes by A. Drucker The suggestions below address common issues arising in student homework submissions. Absorbing

More information

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm: Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9

More information

1 Dynamic Programming

1 Dynamic Programming CS161 Lecture 13 Dynamic Programming and Greedy Algorithms Scribe by: Eric Huang Date: May 13, 2015 1 Dynamic Programming The idea of dynamic programming is to have a table of solutions of subproblems

More information

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4. COMPSCI 330: Design and Analysis of Algorithms 8/28/2014 Lecturer: Debmalya Panigrahi Lecture #2 Scribe: Yilun Zhou 1 Overview This lecture presents two sorting algorithms, quicksort and mergesort, that

More information

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

n 1 x i = xn 1 x 1. i= (2n 1) = n 2. Questions 1. Use mathematical induction to prove that, for any n 1 n 1 x i = xn 1 x 1. (Recall that I proved it in a different way back in lecture 2.) 2. Use mathematical induction to prove that, for all

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

Data Structures and Algorithms CSE 465

Data Structures and Algorithms CSE 465 Data Structures and Algorithms CSE 465 LECTURE 4 More Divide and Conquer Binary Search Exponentiation Multiplication Sofya Raskhodnikova and Adam Smith Review questions How long does Merge Sort take on

More information

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq

More information

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017 CMSC 45 CMSC 45: Lecture Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct, Reading: Section. in KT. Dynamic Programming: In this lecture we begin our coverage of an important algorithm design

More information

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place? Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n

More information

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong Department of Computer Science and Engineering Chinese University of Hong Kong In this lecture, we will design the merge sort which sorts n elements in O(n log n) time. The algorithm illustrates a technique

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

CS102 Sorting - Part 2

CS102 Sorting - Part 2 CS102 Sorting - Part 2 Prof Tejada 1 Types of Sorts Incremental Approach Bubble Sort, Selection Sort, Insertion Sort, etc. Work slowly toward solution one step at a time Generally iterative in nature Divide

More information

Lecture 2: Divide and Conquer

Lecture 2: Divide and Conquer Lecture 2: Divide and Conquer Paradigm Convex Hull Median finding Paradigm Given a problem of size n divide it into subproblems of size n, a 1, b >1. Solve each b subproblem recursively. Combine solutions

More information

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson Component 02 Algorithms and programming Sorting Algorithms and Searching Algorithms 1 BUBBLE SORT Bubble sort is a brute force and iterative sorting algorithm where each adjacent item in the array is compared.

More information

CS141: Intermediate Data Structures and Algorithms Greedy Algorithms

CS141: Intermediate Data Structures and Algorithms Greedy Algorithms CS141: Intermediate Data Structures and Algorithms Greedy Algorithms Amr Magdy Activity Selection Problem Given a set of activities S = {a 1, a 2,, a n } where each activity i has a start time s i and

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 03 / 25 / 2013 Instructor: Michael Eckmann Today s Topics Comments/Questions? More on Recursion Including Dynamic Programming technique Divide and Conquer techniques

More information

Solutions to Problem Set 1

Solutions to Problem Set 1 CSCI-GA.3520-001 Honors Analysis of Algorithms Solutions to Problem Set 1 Problem 1 An O(n) algorithm that finds the kth integer in an array a = (a 1,..., a n ) of n distinct integers. Basic Idea Using

More information

The divide and conquer strategy has three basic parts. For a given problem of size n,

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

CPSC 320 Midterm 2 Thursday March 13th, 2014

CPSC 320 Midterm 2 Thursday March 13th, 2014 CPSC 320 Midterm 2 Thursday March 13th, 2014 [12] 1. Answer each question with True or False, and then justify your answer briefly. [2] (a) The Master theorem can be applied to the recurrence relation

More information

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

Sorting. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1 Sorting Data Structures and lgorithms CSE 373 SU 18 BEN JONES 1 Warmup Discuss with your neighbors: What considerations do we think about when choosing a sorting algorithm? So far we have seen: selection

More information

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers: CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers: 6 1 4 2 3 8 7 5 2 Given the following array (list),

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,

More information

Programming and Data Structure

Programming and Data Structure Programming and Data Structure Dr. P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture # 09 Problem Decomposition by Recursion - II We will

More information

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1 Merge Sort Algorithm Analysis November 15, 2017 Hassan Khosravi / Geoffrey Tien 1 The story thus far... CPSC 259 topics up to this point Priority queue Abstract data types Stack Queue Dictionary Tools

More information

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8 Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology 6046J/18410J Professors Erik Demaine and Shafi Goldwasser Handout 8 Problem Set 2 This problem set is due in class on

More information

Multiple-choice (35 pt.)

Multiple-choice (35 pt.) CS 161 Practice Midterm I Summer 2018 Released: 7/21/18 Multiple-choice (35 pt.) 1. (2 pt.) Which of the following asymptotic bounds describe the function f(n) = n 3? The bounds do not necessarily need

More information

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg CSE 1 Greedy Alg: Union Find/Dijkstra s Alg Shayan Oveis Gharan 1 Dijkstra s Algorithm Dijkstra(G, c, s) { d s 0 foreach (v V) d[v] //This is the key of node v foreach (v V) insert v onto a priority queue

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 9 (Part II) Recursion MOUNA KACEM Recursion: General Overview 2 Recursion in Algorithms Recursion is the use of recursive algorithms to solve a problem A recursive algorithm

More information

Algorithms - Ch2 Sorting

Algorithms - Ch2 Sorting Algorithms - Ch2 Sorting (courtesy of Prof.Pecelli with some changes from Prof. Daniels) 1/28/2015 91.404 - Algorithms 1 Algorithm Description Algorithm Description: -Pseudocode see conventions on p. 20-22

More information

Question Points Score TOTAL 50

Question Points Score TOTAL 50 UCSD CSE 101 Section B00, Winter 2015 MIDTERM February 5, 2015 NAME: Student ID: Question Points Score 1 10 2 10 3 10 4 10 5 10 TOTAL 50 INSTRUCTIONS. Be clear and concise. Write your answers in the space

More information

CPSC W2 Midterm #2 Sample Solutions

CPSC W2 Midterm #2 Sample Solutions CPSC 320 2014W2 Midterm #2 Sample Solutions March 13, 2015 1 Canopticon [8 marks] Classify each of the following recurrences (assumed to have base cases of T (1) = T (0) = 1) into one of the three cases

More information

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

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original

More information

Dynamic Programming Group Exercises

Dynamic Programming Group Exercises Name: Name: Name: Dynamic Programming Group Exercises Adapted from material by Cole Frederick Please work the following problems in groups of 2 or 3. Use additional paper as needed, and staple the sheets

More information

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search

More information

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7 CSE341T 09/20/2017 Lecture 7 Scan and Quicksort 1 Scan Scan is a very useful primitive for parallel programming. We will use it all the time in this class. First, lets start by thinking about what other

More information

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

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer Module 2: Divide and Conquer Divide and Conquer Control Abstraction for Divide &Conquer 1 Recurrence equation for Divide and Conquer: If the size of problem p is n and the sizes of the k sub problems are

More information

Back to Sorting More efficient sorting algorithms

Back to Sorting More efficient sorting algorithms Back to Sorting More efficient sorting algorithms Merge Sort Strategy break problem into smaller subproblems recursively solve subproblems combine solutions to answer Called divide-and-conquer we used

More information

Binary Search and Worst-Case Analysis

Binary Search and Worst-Case Analysis Department of Computer Science and Engineering Chinese University of Hong Kong A significant part of computer science is devoted to understanding the power of the RAM model in solving specific problems.

More information

Pseudo code of algorithms are to be read by.

Pseudo code of algorithms are to be read by. Cs502 Quiz No1 Complete Solved File Pseudo code of algorithms are to be read by. People RAM Computer Compiler Approach of solving geometric problems by sweeping a line across the plane is called sweep.

More information

1 More on the Bellman-Ford Algorithm

1 More on the Bellman-Ford Algorithm CS161 Lecture 12 Shortest Path and Dynamic Programming Algorithms Scribe by: Eric Huang (2015), Anthony Kim (2016), M. Wootters (2017) Date: May 15, 2017 1 More on the Bellman-Ford Algorithm We didn t

More information

Introduction to Computers and Programming. Today

Introduction to Computers and Programming. Today Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 10 April 8 2004 Today How to determine Big-O Compare data structures and algorithms Sorting algorithms 2 How to determine Big-O Partition

More information

CS205: Scalable Software Systems

CS205: Scalable Software Systems CS205: Scalable Software Systems Lecture 3 September 5, 2016 Lecture 3 CS205: Scalable Software Systems September 5, 2016 1 / 19 Table of contents 1 Quick Recap 2 Type of recursive solutions 3 Translating

More information

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms Computer Science 4U Unit 1 Programming Concepts and Skills Algorithms Algorithm In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation,

More information

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming. Dynamic Programming Deliverables Dynamic Programming basics Binomial Coefficients Weighted Interval Scheduling Matrix Multiplication /1 Knapsack Longest Common Subsequence 6/12/212 6:56 PM copyright @

More information

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide and-conquer is a very common and very powerful algorithm design technique. The general idea:

More information

Algorithm Analysis. Performance Factors

Algorithm Analysis. Performance Factors Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases Every algorithm has certain inputs that allow it

More information

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem Divide-and-conquer method: Divide-and-conquer is probably the best known general algorithm design technique. The principle behind the Divide-and-conquer algorithm design technique is that it is easier

More information

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au CSE 43 Java Sorting Reading: Ch. 3 & Sec. 7.3 Sorting Binary search is a huge speedup over sequential search But requires the list be sorted Slight Problem: How do we get a sorted list? Maintain the list

More information

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02) Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 04 Lecture - 01 Merge Sort (Refer

More information

Search,Sort,Recursion

Search,Sort,Recursion Search,Sort,Recursion Searching, Sorting and Recursion Searching Linear Search Inserting into an Array Deleting from an Array Selection Sort Bubble Sort Binary Search Recursive Binary Search Searching

More information

9. The Disorganized Handyman

9. The Disorganized Handyman 9. The Disorganized Handyman A bad handyman always blames his tools. Famous Proverb. What if my hammer is made of paper? Can I blame it then? Author Unknown. Programming constructs and algorithmic paradigms

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10 Recursion and Search MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Recursion: General Overview 2 Recursion in Algorithms Recursion is the use of recursive algorithms to

More information

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

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 6 Divide and Conquer Algorithm Design Technique Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide a problem instance into two

More information

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 3 Notes. Class URL:

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 3 Notes. Class URL: Notes slides from before lecture CSE 21, Winter 2017, Section A00 Lecture 3 Notes Class URL: http://vlsicad.ucsd.edu/courses/cse21-w17/ Notes slides from before lecture Notes January 18 (1) HW2 has been

More information

SNS COLLEGE OF ENGINEERING,

SNS COLLEGE OF ENGINEERING, SNS COLLEGE OF ENGINEERING, COIMBATORE Department of Computer Science and Engineering QUESTION BANK(PART A) GE8151 - PROBLEM SOLVING AND PYTHON PROGRAMMING TWO MARKS UNIT-I 1. What is computer? Computers

More information

Divide and Conquer. Bioinformatics: Issues and Algorithms. CSE Fall 2007 Lecture 12

Divide and Conquer. Bioinformatics: Issues and Algorithms. CSE Fall 2007 Lecture 12 Divide and Conquer Bioinformatics: Issues and Algorithms CSE 308-408 Fall 007 Lecture 1 Lopresti Fall 007 Lecture 1-1 - Outline MergeSort Finding mid-point in alignment matrix in linear space Linear space

More information

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication Shayan Oveis Gharan 1 Finding the Closest Pair of Points Closest Pair of Points (non geometric) Given n points and arbitrary distances

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 16 Dynamic Programming (plus FFT Recap) Adam Smith 9/24/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Discrete Fourier Transform

More information

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer CSC 8301- Design and Analysis of Algorithms Lecture 5 Decrease and Conquer Algorithm Design Technique Decrease-and-Conquer This algorithm design technique is based on exploiting a relationship between

More information

CSc 110, Spring 2017 Lecture 39: searching

CSc 110, Spring 2017 Lecture 39: searching CSc 110, Spring 2017 Lecture 39: searching 1 Sequential search sequential search: Locates a target value in a list (may not be sorted) by examining each element from start to finish. Also known as linear

More information

g(n) time to computer answer directly from small inputs. f(n) time for dividing P and combining solution to sub problems

g(n) time to computer answer directly from small inputs. f(n) time for dividing P and combining solution to sub problems .2. Divide and Conquer Divide and conquer (D&C) is an important algorithm design paradigm. It works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until

More information

Sorting & Growth of Functions

Sorting & Growth of Functions Sorting & Growth of Functions CSci 588: Data Structures, Algorithms and Software Design Introduction to Algorithms, Cormen et al., Chapter 3 All material not from online sources or text copyright Travis

More information

EECS 2011M: Fundamentals of Data Structures

EECS 2011M: Fundamentals of Data Structures M: Fundamentals of Data Structures Instructor: Suprakash Datta Office : LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle Note: Some slides in this lecture are adopted from James

More information